Parameterizing a test using CppUnit

前端 未结 7 1407
情话喂你
情话喂你 2021-01-05 07:56

My organization is using CppUnit and I am trying to run the same test using different parameters. Running a loop inside the test is not a good option as any failure will abo

7条回答
  •  难免孤独
    2021-01-05 08:25

    Upon of the suggestion of Marcin i've implemented some macros aiding to define parameterized CppUnit tests.

    With this solution you just need to replace the old macros CPPUNIT_TEST_SUITE and CPPUNIT_TEST_SUITE_END within the class's header file:

    CPPUNIT_PARAMETERIZED_TEST_SUITE(, );
    
    /*
     * put plain old tests here.
     */
    
    CPPUNIT_PARAMETERIZED_TEST_SUITE_END();
    

    In the implementation file you need to replace the old CPPUNIT_TEST_SUITE_REGISTRATION macro with:

    CPPUNIT_PARAMETERIZED_TEST_SUITE_REGISTRATION ( ,  )
    

    These macros require you to implement the methods:

    static std::vector parameters();
    void testWithParameter(ParameterType& parameter);
    
    • parameters(): Provides a vector with the parameters.
    • testWithParameter(...): Is called for each parameter. This is where you implement your parameterized test.

    A detailed explanation can be found here: http://brain-child.de/engineering/parameterizing-cppunit-tests

    The german version can be found here: http://brain-child.de/engineering/parametrierbare-tests-cppunit

提交回复
热议问题