diff --git a/doc/testing/how-to-write-tests.texi b/doc/testing/how-to-write-tests.texi index 91a487098..9fdc6ffe3 100644 --- a/doc/testing/how-to-write-tests.texi +++ b/doc/testing/how-to-write-tests.texi @@ -20,10 +20,30 @@ There are many ways to test that a model is valid. In this chapter, we hope to cover some common cases that can be used as a guide to writing new tests. +@section Sample TestSuite skeleton + +When starting from scratch (i.e. not adding a TestCase to an existing +TestSuite), these things need to be decided up front: + +@itemize @bullet +@item What the test suite will be called +@item What type of test it will be (Build Verification Test, Unit Test, +System Test, or Performance Test) +@item Where the test code will live (either in an existing ns-3 module or +separately in src/test/ directory). You will have to edit the wscript +file in that directory to compile your new code, if it is a new file. +@end itemize + +See the file @code{src/test/sample-test-suite.cc} and corresponding +wscript file in that directory for a simple example, and see the directories +under @code{src/test} for more complicated examples. + @cartouche @emph{The rest of this chapter remains to be written} @end cartouche +@section How to add an example program to the test suite + @section Testing for boolean outcomes @section Testing outcomes when randomness is involved diff --git a/src/test/sample-test-suite.cc b/src/test/sample-test-suite.cc new file mode 100644 index 000000000..424a6e864 --- /dev/null +++ b/src/test/sample-test-suite.cc @@ -0,0 +1,66 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ + +// An essential include is test.h +#include "ns3/test.h" + +// Do not put your test classes in namespace ns3. You may find it useful +// to use the using directive to access the ns3 namespace directly +using namespace ns3; + +// This is an example TestCase. +class SampleTestCase1 : public TestCase +{ +public: + SampleTestCase1 (); + virtual ~SampleTestCase1 (); + +private: + virtual bool DoRun (void); +}; + +// Add some help text to this case to describe what it is intended to test +SampleTestCase1::SampleTestCase1 () + : TestCase ("Sample test case (does nothing)") +{ +} + +// This destructor does nothing but we include it as a reminder that +// the test case should clean up after itself +SampleTestCase1::~SampleTestCase1 () +{ +} + +// +// This method is the pure virtual method from class TestCase that every +// TestCase must implement +// +bool +SampleTestCase1::DoRun (void) +{ + // A wide variety of test macros are available in src/core/test.h + NS_TEST_ASSERT_MSG_EQ (true, true, "true doesn't equal true for some reason"); + // Use this one for floating point comparisons + NS_TEST_ASSERT_MSG_EQ_TOL (0.01, 0.01, 0.001, "Numbers are not equal within tolerance"); + + // Return false if an error has _not_ occurred + return false; +} + +// The TestSuite class names the TestSuite, identifies what type of TestSuite, +// and enables the TestCases to be run. Typically, only the constructor for +// this class must be defined +// +class SampleTestSuite : public TestSuite +{ +public: + SampleTestSuite (); +}; + +SampleTestSuite::SampleTestSuite () + : TestSuite ("sample", BVT) +{ + AddTestCase (new SampleTestCase1); +} + +// Do not forget to allocate an instance of this TestSuite +SampleTestSuite sampleTestSuite; diff --git a/src/test/wscript b/src/test/wscript new file mode 100644 index 000000000..494c1df3c --- /dev/null +++ b/src/test/wscript @@ -0,0 +1,12 @@ +## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*- + +def build(bld): + test = bld.create_ns3_module('test', ['core']) + test.source = [ + 'sample-test-suite.cc', + ] + + headers = bld.new_task_gen('ns3header') + headers.module = 'test' + headers.source = [ + ] diff --git a/src/wscript b/src/wscript index f4e035044..5cd3fce8b 100644 --- a/src/wscript +++ b/src/wscript @@ -42,6 +42,7 @@ all_modules = ( 'devices/mesh/flame', 'applications/ping6', 'applications/radvd', + 'test', 'test/ns3tcp', 'test/ns3wifi', 'contrib/flow-monitor',