[C++] Unit testing of a class HELP

In summary: I am following the instructions on the website.assert(0 == sweaterInventoryBefore);redSweater.addInventory(sweaterShipment);assert(sweaterShipment == redSweater.getQuantityRemaining());Hi EvanET! Welcome to MHB! ;)assert(0 == sweaterInventoryBefore);redSweater.addInventory(sweaterShipment);assert(sweaterShipment == redSweater.getQuantityRemaining());
  • #1
EvanET
10
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
  • #2
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());
 
  • #3
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.
 
  • #4
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?
 
  • #5
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
 

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

Unit testing is a software testing method in which individual units or components of a program are tested to ensure that they are functioning correctly. In the context of a class, unit testing involves testing the methods and functions within the class to ensure they produce the expected results. It is important for a class because it helps identify and fix bugs and errors early on in the development process, leading to more reliable and efficient code.

2. How do you write unit tests for a class in C++?

To write unit tests for a class in C++, you can use a testing framework such as Google Test or Boost.Test, which provide functions and macros specifically designed for unit testing. These frameworks allow you to create test cases that cover different scenarios and input values for your class methods and functions. You can also use mocking frameworks like Google Mock to simulate external dependencies and isolate your class for testing.

3. What are the benefits of using unit testing for a class?

Unit testing has several benefits for a class, including early detection of bugs and errors, improved code quality, and faster development time. By writing tests for each method and function in the class, you can quickly identify and fix any issues that may arise. This leads to more reliable and maintainable code. Additionally, unit testing can serve as documentation for the class, as it clearly outlines the expected behavior of each method and function.

4. Can you unit test private methods in a class?

In C++, private methods in a class cannot be accessed directly by the testing code. However, you can use the friend keyword to declare the testing code as a friend of the class, allowing it to access private members. Alternatively, you can also test private methods indirectly by testing the public methods that call those private methods.

5. How do you ensure complete code coverage when unit testing a class?

Code coverage refers to the percentage of code that is covered by unit tests. To ensure complete code coverage when unit testing a class, you can use a code coverage tool such as gcov or lcov. These tools provide a report showing which lines of code were executed during the tests, allowing you to identify any areas that were not covered. Additionally, writing multiple test cases for each method and function can also help achieve complete code coverage.

Similar threads

  • Programming and Computer Science
Replies
2
Views
12K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
4
Views
781
  • Programming and Computer Science
Replies
3
Views
725
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
986
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top