-
Notifications
You must be signed in to change notification settings - Fork 6
Unit Tests
We use the NS3 xUnit-style unit testing framework, but have added a few extra macros and functions to make writing tests easier! We like unit tests, so there are a lot of them.
We use an out-of-source tree directory ccnx/test for all the unit tests. This directory follows the same structure as ccnx/model, so each subdirectory under model has a corresponding subdirectory under test. We aim for every source code module (.cc file except examples) having a corresponding unit test module and for reasonable unit test coverage.
The header ccnx/test/TestMacros.h defines the BeginTest() and EndTest() macros. These wrap the creation of the derived ns3::TestCase class to reduce the clutter in a test suite with many tests.
For example, to test a class Foo, the test suite would look like this:
namespace TestSuiteFoo {
BeginTest(TestGetVersion)
{
printf("DoRun Test GetVersion() method\n");
NS_TEST_EXPECT_MSG_EQ (true, true, "The truth");
}
EndTest()
static class TestSuiteFoo : public TestSuite
{
public:
TestSuiteFoo() : TestSuite ("TestSuiteFoo", UNIT)
{
AddTestCase (new TestGetVersion (), TestCase::QUICK);
}
} g_TestSuiteFoo;
}; // namespace
- A TestSuite is made up of TestCase classes and a TestSuite class that wraps them together.
- Because all tests are compiled in to the same library, all TestCase and TestSuite must have unique names.
- In a test module for class CCNxFoo:
- Use a namespace to wrap all unit tests and the test suite. Name it
ns3::ccnx::CCNxFooTests. - Use BeginTest() and EndTest() to wrap the TestCase classes. To test a method named Bar, use a name like
TestBarorTestBar_ConditionXandTestBar_ConditionY. - Use a TestSuite named
TestSuiteCCNxFooand a global variable g_TestSuiteCCNxFoo.
- Use a namespace to wrap all unit tests and the test suite. Name it
Copyright (c) 2016, Xerox Corporation (Xerox) and Palo Alto Research Center (PARC). All rights reserved.