First whats the class I use if I want to setup a test like:

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

Seems like the class to use is TestSuite?


So now I just created a very simple class:

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

class ParserUnitTests : public CppUnit::TestFixture {

public:
    void testCanDoUnitTest();
};

And its corresponding cpp:

#include "ParserUnitTests.h"

CPPUNIT_TEST_SUITE(ParserUnitTests);

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

CPPUNIT_TEST(ParserUnitTests::testCanDoUnitTest);

I am getting errors like "expected a declaration" on the CPPUNIT_TEST_SUITE line... seems very different from where I come from ... more modern languages ... like JS/Python ... Seems like here its more explicit? I must tell CppUnit which class/test cases to run? Ok, but whats causing the errors?

The code in the CppUnit cookbook is mainly snipplets and its hard to figure out what are the imports required and what should go where ... perhaps someone can guide me?

有帮助吗?

解决方案

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.

其他提示

This is how I use 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;
  }
};

And :

#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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top