我们可以使用 CppUnit 将参数传递给 C++ 中的测试用例函数吗?

Can we pass parameters to the test cases functions in C++ using CppUnit?

我正在使用 cppunit 测试我的 C++ 代码。我已经这样写了我的测试夹具

class MainTestFixture : public TestFixture
{
    CPPUNIT_TEST_SUITE(MainTestFixture);    
    CPPUNIT_TEST(Addition);
    CPPUNIT_TEST(Multiply);
    CPPUNIT_TEST_SUITE_END();

public: 
    void setUp(void);
    void tearDown(void);
protected:
    // Test Functions 
    void Addition(void);
    void Multiply(void);
};

现在如果我实现像

这样的测试用例
void MainTestFixture::Addition()
{
    // CPPUNIT_ASSERT(condition);
}
void MainTestFixture::Multiply()
{
    // CPPUNIT_ASSERT(condition);
}

在上面的代码中,我可以将参数传递给加法和乘法函数吗?

因为我已经为 运行 制作了一个套件,如下所示

#include "MainTestFixture.h"

CPPUNIT_TEST_SUITE_REGISTRATION(MainTestFixture);

using namespace CPPUNIT_NS;
int main()
{
    // informs test-listener about testresults
    CPPUNIT_NS::TestResult testresult;

    // register listener for collecting the test-results
    CPPUNIT_NS::TestResultCollector collectedresults;
    testresult.addListener (&collectedresults);

    // register listener for per-test progress output
    CPPUNIT_NS::BriefTestProgressListener progress;
    testresult.addListener (&progress);

    // insert test-suite at test-runner by registry
    CPPUNIT_NS::TestRunner testrunner;
    testrunner.addTest (CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest ());
    testrunner.run(testresult);

    // output results in compiler-format
    CPPUNIT_NS::CompilerOutputter compileroutputter(&collectedresults, std::cerr);
    compileroutputter.write ();

    // Output XML for Jenkins CPPunit plugin
    ofstream xmlFileOut("cppMainUnitTest.xml");
    XmlOutputter xmlOut(&collectedresults, xmlFileOut);
    xmlOut.write();

    // return 0 if tests were successful
    return collectedresults.wasSuccessful() ? 0 : 1;
}

不,你不能。 Addition() 是回调,它将在 CPPUNIT 引擎中注册并由驱动程序调用 - 因此它应该使用 void(void) 接口。相反,您可以将参数定义为 MainTestFixture 的成员。

class MainTestFixture : public TestFixture
{
    CPPUNIT_TEST_SUITE(MainTestFixture);    
    CPPUNIT_TEST(Addition);
    CPPUNIT_TEST(Multiply);
    CPPUNIT_TEST_SUITE_END();

public: 
    void setUp(void);
    void tearDown(void);
protected:
    // Test Functions 
    void init_fixture();
    void Addition(void);
    void Multiply(void);
protected: 
    //data members
    param1_t m_param1;
    param2_t m_param2;

};

void MainTestFixture::init_fixture()
{
     m_param1 = ...;
     m_param2 = ...;
}
void MainTestFixture::Addition()
{
    param1_t res= m_param1 + ...;
    // CPPUNIT_ASSERT(condition);
}