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 );

TEST_ASSERT_EQUAL_INT_MESSAGE( 5, x, "This is bad!" );
TEST_FAIL_MESSAGE( "This is a gentle reminder" );