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
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 12K views
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.
 
Physics 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!