A method for automatically generating unit test scripts based on the CppUnit framework
By using a unit test script automatic generation method based on the CppUnit framework, which utilizes Excel spreadsheets to automate the generation of unit test scripts, the problem of complex script writing and high error rate in embedded software development is solved, thereby improving testing efficiency and quality.
Patent Information
- Application Number
- CN202311535778.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-17
- Publication Date
- 2026-11-10
- Estimated Expiration
- 2043-11-17
AI Technical Summary
In embedded software development, using the CppUnit framework for unit testing involves a large workload of script writing and modification, which is prone to errors, resulting in low testing efficiency and quality, and increasing the learning cost for testers.
Design a method for automatically generating unit test scripts based on the CppUnit framework. The method enables the development of test case scripts by filling in an Excel spreadsheet, reducing the difficulty of script writing, standardizing script format, and generating compileable and executable unit test scripts with one click.
The complex CppUnit unit testing framework is transformed into a simple Excel spreadsheet, reducing the error rate, improving the efficiency and quality of unit testing, and enabling rapid writing and batch design of test cases.
Smart Images

Figure CN117591403B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of software engineering technology, and more specifically to a method for automatically generating unit test scripts based on the CppUnit framework. Background Technology
[0002] In the development and maintenance of embedded software, unit testing is necessary to improve software product quality and reduce code defect leaks. CppUnit, ported from JUnit, is an open-source unit testing framework widely used in embedded software unit testing. However, using CppUnit for unit testing involves a significant amount of work in script writing and modification, involving numerous technical details. Manual script writing is prone to errors, resulting in a substantial time commitment for unit testing as script issues are resolved. This impacts the efficiency and quality of unit testing and increases the learning curve for testers. Summary of the Invention
[0003] Purpose of the invention: The purpose of this invention is to provide a method for automatically generating unit test scripts based on the CppUnit framework. This method allows users to complete the development of test case scripts simply by filling in an Excel spreadsheet, reducing the difficulty of script writing, standardizing script format, reducing the error rate in the script writing process, and automatically generating compilable and executable unit test scripts with one click, thereby improving the quality of unit test scripts and testing efficiency.
[0004] Technical solution: The present invention provides a method for automatically generating unit test scripts based on the CppUnit framework, comprising the following steps:
[0005] (1) Analyze the syntax of the CppUnit unit testing framework, identify key elements, and design a standard Excel test case template, defining the keywords and attributes required to generate the script in the template;
[0006] (2) Design the front-end UI page of the unit test script automatic generation tool, including browsing the Excel test case path, filling in the top-level folder name of the source code, selecting the software architecture, filling in the list of stub functions with no return value, generating the initial test file and the unit test script button;
[0007] (3) Browse the Excel test case template filled in by the user, search and traverse the keywords in the header, verify the compliance of the information filled in by the user and the correctness of the template, and give error prompts;
[0008] (4) Process the Excel test case template, determine whether stub functions need to be stubbed, find and store each input, output and stub function, and collect the necessary information for script generation;
[0009] (5) Extract information from the stored data structure and format it to generate a .cpp test script file that meets the CppUnit unit testing framework;
[0010] (6) Extract information from the stored data structure and format it to generate a .h test script file that meets the CppUnit unit testing framework.
[0011] Furthermore, step (1) includes the following steps:
[0012] (11) Design an Excel test case template for each function under test, named "test_function_name", containing 3 sheets: the first sheet is named the function name, the second sheet is named "variable information mapping table", and the third sheet is named "test information";
[0013] (12) In the first sheet, each column of the table corresponds to a different attribute of the test case. The header of the first row is used to inform the user what information needs to be filled in. From the second row onwards, each row represents a specific test case. The contents of the header are as follows: CaseID test case number, ReqID requirement number, LoopCnt number of loops for the test case, input parameters, output parameters, test method, and test case description.
[0014] (13) Use the characteristic character "I / O" to separate input and output parameters; use the characteristic character "END" to separate output parameters from test methods; fill all input parameters of the function under test into the first row of input parameters; fill all output parameters of the function under test into the first row of output parameters; if the input has a stub function, add the characteristic character "[I]" before the function name of the stub function; if the function has a return value, add the characteristic character "[R]" before the return value and use it as an output parameter;
[0015] (14) Starting from the second line, the user fills in the test case information according to the actual situation, calculates the expected output, identifies the test method used, and describes the scenarios covered by the test case.
[0016] (15) On the second sheet, supplement the corresponding attributes of all inputs and outputs in the order of the first sheet, including variable name, variable type and variable attribute;
[0017] (16) The third sheet is used to record information about the testing process, including the testers, test dates, code versions, requirement versions, and reviewers.
[0018] Furthermore, variable attributes include: global variables, general parameters, and pointer parameters;
[0019] Furthermore, the input parameters include: formal parameters, global variables, and stub functions; the output parameters include: pointers, global variables, and return values.
[0020] Furthermore, the testing methods include: equivalence class, boundary value, repeatability, special path, MCDC, loop verification, overflow verification, and division by zero verification. Select "√" or "×" in the list to indicate the testing method used.
[0021] Furthermore, step (2) includes the following steps:
[0022] (21) Create a folder named "test_function_name" for each function under test to store the Excel test case design table and test script; save the Excel test case template and related path information by designing a "Browse" button.
[0023] (22) Design a text box for “Source Code Top-Level Folder Name”, which users can fill in according to their actual situation.
[0024] (23) Design a "Software Architecture" drop-down menu so that users can choose between "General Architecture" or "Non-General Architecture" based on their actual situation.
[0025] (24) Design a multi-line text box for users to fill in the name of the stub function; no formal parameters need to be filled in for the stub function, and if there are multiple stub functions, they are separated by a newline character.
[0026] (25) Design the "Generate Initial Test File" button and the "Generate Script" button to trigger the background automatic generation logic.
[0027] Furthermore, step (3) includes the following steps:
[0028] (31) When a user uses the “Generate Script” function, the path where the file is located is verified to be valid. If there is an error, a prompt will be given.
[0029] (32) Read the information of each sheet page, search and record the column number where the keywords "I / O", "END" and "use case description" are located in the table header, and store the required information.
[0030] (33) Perform a validity check on the Excel test case design template, including but not limited to: whether the table and each sheet are named correctly, whether there are blanks in the table header, whether there are missing keywords, whether there are duplicate stub function names, and whether there are inconsistencies in the number, type, and order of variables in the first two sheets; if there are errors, specific error messages will be displayed; if the check passes, the test script can continue to be generated.
[0031] Furthermore, step (4) includes the following steps:
[0032] (41) Delete the columns containing I / O and END to generate an array structure containing only valid columns.
[0033] (42) Iterate through the variable properties in the second sheet and calculate the total number of input parameters, including "general parameters" and "pointer parameters".
[0034] (43) If the number of stub return values and the list of stub functions are both empty, then the use case is considered to be free of stubs; otherwise, it is determined that stubs are required.
[0035] (44) Loop through the header information to get the input array, output array, formal parameter input and formal parameter output in test_fun, find the type of each formal parameter and store it in the paramType (parameter type) array.
[0036] (45) Search for stub functions with return values in the input, search for stub functions without return values in the stub function list, process the prefixes and suffixes in the data, obtain the stub function names, and store them in the class structure of stub function information.
[0037] Furthermore, step (5) includes the following steps:
[0038] (51) Refer to the framework of CppUnit unit testing and the organization of various files to generate pre-compiled macros, header file inclusions, and basic information of initialization functions;
[0039] (52) Based on the stored stub function information, generate the scheduling order and definition of the stub functions in the .cpp file.
[0040] (53) Format the definition of the two initialization functions for global variables and expected output, and generate the call to the initialization function for global variables and expected output in setUp();
[0041] (54) Format and generate the tearDown() function to release resources;
[0042] (55) Generate the function definition of test_fun(), and set CaseID, ReqID, LoopCnt and function input and output as formal parameters;
[0043] (56) Read the stored input and output values of each test case and generate the actual call relationship of each test case to test_fun();
[0044] (57) Generate design information and traceability information for all use cases in the form of annotations.
[0045] Furthermore, step (6) includes the following steps:
[0046] (61) Refer to the framework of CppUnit unit testing and the organization of various files to generate pre-compiled macros, header file inclusions, and macro definition basic information.
[0047] (62) Using the name of the function under test + TC as the class name, generate the inherited class of TestFixture in a formatted manner, including the calls to CPPUNIT_TEST_SUITE, CPPUNIT_TEST and CPPUNIT_TEST_SUITE_END;
[0048] (63) Generate the method declarations, setUp() and tearDown() declarations for test_case1 to test_caseN based on the number of stored test cases;
[0049] (64) Generate the function definition of test_fun(), and design CaseID, ReqID, LoopCnt and function input and output as parameters;
[0050] (65) Generate the declarations of the private methods InitExpectVariable(), InitGlobalVariables(), and CheckExpect(), as well as the declarations of their internal variables.
[0051] Beneficial effects: Compared with the prior art, the present invention has the following significant advantages: It transforms the relatively complex Cppunit unit testing framework and script development work into a relatively simple and low-tech Excel spreadsheet filling task, lowering the threshold for unit testing and allowing testers to focus more on verifying product conformity rather than the correctness of intermediate products during the testing process; it can automatically check and verify the test case design forms filled in by users; it enables rapid writing of test case designs, and even batch design of test cases, greatly improving the efficiency of script development. Attached Figure Description
[0052] Figure 1 This is a flowchart of the present invention. Detailed Implementation
[0053] The technical solution of the present invention will be further described below with reference to the accompanying drawings.
[0054] like Figure 1 As shown, this embodiment of the invention provides a method for automatically generating unit test scripts based on the CppUnit framework, including the following steps:
[0055] (1) Analyze the syntax of the CppUnit unit testing framework, identify key elements, and design a standard Excel test case template. The template defines the keywords and attributes required to generate the script. Key elements include: the syntax structure of the CppUnit framework, file inclusion relationships, class inheritance relationships, function call relationships, determining the location of function declarations and definitions, and variable scope. This includes the following steps:
[0056] (11) Design an Excel test case template for each function under test, named "test_function_name", containing 3 sheets: the first sheet is named the function name, the second sheet is named "variable information mapping table", and the third sheet is named "test information";
[0057] (12) In the first sheet, each column of the table corresponds to a different attribute of the test case. The header of the first row tells the user what information needs to be filled in. From the second row onwards, each row represents a specific test case. The contents of the header are as follows: CaseID (test case number), ReqID (requirement number), LoopCnt (number of loops for the test case), input parameters, output parameters, test method, and test case description. Input parameters include: formal parameters, global variables, and stub functions. Output parameters include: pointers, global variables, and return values. Test methods include: equivalence class, boundary value, repeatability, special path, MCDC, loop verification, overflow verification, and division by zero verification. Select "√" or "×" in the list to indicate the test method used.
[0058] (13) Use the characteristic character "I / O" to separate input and output parameters; use the characteristic character "END" to separate output parameters from test methods; fill all input parameters of the function under test into the first row of input parameters; fill all output parameters of the function under test into the first row of output parameters; if the input has a stub function, add the characteristic character "[I]" before the function name of the stub function; if the function has a return value, add the characteristic character "[R]" before the return value and use it as an output parameter;
[0059] (14) Starting from the second line, the user fills in the test case information according to the actual situation, calculates the expected output, identifies the test method used, and describes the scenarios covered by the test case.
[0060] (15) On the second sheet, complete the corresponding attributes of all inputs and outputs in the order of the first sheet, including variable name, variable type and variable attributes; variable attributes include: global variables, general parameters and pointer parameters;
[0061] (16) The third sheet is used to record information about the testing process, including the testers, test dates, code versions, requirement versions, and reviewers.
[0062] (2) Design the front-end UI page of the unit test script automatic generation tool, including browsing the Excel test case path, entering the top-level folder name of the source code, selecting the software architecture, entering the list of stub functions with no return value, generating the initial test file, and the unit test script button; including the following steps:
[0063] (21) Create a folder named "test_function_name" for each function under test to store the Excel test case design table and test script; save the Excel test case template and related path information by designing a "Browse" button.
[0064] (22) Design a text box for “Source Code Top-Level Folder Name”, which users can fill in according to their actual situation.
[0065] (23) Design a "Software Architecture" drop-down menu so that users can choose between "General Architecture" or "Non-General Architecture" based on their actual situation.
[0066] (24) Design a multi-line text box for users to fill in the name of the stub function; no formal parameters need to be filled in for the stub function, and if there are multiple stub functions, they are separated by a newline character.
[0067] (25) Design the "Generate Initial Test File" button and the "Generate Script" button to trigger the background automatic generation logic.
[0068] (3) Browse the Excel test case template filled in by the user, search and traverse the keywords in the header, verify the compliance of the information filled in by the user and the correctness of the template, and give error prompts; including the following steps:
[0069] (31) When a user uses the “Generate Script” function, the path where the file is located is verified to be valid. If there is an error, a prompt will be given.
[0070] (32) Read the information of each sheet page, search and record the column number where the keywords "I / O", "END" and "use case description" are located in the table header, and store the required information.
[0071] (33) Perform a validity check on the Excel test case design template, including but not limited to: whether the table and each sheet are named correctly, whether there are blanks in the table header, whether there are missing keywords, whether there are duplicate stub function names, and whether there are inconsistencies in the number, type, and order of variables in the first two sheets; if there are errors, specific error messages will be displayed; if the check passes, the test script can continue to be generated.
[0072] (4) Process the Excel test case template, determine whether stub functions need to be stubbed, find and store each input, output and stub function, and collect the necessary information for script generation; including the following steps:
[0073] (41) Delete the columns containing I / O and END to generate an array structure containing only valid columns.
[0074] (42) Iterate through the variable properties in the second sheet and calculate the total number of input parameters, including "general parameters" and "pointer parameters".
[0075] (43) If the number of stub return values and the list of stub functions are both empty, then the use case is considered to be free of stubs; otherwise, it is determined that stubs are required.
[0076] (44) Loop through the header information to get the input array, output array, formal parameter input and formal parameter output in test_fun, find the type of each formal parameter and store it in the paramType (parameter type) array.
[0077] (45) Search for stub functions with return values in the input, search for stub functions without return values in the stub function list, process the prefixes and suffixes in the data, obtain the stub function names, and store them in the class structure of stub function information.
[0078] (5) Extract information from the stored data structure and format it to generate a .cpp test script file that meets the CppUnit unit testing framework; including the following steps:
[0079] (51) Refer to the framework of CppUnit unit testing and the organization of various files to generate pre-compiled macros, header file inclusions, and basic information of initialization functions;
[0080] (52) Based on the stored stub function information, generate the scheduling order and definition of the stub functions in the .cpp file.
[0081] (53) Format the definition of the two initialization functions for global variables and expected output, and generate the call to the initialization function for global variables and expected output in setUp();
[0082] (54) Format and generate the tearDown() function to release resources;
[0083] (55) Generate the function definition of test_fun(), and set CaseID, ReqID, LoopCnt and function input and output as formal parameters;
[0084] (56) Read the stored input and output values of each test case and generate the actual call relationship of each test case to test_fun();
[0085] (57) Generate design information and traceability information for all use cases in the form of annotations.
[0086] (6) Extract information from the stored data structure and format it to generate a .h test script file that meets the CppUnit unit testing framework. This includes the following steps:
[0087] (61) Refer to the framework of CppUnit unit testing and the organization of various files to generate pre-compiled macros, header file inclusions, and macro definition basic information.
[0088] (62) Using the name of the function under test + TC as the class name, generate the inherited class of TestFixture in a formatted manner, including the calls to CPPUNIT_TEST_SUITE, CPPUNIT_TEST and CPPUNIT_TEST_SUITE_END;
[0089] (63) Generate the method declarations, setUp() and tearDown() declarations for test_case1 to test_caseN based on the number of stored test cases;
[0090] (64) Generate the function definition of test_fun(), and design CaseID, ReqID, LoopCnt and function input and output as parameters;
[0091] (65) Generate the declarations of the private methods InitExpectVariable(), InitGlobalVariables(), and CheckExpect(), as well as the declarations of their internal variables.
Claims
1. A method for automatically generating unit test scripts based on the CppUnit framework, characterized in that, Includes the following steps: (1) Analyze the syntax of the CppUnit unit testing framework, identify key elements, and design a standard Excel test case template. The template defines the keywords and attributes required to generate the script; including the following steps: (11) Design an Excel test case template for each function under test, named "test_function_name", containing 3 sheets: the first sheet is named the function name, the second sheet is named "variable information mapping table", and the third sheet is named "test information"; (12) Each column of the table in the first sheet corresponds to a different attribute of the test case. The header of the first row is used to inform the user what information needs to be filled in. From the second row onwards, each row represents a specific test case. The contents of the header are, in order: CaseID, ReqID, LoopCnt test case loop count, input parameter filling, output parameter filling, test method, and test case description. (13) Use the characteristic character "I / O" to separate input and output parameters; use the characteristic character "END" to separate output parameters from test methods; fill all input parameters of the function under test into the first row of input parameters; fill all output parameters of the function under test into the first row of output parameters; if the input has a stub function, add the characteristic character "[I]" before the function name of the stub function; if the function has a return value, add the characteristic character "[R]" before the return value and use it as an output parameter; (14) Starting from the second line, the user fills in the test case information according to the actual situation, calculates the expected output, identifies the test method used, and describes the scenarios covered by the test case. (15) On the second sheet, supplement the corresponding attributes of all inputs and outputs in the order of the first sheet, including variable name, variable type and variable attribute; (16) The third sheet is used to record information about the testing process, including the testers, test dates, code versions, requirement versions, and reviewers; (2) Design the front-end UI page of the unit test script automatic generation tool, including browsing the Excel test case path, filling in the top-level folder name of the source code, selecting the software architecture, filling in the list of stub functions with no return value, generating the initial test file and the unit test script button; (3) Browse the Excel test case template filled in by the user, search and traverse the keywords in the header, verify the compliance of the information filled in by the user and the correctness of the template, and give error prompts; (4) Process the Excel test case template, determine whether stub functions need to be stubbed, find and store each input, output and stub function, and collect the necessary information for script generation; (5) Extract information from the stored data structure and format it to generate a .cpp test script file that meets the CppUnit unit testing framework; (6) Extract information from the stored data structure and format it to generate a .h test script file that meets the CppUnit unit testing framework.
2. The method for automatically generating unit test scripts based on the CppUnit framework according to claim 1, characterized in that, Variable attributes include: global variables, general parameters, and pointer parameters.
3. The method for automatically generating unit test scripts based on the CppUnit framework according to claim 1, characterized in that, The input parameters include: formal parameters, global variables, and stub functions; the output parameters include: pointers, global variables, and return values.
4. The method for automatically generating unit test scripts based on the CppUnit framework according to claim 1, characterized in that, The testing methods include: equivalence class, boundary value, repeatability, special path, MCDC, loop verification, overflow verification, and division by zero verification.
5. The method for automatically generating unit test scripts based on the CppUnit framework according to claim 1, characterized in that, Step (2) includes the following steps: (21) Create a folder named "test_function_name" for each function under test to store the Excel test case design sheet and test script; save the Excel test case template and related path information by designing a "Browse" button; (22) Design a text box for "Source Code Top-Level Folder Name" for users to fill in according to their actual situation; (23) Design a "Software Architecture" drop-down menu so that users can choose between "General Architecture" or "Non-General Architecture" based on their actual situation; (24) Design a multi-line text box for users to fill in the stub function name; no formal parameters need to be filled in for the stub function here, and if there are multiple stub functions, they are separated by a newline character; design a "Generate initial test file" button and a "Generate script" button to trigger the background automatic generation logic.
6. The method for automatically generating unit test scripts based on the CppUnit framework according to claim 1, characterized in that, Step (3) includes the following steps: (31) When a user uses the "Generate Script" function, the path where the file is located is verified to be valid. If there is an error, a prompt will be given. (32) Read the information from each sheet, search for and record the column numbers of the keywords "I / O", "END", and "use case description" in the header, and store the required information. (33) Perform a validity check on the Excel test case design template, including: whether the table and each sheet are named correctly, whether there are blanks in the table header, whether there are missing keywords, whether there are duplicate stub function names, and whether there are inconsistencies in the number, type, and order of variables in the first two sheets; if there are errors, specific error messages will be displayed; if the check passes, continue to generate test scripts.
7. The method for automatically generating unit test scripts based on the CppUnit framework according to claim 1, characterized in that, Step (4) includes the following steps: (41) Delete the columns containing I / O and END, and generate an array structure containing only valid columns; (42) Iterate through the variable properties in the second sheet and calculate the total number of input parameters, including "general parameters" and "pointer parameters"; (43) If the number of stub return values and the list of stub functions are both empty, then the use case is considered to be free of stubs; otherwise, it is determined that stubs are required. (44) Loop through the header information to get the input array, output array, formal parameter input and formal parameter output in test_fun, find out the type of each formal parameter and store it in paramType, i.e., the parameter type array; (45) Search for stub functions with return values in the input, search for stub functions without return values in the stub function list, process the prefixes and suffixes in the data, obtain the stub function names, and store them in the class structure of stub function information.
8. The method for automatically generating unit test scripts based on the CppUnit framework according to claim 1, characterized in that, Step (5) includes the following steps: (51) Refer to the framework of CppUnit unit testing and the organization of various files to generate precompiled macros, header files, and basic information of initialization functions; (52) Based on the stored stub function information, generate the scheduling order and definition of the stub functions in the .cpp file; (53) Format the definition of the two initialization functions for global variables and expected output, and generate the call to the initialization function for global variables and expected output in setUp(); (54) Format and generate the tearDown() function to release resources; (55) Generate the function definition of test_fun(), and set CaseID, ReqID, LoopCnt and function input and output as formal parameters; (56) Read the stored input and output values of each test case and generate the actual call relationship of each test case to test_fun(); (57) Generate design information and traceability information for all use cases in the form of annotations.
9. The method for automatically generating unit test scripts based on the CppUnit framework according to claim 1, characterized in that, Step (6) includes the following steps: (61) Refer to the framework of CppUnit unit testing and the organization of various files to generate precompiled macros, header files, and macro definition basic information; (62) Using the name of the function under test + TC as the class name, generate the inherited class of TestFixture in a formatted manner, including the calls to CPPUNIT_TEST_SUITE, CPPUNIT_TEST and CPPUNIT_TEST_SUITE_END; (63) Generate the method declarations, setUp() and tearDown() declarations for test_case1 to test_caseN based on the number of stored test cases; (64) Generate the function definition of test_fun(), and design CaseID, ReqID, LoopCnt and function input and output as parameters; (65) Generate the declarations of the private methods InitExpectVariable(), InitGlobalVariables(), and CheckExpect(), as well as the declarations of their internal variables.
Citation Information
Patent Citations
Test case configuration system and method and automatic test system and method
CN110888818A
Unit test system and method for automatic generating stub and driving function
CN1873626A
