C language unit test frame - CHECK

zhaozj2021-02-16  62

Although it has been developed with C language on UNIX for a while, I have to admit that my own unit test is not good. Just recently have a new development task, I learned some knowledge about test-driven development and prepare to improve my unit test. XP programming has been raised for a while, and has formed a lot of excellent unit test frameworks, such as JUnit, want to use Java friends, is very familiar with the test framework. I remember "Programmer" magazine also has a special TDD as a topic. In fact, I am really envious of Java programmers ^ _ ^, they always have a variety of excellent tools. Hey. . . ,Pro-Yuan Xian fish, as retreat webs. CPPUnit is an excellent C unit test framework, so it should also be used as a C language unit test framework. However, here I didn't choose CPPUnit, but directly selected a unit test framework for C language. Which unit test framework is used in C language, I don't have this experience ^ _ ^. If the friend has experience in the C language unit test, I really hope that you can give me help, here I thank you first, ^ _ ^! Just like what I said, because I don't have a lot of tests like this, so I just introduced the basic method of use of Check, a process of tested a key unit test environment. CHECK-related knowledge is that I am only in the morning, and I have summed up at night. I am a typical educational school to sell ^ _ ^.

I will introduce a program that implements addition functions (that is, 2 numbers, the program returns these two numbers, simple enough, ^ _ ^) unit testing process. First of all, I built 3 directories: include, add, unit_test. In the include directory contains UNI_TEST.H (I will introduce below this file), add.h, check.h (this file is one header file in the source code of the test framework. In the process of establishing a unit test, you need to include This header file). Fill in uniit_test.h and add.h to some of the most basic code uni_test.h # ifndef _Unit_test_h # define _Unit_test_h # Endif

#include "check.h"

#ifdef __cplusplusextern "c" {#ENDIF

#ifdef __cplusplus} #ENDIF

#ENDIF

In the above code, we contain the check.h header file. In the add.h header file, the basic code is similar in addition to not containing the header file.

Then, we build Add.c files in the Add directory and in it #include "add.h".

In the Unit_Test directory, we build TEST_ADD.C files (used to write test cases, including check.h), Test_main.c file (this file will be introduced, this contains main function) and libcheck.a (This static library is to compile the CHECK frame source code generation, which needs to be connected during compiling test cases.

OK, all the best, start writing test cases. Add Test case START_TEST (TEST_ADD) {FAIL_UNLESS (Add (2, 3) == 5, "GOD, 2 3! = 5");} end_test

Through this way, we define a test case. This test case name is Test_Add. And we expect Add (2, 3) to return 5 through macro fail_unless, if you do not return 5, then we will output the information of GOD, 2 3! = 5. At the same time, the test case is not passed by pass ^ _ ^, but Fail. Now we compile TEST_ADD.C, Test_main.c and add.c, which is of course compiled, because we have not realized current code. Add the following implementation code: int Add (INT i, int J) {RETURN 0;}

The corresponding function prototype is also added in Add.h.

Here we return 0 in implementing the code, deliberately make the test cases pass, because in TDD, it is not necessary to pass / through / reconfigure such a continuation process.

Now we compile the code, which is certainly compiled. However, we have not run our test cases yet. OK, is when adding our test case in Test_add.c: Suite * make_add_suite (void) {suite * s = suite_create ("add"); // Establish a test kit (I don't know, do you do this translation? ^ _ ^) TCase * tc_add = tcase_create ("add"); // Establish a test case

Suite_add_tcase (s, tc_add); // add the test case set to tcase_add_test (tc_add, test_add); // Add our test case to the test set

Return S;

Add function prototypes in Unit_Test.h: Suite * make_add_suite (void);

OK, it is time to introduce Test_main.c, the file code is as follows: #include "unit_test.h" #include

INT main (void) {Int n; srunner * SR;

SR = srunner_create (Make_ADD_SUITE ()); // Add Suite to Srunner

Srunner_run_all (sr, ck_normal); // Run all test cases

n = srunner_ntests_failed (sr); sRunner_Free (SR);

Return (n == 0)? EXIT_SUCCESS: EXIT_FAILURE;}

I want a smart friend, also guessed, why run the main function and test cases of the test case itself placed in different source files. It is convenient to add a test case, for example, I now add a sub-Sub program, so in order to keep a clear supile, the test of the SUB should organize the source file test_sub.c, now just need to be in Test_Main.c Srunner is added to the SUITE of Sub.

Now compile test case related files, run. Will see our test cases. How much passes, how much is not passed, and there is no passive test case fail there, etc.

Through the above introduction, you can find that the Check test framework and other test framework, such as the way CPPUnit is used. In fact, it is very simple from the perspective of using the test framework itself. It's hard to do it, how to do it, how to do it, when the program needs to access the database, how should we complete the preparation of test cases, these are difficult. I decided, go out tomorrow to buy a "test driver development" to see, then pay attention to the method of testing in the process of testing during the encoding process. When I have this experience, I will share it with everyone, and I welcome the brothers who already have this experience to give their own experience, refer to my mistakes in the article above, let my generation benefit! !

转载请注明原文地址:https://www.9cbs.com/read-22312.html

New Post(0)