Unit test accessing private variables

前端 未结 6 1500
无人及你
无人及你 2021-01-05 04:55

I have a unit test class Tester; I want it to access private fields of a Working class.

class Working {
    // ...
    private:
            


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-05 05:28

    Generally, your unit tests should not evaluate private variables. Write your tests to the interface, not the implementation.

    If you really need to check that a private variable has a particular characteristic, consider using assert() rather than trying to write a unit test for it.

    A longer answer (written for C# rather than C++, but the same principles apply) is at https://stackoverflow.com/a/1093481/436641.

提交回复
热议问题