Chapter 2. Writing embUnit Tests

Embedded Unit Framework

Tests, Test Cases and Suites

Below is a program listing for unit tests of module counter.c. The full example can be found in /examples folder.

1

Include references for embUnit framework and unit under test.

2

setUp() and tearDown() functions for test case.

3

Unit test for checking the counter is initialized on construction.

4

Collect all the unit tests under fixtures with a string as name of test.

5

Create a new test case from setUp(), tearDown(), and fixtures.

6

Return with pointer to test suite just created.

          

#include <embUnit/embUnit.h>                         1  
#include "counter.h"

CounterRef counterRef;

static void setUp(void)                              2  
{
	counterRef = Counter_counter();
}

static void tearDown(void)
{
	Counter_dealloc(counterRef);
}

static void testInit(void)                           3  
{
	TEST_ASSERT_EQUAL_INT(0, Counter_value(counterRef));
}

static void testSetValue(void)
{
	Counter_setValue(counterRef,1);
	TEST_ASSERT_EQUAL_INT(1, Counter_value(counterRef));

	Counter_setValue(counterRef,-1);
	TEST_ASSERT_EQUAL_INT(-1, Counter_value(counterRef));
}

TestRef CounterTest_tests(void)
{
  EMB_UNIT_TESTFIXTURES(fixtures) {                  4  
    new_TestFixture("testInit",testInit),
    new_TestFixture("testSetValue",testSetValue),
	};
                                                     5  
  EMB_UNIT_TESTCALLER(CounterTest,"CounterTest",setUp,tearDown,fixtures);
  return (TestRef)&CounterTest;                      6  
}