C language unit test method, system and equipment based on Cmocka and storage medium
By automatically generating instrumentation functions and implementing version control, the problems of time-consuming and labor-intensive manual operations and high version management costs in C language project unit testing are solved, and an efficient automated testing process is achieved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- SICHUAN ZHONGRUI INFORMATION TECH CO LTD
- Filing Date
- 2025-12-29
- Publication Date
- 2026-04-14
AI Technical Summary
In C language project unit testing, existing technologies suffer from problems such as test preparation relying on manual operations, which are time-consuming, labor-intensive, and prone to errors; instrumentation function generation lacks automated means; and version management is costly.
By scanning the source files to be tested, extracting function prototype information, automatically generating instrumented functions and performing version control, and using conditional compilation technology to generate Makefile files, an automated testing process is achieved.
It can identify functions to be instrumented, generate instrumented functions and manage versions without manual intervention, simplifying the testing process, improving testing efficiency, and reducing error rates and management costs.
Smart Images

Figure CN121858415A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of software testing technology, specifically to a C language unit testing method, system, device, and storage medium based on Cmocka. Background Technology
[0002] When using the Cmocka framework for unit testing of C language projects, the following technical pain points are commonly encountered:
[0003] (1) Test preparation work relies on a lot of manual operation. Testers need to manually write instrumentation functions (also called stubbing functions), configure compilation files, and manually mark the functions to be instrumented. This is not only time-consuming and laborious, but also very easy to cause test errors due to human operation errors.
[0004] (2) The instrumentation function generation lacks automated means. Testers need to manually identify the functions to be instrumented in the project one by one, and then manually write the instrumentation function by adding the "__wrap_" prefix to the target function. This process is inefficient and cannot adapt to scenarios where functions change frequently.
[0005] (3) Lack of instrumentation function management mechanism. When there are multiple test versions of a project, if the instrumentation functions are not effectively managed, problems such as conflicts between different versions of instrumentation functions and compilation failures may occur. Especially in large C language projects, the cost of manually managing instrumentation functions increases exponentially, and may even make it difficult to continue the testing work. Summary of the Invention
[0006] The purpose of this invention is to provide a C language unit testing method, system, device, and storage medium based on Cmocka, which solves the problems of the lack of automated means for instrumentation function generation in existing unit testing, the need for manual intervention leading to time-consuming, labor-intensive, error-prone, inefficient, and costly instrumentation function version management.
[0007] The present invention solves the above problems through the following technical solution:
[0008] A C language unit testing method based on Cmocka includes:
[0009] Step S1: Scan the set of source files to be tested, extract the function prototype information of all functions and store it;
[0010] Step S2: Perform compilation and linking analysis on the source file to be tested to obtain an initial set of functions to be instrumented. Combine the initial functions to be instrumented with the function prototype information to determine the complete prototype information of each function to be instrumented, and obtain the target set of functions to be instrumented.
[0011] Step S3: Automatically generate and store the corresponding instrumentation function based on the complete prototype information of the target function to be instrumented, and use conditional compilation technology to implement version control of the instrumentation function;
[0012] Step S4: Generate a complete Makefile for compilation based on the list of mock functions of the instrumented functions;
[0013] Step S5: Call the make tool to execute the complete Makefile file to complete the compilation and unit test execution.
[0014] This invention enables the automatic generation of instrumentation functions without manual intervention, improving efficiency and reducing the probability of human error; it adds corresponding conditional compilation instructions to different versions of instrumentation functions to ensure that the corresponding version of the instrumentation function can be called in different test scenarios, avoiding version conflicts, and realizing automatic management of instrumentation function versions, reducing management costs; it automatically generates compilation files and performs automatic compilation and unit testing, realizing an automated testing process.
[0015] Furthermore, the function prototype information includes the function return type, function name, parameter types, and number of parameters.
[0016] Furthermore, the step of performing compilation and linking analysis on the source file to be tested to obtain the initial set of functions to be instrumented includes:
[0017] The C language compiler (such as GCC) is invoked to compile and link the source code of the source file to be tested, simulating the actual compilation process.
[0018] During the compilation and linking process, key information output by the compiler is collected in real time, including the symbol table and undefined symbols.
[0019] From the collected key information, all undefined variables or functions are filtered out, and after deduplication, a set of initialization variables and functions to be instrumented is formed.
[0020] Furthermore, the complete prototype information includes return values and a parameter list.
[0021] Furthermore, the step of automatically generating and storing the instrumentation function based on the prototype information of the target instrumentation function includes:
[0022] The instrumentation function is automatically generated based on the complete prototype information of the target function. The instrumentation function follows the naming conventions of the Cmocka framework, that is, the "__wrap_" prefix is added before the original function name. For functions with return values, the mock_type(T) interface is inserted inside the function body to simulate the return of test data of type T. For example, if the original function prototype is "int func(int a, int b)", the automatically generated instrumentation function is "int __wrap_func(int a, intb) { return mock_type(int);}".
[0023] All automatically generated instrumentation functions are saved to the mock_func.c file to achieve centralized storage of instrumentation functions.
[0024] Furthermore, the specific method for implementing version control of instrumentation functions using conditional compilation technology is as follows: by defining version macros (such as "#define MOCK_VERSION 1.00"), corresponding conditional compilation instructions are added to instrumentation functions of different versions so that the corresponding version of the instrumentation function can be called in different test scenarios, thus avoiding version conflicts.
[0025] Further, step S4 specifically includes:
[0026] The list of instrumented mock functions is automatically written into the Makefile as compilation parameters. At the same time, the compilation and linking configurations related to the Cmocka framework in the Makefile are completed (such as specifying the Cmocka library path, linking options, etc.), generating a complete Makefile that can be used directly for compilation.
[0027] Furthermore, step S5 specifically includes:
[0028] The make tool is invoked to execute the Makefile, which completes the compilation and linking of the code under test, instrumented functions, and the Cmocka testing framework, generating an executable test program.
[0029] Running the executable test program, combined with the assertion mechanism and test case management function of the Cmocka framework, enables automated unit testing of C language projects.
[0030] A C language unit testing system based on Cmocka includes:
[0031] The function prototype information extraction module is used to scan the set of source files to be tested, extract the function prototype information of all functions and store it.
[0032] The module for obtaining functions to be instrumented is used to perform compilation and linking analysis on the source file to be tested to obtain an initial set of functions to be instrumented, and to combine the initial functions to be instrumented with the function prototype information to determine the complete prototype information of each function to be instrumented, thereby obtaining the target set of functions to be instrumented.
[0033] The automatic instrumentation and version control module is used to automatically generate and store the corresponding instrumentation function based on the complete prototype information of the target function to be instrumented, and to implement version control of the instrumentation function using conditional compilation technology.
[0034] The automatic compilation module is used to generate a complete Makefile for compilation based on the list of mock functions of the instrumented functions;
[0035] The compilation and unit test execution module is used to call the make tool to execute the complete Makefile file, thereby completing the compilation and unit test execution.
[0036] An electronic device includes a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the C language unit testing method based on Cmocka.
[0037] A computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the C language unit testing method based on Cmocka.
[0038] Compared with the prior art, the present invention has the following advantages and beneficial effects:
[0039] (1) This invention can automatically identify the function to be stubbed, create the stub function, manage the stub function version based on the project source code without manual intervention, and automatically generate Makefile compilation file based on the stub function information, thereby simplifying the testing process, improving testing efficiency, and reducing the testing and maintenance costs of large projects.
[0040] (2) The present invention eliminates the need for manual writing of stub functions, marking of functions to be stubbed, and configuration of compilation files, reducing test preparation time from several hours to minutes, greatly improving test efficiency and achieving a high degree of automation.
[0041] (3) This invention automatically identifies the function to be instrumented through compiler symbol analysis and syntax parsing, avoiding the problems of missed or incorrect judgments in manual identification. The instrumentation function generation logic follows the Cmocka specification, reducing the syntax error rate and improving accuracy.
[0042] (4) This invention uses conditional compilation to implement stub function version control, supports switching of test requirements in multiple scenarios, solves the problem of conflict between multiple versions of stub functions in large projects, reduces management costs by more than 60%, and improves version management efficiency.
[0043] (5) This invention supports C language projects of different sizes and adapts different compiler paths and Cmocka library locations through configuration files. It does not require customized development for specific projects and has a wide range of applications and strong versatility. Attached Figure Description
[0044] Figure 1 This is a flowchart of a C language unit testing method based on Cmocka, as described in an embodiment of the present invention. Detailed Implementation
[0045] The present invention will be further described in detail below with reference to embodiments, but the implementation of the present invention is not limited thereto.
[0046] Example:
[0047] Combined with appendix Figure 1 As shown, a C language unit testing method based on Cmocka includes:
[0048] Step S1: Scan the source files, obtain function prototypes, etc., and store them.
[0049] In this step, the set of source files to be tested is scanned, and the function prototype information of all functions is extracted and stored. The function prototype information includes the function return type, function name, parameter type and number of parameters, etc.
[0050] Step S2: Obtain the set of functions to be instrumented
[0051] In this step, the source file to be tested is compiled and linked to obtain an initial set of functions to be instrumented. The initial functions to be instrumented are combined with the function prototype information to determine the complete prototype information of each function to be instrumented, and the target set of functions to be instrumented is obtained.
[0052] Furthermore, step S2 specifically includes:
[0053] The C language compiler (such as GCC) is invoked to compile and link the source code of the source file to be tested, simulating the actual compilation process.
[0054] During the compilation and linking process, key information output by the compiler is collected in real time, including the symbol table and undefined symbols.
[0055] All undefined variables or functions are filtered out from the collected key information, and after deduplication, a set of initialization variables and functions to be instrumented is formed.
[0056] The initial function to be instrumented is precisely matched with the function prototype information to determine the complete prototype information of each function to be instrumented (such as return value and parameter list), and finally the target set of functions to be instrumented is formed.
[0057] Step S3: Use the instrumentation tool to perform instrumentation and perform version control.
[0058] In this step, the corresponding instrumentation function is automatically generated and stored based on the complete prototype information of the target function to be instrumented, and the version control of the instrumentation function is implemented using conditional compilation technology.
[0059] Furthermore, step S3 specifically includes:
[0060] The instrumentation function is automatically generated based on the complete prototype information of the target function. The instrumentation function follows the naming conventions of the Cmocka framework, that is, the "__wrap_" prefix is added before the original function name. For functions with return values, the mock_type(T) interface is inserted inside the function body to simulate the return of test data of type T. T is a data type, which can be int, etc. For example, if the original function prototype is "int func(int a, int b)", the automatically generated instrumentation function is "int __wrap_func(int a, int b) { return mock_type(int);}".
[0061] All automatically generated instrumentation functions are saved to the mock_func.c file to achieve centralized storage of instrumentation functions;
[0062] By defining version macros (such as "#define MOCK_VERSION 1.00"), corresponding conditional compilation directives can be added to instrumentation functions of different versions, so that the corresponding version of the instrumentation function can be called in different test scenarios, thus avoiding version conflicts.
[0063] Step S4: Add the instrumentation function information as compilation parameters to the Makefile compilation file.
[0064] In this step, a complete Makefile for compilation is generated based on the list of mock functions of the instrumented functions;
[0065] Further, step S4 specifically includes:
[0066] The list of instrumented mock functions is automatically written into the Makefile as compilation parameters. At the same time, the compilation and linking configurations related to the Cmocka framework in the Makefile are completed (such as specifying the Cmocka library path, linking options, etc.), generating a complete Makefile that can be used directly for compilation.
[0067] Step S5: Compile using make and perform unit tests using Cmocka.
[0068] In this step, the make tool is invoked to execute the complete Makefile, completing the compilation and unit test execution.
[0069] Furthermore, step S5 specifically includes:
[0070] The make tool is invoked to execute the Makefile, which completes the compilation and linking of the code under test, instrumented functions, and the Cmocka testing framework, generating an executable test program.
[0071] Running the executable test program, combined with the assertion mechanism and test case management function of the Cmocka framework, enables automated unit testing of C language projects.
[0072] This invention enables the automatic generation of instrumentation functions without manual intervention, improving efficiency and reducing the probability of human error; it adds corresponding conditional compilation instructions to different versions of instrumentation functions to ensure that the corresponding version of the instrumentation function can be called in different test scenarios, avoiding version conflicts, and realizing automatic management of instrumentation function versions, reducing management costs; it automatically generates compilation files and performs automatic compilation and unit testing, realizing an automated testing process.
[0073] The key steps of the above method are illustrated below using a specific unit testing case:
[0074] 1. Environment Configuration: Set up a runtime environment that includes the GCC compiler, the Cmocka testing framework (version 1.1.5 and above), and the Python interpreter (version 3.7 and above). The Python interpreter is used to implement the logic control of each module, and the GCC compiler provides compilation, linking, and symbol information collection capabilities.
[0075] 2. Source code import: Import the source code of the C language project to be tested into the tool, specify the path of the source file to be tested through the configuration file, and write the source code path and libcmocka library path into the Makefile.
[0076] 3. Source code scanning execution: Start the source code scanning module, call Python's "pycparser" library to perform syntax parsing on the source file to be tested, extract function prototypes, and store them in the SQLite database. Each record contains the function name, return value type, parameter list, and file path.
[0077] 4. Instrumentation Function Identification: The instrumentation function identification module calls the GCC compiler to compile the source code, collects undefined symbols through the "nm*.o" command, filters out undefined functions, removes duplicates, matches them with functions in the database, determines their prototypes, and forms a set of instruments to be instrumented.
[0078] 5. Instrumentation Function Generation and Version Management: Read the prototype of the function to be instrumented, automatically generate the instrumentation function "int __wrap_func(int key) { return mock_type(int);}", and write it to the "mock / mock_func.c" file; add version macro definitions "#define MOCK_V1 1" and "#define MOCK_V2 2" to the beginning of the file. If the return logic needs to be modified later, add conditional compilation code "#if defined(MOCK_V1) return mock_type(int); #elif defined(MOCK_V2) return mock_type(int) * 2; #endif" to achieve version switching.
[0079] 6. Compilation File Generation and Test Execution: Read the path to "mock / mock_func.c" and the information of the file to be compiled, generate a Makefile, which includes configurations such as "CC=gcc", "CFLAGS=-Wall -I / include", "LDFLAGS=-L / usr / local / lib -lcmocka", and the linking rule "test_exec: build / main.o mock / mock_func.c"; call the "make" command to execute the compilation, generate the executable file "test_exec", and output the test report through Cmocka after running it.
[0080] The unit testing method of this embodiment has the following advantages:
[0081] (1) High degree of automation: The entire process does not require manual writing of instrumentation functions, marking of functions to be instrumented and configuration of compilation files, which reduces the test preparation time from several hours to minutes, greatly improving test efficiency.
[0082] (2) Improved accuracy: The compiler automatically identifies the function to be instrumented through symbol analysis and syntax parsing, avoiding the problems of missed or incorrect judgments in manual identification. The instrumentation function generation logic follows the Cmocka specification, reducing the syntax error rate.
[0083] (3) Efficient version management: The instrumentation function version control is implemented by conditional compilation, which supports switching of test requirements in multiple scenarios, solves the problem of conflict between multiple versions of instrumentation functions in large projects, and reduces management costs by more than 60%.
[0084] (4) High versatility: It supports C language projects of different sizes, adapts to different compiler paths and Cmocka library locations through configuration files, and does not require customized development for specific projects, making it widely applicable.
[0085] On the other hand, this embodiment proposes a C language unit testing system based on Cmocka, corresponding to the aforementioned C language unit testing method based on Cmocka, including:
[0086] The function prototype information extraction module is used to scan the set of source files to be tested, extract the function prototype information of all functions and store it.
[0087] The module for obtaining functions to be instrumented is used to perform compilation and linking analysis on the source file to be tested to obtain an initial set of functions to be instrumented, and to combine the initial functions to be instrumented with the function prototype information to determine the complete prototype information of each function to be instrumented, thereby obtaining the target set of functions to be instrumented.
[0088] The automatic instrumentation and version control module is used to automatically generate and store the corresponding instrumentation function based on the complete prototype information of the target function to be instrumented, and to implement version control of the instrumentation function using conditional compilation technology.
[0089] The automatic compilation module is used to generate a complete Makefile for compilation based on the list of mock functions of the instrumented functions;
[0090] The compilation and unit test execution module is used to call the make tool to execute the complete Makefile file, thereby completing the compilation and unit test execution.
[0091] Since the C language unit testing system based on Cmocka in this embodiment corresponds to the aforementioned C language unit testing method based on Cmocka, the C language unit testing system based on Cmocka also has the same beneficial effects as the C language unit testing method based on Cmocka.
[0092] Furthermore, this embodiment also proposes an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the C language unit testing method based on Cmocka.
[0093] Finally, this embodiment proposes a computer-readable storage medium storing a computer program that, when executed by a processor, implements the C language unit testing method based on Cmocka.
[0094] Although the present invention has been described herein with reference to illustrative embodiments, the above embodiments are merely preferred embodiments of the present invention, and the implementation of the present invention is not limited to the above embodiments. It should be understood that those skilled in the art can devise many other modifications and implementations, which will fall within the scope and spirit of the principles disclosed in this application.
Claims
1. A C language unit testing method based on Cmocka, characterized in that, include: Step S1: Scan the set of source files to be tested, extract the function prototype information of all functions and store it; Step S2: Perform compilation and linking analysis on the source file to be tested to obtain an initial set of functions to be instrumented. Combine the initial functions to be instrumented with the function prototype information to determine the complete prototype information of each function to be instrumented, and obtain the target set of functions to be instrumented. Step S3: Automatically generate and store the corresponding instrumentation function based on the complete prototype information of the target function to be instrumented, and use conditional compilation technology to implement version control of the instrumentation function; Step S4: Generate a complete Makefile for compilation based on the list of mock functions of the instrumented functions; Step S5: Call the make tool to execute the complete Makefile file to complete the compilation and unit test execution.
2. The C language unit testing method based on Cmocka according to claim 1, characterized in that, The function prototype information includes the function return type, function name, parameter types, and number of parameters.
3. The C language unit testing method based on Cmocka according to claim 1, characterized in that, The steps of performing compilation and linking analysis on the source file to be tested to obtain the initial set of functions to be instrumented include: The source code of the file to be tested is compiled and linked to simulate the actual compilation process. During the compilation and linking process, key information output by the compiler is collected in real time, including the symbol table and undefined symbols. From the collected key information, all undefined variables or functions are filtered out, and after deduplication, a set of initialization variables and functions to be instrumented is formed.
4. The C language unit testing method based on Cmocka according to claim 1, characterized in that, The step of automatically generating and storing the instrumentation function based on the prototype information of the target instrumentation function includes: The corresponding instrumentation function is automatically generated based on the complete prototype information of the target function to be instrumented. The instrumentation function adds the prefix "__wrap_" before the original function name. For functions with return values, the mock_type(T) interface is inserted inside the function body to simulate the return of test data of type T. All automatically generated instrumentation functions are saved to the mock_func.c file to achieve centralized storage of instrumentation functions.
5. The C language unit testing method based on Cmocka according to claim 4, characterized in that, The specific method for implementing version control of instrumentation functions using conditional compilation technology is as follows: by defining version macros, corresponding conditional compilation instructions are added to instrumentation functions of different versions so that the corresponding version of the instrumentation function can be called in different test scenarios.
6. The C language unit testing method based on Cmocka according to claim 4, characterized in that, Step S4 specifically includes: The list of instrumented mock functions is automatically written into the Makefile as compilation parameters. At the same time, the compilation and linking configurations related to the Cmocka framework in the Makefile are completed, generating a complete Makefile that can be used directly for compilation.
7. The C language unit testing method based on Cmocka according to claim 6, characterized in that, Step S5 specifically includes: The make tool is invoked to execute the Makefile, which completes the compilation and linking of the code under test, instrumented functions, and the Cmocka testing framework, generating an executable test program. Running the executable test program, combined with the assertion mechanism and test case management function of the Cmocka framework, enables automated unit testing of C language projects.
8. A C language unit testing system based on Cmocka, characterized in that, include: The function prototype information extraction module is used to scan the set of source files to be tested, extract the function prototype information of all functions and store it. The module for obtaining functions to be instrumented is used to perform compilation and linking analysis on the source file to be tested to obtain an initial set of functions to be instrumented, and to combine the initial functions to be instrumented with the function prototype information to determine the complete prototype information of each function to be instrumented, thereby obtaining the target set of functions to be instrumented. The automatic instrumentation and version control module is used to automatically generate and store the corresponding instrumentation function based on the complete prototype information of the target function to be instrumented, and to implement version control of the instrumentation function using conditional compilation technology. The automatic compilation module is used to generate a complete Makefile for compilation based on the list of mock functions of the instrumented functions; The compilation and unit test execution module is used to call the make tool to execute the complete Makefile file, thereby completing the compilation and unit test execution.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the C language unit testing method based on Cmocka as described in any one of claims 1-7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When executed by a processor, the computer program implements the C language unit testing method based on Cmocka as described in any one of claims 1-7.