[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

Discussion Overview

The discussion revolves around unit testing a C++ class, specifically the `InventoryTag` class, which manages inventory quantities. Participants are seeking help with implementing unit tests for the `addInventory` method and troubleshooting issues related to test results.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares the code for the `InventoryTag` class and expresses difficulty in writing unit tests for the `addInventory` method.
  • Another participant suggests using assertions to test the functionality of `addInventory`, proposing specific test cases.
  • A participant reports that removing the assertions allows the code to run but leads to unexpected test results, indicating a failure in the unit test for a specific case.
  • There is mention of an external program that may have specific expectations for the unit tests, which is causing confusion for one participant.
  • One participant notes they are using zyBooks, implying that the context of their learning platform may influence their approach to unit testing.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the correct implementation of the unit tests. There are differing experiences regarding the functionality of the provided code and the expected outcomes of the tests.

Contextual Notes

There are unresolved issues regarding the specific requirements for the unit tests and the behavior of the `addInventory` method, particularly concerning the conditions under which inventory is updated.

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