Unit test accessing private variables

前端 未结 6 1499
无人及你
无人及你 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:29

    If you absolutely must do this, you could conditionally compile your code so that TestBase is a friend only when unit testing:

    class Working {
        // ...
        private:
        int m_variable;
    
    #ifdef UNIT_TESTING
        friend class TestBase;
    #endif
    };
    

提交回复
热议问题