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

  • Context: C/C++ 
  • Thread starter Thread starter Teh
  • Start date Start date
  • Tags Tags
    Class Testing Unit
Click For Summary
SUMMARY

The discussion focuses on writing a unit test for the addInventory() method in a C++ class named InventoryTag. The user is tasked with verifying that the inventory quantity updates correctly when items are added. The expected output for a failed test case is specified, highlighting the need for precise error messaging. The conversation also raises the question of why a unit testing framework is not being utilized to streamline the testing process.

PREREQUISITES
  • C++ programming knowledge
  • Understanding of class and object-oriented programming concepts
  • Familiarity with unit testing principles
  • Basic knowledge of conditional statements (if-else)
NEXT STEPS
  • Implement a unit test using a C++ unit testing framework like Google Test
  • Learn about mocking dependencies in unit tests
  • Explore best practices for writing effective unit tests in C++
  • Investigate error handling techniques in C++ for robust testing
USEFUL FOR

C++ developers, software testers, and anyone interested in improving their unit testing skills for object-oriented programming.

Teh
Messages
47
Reaction score
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
the 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.
 
thanks for the help I needed to use if and else statement thanks!
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K