ibooksonline

Unity Test File

Test cases that belong to a group are put in the same file. The typical name for the file is GroupNameTest.c. The GroupName is usually named after the module under test, such as CircularBuffer. When a module needs more than one TEST_GROUP to form a group of tests with common setup, introduce another group like CircularBufferPrint for the tests that are concerned with printing a CircularBuffer.

Here is a summary of the elements of a TEST_GROUP that go in a test file:

 //test harness include
 #include "unity_fixture.h"
 
 //#includes for module under test
 
 TEST_GROUP(GroupName)
 
 //Define file scope data accessible to test group members prior to TEST_SETUP.
 
 TEST_SETUP(GroupName)
 {
  //initialization steps are executed before each TEST
 }
 
 TEST_TEAR_DOWN(GroupName)
 {
  //clean up steps are executed after each TEST
 }
 
 TEST(GroupName, UniqueTestName)
 {
  /*
  * A TEST contains:
  * TEST specific initializations
  * operations on the code under test
  * TEST specific condition checks
  */
 }
 
 //There can be many tests in a TEST_GROUP
 TEST(GroupName, AnotherUniqueTestName)
 {
  /*
  * Some more test statements
  */
 }
 
 //Each group has a TEST_GROUP_RUNNER
 TEST_GROUP_RUNNER(GroupName)
 {
  //Each TEST has a corresponding RUN_TEST_CASE
  RUN_TEST_CASE(GroupName, UniqueTestName);
  RUN_TEST_CASE(GroupName, AnotherUniqueTestName);
 }

A TEST is a macro used to declare test cases. A TEST_GROUP associates numerous TEST cases with their TEST_SETUP and TEST_TEAR_DOWN functions. In the previous code example, the common parameter, GroupName, associates the TEST_GROUP_RUNNER, TEST_GROUP, TEST_SETUP, TEST_TEAR_DOWN, and each of the TEST cases. You could have multiple TEST_GROUPs in a file, but it is more typical to have only one.

  • TEST_GROUP(GroupName) defines the test group. Each TEST_GROUP in your test build must have a unique name.

  • TEST_SETUP(GroupName) is run before every TEST in the TEST_GROUP. Common initialization for each associated TEST goes into TEST_SETUP.

  • TEST_TEAR_DOWN(GroupName) is run after each TEST in the TEST_GROUP, restoring the system to its previous state. Common cleanup code goes into TEST_TEAR_DOWN.

  • TEST(GroupName, TestName) defines all the steps of a test case. The GroupName and TestName pairs must be unique in the test build.

  • TEST_GROUP_RUNNER(GroupName) is responsible for invoking RUN_TEST_CASE for each TEST in the group. If you forget to enter your TEST here, it won’t run. To keep from having to scroll to your TEST_GROUP_RUNNER, you can put it in a separate file, typically named GroupNameTestRuner.c.

  • RUN_TEST_CASE(GroupName, TestName) runs the associated TEST.

It’s important to note that the first failure terminates the calling test.