Preface in embedded development, testing is an important part, but developers often ignore it. Therefore, the close integration of automated testing and code is the topic of this article. During code maintenance, our developers usually use the most version library tool. Most of the time the Code is modified, the code is submitted as soon as it is locally compiled. However, the following problem is ignored:
Will the program run normally after compilation?Obviously, this is not true. In preparation, you need to prepare the following tools: 1. Linux -- I used ubuntu2 here, and GCC -- Ubuntu is the default built-in 3. Hg -- version library management tool 4, unity -- test fixture 5, scons -- an automated compilation tool (you can also use makefile) to create a test project. First, we need to build a version library on the server,
>>mkdir hg_hook>>cd hg_hook>>hg init>>touch main.c>>touch Sconstruct
Then, modify main. C and the code is as follows:
#include <stdio.h>int main(void){ printf("hello world!\r\n"); return 0;}
Modify the sconstruct file with the following code:
import osimport sysBUILD = 'debug'# toolchainsCC = 'gcc'if BUILD == 'debug': CFLAGS = ' -Wall -g -O0'else: CFLAGS = ' -O2'CFLAGS += ' -I'+os.getcwd() + '/'TARGET = 'main.out'env = Environment(CC=CC, CCFLAGS=CFLAGS)env.Program(TARGET, 'main.c')env.Command('-r', TARGET, './'+TARGET)
Scons is an automatic compilation tool similar to makefile. If you have any questions, you can Google it online. Many scons are introduced.
Here I will introduce the last line:
env.Command('-r', TARGET, './'+TARGET)
This is a custom command that automatically executes the executable files generated after compilation. This makes it easy for us to test the program running results.
Input scons-R on the terminal to view the output result.
After the above work is completed, we can submit the source file once.
>>hg add .>>hg commit -m "packed init"
In this way, we have a version library. Modify the version library configuration because we need to use the Hg hook function, so we need to modify the Hg configuration file.
>>thg
Is the built-in GUI of Hg, go to file-> setting;
Click Edit file and enter:
[hooks]changegroup = updateupdate = scons -r
The first line indicates that update is automatically called when a user submits code. The second line indicates that scons-R is executed when Hg update is executed.
Click OK. Then enter:
>>hg serve
Start the version Library server.
The user submits the code. First, we clone the code from the server, and then modify the main. C code locally:
# Include <stdio. h> int main (void) {printf ("Hello world! \ R \ n "); X = 6; // added error code return 0 ;}
Run the local commit command and then pull to the server. The following information is displayed in the log window:
We can see that remote has returned a lot of information, and the compilation result is also returned. We can see that the code we submitted is not compiled correctly first. Here, we can basically use the hook of the version library to automatically compile the code to detect errors in the compilation phase of the program. Add the unity test fixture to the Hg-hook directory and add the sconscript file to the unity directory. The content is as follows:
objs = Object(Glob('*.c'))Return('objs')
Modify the sconstruct file under the Hg-hook directory and add several lines of code:
Import osimport sysbuild = 'debug' # toolchainscc = 'gcc 'If build = 'debug': cflags ='-wall-g-o0' else: cflags = '-O2' cflags + = '-I' + OS. getcwd () + <span style = "font-family: Arial, Helvetica, sans-serif;"> '-I' + OS. getcwd () + '/unit' </span> my_root = OS. path. normpath (OS. getcwd () # added code: Get the current root directory target = 'main. out 'env = environment (Cc = cc, ccflags = cflags) objs = Env. object (glob ("*. C ") # obtain the current source file object objs + = sconscript (['unity/sconscript']) # obtain all source file objects in the Unity folder Env. program (target, objs) ENV. command ('-R', target ,'. /'+ target)
Move the source program we remove the Hello world code from the main, create a hello-world.c, hello-world.h2 a source file; that is, the program hello-world.c we need to test
#include Hello-world.h
#ifndef __HELLO_WORLD_H__#define __HELLO_WORLD_H__void hello_world(void);#endif
Then we modify main. C:
#include <unity_fixture.h>static void run_all_test(void){ RUN_TEST_GROUP(hello_world);}static char *arg_string[] ={ "main", "-v",};int main(int argc, char *argv[]){ argc = sizeof(arg_string)/sizeof(char *); argv = arg_string; return UnityMain(argc, argv, run_all_test);}
This is the frame of our test fixture. Add a test case below.
Add test cases we add hello-world-test.c files for our function to test hello-world.
#include "unity_fixture.h"#include "hello-world.h"TEST_GROUP(hello_world);TEST_SETUP(hello_world){ printf("\r\nstart\r\n");}TEST_TEAR_DOWN(hello_world){ printf("end\r\n");}TEST(hello_world, test1){ hello_world();}
Add a hello-world-runner.c file for testing the startup of the fixture:
#include "unity_fixture.h"TEST_GROUP_RUNNER(hello_world){ RUN_TEST_CASE(hello_world, test1);}
Then we run Hg commit and Hg push. We can see that:
The Unity test fixture passed the hello-world test.
Automated Testing of the Hg version Library