What is the proper way to write a unit test for addInventory() in C++?

In summary, the conversation was about writing a unit test for the addInventory() function. The test should check if the quantity of inventory is updated correctly after adding a shipment. The output for a failed test should include the message "UNIT TEST FAILED" preceded by three spaces. It is recommended to use a unit testing framework to make the process easier.
  • #1
Teh
47
0
I got one correct for my code...what i am missing...any tips or guide?

Write a unit test for addInventory(). Call redSweater.addInventory() with parameter sweaterShipment. Print the shown error if the subsequent quantity is incorrect. Sample output for failed unit test given initial quantity is 10 and sweaterShipment is 50:

Beginning tests.
UNIT TEST FAILED: addInventory()
Tests complete.
Note: UNIT TEST FAILED is preceded by 3 spaces.


Code:
#include <iostream>
using namespace std;

class InventoryTag {
public:
   InventoryTag();
   int getQuantityRemaining() const;
   void addInventory(int numItems);

private:
   int quantityRemaining;
};

InventoryTag::InventoryTag() {
    quantityRemaining = 0;
}

int InventoryTag::getQuantityRemaining() const {
   return quantityRemaining;
}

void InventoryTag::addInventory(int numItems) {
   if (numItems > 10) {
      quantityRemaining = quantityRemaining + numItems;
   }
}

int main() {
   InventoryTag redSweater;
   int sweaterShipment = 0;
   int sweaterInventoryBefore = 0;

   sweaterInventoryBefore = redSweater.getQuantityRemaining();
   sweaterShipment = 25;

   cout << "Beginning tests." << endl;

   // FIXME add unit test for addInventory

   /* Your solution goes here  */
   addInventory()
 
   redSweater.addInventory(sweaterShipment);
 
  
   cout << "Tests complete." << endl;

   return 0;
}

Inventory is 0, shipment is 25. Testing that quantityRemaining was updated to 25.
Your value: 25
Testing with sweaterShipment of 25. addInventory updates quantityRemaining.
Your output: Beginning tests.
Tests complete.
Inventory is 25, shipment is 5. Testing that quantityRemaining remains 25.
Your value: 25
✖ Testing sweaterShipment of 5. addInventory does not update quantityRemaining.
Expected output: Beginning tests.
UNIT TEST FAILED: addInventory()
Tests complete.
Your output: Beginning tests.
Tests complete.
 
Technology news on Phys.org
  • #2
Teh said:
I got one correct for my code...what i am missing...any tips or guide?

Write a unit test for addInventory(). Call redSweater.addInventory() with parameter sweaterShipment. Print the shown error if the subsequent quantity is incorrect. Sample output for failed unit test given initial quantity is 10 and sweaterShipment is 50:

Beginning tests.
UNIT TEST FAILED: addInventory()
Tests complete.
Note: UNIT TEST FAILED is preceded by 3 spaces.


You need to write code to add the amount of inventory specified, which you have done. Now you just need to check if you get the expected quantity after adding the inventory. If the check fails print the message they've asked for. I'm just not sure how the values they've provided as a sample would actually fail.

On another note if your being asked to do unit testing, why are they not having you use a unit testing framework to make the process of creating the tests easier so you can focus on the important part of unit testing which is determining which tests you require.
 
  • #3
thanks for the help I needed to use if and else statement thanks!
 

FAQ: What is the proper way to write a unit test for addInventory() in C++?

What is unit testing and why is it important for a class?

Unit testing is the process of testing individual units or components of code to ensure they are functioning correctly. In the context of a class, unit testing involves testing each method and function within the class to ensure they produce the expected results. Unit testing is important for a class because it helps identify and fix bugs early in the development process, ensures code reliability, and allows for easier maintenance and updates in the future.

How do you write unit tests for a class?

To write unit tests for a class, you should first identify the methods and functions within the class that need to be tested. Then, create test cases that cover different scenarios and expected outcomes for each method and function. Finally, use a testing framework or tool, such as JUnit or NUnit, to run the tests and verify the results.

What are some best practices for unit testing a class?

Some best practices for unit testing a class include writing tests that are independent of each other, testing both valid and invalid inputs, covering edge cases, and ensuring tests are repeatable and automated. It is also important to test all public methods and functions within the class and to use descriptive and meaningful test names.

How do you handle dependencies when unit testing a class?

There are a few different ways to handle dependencies when unit testing a class. One approach is to use mocking frameworks, which allow you to create mock objects that simulate the behavior of the dependencies. Another approach is to refactor the code and break up dependencies into separate classes or interfaces, making it easier to test each component independently.

Can you have 100% code coverage with unit testing?

It is possible to have 100% code coverage with unit testing, but it is not always necessary or practical. Code coverage refers to the percentage of code that is covered by tests, and having 100% coverage does not guarantee that all possible scenarios and edge cases have been tested. It is important to focus on writing meaningful and effective tests rather than striving for 100% coverage.

Similar threads

Replies
4
Views
3K
Replies
1
Views
1K
Replies
5
Views
2K
Replies
6
Views
3K
Replies
2
Views
8K
Replies
75
Views
5K
Back
Top