Objective:
Although some C + + Automation unit Testing framework can also be used for C language unit testing, but then we write C language program need to conform to C + + standards, so that some of the characteristics of C is not used, limited the use of C features is not very good, so found a full C implementation of the automated testing framework. Unity is a simple and straightforward automated unit testing framework that consists of a small number of files.
Build the Environment:
1. Download the latest version of Unity source from GitHub: https://github.com/ThrowTheSwitch/Unity
2. File Directory Introduction
SRC This folder contains unity.c, Unity.h, Unity_internals.h, which is the most basic unity composition, with these three files you can begin to write test cases.
Docs Unity-related documents are here
Examples uses unity for several examples, including basic use, fixture usage, and the use of Ruby Auto-generated use cases
The extras is attached to the fixture to achieve the function.
Test Unity all the tests
Auto simplifies the ruby script for test case building
3. According to the different target platform to configure the Unity_config.h file, I am using the cortex-m3, the configuration is as follows:
#define Unity_exclude_limits_h
Call Limits.h, Uint_max and Ulong_max are 32-bit by default
#define UNITY_POINTER_WIDTH 32
Set Pointer to 32 bits
#define Unity_include_float
#define Unity_include_double
Supports float and double types
#ifdef __gnuc__
#define UNITY_OUTPUT_CHAR (a) __io_putchar (a)
#else
#define UNITYPUTC (a) FPUTC (A, stdout)
#define UNITY_OUTPUT_CHAR (a) UNITYPUTC (a)
#endif
Keil ARMCC If you use the GNU standard, you need to redirect to __io_putchar otherwise redirect to FPUTC.
#define Unity_weak_attribute __attribute ((WEAK))
Set the setup, Teardown, Suitesetup, suiteteardown as weak functions in unity, such as having the setup function in your own project is automatically invalid in unity and does not create a redefinition problem.
Specific to the source code is subject to the introduction.
4. Add the Unity_include_config_h macro to the unity_internals.h.
5. Add # include <stdio.h> in Unity.h
6. Add Unity source files to Keil
7. Import header file directory path
The redirect to printf in 8.debug.c.
#ifdef __gnuc__#definePutchar_prototype int __io_putchar (int ch)/* with gcc/raisonance, small printf (option LD Linker->libraries->small printf set to ' Yes ') calls __io_putchar () */#else #definePutchar_prototype int FPUTC (int ch, FILE *f)#endif/* __gnuc__ */putchar_prototype{BYTE C=(BYTE) ch; if(DEMCR &Trcena) { while(Itm_port32 (0) ==0) {} itm_port8 (0) =C; } returnch;}
9. Switch to SW mode
10. Set the clock according to the processor and open ITM Port 0
11. Write the test case according to the test document in examples, open the debug (printf) Viewer and run the following effect:
Building Automation Unit Test framework in Keil Unity