[C++] Unit testing of a class HELP

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

The forum discussion centers on unit testing the addInventory method of the InventoryTag class in C++. The user, EvanET, struggles to implement the unit test correctly, particularly when testing the condition where the number of items added is less than or equal to 10. The suggested solution involves using assertions to verify the expected behavior of the method. However, the user encounters a failure when testing with a shipment of 5 items, indicating that the method does not update the inventory as expected.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with class and object-oriented programming concepts
  • Knowledge of unit testing principles and practices
  • Experience with assertion-based testing frameworks
NEXT STEPS
  • Implement additional test cases for various scenarios in addInventory
  • Learn about C++ testing frameworks such as Google Test
  • Explore the concept of test-driven development (TDD) in C++
  • Review the use of assertions in unit tests to validate expected outcomes
USEFUL FOR

C++ developers, software testers, and anyone interested in implementing unit tests for object-oriented programming classes.

EvanET
Messages
10
Reaction score
0
I'm given the code:
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  */

   cout << "Tests complete." << endl;

   return 0;
}

I've tried everything to my knowledge and used my textbook, still stuck.
 
Technology news on Phys.org
EvanET said:
I'm given the code:
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  */

   cout << "Tests complete." << endl;

   return 0;
}

I've tried everything to my knowledge and used my textbook, still stuck.

Hi EvanET! Welcome to MHB! ;)

How about something like:
Code:
assert(0 == sweaterInventoryBefore);
redSweater.addInventory(sweaterShipment);
assert(sweaterShipment == redSweater.getQuantityRemaining());
 
I like Serena said:
Hi EvanET! Welcome to MHB! ;)

How about something like:
Code:
assert(0 == sweaterInventoryBefore);
redSweater.addInventory(sweaterShipment);
assert(sweaterShipment == redSweater.getQuantityRemaining());

if i use the code it doesn't work, i took out the assert, the code ran but here are my results:

[PASS]Inventory is 0, shipment is 25. Testing that quantityRemaining was updated to 25.
Your value: 25
[PASS]Testing with sweaterShipment of 25. addInventory updates quantityRemaining.
Your output: Beginning tests.
Tests complete.
[PASS]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.
Tests aborted.
 
EvanET said:
if i use the code it doesn't work, i took out the assert, the code ran but here are my results:

[PASS]Inventory is 0, shipment is 25. Testing that quantityRemaining was updated to 25.
Your value: 25
[PASS]Testing with sweaterShipment of 25. addInventory updates quantityRemaining.
Your output: Beginning tests.
Tests complete.
[PASS]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.
Tests aborted.

It appears there's some program running around this program that expects something specific, but for me that is unknown.
Which instruction did you receive about writing unit tests?
 
I like Serena said:
It appears there's some program running around this program that expects something specific, but for me that is unknown.
Which instruction did you receive about writing unit tests?
I am using zybooks
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
12K
Replies
12
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 35 ·
2
Replies
35
Views
4K