Search code examples
unit-testingqtprotected

Unit Testing of a protected slot


Hello I tried to implement unit testing for a protected slot In the testing function I made an object of a class and called a protected of that class using that object But it gave me an error as follows:

/home/puneet/puneet/office/alkimia/payment/backend/backend.h: In member function ‘void BackendTest::test_initialization()’: /home/puneet/puneet/office/alkimia/payment/backend/backend.h:70: error: ‘void Backend::initializeUsers(const QStringList&)’ is protected


Solution

  • The way I usually handle this for tests is to use the following defines:

    If the header file for the class you are testing is "myclass.h", where you #include it in the test file, do:

    #define protected public
    #include "myclass.h"
    #undef protected
    

    This tells the compiler to treat the protected methods as public instead. You can do the same for private functions.