-5
BufferContainer::BufferContainer( uint8_t* buffer, size_t size, size_t usz):
            buffer_ptr(buffer),
            buffer_position(0),
            total_size(size),
            required_size(0),
            used_size(0) {}

I need to test the above constructor (unit testing) . Please help!

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59
1User
  • 614
  • 3
  • 13
  • 4
    There's no logic there, so nothing really needs testing. Except perhaps the design of the constructor.... (unused parameters are rarely a good idea) – Mat Aug 12 '15 at 09:16
  • 2
    Call it then check post conditions with your getters ? – Jarod42 Aug 12 '15 at 09:17
  • 1
    what do you want to test in your unit test(s)? – Hcorg Aug 12 '15 at 09:17
  • @Hcorg write an unit test to cover above function(code coverage) – 1User Aug 12 '15 at 09:18
  • Jarod42 please let me know in code terms – 1User Aug 12 '15 at 09:22
  • 1
    @1User it's not a function, it's constructor. as Mat points - there's no logic in here, so it's hard to unit test. You could just write constructor and check by getters it set all what is needed, but that's not really a good test... – Hcorg Aug 12 '15 at 09:26
  • @Hcorg as a part of code coverage , i must need to write an test for constructors also ..so i am seeking help – 1User Aug 12 '15 at 09:29
  • constructor coverage should be included in 'normal' unit-tests - when you want to test *functionality* of BufferContainer you have to construct it, then call some method on it. Unit test will test behavior of method, yet constructor will be also tested (as precondition of method) and code should be covered – Hcorg Aug 12 '15 at 09:59

2 Answers2

2

You are testing a constructor, you cannot just invoke it like a normal function. Even if you were able to do that, the issue is that constructors don't return anything so you cannot see if they did the work they were intended to do.

Constructors initialize internal state of the object. So the best way is to

  • Create an instance of BufferContainer.
  • I am sure there would be other methods of this class that rely upon the internal state. Create test cases that tests these methods.
Paani
  • 560
  • 3
  • 11
0

Maybe you can use the following steps:

  1. Use the constructor for creating a object A.

  2. Check the value of member variable in A. According to checking the value of member variable, you can assure that whether your constructor works well or not.

EDIT: In order to check the private member in A, you should provide the get() function. For example:

`class Example
{
public:
    Example(const int& a):_a(a){}
    ~Example(){}
    const int& get() const
    {
        return _a;
    }
private:
    int _a;
}`

When you test, you can do as it:

int main()
{
    Example A(5);
    std::cout << A.get() << endl;
    return 0;
}
cwfighter
  • 502
  • 1
  • 5
  • 20
  • Regarding point 2 above, I don't think it is a good idea to check private member variables in unit tests. Look at : http://stackoverflow.com/questions/8618710/unit-test-accessing-private-variables – Paani Aug 12 '15 at 10:07
  • @Paani, if it is a private member, you can use `get()` function to get the value of member ! – cwfighter Aug 12 '15 at 11:24
  • Sure, but only if such a getter function was part of the actual requirements from the class. It won't be a good idea to add public getters just for unit testing. – Paani Aug 12 '15 at 12:55