The following example gives a small demonstration of how to use nlUnitTest to write a unit test
#include <lib/support/UnitTestContext.h> #include <lib/support/UnitTestRegistration.h> #include <nlunit-test.h> class YourTestContext : public Test::AppContext { ... }; static void TestName(nlTestSuite * apSuite, void * apContext) { // If you register the test suite with a context, cast // apContext as appropriate YourTestContext * ctx = static_cast<YourTestContext *>(aContext); // Do some test things here, then check the results using NL_TEST_ASSERT NL_TEST_ASSERT(apSuite, <boolean condition>) } static const nlTest sTests[] = { NL_TEST_DEF("TestName", TestName), // Can have multiple of these NL_TEST_SENTINEL() // If you forget this, you’re going to have a bad time }; nlTestSuite sSuite = { "TheNameOfYourTestSuite", // Test name &sTests[0], // The list of tests to run TestContext::Initialize, // Runs before all the tests (can be nullptr) TestContext::Finalize // Runs after all the tests (can be nullptr) }; int YourTestSuiteName() { return chip::ExecuteTestsWithContext<YourTestContext>(&sSuite); // or “without” } CHIP_REGISTER_TEST_SUITE(YourTestSuiteName)
Each test gets an nlTestSuite object (apSuite) that is passed into the test assertions, and a void* context (apContext) that is yours to do with as you require.
The apContext should be derived from Test::AppContext
See TestSpan.cpp for a great example of a good unit test.
We have a small number of unit testing utilities that should be used in unit tests.
Consider adding more utilities for general use if you require them for your tests.
The mock clock is located in src/system/SystemClock.h as System::Clock::Internal::MockClock
.
To use the mock clock, use the chip::System::SystemClock()
function as normal. In the test, instantiate a MockClock and use the SetSystemClockForTesting
to inject the clock. The Set and Advance functions in the MockClock can then be used to set exact times for testing. This allows testing specific edge conditions in tests, helps reduce test flakiness due to race conditions, and reduces the time required for testing as tests no long require real-time waits.
The TestPersistentStorageDelegate is an in-memory version of storage that easily allows removal of keys, presence checks, etc. It is available at src/lib/support/TestPersistentStorageDelegate.h