Class testing question

This is a discussion on Class testing question within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to unit test a class I wrote. I wasn't sure how I should go about checking ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    34

    Class testing question

    Hi,

    I am trying to unit test a class I wrote. I wasn't sure how I should go about checking the values of private member variables after calling functions that set them. I was hoping that people with experiences on the subject could help me with it.

    Code:
    class A
    {
    public:
        void func()
    private:
        int m_var;
    };
    The class has a public function func() that changes m_var when called. I didn't have m_var made public or expose it through a public getter function because it was necessary not to expose the variable to develoeprs. In this situation, how do I test that func() changed the variable in an expected way?

    If m_var were public, I would do
    Code:
    int main(int argc, char[] argv)
    {
        int var = 0; // 0 is the value of m_var supposed to be set by func()
        A a;
        a.func(); // it will call func()
        if(a.m_var != var)
        {
            cout << "func() failed to properly set A::m_var";
        }
    }
    But I cannot do this because m_var is a private member. Is having a getter for m_var the only option in this situation? or is it recommended to use the "friend" keyword to have a unit test class access the private variable? Thanks!

  2. #2
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    It is generally considered good practice to use accessor functions to retrieve members from a class, rather than exposing them directly to the user.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Have an accessor function outside int main() that modifies the private variable, like this:

    Code:
    A::func()  {
    m_var = var; //define protected variable inside class "A"
    }
    or whatever.
    Last edited by Programmer_P; 05-26-2009 at 12:15 AM. Reason: fixing typo

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Several options here...

    1) Do nothing. Setters and getters are trivial and don't require unit tests.

    2) Add a getter function. It matches the setter and is better than a public member variable, but it adds to the public interface for the sole purpose of testing.

    3) Make your test class a friend of the class being tested.

    Code:
    #include <cassert>
    
    class Boo {
    	int i;
    	friend class BooTest;
    public:
    	Boo(int j) : i(j) {}
    	void set(int p) { i = p; }
    };
    
    class BooTest {
    public:
    	void testSet()
    	{
    		Boo b(7);
    		b.set(0);
    
    		assert(b.i == 0);
    	}
    };
    	
    int main()
    {
    	BooTest bt;
    	bt.testSet();
    
    	return 0;
    }
    4) Write "self-testing" code, as Cline, Lomow, and Girou suggest in the C++ FAQs book:

    Code:
    #include <cassert>
    
    class Boo {
    	int i;
    public:
    	Boo(int j) : i(j) {}
    	void set(int p) 
    	{ 
    		i = p; 
    #ifdef UNITTEST
    		assert(i == p);  
    #endif
    	}
    };
    
    int main()
    {
    	Boo b(4);
    	b.set(1);
    
    	return 0;
    }
    I'd opt for 1) for simple setters (and would try to refactor the setters out of the code).
    Last edited by medievalelks; 05-26-2009 at 06:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-13-2008, 05:57 PM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 06:08 AM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 10:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21