-
Notifications
You must be signed in to change notification settings - Fork 46
Testing
We will be using the Unity testing framework for C. [https://github.com/ThrowTheSwitch/Unity]
Every test will call a user-supplied function before and after the test. Here are declarations for these functions:
void tearDown(void);
void setUp(void);
Mine do nothing, but they may be useful depending on your test dependencies.
Finally, you need something to test. You can test anything you like with Unity. There are too many options to list here, see unity.h for the full list.
At any rate, here is my olsr-test.c file in its entirety:
#include <ross.h>
#include "unity.h"
void tearDown(void)
{
}
void setUp(void)
{
}
void test_region(void)
{
TEST_ASSERT_EQUAL(region(15), 0);
}
The ruby script will pull out any function starting with test
and add it to the ruby-generated test runner. It will also call setUp()
before each test and subsequently call tearDown()
after each test has completed.
This solution started with this link.
Start by creating a model with a CMakeLists.txt file that looks like the following:
INCLUDE_DIRECTORIES(${ROSS_SOURCE_DIR})
SET(olsr_srcs
olsr-driver.c
olsr.h
)
ADD_EXECUTABLE(olsr-j olsr-main.c ${olsr_srcs})
TARGET_LINK_LIBRARIES(olsr-j ROSS m)
IF(UNIT_TESTS)
SET(TEST_DIR ${ROSS_SOURCE_DIR}/../test)
INCLUDE_DIRECTORIES(${TEST_DIR})
# Generate our test_Runner.c file from our C file containing the tests
ADD_CUSTOM_COMMAND(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/olsr-test_Runner.c
COMMAND /usr/bin/ruby ${CMAKE_SOURCE_DIR}/test/auto/generate_test_runner.rb ${CMAKE_CURRENT_SOURCE_DIR}/olsr-test.c
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/olsr-test.c
)
ADD_EXECUTABLE(olsr-test ${TEST_DIR}/unity.c olsr-test.c olsr-test_Runner.c ${olsr_srcs})
TARGET_LINK_LIBRARIES(olsr-test ROSS m)
GET_TARGET_PROPERTY(test_binary olsr-test LOCATION)
# Make sure you run the test executable after building the binary
ADD_CUSTOM_TARGET(tester ALL DEPENDS olsr-test)
ADD_CUSTOM_COMMAND(
TARGET tester
COMMAND ${test_binary}
DEPENDS olsr-test
)
ENDIF(UNIT_TESTS)
Please note that your model will not have olsr in the filenames. Actually, some of the above should already be familiar to you. The new part is in the
IF(UNIT_TESTS)
block. This will only be turned on if you enable unit testing in ROSS (you can do this at configuration time using ccmake). Your CMakeLists.txt should look very similar to this. Instead of generated olsr-test_Runner.c
, you will generate your-model-test_Runner.c
. It gets generated by running a ruby program on your your-model-test.c
. It adds an executable, olsr-test
to your build. The ADD_CUSTOM
commands at the bottom make sure your tests run every time you make a change.