2

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?

2
  • might be that the compiler wants the declaration of the function parserUnitTests before the constant , try moving the function above the const and see what happens. Commented Nov 3, 2012 at 15:12
  • The slashes in your include paths are backwards. Commented Jun 7, 2017 at 12:53

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

i am working on cppunit from few days,i have small problem. How do i register 3 or more SUITE_REGISTRATION into a single one . i,e CPPUNIT_TEST_SUITE_REGISTRATION( ParserUnitTest ) and CPPUNIT_TEST_SUITE_REGISTRATION( FormUnitTest ) and goes on . I want to combine all of this into single XML output file . Individual test cases work fine. Any suggestions would be helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.