1

首先,如果我想设置一个测试,我使用什么类:

class MyClassTests 
    setup()
    teardown()
    testDoingSomething()
    testDoingA()

似乎要使用的类是TestSuite?


所以现在我创建了一个非常简单的类:

# ParserUnitTests.h
#include <cppunit\TestFixture.h>;
#include <cppunit\extensions\HelperMacros.h>

class ParserUnitTests : public CppUnit::TestFixture {

public:
    void testCanDoUnitTest();
};

及其对应的cpp:

#include "ParserUnitTests.h"

CPPUNIT_TEST_SUITE(ParserUnitTests);

void ParserUnitTests::testCanDoUnitTest() {
    CPPUNIT_ASSERT_EQUAL(1, 2, "Expected failure");
}

CPPUNIT_TEST(ParserUnitTests::testCanDoUnitTest);

我收到类似“预期声明”之类的错误CPPUNIT_TEST_SUITE...似乎与我来自的地方非常不同...更现代的语言...如 JS/Python ...似乎在这里更明确?我必须告诉 CppUnit 运行哪些类/测试用例?好的,但是是什么导致了错误?

CppUnit 食谱中的代码主要是snipplets,很难弄清楚需要什么进口以及应该去哪里......也许有人可以指导我?

4

2 回答 2

2

With some small changes your code should work.

// ParserUnitTests.h
#include <cppunit\TestFixture.h>;
#include <cppunit\extensions\HelperMacros.h>

class ParserUnitTests : public CppUnit::TestFixture {

    CPPUNIT_TEST_SUITE(ParserUnitTest);
    CPPUNIT_TEST(testCanDoUnitTest);
    CPPUNIT_TEST_SUITE_END();

public:
    void testCanDoUnitTest();
};

CPPUNIT_TEST_SUITE_REGISTRATION( ParserUnitTest );

and

// ParserUnitTests.cpp
#include "ParserUnitTests.h"

void ParserUnitTests::testCanDoUnitTest() {
    CPPUNIT_ASSERT_EQUAL(1, 2, "Expected failure");
}

then you only need a main (which I copied just from the Cppunit cookbook)

// main.cpp
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>

int main()
{
    CppUnit::TextUi::TestRunner runner;
    CppUnit::TestFactoryRegistry &registry = CppUnit::TestFactoryRegistry::getRegistry();
    runner.addTest( registry.makeTest() );
    bool wasSuccessful = runner.run();
    return !wasSuccessful;
}

So the only change is that you need to declare the test suite in the header file inside the TestFixture declaration.

于 2012-11-03T21:39:11.053 回答
1

这就是我使用 CppUnit 的方式:

#include "MyClass.hpp"

struct callable
{
  void operator()()
  {
  }
};

class MyClassTest : public CppUnit::TestCase
{
public:
  void testEquality()
  {
    CPPUNIT_ASSERT(1 == 1);
  }

  void testCreation()
  {
    MyClass<callable>* tp = new MyClass<callable>(1);
    CPPUNIT_ASSERT(tp->done() == true);
    delete tp;
  }

  static CppUnit::Test* suite()
  {
    CppUnit::TestSuite* suiteOfTests = new CppUnit::TestSuite("MyClassTest");
    suiteOfTests->addTest(new CppUnit::TestCaller<MyClassTest>("testEquality",
                                              &ThreadPoolTest::testEquality));
    return suiteOfTests;
  }
};

和 :

#include <cstdlib>
#include <iostream>
#include <limits>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/XmlOutputter.h>
#include <cppunit/TextOutputter.h>
#include <cppunit/TestResult.h>
#include <cppunit/TestResultCollector.h>

#include "MyClass.hpp"

int main(int argc, char** argv)
{
  CppUnit::TextUi::TestRunner   runner;

  runner.addTest(MyClass::suite());

  runner.run();

  return (EXIT_SUCCESS);
}

Some code is missing in the main but I simplified it so you could see the important bits. Hope this helps.

于 2012-11-03T16:19:48.683 回答