CN112328226A - Embedded system automatic test code generation method and device - Google Patents
Embedded system automatic test code generation method and device Download PDFInfo
- Publication number
- CN112328226A CN112328226A CN202010978991.6A CN202010978991A CN112328226A CN 112328226 A CN112328226 A CN 112328226A CN 202010978991 A CN202010978991 A CN 202010978991A CN 112328226 A CN112328226 A CN 112328226A
- Authority
- CN
- China
- Prior art keywords
- variable
- information
- value
- function
- test
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Granted
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/30—Creation or generation of source code
- G06F8/31—Programming languages or programming paradigms
- G06F8/315—Object-oriented languages
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/36—Preventing errors by testing or debugging software
- G06F11/3668—Software testing
- G06F11/3672—Test management
- G06F11/3684—Test management for test design, e.g. generating new test cases
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/30—Creation or generation of source code
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/40—Transformation of program code
- G06F8/41—Compilation
- G06F8/42—Syntactic analysis
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- General Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Computing Systems (AREA)
- Computer Hardware Design (AREA)
- Quality & Reliability (AREA)
- Debugging And Monitoring (AREA)
Abstract
The present disclosure relates to the field of automated testing, and in particular, to a method and an apparatus for generating an automated testing code of an embedded system, wherein the method includes: analyzing to obtain constant information and a variable list from a C language header file where a tested function is located; analyzing the test case of the tested function to obtain variable value information; and automatically generating a test code of the tested function according to the constant information, the variable list and the variable value information. The method can replace personnel to manually write the test codes, automatically generate the test codes of the tested functions, simplify the complexity of the testers in the coding level, save a large amount of time and energy of the testers, and reduce the occurrence rate of coding errors. Meanwhile, the test code production specification can be unified, and the test code production specification is suitable for various test codes with complex logics.
Description
Technical Field
The present disclosure relates to the field of automated testing, and in particular, to a method and an apparatus for generating an automated test code for an embedded system.
Background
In the function test of the embedded system, generally, function statements are manually read, a function statement and a related variable list are found, then, according to each variable, the statements are declared one by one, spaces are allocated one by one, the assignments are assigned one by one, then, the function to be tested is called, the return value of the function to be tested is obtained, then, the code of the stored file is compiled, and the return value of the function is stored as a variable file. The process is extremely complicated, a large amount of redundant definitions and logics exist in codes written by testers and codes written by developers, the working efficiency of the testers is low due to the current situation, and at least more than 50% of energy is used for repeated and useless work in actual testing work.
In the similar technical field, most processing methods only provide a single simple calling logic based on script execution, cannot process the dimension of a dynamic adjustment variable, a variable boundary value and the like, and need a tester to manually compile a test report. These current situations greatly reduce the work efficiency of the tester, and the energy and time cannot be used in the design of the test cases. And different testers write different test codes (i.e., the difference of the test code generation process is large), which increases the difficulty of maintaining the test codes.
The existing automatic test code generation process of the embedded system is only suitable for generating test codes of complex logic tested functions, and the logic of the test codes is simple and only comprises a single-value type tested function.
Disclosure of Invention
The method is used for solving the technical problems of manpower waste, low working efficiency and large difference in the test code generation process in the manual generation mode of the embedded system test codes in the prior art, and the method for automatically generating the embedded system test codes is not suitable for tested functions with complex logic.
In order to solve the above technical problem, a first aspect of the present disclosure provides an embedded system automation test code generation method, including:
analyzing to obtain constant information and a variable list from a C language header file where a tested function is located;
analyzing the test case of the tested function to obtain variable value information;
and automatically generating a test code of the tested function according to the constant information, the variable list and the variable value information.
In a further embodiment of this document, parsing a C-language header file of a function to be tested to obtain a variable list includes:
according to the grammatical structure of the C language, identifying variable statement, structural body statement, function statement and macro definition information from a header file of a tested function;
analyzing the variable statement and the structural body statement according to the syntax specification of the C language to obtain single-value information, pointer dimension information, array dimension information and variable path information;
obtaining a variable dependency relationship according to macro definition information, single value information, pointer dimension information, array dimension information and variable path information;
analyzing the function statement according to the regular expression to obtain the tested function information, wherein the tested function information comprises: the name of the function to be tested, a return value, a parameter list and input and output marks of the parameters are detected;
and obtaining a variable list according to the single-value information, the macro definition information, the pointer dimension information, the array dimension information, the variable path information, the variable dependency relationship and the measured function information.
In a further embodiment herein, parsing the variable value information from the test cases of the function under test includes:
analyzing the test case of the tested function and determining the variable value type;
and resolving the variable value information according to the variable value type.
In further embodiments herein, parsing the variable value information according to the variable value type includes:
if the variable value type is a file type, reading the file and converting the content of the file into a one-dimensional array;
if the variable value type is the script type, acquiring a script return value, and converting the script return value into a one-dimensional array;
if the variable value type is the boundary type, expanding the boundary to obtain a boundary value sequence;
if the variable value type is a single value type, obtaining a single-point value;
the one-dimensional array, the boundary value sequence and the single-point value constitute variable value information.
In a further embodiment of this document, automatically generating test code of a function under test according to the constant information, the variable list and the variable value information includes:
generating various types of code segments according to the constant information, the variable list and the variable value information by combining the grammar structure and the coding specification of the target language;
and combining the code segments to generate a test code, and associating the test code with the test case of the tested function.
In a further embodiment of this document, generating each type of code segment according to the constant information, the variable list and the variable value information, in combination with the syntax structure and the coding specification of the target language, includes:
generating a macro definition code segment and a constant code segment according to the macro definition information and the constant information;
generating a variable statement code segment and a variable allocation space code segment according to the single-value information, the pointer dimension information, the array dimension information, the variable path information and the variable dependency relationship;
and generating a variable assignment code segment and a tested function return value code segment according to the variable list and the variable value information.
In a further embodiment herein, the method for generating automated test code for embedded systems further comprises:
generating an annotation code segment according to the user information;
and generating a code containing segment according to the C language header file of the tested function.
A second aspect herein provides an embedded system automated test code generation apparatus, comprising:
the first analysis module is used for analyzing the C language header file where the tested function is located to obtain constant information and a variable list;
the second analysis module is used for analyzing the test cases of the tested functions to obtain variable value information;
and the code generation module is used for automatically generating a test code of the tested function according to the constant information, the variable list and the variable value information.
A third aspect of the present disclosure provides a computer device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, wherein the processor implements the embedded system automated test code generation method according to any of the foregoing embodiments when executing the computer program.
A fourth aspect herein provides a computer readable storage medium storing an executable computer program which, when executed by a processor, implements the embedded system automation test code generation method of any one of the preceding claims.
According to the method and the device for generating the automatic test code of the embedded system, constant information and a variable list are obtained by analyzing a C language header file where a tested function is located; analyzing the test case of the tested function to obtain variable value information; according to the constant information, the variable list and the variable value information, a person can be replaced to manually write the test code, the test code of the tested function is automatically generated, a large amount of time and energy (at least more than 50% of workload) of the tester is saved, and the tester can use more time and energy in writing the test case. The problem that a large number of test codes with complicated steps need to be compiled by a tester in the white box test process, artificial coding errors are easily generated in the coding process, and the test efficiency is seriously reduced is solved, the complexity of the tester in the coding level is simplified, and the test can be completed without deeply knowing the syntactic structure of the C language. Meanwhile, the test code production specification can be unified, and the test code generation method is suitable for generating test codes for tested functions of various complex logics.
In order to make the aforementioned and other objects, features and advantages of the present invention comprehensible, preferred embodiments accompanied with figures are described in detail below.
Drawings
In order to more clearly illustrate the embodiments or technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, and it is obvious that the drawings in the following description are only some embodiments of the present invention, and it is obvious for those skilled in the art that other drawings can be obtained according to the drawings without creative efforts.
FIG. 1 is a flow diagram illustrating a method for automated test code generation for an embedded system according to an embodiment of the present disclosure;
FIG. 2 is a flow diagram illustrating a process for parsing a header file of a measured function according to an embodiment of the present disclosure;
FIG. 3 is a flow diagram illustrating a test case parsing process according to an embodiment herein;
FIG. 4 shows a flow diagram of a process for automatically generating test code from parsing results according to embodiments herein;
FIG. 5A shows a first flowchart of a code segment generation process of embodiments herein;
FIG. 5B shows a second flowchart of a code segment generation process of embodiments herein;
FIG. 6 is a block diagram illustrating an apparatus for automated test code generation for embedded systems according to an embodiment of the present disclosure;
FIG. 7 is a block diagram illustrating a computer device according to an embodiment of the present disclosure.
Description of the symbols of the drawings:
610. a first parsing module;
620. a second parsing module;
630. a code generation module;
702. a computer device;
704. a processor;
706. a memory;
708. a drive mechanism;
710. an input/output module;
712. an input device;
714. an output device;
716. a presentation device;
718. a graphical user interface;
720. a network interface;
722. a communication link;
724. a communication bus.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments herein without making any creative effort, shall fall within the scope of protection.
The development languages supported by the system comprise but are not limited to embedded standard C language, matlab language and the like, and the applied embedded systems comprise but are not limited to SylixOS, VxWorks and other embedded systems which are commonly used at present.
In an embodiment of this document, as shown in fig. 1, an automated test code generation method for an embedded system is provided, which is used to solve the technical problems in the prior art that a manual test code generation method for an embedded system wastes manpower, has low work efficiency, and has large differences in test code generation processes, and that an automatic test code generation method for an embedded system is not suitable for a tested function with complex logic. The method for generating the automatic test code of the embedded system is suitable for the situations of complex tested code, high modification frequency, large variable quantity and complex hierarchical structure, can be operated in an intelligent terminal, including a smart phone, a tablet computer, a desktop computer, a server and the like, can be an independent application program, a small program embedded in other programs and the like, or can also be in a webpage form and the like.
Specifically, the method for generating the automatic test code of the embedded system comprises the following steps:
and step 130, automatically generating a test code of the tested function according to the constant information, the variable list and the variable value information.
The function to be tested is obtained by C language programming, the source code of the function to be tested contains the header of the function to be tested, the structural body declaration related to the parameter of the function to be tested, the array, the variable declaration appearing in the pointer dimension, the macro definition, the constant declaration, etc., and step 110 can determine the C language header file from the function source program to be tested (obtained by identifying the # include keyword and recursion). Constant information refers to a constant variable, for example, pi is 3.14, and the variable list includes information of the variable, including an array, a pointer, and the like. In this step, the language header file of the function C to be tested is identified to obtain the statement of the function to be tested (return value, argument list, function name), macro definition, constant statement (including constant information), variable statement (including dimension of variable type, name, array or pointer, statement of dimension of array or pointer), and statement of structure, and then the variable list is obtained according to the statement of the function to be tested, macro definition, variable statement, and statement of structure.
In specific implementation, in order to facilitate the calling of the subsequent steps, the tested function statement, the macro definition, the constant statement, the variable statement and the structural body statement are cached in a dictionary form.
The form parameter list refers to a list of form parameter names identified by a regularized expression in a C language header file, and is simply a character string list, and data objects are stored, including information such as parent-child variables, arrays, pointer dimensions and the like. The variable list refers to memory objects which are stored by converting each form parameter into a variable path through analysis, wherein the variable path is in a source code file and a header file action domain related to a tested function and is used for uniquely identifying a character string of a variable.
In the step 120, the test case of the function to be tested may be determined as follows: the variable list obtained in step 110 is displayed in a page in a form of a tree table, and a user can set the attribute of each node in the tree table through the page, for example, set the dimension, default value, boundary value, and the like of a variable; after the attribute is set, automatically generating a test case framework, and setting corresponding variable values (including single values, files, boundaries, scripts and other multi-format assignments) for each variable by a user according to actual needs; and after the node attribute and the variable value are set, obtaining a test case.
In specific implementation, the above steps 110 to 130 can be automatically completed by the system, or the user triggers the process of automatically generating the test code by clicking a control page button.
The test code of the tested function generated in the above step 130 generates a test program source file according to a certain rule, taking C language as an example, and the test program source file is a \.
In the embodiment, constant information and a variable list are obtained by analyzing the C language header file where the tested function is located; analyzing the test case of the tested function to obtain variable value information; according to the constant information, the variable list and the variable value information, a person can be replaced to manually write the test code, the test code of the tested function is automatically generated, a large amount of time and energy (at least more than 50% of workload) of the tester is saved, and the tester can use more time and energy in writing the test case. The problem that a large number of complicated test codes with complex steps need to be compiled by a tester in a white box test process, artificial coding errors are easily generated in the coding process, and the test efficiency is seriously reduced is solved, the complexity of the tester in a coding layer is also simplified, and the test can be completed without deeply knowing the syntactic structure of the C language. Meanwhile, the embodiment can unify the production specification of the test code, and is suitable for generating the test code by the tested function with various complex logics. For example, the measured function of the complex logic has a form parameter including tens of numbers, and has no structure array and pointer array, the structure has multiple layers of child nodes, and the multiple layers of child nodes have various combinations of pointers, arrays, substructures, and the like.
In an embodiment of this document, as shown in fig. 2, the parsing process of step 110 is implemented based on a regular expression, a syntax structure of C language, and a coding specification of C language, where the regular expression, the syntax structure of C language, and the coding specification of C language refer to the prior art specifically, and are not described in detail herein, and specifically, the parsing process of step 110 includes:
and step 205, obtaining a variable list according to the macro definition information, the single value information, the pointer dimension information, the array dimension information, the variable path information, the variable dependency relationship and the measured function information.
Specifically, in step 201, the variable declaration is recognized according to the C language syntax structure one, which is as follows:
variable type variable name; // Single value, object
Variable type variable name; // pointer
Variable type variable name [ ]; // array
And recognizing a structural body statement according to the second C language grammar structure, wherein the second C language grammar structure is as follows:
typedef struct_xxx{
variable declaration List
}XXX;
According to the third C language grammar structure, recognizing a function statement, wherein the third C language grammar structure is as follows:
return value function name (argument list);
the names and values of the macro definitions are stored in the macro definition dictionary, and the macro definitions can be recognized through the macro definition dictionary during analysis.
The names and values of the constants are stored in the constant dictionary, and the constants can be identified through the constant dictionary during analysis. In addition, from static const key combinations, constants are identified and added to the constant dictionary.
In the above step 202, the number of stars in the variable declaration is analyzed, the dimension of the pointer is determined, the number of brackets in the variable declaration is analyzed, and the dimension of the array is determined. According to the grammar specification of C language, the dimension of each dimension of the pointer is extracted after the new key word in the equal sign right value, and the dimension of each dimension of the array is extracted in square brackets. A variable is considered to be a single-valued parameter if it has neither an asterisk nor a bracket mark in its declaration.
After the variable list is generated, a user can set a pointer and the dimension of each dimension of the array (the dimension supports single-value data, macro definition, script, function and four arithmetic expressions) through a page.
The structure and its internal members are actually in a one-to-many relationship, maintained by a one-to-many data structure, the structure itself is a parent node, the member variables are child points, the analysis of the structure is a recursive process, when a variable has no child node, the variable is called a leaf node, after all leaf nodes are analyzed, the variable path information of each node is generated, and the variable path records the path information of the member variables in the structure, for example, if the structure a has two member variables b and c, the number of variable paths obtained after the structure a is analyzed is 3, which are a, a.b and a.c respectively.
In step 203, the dependency relationship of the variables is obtained from the results obtained in steps 201 and 202. If other types of variables exist in the right value of the variable or the dimension of the array and the pointer of the variable, the dependency relationship of the variable needs to be analyzed, and assignment needs to be preferentially declared for the dependent variable in subsequent processing logic. For example, if the variable int a ═ 5 is present; when another variable declaration is int × b ═ new int [ a ], then a and b have dependency relationship, b depends on a, and the a variable must be assigned prior to the b variable declaration when the code segment is generated subsequently. Otherwise it may cause a program execution exception. In this context, all variables are finally stored as a one-dimensional array in the form of variable paths, and then all variable paths are saved in the order of assignment according to the priority declaration. The variable dependency relationship is established logically as follows:
giving value prior to the current variable statement;
in the structure body, a father node is prior to a child node, wherein the father node and the child node are provided with variable path information;
the current variable (determined by the macro definition) has priority over the left value declaration assignments.
In the step 204, the input/output marks of the parameters can be determined according to the convention of the C language coding specification, or manually marked, specifically, the information of the function to be tested is graphically displayed, and a human-computer interaction interface is provided, so that the user can complete the definition of the input and output variables of the parameters in the function to be tested only by simple setting, and does not need to write codes.
In step 205, the macro definition information, the pointer dimension information, the array dimension information, and the variable path information corresponding to the input tag and the output tag are respectively stored in the input variable dictionary and the output variable dictionary according to the variable dependency obtained in step 203.
In an embodiment of this document, as shown in fig. 3, the step 120 of obtaining variable value information from the test case of the function under test by parsing includes:
The variable value types determined in step 301 include, but are not limited to, a file type, a script type, a boundary type, and a single value type.
In detail, step 302 parses the variable value information according to the variable value type, including:
and if the variable value type is the file type, reading the file and converting the file content into a one-dimensional array. In order to ensure the consistent number of data and ensure that the ascending and descending dimension process does not go beyond the boundary, for example, a 3x4x5 shaping array, when converting into a one-dimensional array, its number of data bytes should be: before converting the file content into a one-dimensional array, the 3x4x5xsizeof (int), that is, a one-dimensional array containing 60 int-type data, further includes verifying whether the file content matches according to the data type and dimension information of the variables (which may be determined by a variable list), and if so, converting the file content into a one-dimensional array.
If the variable value type is the script type, the script can be stored in the server in an uploading mode, or the script can be selected from existing scripts in the server, the script running environment is configured, the script is executed, a script return value is obtained, and the script return value is converted into a one-dimensional array.
And if the variable value type is the boundary type, expanding the boundary to obtain a boundary value sequence, and caching the boundary value sequence into the memory. The boundary value convention must be a set of values enclosed in brackets, taking the maximum and minimum values in the series, and adding or subtracting the maximum and minimum values respectively to give the value of the minimum step (e.g. maximum value 100, step 1, 100 will expand to three values of 99, 100, 101, further e.g. minimum value 0.0, step 0.2, 0.0 will expand to three values of-0.2, 0.0, 0.2).
And if the variable value type is a single-value type, obtaining a single-value. The single-value type is, for example, one of int, float, double, char (supporting unsigned), and is cached in the memory in the form of a single-point value.
The file type, the script type and the boundary type are assigned according to array or pointer dimension memorability, and the single value type variable value can be directly assigned.
The one-dimensional array, the boundary value sequence and the single-point value constitute variable value information. The variable value information is cached in the memory in the form of a dictionary, the key of the dictionary is the variable path, and the value of the dictionary is the memory value analyzed in the step 302. In particular, a "dictionary" as referred to herein, refers to a data structure in a programming language, which is a variable container model and can store any type of object. Each key value in the dictionary can be represented by a key-value pair, the key and value in the key-value pair are divided by colons, each key-value pair is divided by commas, and the whole dictionary is included in parentheses. In the whole dictionary, the key must have uniqueness, the value must not be, the value can take any data type, but the key must be an immutable data type, such as: string, integer, tuple. The dictionary query is efficient, and a memory address is calculated by using a key inside the dictionary query, which generally uses a hash.
In an embodiment of this document, as shown in fig. 4, the step 130 automatically generates the test code of the function under test according to the constant information, the variable list and the variable value information, including:
The target language in step 401 includes, but is not limited to, a C language, a matlab language, a java language, a phthon language, and the like, and the specific manner of the target language is not limited herein. The syntax structure and coding specification of the target language can be found in the prior art, and are not limited herein. The code segment is a piece of code constituting the test code, and corresponds to different types of code segments, for example, a variable declaration code segment, a variable allocation space code segment, a variable assignment code segment, and a function-under-test return value code segment, which are involved in the C language code. The code segments are stored in the local disk in the form of a file stream.
In some embodiments, as shown in fig. 5A, the step 401 of generating each type of code segment according to the constant information, the variable list and the variable value information, in combination with the syntax structure and the coding specification of the target language, includes:
502, generating a variable statement code segment and a variable allocation space code segment according to the single-value information, the pointer dimension information, the array dimension information, the variable path information and the variable dependency relationship;
In step 501, a macro definition code segment is generated according to the macro definition information according to the following syntax structure:
the # define macro defines a name (macro definition value).
Generating constant code segments according to the constant information according to the following syntax structure:
const constant type constant name is constant value;
in step 502, the code segment format of the variable declaration is in declaration form in any combination of three types:
variable type variable name; a variable of the single value type, the variable type can be a basic data type, and can also be a structure body.
Variable type variable name; // pointer type variable. The number of asterisks can be more than one.
Variable type variable name [ xxx ]; the number of brackets can be more than one, wherein xxx must accord with the C language grammar specification.
The single-value type variable is generated by single-value information and variable path information. Pointer type variables are generated from pointer dimension information, and array type variables are generated from array dimension information.
The order of declaring variables in the variable declaration code segment is declared in sequence according to the variable dependency relationship.
In C language, pointer type variables need to be used after memory space is allocated in advance, the variables for allocating space include input variables and output variables, and when space is allocated, a target language is taken as C language as an example, and the following logic processing is performed:
(1) if the variable has a father node, preferentially processing the distributed space logic of the father node;
(2) if the variable has a dependent variable, the logic of the distributed space of the dependent variable is processed preferentially;
(3) if there are arrays for this variable, then the number of data is calculated from right to left according to the array dimension and dimension, for example, as follows, assuming there is an array:
int a[2][3];
the size of the allocated space is: sizeof (int) 3x 2;
(4) if the variable has a pointer, the number of data is calculated from right to left according to the pointer dimension and dimension, for example, as follows, it is assumed that there is a double-pointer variable:
int × b; wherein the data dimension in b is 3 and the data dimension in b is 2
When allocating pointer space, the principle of going from left to right must be followed, taking b variable as an example, the space of the first asterisk on the left side is allocated first, and the space of the second asterisk on the left side is allocated, the code logic is as follows:
b=malloc(sizeof(int*)*2);
for(int i=0;i<2;i++)
b[i]=malloc(sizeof(int)*3);
(5) the above principles can be used in a free combination.
In step 503, for the variable assignment code segment, the variables to be assigned include the variables marked as input marks in the variable list, and the variables are sequentially assigned according to the variable dependency relationship. Taking a target language as a C language as an example, an assignment process is similar to a process of allocating space, except that a malloc statement is replaced by an assignment statement, if an array or a pointer exists, a one-dimensional array needs to be converted into dimensions of a real array and the pointer, taking a b variable in step 502 as an example to explain the assignment process, firstly, in variable value information of a test case, a data value of the b variable is found, and if data [6] ═ {1,2,3,4,5,6}, the assignment statement is as follows:
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
b[i][j]=data[sizeof(int)*3*i+j]
in some other embodiments, the array may be further treated as a continuous segment of memory space, and assigned to the space pointed to by b [ i ] by a memcpy function. And will not be described in detail herein.
For the tested function return value code segment, the variables to be acquired only include the variables with the output marks in the variable list, and as with the input variables, the values are ensured to be sequentially taken according to the variable dependency relationship. The value taking process is similar to the value taking process, except that when an array or a pointer exists, the value taking process is dimension-up, and the value taking process is dimension-down, namely the dimension of the real array and the pointer needs to be converted into a one-dimensional array. The b variable in step 502 is used to exemplify the value process (assuming that the b variable is an input variable and an output variable), and the value statement is as follows:
data=new int[sizeof(int)*3*2];
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
data[sizeof(int)*3*i+j]=b[i][j];
like assignment, there is also a more optimal way to assign the array to the space pointed by b [ i ] through the memcpy function as a segment of continuous memory space. And will not be described in detail herein.
And caching the data read by the tested function return value code segment into a dictionary of the test result, or outputting the data to a local disk in a variable file form.
In step 504, a return value statement, a function name statement, and a variable list statement are sequentially generated according to the information of the function to be tested, and the return value statement, the function name statement, and the variable list statement constitute a function-to-be-tested calling code segment.
In a specific implementation, as shown in fig. 5B, in addition to generating the code segment, the method further includes:
In step 5011, the user information refers to author information and summary information input by the user, and the annotation code segment may be generated using an annotation code template, where the annotation code segment template includes: file name, file identification, summary information, current version, author information, date, etc. The file name and the file identification are automatically generated according to the specified rule. The summary information is obtained by manually inputting by a user in a test case page. The current version and date system is automatically generated. The author information is obtained by the real name of the login user. The annotation code segment is derived from the file template, and the program first reads the annotation code template and then replaces the specifically identified fields with the current real data. The annotation code template content is as follows:
/*
*Copyright(c)${createtime}
*All rights reserved
file name: $ filename }
File identification: $ identity }
Summary: $ Summary }
Current version: $ version }
Author: $ author }
Completion date: $ modifytime }
*/。
In the above step 5012, the inclusion code segment is used to include header files, which include the C language header file where the tested function is located, and also include a universal header file, where the universal header file is maintained by the user, and when the tested function is single and uncomplicated, the universal header file does not need to be introduced. The header file may be added to the contain code segment according to the following rules:
# include "header filename".
In one embodiment, in order to meet the requirement of the user for personalized test code generation, the process of generating the code segment further includes:
and generating a custom function code segment, wherein the custom function is a function code which is maintained by a user, and comprises contents such as function statement, variable statement, assignment and the like. In specific implementation, the automatic test code generation program can only provide one inlet for implanting user self-defined function codes, the self-defined function is directly copied to the user self-defined function as a character string, and the program does not perform any additional processing.
The automatic test code generation method for the embedded system can achieve the following technical effects:
(1) automatically extracting a variable list according to a header file declared by a tested function;
(2) the analysis of the test cases is automatically realized, the variable value information is obtained, and the user does not need to manually write the variable value information of the test cases;
(3) the test program source code of the tested function is automatically generated, and a user does not need to manually write the test program source code.
Based on the same inventive concept, an automatic test code generation device for an embedded system is also provided, as described in the following embodiments. Because the principle of the embedded system automatic test code generation device for solving the problems is similar to the embedded system automatic test code generation method, the implementation of the embedded system automatic test code generation device can refer to the embedded system automatic test code generation method, and repeated parts are not described again.
As shown in fig. 6, the device for generating an automatic test code of an embedded system includes a plurality of functional units and modules, which may be implemented by a dedicated or general chip, and may also be implemented by a software program, and specifically, the device for generating an automatic test code of an embedded system includes:
the first analysis module 610 is configured to analyze a C language header file in which a function to be tested is located to obtain constant information and a variable list;
the second analysis module 620 is configured to analyze the test case of the function to be tested to obtain variable value information;
and a code generating module 630, configured to automatically generate a test code of the function to be tested according to the constant information, the variable list, and the variable value information.
In a further embodiment of this document, the first parsing module 610 parses the function to be tested from the C-language header file to obtain a variable list, which includes:
according to the grammatical structure of the C language, identifying variable statement, structural body statement, function statement and macro definition information from a header file of a tested function;
analyzing the variable statement and the structural body statement according to the syntax specification of the C language to obtain single-value information, pointer dimension information, array dimension information and variable path information;
obtaining a variable dependency relationship according to macro definition information, single value information, pointer dimension information, array dimension information and variable path information;
analyzing the function statement according to the regular expression to obtain the tested function information, wherein the tested function information comprises: the name of the function to be tested, a return value, a parameter list and input and output marks of the parameters are detected;
and obtaining a variable list according to the macro definition information, the single value information, the pointer dimension information, the array dimension information, the variable path information, the variable dependency relationship and the measured function information.
In a further embodiment herein, the second parsing module 620 parses the variable value information from the test case of the function under test, including: analyzing the test case of the tested function and determining the variable value type; and resolving the variable value information according to the variable value type.
When the method is implemented specifically, the variable value information is analyzed according to the variable value type, and the method comprises the following steps:
if the variable value type is a file type, reading the file and converting the content of the file into a one-dimensional array;
if the variable value type is the script type, acquiring a script and converting the script into a one-dimensional array;
if the variable value type is the boundary type, expanding the boundary to obtain a boundary value sequence;
if the variable value type is a single value type, obtaining a single-point value;
the one-dimensional array, the boundary value sequence and the single-point value constitute variable value information.
In a further embodiment of this document, the code generating module 630 automatically generates the test code of the function under test according to the constant information, the variable list and the variable value information, including:
generating various types of code segments according to the constant information, the variable list and the variable value information by combining the grammar structure and the coding specification of the target language;
and combining the code segments to generate a test code, and associating the test code with the test case of the tested function.
Generating various types of code segments according to the constant information, the variable list and the variable value information by combining the syntactic structure and the coding specification of the target language, wherein the method comprises the following steps:
generating a macro definition code segment and a constant code segment according to the macro definition information and the constant information;
generating a variable statement code segment and a variable allocation space code segment according to the single-value information, the pointer dimension information, the array dimension information, the variable path information and the variable dependency relationship;
generating a variable assignment code segment and a tested function return value code segment according to the variable list and the variable value information;
and generating a tested function calling code segment according to the tested function information.
In further embodiments herein, the code generation module 630 is further configured to generate an annotation code segment according to the user information; and generating a code containing segment according to the C language header file of the tested function.
The embedded system automatic test code generation device provided by the invention obtains constant information and a variable list by analyzing from a C language header file where a tested function is located; analyzing the test case of the tested function to obtain variable value information; according to the constant information, the variable list and the variable value information, a person can be replaced to manually write the test code, the test code of the tested function is automatically generated, a large amount of time and energy (at least more than 50% of workload) of the tester is saved, and the tester can use more time and energy in writing the test case. The problem that a large number of test codes with complicated steps need to be compiled by a tester in the white box test process, artificial coding errors are easily generated in the coding process, and the test efficiency is seriously reduced is solved, the complexity of the tester in the coding level is simplified, and the test can be completed without deeply knowing the syntactic structure of the C language. Meanwhile, the test code production specification can be unified, and the test code production specification is suitable for various test codes with complex logics.
In an embodiment herein, there is also provided a computer device, as shown in fig. 7, the computer device 702 may include one or more processors 704, such as one or more Central Processing Units (CPUs), each of which may implement one or more hardware threads. The computer device 702 may also include any memory 706 for storing any kind of information, such as code, settings, data, etc. For example, and without limitation, the memory 706 can include any one or more of the following in combination: any type of RAM, any type of ROM, flash memory devices, hard disks, optical disks, etc. More generally, any memory may use any technology to store information. Further, any memory may provide volatile or non-volatile retention of information. Further, any memory may represent fixed or removable components of computer device 702. In one case, the computer device 702 may perform any of the operations of the associated instructions when the processor 704 executes the associated instructions stored in any memory or combination of memories, and in particular, the processor 704 when executing implements the embedded system automated test code generation method described in any of the preceding embodiments. The computer device 702 also includes one or more drive mechanisms 708, such as a hard disk drive mechanism, an optical disk drive mechanism, or the like, for interacting with any memory.
In an embodiment of the present disclosure, a computer-readable storage medium is further provided, where a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, the method for generating an embedded system automation test code according to any of the above embodiments is performed.
In an embodiment of the present disclosure, there is further provided a computer readable instruction, wherein when the instruction is executed by a processor, the program causes the processor to execute the method for generating the embedded system automation test code according to any embodiment.
It should be understood that, in various embodiments herein, the sequence numbers of the above-mentioned processes do not mean the execution sequence, and the execution sequence of each process should be determined by its function and inherent logic, and should not constitute any limitation to the implementation process of the embodiments herein.
It should also be understood that, in the embodiments herein, the term "and/or" is only one kind of association relation describing an associated object, meaning that three kinds of relations may exist. For example, a and/or B, may represent: a exists alone, A and B exist simultaneously, and B exists alone. In addition, the character "/" herein generally indicates that the former and latter related objects are in an "or" relationship.
Those of ordinary skill in the art will appreciate that the elements and algorithm steps of the examples described in connection with the embodiments disclosed herein may be embodied in electronic hardware, computer software, or combinations of both, and that the components and steps of the examples have been described in a functional general in the foregoing description for the purpose of illustrating clearly the interchangeability of hardware and software. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the implementation. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present disclosure.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the several embodiments provided herein, it should be understood that the disclosed system, apparatus, and method may be implemented in other ways. For example, the above-described apparatus embodiments are merely illustrative, and for example, the division of the units is only one logical division, and other divisions may be realized in practice, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may also be an electric, mechanical or other form of connection.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purposes of the embodiments herein.
In addition, functional units in the embodiments herein may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into one unit. The integrated unit can be realized in a form of hardware, and can also be realized in a form of a software functional unit.
The integrated unit, if implemented in the form of a software functional unit and sold or used as a stand-alone product, may be stored in a computer readable storage medium. Based on such understanding, the technical solutions of the present invention may be implemented in a form of a software product, which is stored in a storage medium and includes several instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the steps of the methods described in the embodiments of the present invention. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk, and other various media capable of storing program codes.
The principles and embodiments of this document are explained herein using specific examples, which are presented only to aid in understanding the methods and their core concepts; meanwhile, for the general technical personnel in the field, according to the idea of this document, there may be changes in the concrete implementation and the application scope, in summary, this description should not be understood as the limitation of this document.
Claims (10)
1. An automatic test code generation method for an embedded system is characterized by comprising the following steps:
analyzing to obtain constant information and a variable list from a C language header file where a tested function is located;
analyzing the test case of the tested function to obtain variable value information;
and automatically generating a test code of the tested function according to the constant information, the variable list and the variable value information.
2. The method of claim 1, wherein parsing the list of variables from the C-language header file of the function under test comprises:
according to the grammatical structure of the C language, identifying variable statement, structural body statement, function statement and macro definition information from a header file of a tested function;
analyzing the variable statement and the structural body statement according to the syntax specification of the C language to obtain single-value information, pointer dimension information, array dimension information and variable path information;
obtaining a variable dependency relationship according to macro definition information, single value information, pointer dimension information, array dimension information and variable path information;
analyzing the function statement according to the regular expression to obtain the tested function information, wherein the tested function information comprises: the name of the function to be tested, a return value, a parameter list and input and output marks of the parameters are detected;
and obtaining a variable list according to the macro definition information, the single value information, the pointer dimension information, the array dimension information, the variable path information, the variable dependency relationship and the measured function information.
3. The method of claim 2, wherein parsing variable value information from test cases of a function under test comprises:
analyzing the test case of the tested function and determining the variable value type;
and resolving the variable value information according to the variable value type.
4. The method of claim 3, wherein parsing variable value information according to variable value type comprises:
if the variable value type is a file type, reading the file and converting the content of the file into a one-dimensional array;
if the variable value type is the script type, acquiring a script return value, and converting the script return value into a one-dimensional array;
if the variable value type is the boundary type, expanding the boundary to obtain a boundary value sequence;
if the variable value type is a single value type, obtaining a single-point value;
the one-dimensional array, the boundary value sequence and the single-point value constitute variable value information.
5. The method of claim 2, wherein automatically generating test code for a function under test based on the constant information, the variable list, and the variable value information comprises:
generating various types of code segments according to the constant information, the variable list and the variable value information by combining the grammar structure and the coding specification of the target language;
and combining the code segments to generate a test code, and associating the test code with the test case of the tested function.
6. The method of claim 5, wherein generating each type of code segment according to the constant information, the variable list and the variable value information in combination with the syntax structure and the coding specification of the target language comprises:
generating a macro definition code segment and a constant code segment according to the macro definition information and the constant information;
generating a variable statement code segment and a variable allocation space code segment according to the single-value information, the pointer dimension information, the array dimension information, the variable path information and the variable dependency relationship;
generating a variable assignment code segment and a tested function return value code segment according to the variable list and the variable value information;
and generating a tested function calling code segment according to the tested function information.
7. The method of claim 6, further comprising:
generating an annotation code segment according to the user information;
and generating a code containing segment according to the C language header file of the tested function.
8. An automatic test code generation device for an embedded system, comprising:
the first analysis module is used for analyzing the C language header file where the tested function is located to obtain constant information and a variable list;
the second analysis module is used for analyzing the test cases of the tested functions to obtain variable value information;
and the code generation module is used for automatically generating a test code of the tested function according to the constant information, the variable list and the variable value information.
9. A computer device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, wherein the processor implements the embedded system automated test code generation method of any one of claims 1 to 7 when executing the computer program.
10. A computer-readable storage medium, characterized in that the computer-readable storage medium stores an executable computer program, which when executed by a processor implements the embedded system automation test code generation method of any one of claims 1 to 7.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202010978991.6A CN112328226B (en) | 2020-09-17 | 2020-09-17 | Embedded system automatic test code generation method and device |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202010978991.6A CN112328226B (en) | 2020-09-17 | 2020-09-17 | Embedded system automatic test code generation method and device |
Publications (2)
Publication Number | Publication Date |
---|---|
CN112328226A true CN112328226A (en) | 2021-02-05 |
CN112328226B CN112328226B (en) | 2022-03-04 |
Family
ID=74303821
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202010978991.6A Active CN112328226B (en) | 2020-09-17 | 2020-09-17 | Embedded system automatic test code generation method and device |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN112328226B (en) |
Cited By (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN113176897A (en) * | 2021-04-29 | 2021-07-27 | 北京字跳网络技术有限公司 | Information processing method and device, terminal equipment and storage medium |
CN113254027A (en) * | 2021-05-20 | 2021-08-13 | 西安华讯科技有限责任公司 | Method, system, equipment and storage medium for converting control program of semiconductor equipment |
CN113778888A (en) * | 2021-09-16 | 2021-12-10 | 平安银行股份有限公司 | Code automatic test method, system, equipment and storage medium |
CN114064114A (en) * | 2021-11-19 | 2022-02-18 | 云控智行科技有限公司 | Dynamic library generation method and device |
CN115687140A (en) * | 2022-11-10 | 2023-02-03 | 江苏衫数科技集团有限公司 | Test case compiling method and system based on automatic test |
Citations (11)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN1983209A (en) * | 2005-12-14 | 2007-06-20 | 中兴通讯股份有限公司 | System and method for automatically testing software unit |
CN102087629A (en) * | 2011-01-25 | 2011-06-08 | 南京航空航天大学 | Agent embedded software-based invariant test method and tool |
US20120030655A1 (en) * | 2010-07-29 | 2012-02-02 | De Castillo Alejandro | Generating and Modifying Textual Code Interfaces from Graphical Programs |
CN102495802A (en) * | 2011-12-26 | 2012-06-13 | 华为技术有限公司 | Method and device for testing software system, and computer system |
CN103631720A (en) * | 2013-12-20 | 2014-03-12 | 百度在线网络技术(北京)有限公司 | Method and device for generating test case |
WO2014115189A1 (en) * | 2013-01-28 | 2014-07-31 | Nec Corporation | Method and system for transforming specification scripts to program code |
CN104156314A (en) * | 2014-08-14 | 2014-11-19 | 北京航空航天大学 | Code reuse method applied to test system |
US20160092181A1 (en) * | 2014-09-30 | 2016-03-31 | Advanced Micro Devices, Inc. | Automatic source code generation for accelerated function calls |
US20160170864A1 (en) * | 2014-12-10 | 2016-06-16 | General Electric Company | Integrated automated test case generation for safety-critical software |
US20170116109A1 (en) * | 2015-10-23 | 2017-04-27 | Microsoft Technology Licensing, Llc | Missing include suggestions for external files |
CN107133174A (en) * | 2017-05-04 | 2017-09-05 | 浙江路港互通信息技术有限公司 | Test case code automatically generating device and method |
-
2020
- 2020-09-17 CN CN202010978991.6A patent/CN112328226B/en active Active
Patent Citations (12)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN1983209A (en) * | 2005-12-14 | 2007-06-20 | 中兴通讯股份有限公司 | System and method for automatically testing software unit |
US20120030655A1 (en) * | 2010-07-29 | 2012-02-02 | De Castillo Alejandro | Generating and Modifying Textual Code Interfaces from Graphical Programs |
CN102087629A (en) * | 2011-01-25 | 2011-06-08 | 南京航空航天大学 | Agent embedded software-based invariant test method and tool |
CN102495802A (en) * | 2011-12-26 | 2012-06-13 | 华为技术有限公司 | Method and device for testing software system, and computer system |
WO2014115189A1 (en) * | 2013-01-28 | 2014-07-31 | Nec Corporation | Method and system for transforming specification scripts to program code |
CN103631720A (en) * | 2013-12-20 | 2014-03-12 | 百度在线网络技术(北京)有限公司 | Method and device for generating test case |
CN104156314A (en) * | 2014-08-14 | 2014-11-19 | 北京航空航天大学 | Code reuse method applied to test system |
US20160092181A1 (en) * | 2014-09-30 | 2016-03-31 | Advanced Micro Devices, Inc. | Automatic source code generation for accelerated function calls |
US20160170864A1 (en) * | 2014-12-10 | 2016-06-16 | General Electric Company | Integrated automated test case generation for safety-critical software |
CN105701008A (en) * | 2014-12-10 | 2016-06-22 | 通用电气公司 | Integrated automated test case generation for safety-critical software |
US20170116109A1 (en) * | 2015-10-23 | 2017-04-27 | Microsoft Technology Licensing, Llc | Missing include suggestions for external files |
CN107133174A (en) * | 2017-05-04 | 2017-09-05 | 浙江路港互通信息技术有限公司 | Test case code automatically generating device and method |
Non-Patent Citations (1)
Title |
---|
VIKRAM A PUNATHAMBEKAR: "How to interpret complex C/C++ declarations", 《HTTPS://WWW.CODEPROJECT.COM/ARTICLES/7042/HOW-TO-INTERPRET-COMPLEX-C-C-DECLARATIONS》 * |
Cited By (8)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN113176897A (en) * | 2021-04-29 | 2021-07-27 | 北京字跳网络技术有限公司 | Information processing method and device, terminal equipment and storage medium |
CN113254027A (en) * | 2021-05-20 | 2021-08-13 | 西安华讯科技有限责任公司 | Method, system, equipment and storage medium for converting control program of semiconductor equipment |
CN113254027B (en) * | 2021-05-20 | 2024-05-14 | 西安华讯科技有限责任公司 | Semiconductor device control program conversion method, system, device and storage medium |
CN113778888A (en) * | 2021-09-16 | 2021-12-10 | 平安银行股份有限公司 | Code automatic test method, system, equipment and storage medium |
CN113778888B (en) * | 2021-09-16 | 2024-05-17 | 平安银行股份有限公司 | Code automatic test method, system, equipment and storage medium |
CN114064114A (en) * | 2021-11-19 | 2022-02-18 | 云控智行科技有限公司 | Dynamic library generation method and device |
CN115687140A (en) * | 2022-11-10 | 2023-02-03 | 江苏衫数科技集团有限公司 | Test case compiling method and system based on automatic test |
CN115687140B (en) * | 2022-11-10 | 2024-01-30 | 江苏衫数科技集团有限公司 | Test case writing method and system based on automatic test |
Also Published As
Publication number | Publication date |
---|---|
CN112328226B (en) | 2022-03-04 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN112328226B (en) | Embedded system automatic test code generation method and device | |
US10162610B2 (en) | Method and apparatus for migration of application source code | |
US8381175B2 (en) | Low-level code rewriter verification | |
US20010037492A1 (en) | Method and apparatus for automatically extracting verification models | |
CN108984155B (en) | Data processing flow setting method and device | |
CN111399853A (en) | Templated deployment method of machine learning model and custom operator | |
CN113366453A (en) | Generating test models from behavior driven development scenarios based on behavior driven development step definitions and similarity analysis using neuro-language programming and machine learning mechanisms | |
JP2017174418A (en) | Data structure abstraction for model checking | |
Chen et al. | Hopper: Interpretative fuzzing for libraries | |
US11593076B2 (en) | Method for merging architecture data | |
CN111190643A (en) | Program code annotation generation method, system, electronic device and storage medium | |
Vélez et al. | Challenges in migrating imperative deep learning programs to graph execution: an empirical study | |
CN112699042B (en) | Unit test case generation method and device | |
CN110659063A (en) | Software project reconstruction method and device, computer device and storage medium | |
CN114385148A (en) | Method, device, equipment and storage medium for realizing linkage function | |
CN117908876A (en) | Method, device and equipment for generating system page under complex authority scene in government or enterprise informatization field based on model driving | |
US11442845B2 (en) | Systems and methods for automatic test generation | |
Gonçalves et al. | ReFlO: An interactive tool for pipe-and-filter domain specification and program generation | |
Long et al. | Automatic inference of code transforms and search spaces for automatic patch generation systems | |
CN114816364A (en) | Method, device and application for dynamically generating template file based on Swagger | |
CN107577476A (en) | A kind of Android system source code difference analysis method, server and medium based on Module Division | |
Hojaji et al. | Lossless compaction of model execution traces | |
KR102117165B1 (en) | Method and apparatus for testing intermediate language for binary analysis | |
CN108628606B (en) | Method and system for generating WEB network management application program of embedded equipment | |
CN116755684B (en) | OAS Schema generation method, device, equipment and medium |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant | ||
GR01 | Patent grant |