Unity Syntax and Macros

Test Functions

Your test code is organized in test cases, where each test case is just a C function:

#include "unity.h"

void testSomethingInterestingShouldHappen(void)
{
  // your test code goes here
}

ATTENTION: In order to distinguish unit tests from arbitrary C functions in the same file, the script that generates the test runner considers only those functions whose name starts with "test" as unit tests. Please make sure that you respect this rule!

If you have common code that is run before and after each test case, move it to these two special functions:

void setUp(void)
{
  // prepare test environment
}

void tearDown(void)
{
  // clean up after test
}

 

Assertion Macros

Testing a Boolean expression is true:

TEST_ASSERT( a == 4 );

Testing a Boolean expression with explicit outcome:

TEST_ASSERT_TRUE( flag );
TEST_ASSERT_FALSE( !flag );

Testing a Boolean expression, supplying a message that will be displayed if it fails:

TEST_ASSERT_MESSAGE( 2 == 1 + 1, "The end is coming!");

Testing equality of various types:

TEST_ASSERT_EQUAL_INT( 5, x );
TEST_ASSERT_EQUAL_UINT16( 0xcafe, cup );
TEST_ASSERT_EQUAL_FLOAT( 3.15, pi );
TEST_ASSERT_EQUAL_STRING( "Hello world!", s );

This also works for arrays:

TEST_ASSERT_EQUAL_INT_ARRAY( expectedArray, a, numElements );

You can add a message to about anything of the above:

TEST_ASSERT_EQUAL_INT_MESSAGE( 5, x, "This is bad!" );

This test always fails, it can be used to remind you of a missing test case:

TEST_FAIL_MESSAGE( "This is a gentle reminder" );

You can ignore (the rest of) a test case if there are some issues you want to postpone. Use with care!

TEST_IGNORE( ); // FIXME: should be reactivated later