Nothing Special   »   [go: up one dir, main page]

CN114968806A - Method, device, equipment and medium for generating unit test case - Google Patents

Method, device, equipment and medium for generating unit test case Download PDF

Info

Publication number
CN114968806A
CN114968806A CN202210679196.6A CN202210679196A CN114968806A CN 114968806 A CN114968806 A CN 114968806A CN 202210679196 A CN202210679196 A CN 202210679196A CN 114968806 A CN114968806 A CN 114968806A
Authority
CN
China
Prior art keywords
unit
test
tested
mock
item
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.)
Pending
Application number
CN202210679196.6A
Other languages
Chinese (zh)
Inventor
黄绍丽
张翔
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Ping An International Financial Leasing Co Ltd
Original Assignee
Ping An International Financial Leasing Co Ltd
Priority date (The priority date 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 date listed.)
Filing date
Publication date
Application filed by Ping An International Financial Leasing Co Ltd filed Critical Ping An International Financial Leasing Co Ltd
Priority to CN202210679196.6A priority Critical patent/CN114968806A/en
Publication of CN114968806A publication Critical patent/CN114968806A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3684Test management for test design, e.g. generating new test cases
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3688Test management for test execution, e.g. scheduling of test suites

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Computer Hardware Design (AREA)
  • Quality & Reliability (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Debugging And Monitoring (AREA)

Abstract

The application discloses a method, a device, equipment and a medium for generating a unit test case, wherein the method comprises the steps of matching a project to be tested with a plurality of sets of reference codes in a sample library, and determining a plurality of tested units of the project to be tested and corresponding object models; determining a test strategy of each test unit according to the object model of each tested unit; analyzing the dependency relationship of each tested unit in the item to be tested, and generating a plurality of dependency Mock of each testing unit according to the analysis result of the dependency relationship; and generating a plurality of unit test cases of the item to be tested according to the test strategy of each test unit and the corresponding plurality of dependent Mock. The method and the device greatly save the time for developing and testing personnel to compile the unit test, improve the readability of the unit test case, and obviously improve the use efficiency of the Mock.

Description

Method, device, equipment and medium for generating unit test case
Technical Field
The application relates to the technical field of code testing, in particular to a method, a device, equipment and a medium for generating a unit test case.
Background
The earlier a problem is discovered during the software development cycle, the lower the cost of repairing the problem. If the unit test is done and the potential design errors in the code are removed, the subsequent high test level, such as the integration test, the system test, etc., can save a lot of cost. And sufficient unit test needs to occupy a lot of workload of research and development, and the ratio of the workload of the unit test to the workload of the common code test can even reach 1: 1-1.5: 1.
However, in the prior art, automation of unit test is not widely implemented, which mainly means that the complexity of the items to be tested is different. If the selected tested codes are all simple codes, such as calculator functions, judgment triangles, time stamp conversion and the like, good test coverage can be achieved.
However, the software project which is put on production is usually complicated and various problems are encountered when automatically generating unit test cases: the code cannot be generated or the time consumption is too long, the readability of the generated code is poor, the generated code cannot run, and the like; meanwhile, a test strategy is lacked, and all tested codes are generated in a non-side-emphasis manner, so that the problems of excessive unit test cases and large test workload are caused.
Disclosure of Invention
The embodiment of the application provides a method, a device, equipment and a medium for generating unit test cases, aiming at the situations, the method can realize automatic generation of the unit test cases, and is small in number of the test cases and strong in readability.
In a first aspect, an embodiment of the present application provides a method for generating a unit test case, where the method includes:
matching a project to be tested with a plurality of sets of reference codes in a sample library, and determining a plurality of units to be tested of the project to be tested and object model code projects of all the units to be tested;
determining a test strategy of each test unit according to the object model of each tested unit;
analyzing the dependency relationship of each test unit, and generating a plurality of dependency Mock of each test unit according to the analysis result of the dependency relationship;
and generating a plurality of unit test cases of the item to be tested according to the test strategy of each test unit and the corresponding plurality of dependent Mock, wherein the unit test cases comprise unit test scripts and Mock codes.
In a second aspect, an embodiment of the present application further provides a method for generating a unit test case, where the method includes:
the matching unit is used for matching an item to be tested with a plurality of sets of reference codes in a sample library and determining a plurality of tested units of the item to be tested and object model code items of the tested units;
the strategy hit unit is used for determining the test strategy of each test unit according to the object model of each tested unit;
the Mock generation unit is used for analyzing the dependency relationship of each test unit and generating a plurality of dependency Mock of each test unit according to the analysis result of the dependency relationship;
and the case generating unit is used for generating a plurality of unit test cases of the item to be tested according to the test strategies of the test units and the corresponding dependence Mock, and the unit test cases comprise unit test scripts and Mock codes.
In a third aspect, an embodiment of the present application further provides an electronic device, including: a processor; and a memory arranged to store computer executable instructions that, when executed, cause the processor to perform any of the methods described above.
In a fourth aspect, this application embodiment also provides a computer-readable storage medium storing one or more programs which, when executed by an electronic device including a plurality of application programs, cause the electronic device to perform any of the methods described above.
The embodiment of the application adopts at least one technical scheme which can achieve the following beneficial effects:
the method comprises the steps of matching items to be tested with a plurality of sets of reference codes in a sample library established in advance, determining a plurality of tested units of the items to be tested, and determining a testing strategy hit by each tested unit according to an object model of the tested units; and finally, automatically generating a unit test case of each test unit according to the test strategy and the dependency Mock of each test unit to obtain a plurality of unit test cases of the test unit. The method and the device can generate unit test codes and dependent Mock codes of the items to be tested according to needs, and greatly save time for development and testers to compile unit tests; the coding structure of the unit test case is standardized; the generated code style is similar to that of the sample library, and the readability of the unit test case is improved; in the process of generating the unit test case, multiplexing can be realized by relying on the Mock, and the use efficiency of the Mock is obviously improved.
Drawings
The accompanying drawings, which are included to provide a further understanding of the application and are incorporated in and constitute a part of this application, illustrate embodiment(s) of the application and together with the description serve to explain the application and not to limit the application. In the drawings:
FIG. 1 is a flow diagram illustrating a method for generating a unit test case according to an embodiment of the present application;
FIG. 2 shows a schematic diagram of the results of a dependency analysis of an item under test according to one embodiment of the present application;
fig. 3 is a schematic structural diagram illustrating a device for generating a unit test case according to another embodiment of the present application;
fig. 4 is a schematic structural diagram of an electronic device in an embodiment of the present application.
Detailed Description
In order to make the objects, technical solutions and advantages of the present application more apparent, the technical solutions of the present application will be described in detail and completely with reference to the following specific embodiments of the present application and the accompanying drawings. It should be apparent that the described embodiments are only some of the embodiments of the present application, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
The technical solutions provided by the embodiments of the present application are described in detail below with reference to the accompanying drawings.
The method comprises the steps of comparing and matching a project to be tested with reference codes in a sample library to determine a plurality of tested units of the project to be tested, and attributing a test strategy of each tested unit according to an object model of the tested unit; and simultaneously, analyzing the dependency relationship among the tested units in the to-be-tested project to obtain the dependency units depended on by the test units, determining the dependency units of the test units from the Mock of all the dependency units by each dependency unit, multiplexing the Mock in the process, and automatically generating the unit test cases according to the test strategies and the dependency units of the test units. The unit test cases generated by the method are high in readability, small in number of cases and high in use of Mock.
Fig. 1 is a schematic flow diagram illustrating a method for generating a unit test case according to an embodiment of the present application, and as can be seen from fig. 1, the present application at least includes steps S110 to S140:
step S110: matching the item to be tested with a plurality of sets of reference codes in a sample library, and determining a plurality of units to be tested of the item to be tested and an object model of each unit to be tested.
The sample library is established in advance, wherein a plurality of sets of reference codes and abstract information of each set of reference codes can be stored, the reference codes can be stored in a specific form, such as an abstract syntax tree structure (AST), and the abstract information of the reference codes can include, but is not limited to, a plurality of units to be tested of the reference codes, business logic of each unit to be tested, an object model, a test script template of each unit to be tested, and the like.
The sets of reference codes include, but are not limited to, code items of different languages, such as C # language, Java language; which may contain multiple sets of code items of different architecture and different styles in each language.
The benchmark code is formed by extracting typical codes or code groups according to different frameworks and code structures from a code warehouse. In software project development, the code structure and code style of different enterprises are very different, or in the same organization, the built project depends on different frameworks, and the code structure and style are different. Thus, multiple sets of reference codes may have different code structures and styles; and the established sample library can be different for different enterprises.
When generating a unit test case of an item to be tested, the item to be tested may be first represented in a specific form, such as in the form of an abstract syntax tree structure, and then a plurality of items to be tested of the item to be tested are determined. The unit under test (unit testing) of items in different languages is understood as checking and verifying the minimum testable unit in software, and the meaning of the unit in the unit test is generally determined according to the actual situation, for example, the unit in C language refers to a function, the unit in Java refers to a class, and the graphical software may refer to a window or a menu. In general, a unit is a minimum tested function module specified manually, and in some embodiments of the present application, a node of the abstract syntax tree structure may be used as a tested unit of the item to be tested.
And then comparing and matching the structure and style of the item to be tested with a plurality of sets of reference codes in the sample library, and hitting one or more sets of reference codes.
And then finding out corresponding grammar units of the tested units of the items to be tested, which are the same as or similar to the hit reference codes, analyzing to obtain object models of the corresponding grammar units, and taking the object models as the object models of the tested units.
For example, if the item to be tested includes a unit a to be tested, and the hit reference code includes a corresponding syntax unit B similar to the unit a to be tested, the object model of the corresponding syntax unit B is analyzed or read, and the obtained object model result is used as the object model of the unit a to be tested. The object model can be understood as the definition of the property of the unit under test, and if the object model of one unit under test is the underlying code, the object model can be understood as the underlying code.
Step S120: and determining the test strategy of each test unit according to the object model of each tested unit.
For different object models, corresponding test strategies are set in advance, namely each object model corresponds to one test strategy, and different object models correspond to different test strategies. As in some embodiments of the present application, the object model mainly includes an underlying code object and a business logic object, and the test policy mainly includes a first test policy and a second test policy, wherein the underlying code object corresponds to the first test policy and the business logic object corresponds to the second test policy. It should be noted that the underlying code object and the service logic object, as well as the first test policy and the second test policy are only exemplary descriptions, the object model may be divided according to the specific service of the item to be tested, and the first test policy and the second test policy may also be set according to the specific content of the corresponding object model, which is not limited in the present application.
The hit test strategy of a tested unit is determined according to the object model of the tested unit, if the object model of the tested unit is the bottom code object, the hit test strategy is the first test strategy in the step, if the object model of the tested unit is the business logic object, the hit test strategy is the second test strategy in the step.
Step S130: and analyzing the dependency relationship of each tested unit in the item to be tested, and generating a plurality of dependency Mock of each testing unit according to the analysis result of the dependency relationship.
Often, there is a dependency relationship between multiple test units of the item to be tested, for example, the tested unit a needs to call the result of the tested unit B or return data for operation, and this situation can be understood as that the tested unit a depends on the tested unit B.
As shown in fig. 2, fig. 2 shows a schematic diagram of a result of analyzing the dependency relationship of the item to be tested according to an embodiment of the present application, and as can be seen from fig. 2, a tested unit a depends on a tested unit B, a tested unit C, a tested unit F, and a tested unit D; the unit under test B depends on the unit under test E and the unit under test F; the unit under test C depends on the unit under test D and the unit under test H.
From the results shown in FIG. 2, a dependency Mock for each test cell is generated. The function of Mock is to simulate some objects which are not easily constructed or not easily obtained by a virtual object (which may be called Mock) during the test process so as to perform the test method of the test. Taking unit under test a as an example, Mock of unit under test B, unit under test C, unit under test F, and unit under test D needs to be generated. It should be noted that, for a unit under test, it is only necessary to generate the Mock of the unit under test on which it directly depends, but it is not necessary to generate the Mock of the unit under test on which it indirectly depends. The Mock of the unit under test B, the unit under test C, the unit under test F, and the unit under test D is recorded as the dependent Mock of the unit under test A.
Taking the unit under test B as an example, the dependency Mock is generated, including Mock of the unit under test E and the unit under test F.
It can be seen that the dependent Mock of the tested unit a includes Mock of the tested unit B, the tested unit C, the tested unit F and the tested unit D, and the dependent Mock of the tested unit B includes Mock of the tested unit E and the tested unit F, that is, both include Mock of the tested unit F.
Step S140: and generating a plurality of unit test cases of the item to be tested according to the test strategy of each test unit and the corresponding plurality of dependent Mock, wherein the unit test cases comprise unit test scripts and Mock codes.
And finally, automatically generating codes of the unit test cases according to the test strategy of the test unit and a plurality of corresponding dependency Mock codes, wherein the unit test cases comprise unit test scripts and Mock codes, the corresponding unit test scripts can be operated when one tested unit is tested when the unit test cases are operated, and the corresponding dependency Mock can be read when the result of the dependency unit is needed so as to complete the execution of the unit test scripts.
The code of the automatically generated unit test case can be stored in a default directory of the project, and each unit under test can generate two files, for example, a.java of the unit under test can correspondingly generate a _ test.java and a _ mock.java, wherein the a _ test.java is a unit test script, and the a _ mock.java is code of the unit under test depending on Mock.
As can be seen from the method shown in fig. 1: the method comprises the steps of matching items to be tested with a plurality of sets of reference codes in a sample library established in advance, determining a plurality of tested units of the items to be tested, and determining a testing strategy hit by each tested unit according to an object model of the tested units; and finally, automatically generating a unit test case of each test unit according to the test strategy and the dependency Mock of each test unit to obtain a plurality of unit test cases of the test unit. The method and the device can generate unit test codes and dependent Mock codes of the items to be tested according to needs, and greatly save the time for development and testers to compile unit tests; the coding structure of the unit test case is standardized; the generated code style is similar to that of the sample library, and the readability of the unit test case is improved; in the process of generating the unit test case, multiplexing can be realized by relying on the Mock, and the use efficiency of the Mock is obviously improved.
In some embodiments of the present application, in the method, the matching the item to be tested with a plurality of sets of reference codes in a sample library, and determining a plurality of units under test of the item to be tested and an object model of each unit under test includes: respectively representing the item to be tested and the sets of reference codes in the form of an abstract syntax tree, wherein the abstract syntax tree comprises a plurality of nodes; taking the plurality of nodes of the item to be tested as a plurality of units to be tested of the item to be tested; carrying out project frame and code structure matching on each node of the project to be tested and each node of the multiple sets of reference codes, and determining a reference test unit corresponding to each tested unit; and taking the object model of the benchmark test unit as the object model corresponding to the tested unit, wherein the object model comprises an underlying code object and a business logic object.
In order to more conveniently match the item to be tested with the reference code in the sample library, the item to be tested and the reference code in the sample library can be represented in the form of an abstract syntax tree, wherein the Abstract Syntax Tree (AST) is a tree representation method for an abstract syntax structure of a source program, and each node in the abstract syntax tree can represent a structure of the source code. The abstract syntax tree is not dependent on the source language of the syntax, and the tree structure of the syntax tree is more operable than the linear structure of the source code and is easier to analyze and use. And then taking the plurality of nodes of the item to be tested as a plurality of tested units of the item to be tested, matching each node of the item to be tested with each node of a plurality of sets of reference codes in a sample library, specifically, comparing contents such as a project frame, a code structure, a code style and the like, and finding out a node which is the same as or similar to the tested unit of the item to be tested in the reference codes to be used as a reference testing unit. Then determining an object model of the benchmark test unit, and if the object model of the benchmark test unit already exists, directly reading the object model of the benchmark test unit; if the Object model of the benchmark test unit does not exist, analyzing the Object model of the benchmark test unit, and if the benchmark test unit is determined to be BO (Business Object) and DAO (Data Access Object Pattern) of the Java Web project bottom layer through analysis, determining the Object model of the tested unit corresponding to the benchmark test unit to be a bottom layer code Object; and if the benchmark test unit is determined to be a List object of the core business logic through analysis, determining the object model of the unit under test corresponding to the benchmark test unit to be a business logic object.
In some embodiments of the present application, in the above method, the test policy comprises a first test policy and a second test policy; the determining the test strategy of each test unit according to the object model of each tested unit comprises: if the object model of one tested unit is a bottom code object, determining the test strategy of the tested unit as a first test strategy; and if the object model of one tested unit is a business logic object, determining the test strategy of the tested unit as a second test strategy.
In some embodiments of the present application, the first test strategy is formulated for an underlying code object, the second test strategy is formulated for a business logic object, in the first test strategy, the tested units of the underlying code are basically and automatically generated by a tool, and there is no need to regenerate unit test codes for the tested units, so readability is improved in terms of code simplicity, and therefore the first test strategy is to not generate unit test cases for the test units; when the object model of a tested unit is a business logic object, the tested unit needs to be tested, and in the second test strategy, the second test strategy can set a side to reconstruct various parameters to complete comprehensive branch coverage, even cover abnormal calling conditions and the like. The specific content of the first test strategy and the second test strategy is not limited in the present application, and may be limited according to specific service requirements.
When determining a test strategy hit by a tested unit, determining according to an object model of the tested unit, and when the object model of the tested unit is a bottom code object, hitting a first test strategy; when the object model of one tested unit is a business logic object, the second testing strategy is hit.
In some embodiments of the present application, in the method, the performing a dependency analysis on each of the test units in the item to be tested includes: calling a dependency analysis tool, traversing the item to be tested, and determining the integral dependency relationship of the item to be tested according to the function call relationship among all tested units in the item to be tested; and analyzing the whole dependency relationship, and determining a plurality of dependency units of each tested unit.
When analyzing the dependency relationship among a plurality of tested units of the to-be-tested units, a corresponding dependency analysis tool can be called according to the language of the to-be-tested item, and the function call relationship among the tested units is analyzed to obtain the overall dependency relationship of the to-be-tested item, where the overall dependency relationship may be a dependency relationship analysis result as shown in fig. 2. Then, it is sufficient to analyze the overall dependency relationship to specifically obtain which dependent unit of the unit under test, and it should be noted that the dependent unit of one unit under test only includes the unit on which it directly depends.
When analyzing the dependency relationship among a plurality of tested units of the tested units, the external dependency of each method in the tested units can be analyzed, for the items to be tested of the Java language, Java reflection and byte code analysis tools can be used, such as ASM (an ASM code analysis tool, and has no uniform Chinese name in the industry), Javassist (a code analysis tool, and has no uniform Chinese name in the industry) and the like, which are open source analysis, editing and Java byte code creation tools, and a set of methods for analyzing class information is provided, including reading method parameter names and the like. For the items to be tested in the C # language, the call relation of the analysis test method can adopt an open source item Mono.Cecil (a code analysis tool, unified Chinese names are temporarily absent in the industry), Mono.Cecil can convert the test method into an IL code (intermediate code), and the external call relation is obtained through analysis according to the IL code.
In some embodiments of the present application, in the method, the generating a plurality of dependent Mock for each unit under test according to the result of the dependency analysis includes: performing redundancy removal processing on the determined multiple dependent units to obtain a plurality of target dependent units; generating a Mock corresponding to each target dependent unit, if the language of the target dependent unit is C, generating the Mock corresponding to the target dependent unit based on a Microsoft faces tool, and adding an index variable in the Mock; and if the language of the target dependent unit is Java, generating a Mock corresponding to the target dependent unit based on a Mock tool.
Since the Mock is reusable, the Mock corresponding to one dependent unit does not need to be repeatedly generated, and therefore, the redundancy removing processing is performed on all determined dependent units first, still taking the embodiment shown in fig. 2 as an example, in fig. 2, the dependent Mock required by the unit under test a includes the unit under test B, the unit under test C, the unit under test D, and the unit under test F, the dependent Mock required by the unit under test B includes the unit under test E and the unit under test F, the dependent Mock required by the unit under test C includes the unit under test D and the unit under test H, and the determination target dependent unit includes the unit under test B, the unit under test C, the unit under test D, the unit under test E, and the unit under test Ff is performed through the redundancy removing processing.
And then generating Mock corresponding to each target dependent unit, namely generating Mock of the unit under test B, the unit under test C, the unit under test D, the unit under test E and the unit under test F, which can be respectively marked as Mock B, Mock C, Mock E and Mock F.
For the generating method of Mock, different methods can be used for different program languages, for example, for a tested unit of C # language, Microsoft can be used to provide a set of quick Mock frame named Microsoft keys, since Mock of Microsoft keys can only return one value for one method, but needs to return different results for calling an external method for a test method for multiple times, in this application, a variable of an index is added to Mock to indicate the number of times the external calling method runs in the code, and different results are returned according to the index at the time of testing.
For the tested unit in Java language, but not limited to, mockitt (a Mock tool, having no uniform Chinese name temporarily in the industry) can be adopted, which is capable of conveniently simulating (Mock) final class, static method, constructor, private static final attribute in unit test compared with Mockito (a Mock tool, having no uniform Chinese name temporarily in the industry), easylmock (a Mock tool, having no uniform Chinese name temporarily in the industry) and other Mock tools. The Mock operations of different test methods are isolated from each other and cannot influence each other, namely, the Mock effect generated by the Mock operation called in the current test method is invalid after the method is executed.
In the process of generating Mock, default logic of the Mock unit labeled by @ Mock is that a corresponding Mock value is returned according to the called times, and the Mock unit can be rewritten in the application to realize any self-defined logic, such as verifying the called times of the Mock unit and throwing out an exception to simulate an exception condition; of course, the method can also be realized by invoking the original method through Invocation in JMockit.
In addition, in some embodiments of the present application, in order to improve the use efficiency of Mock, the packet name prefix of Mock is not performed by default, such as the following prefixes in Java language:
java./sun./java. x./org. xml/org.w3c/org.omg./sunw./org.jcp./org.ietf./daikon./com.google.common/org.exsyst/org.joda.time, etc.
In some embodiments of the present application, in the above method, the sample library is obtained by: representing a plurality of sets of original codes in a code warehouse in an abstract syntax tree form to obtain abstract syntax trees of a plurality of projects; merging and removing redundancy of abstract syntax trees of a plurality of projects adopting the same language; and taking each processed abstract syntax tree as a set of reference code items.
For the establishment of the sample library, the present application is not limited, and for the convenience of the execution of the subsequent steps, some more typical codes can be selected from a code warehouse of a target enterprise and recorded as original codes, then the codes are expressed in the form of abstract syntax trees, various nodes in the abstract syntax trees of a plurality of items in the same language are merged and subjected to redundancy removal, and finally each abstract syntax tree after processing is used as a set of reference code items.
Fig. 3 is a schematic structural diagram illustrating a device for generating unit test cases according to another embodiment of the present application, and as can be seen from fig. 3, the device 300 for generating unit test cases includes:
the matching unit 310 is used for matching an item to be tested with a plurality of sets of reference codes in a sample library, and determining a plurality of units to be tested of the item to be tested and an object model of each unit to be tested;
a strategy hit unit 320, configured to determine a test strategy of each test unit according to the object model of each tested unit;
a Mock generating unit 330, configured to perform dependency relationship analysis on each of the test units, and generate a plurality of Mock of each test unit according to a result of the dependency relationship analysis;
the case generating unit 340 is configured to generate a plurality of unit test cases of the item to be tested according to the test policies of the test units and the corresponding Mock, where the unit test cases include unit test scripts and Mock codes.
In some embodiments of the present application, in the above apparatus, the matching unit 310 is configured to represent the item to be tested and the sets of reference codes respectively as an abstract syntax tree, where the abstract syntax tree includes a plurality of nodes; taking the plurality of nodes of the item to be tested as a plurality of units to be tested of the item to be tested; carrying out project frame and code structure matching on each node of the project to be tested and each node of the sets of reference codes, and determining a reference test unit corresponding to each tested unit; and taking the object model of the benchmark test unit as the object model corresponding to the tested unit, wherein the object model comprises an underlying code object and a business logic object.
In some embodiments of the present application, in the above apparatus, the test policy comprises a first test policy and a second test policy; a policy hit unit 320, configured to determine that the test policy of the test unit is the first test policy if the object model of the tested unit is the underlying code object; and if the object model of one tested unit is a business logic object, determining the test strategy of the tested unit as a second test strategy.
In some embodiments of the present application, in the above apparatus, the Mock generating unit 330 is configured to invoke a dependency analysis tool, traverse the item to be tested, and determine an overall dependency relationship of the item to be tested according to a function invocation relationship between each tested unit in the item to be tested; and analyzing the whole dependency relationship, and determining a plurality of dependency units of each tested unit.
In some embodiments of the present application, in the above apparatus, the Mock generating unit 330 is configured to perform redundancy elimination on the determined multiple dependency units to obtain multiple target dependency units; generating a Mock corresponding to each target dependent unit, if the language of the target dependent unit is C, generating the Mock corresponding to the target dependent unit based on a Microsoft faces tool, and adding an index variable in the Mock; and if the language of the target dependent unit is Java, generating a Mock corresponding to the target dependent unit based on a Mock tool.
In some embodiments of the present application, in the above apparatus, the use case generating unit 340 is configured to confirm that the unit test case of a unit under test is not generated if the test policy of the unit under test is the first test policy; and if the test strategy of one tested unit is the second test strategy, generating a unit test script according to the service logic of the tested unit, and correspondingly storing the unit test script of the tested unit and a plurality of corresponding Mock.
In some embodiments of the present application, in the apparatus, the apparatus further includes a sample library creating unit, configured to represent, in the form of an abstract syntax tree, multiple sets of original item codes in the code repository, so as to obtain an abstract syntax tree of multiple items; merging and removing redundancy of abstract syntax trees of a plurality of projects adopting the same language; and taking each processed abstract syntax tree as a set of reference code items.
Fig. 4 is a schematic structural diagram of an electronic device according to an embodiment of the present application. Referring to fig. 4, at a hardware level, the electronic device includes a processor, and optionally further includes an internal bus, a network interface, and a memory. The Memory may include a Memory, such as a Random-Access Memory (RAM), and may further include a non-volatile Memory, such as at least 1 disk Memory. Of course, the electronic device may also include hardware required for other services.
The processor, the network interface, and the memory may be connected to each other via an internal bus, which may be an ISA (Industry Standard Architecture) bus, a PCI (Peripheral Component Interconnect) bus, an EISA (Extended Industry Standard Architecture) bus, or the like. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, only one double-headed arrow is shown in FIG. 4, but that does not indicate only one bus or one type of bus.
And the memory is used for storing programs. In particular, the program may include program code comprising computer operating instructions. The memory may include both memory and non-volatile storage and provides instructions and data to the processor.
The processor reads the corresponding computer program from the nonvolatile memory into the memory and then runs the computer program to form a generating device of the unit test case on the logic level. And the processor is used for executing the program stored in the memory and is specifically used for executing the method.
The method executed by the unit test case generation device according to the embodiment shown in fig. 3 of the present application may be applied to a processor, or may be implemented by a processor. The processor may be an integrated circuit chip having signal processing capabilities. In implementation, the steps of the above method may be performed by integrated logic circuits of hardware in a processor or instructions in the form of software. The Processor may be a general-purpose Processor, including a Central Processing Unit (CPU), a Network Processor (NP), and the like; but also Digital Signal Processors (DSPs), Application Specific Integrated Circuits (ASICs), Field Programmable Gate Arrays (FPGAs) or other Programmable logic devices, discrete Gate or transistor logic devices, discrete hardware components. The various methods, steps, and logic blocks disclosed in the embodiments of the present application may be implemented or performed. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like. The steps of the method disclosed in connection with the embodiments of the present application may be directly implemented by a hardware decoding processor, or implemented by a combination of hardware and software modules in the decoding processor. The software module may be located in ram, flash memory, rom, prom, or eprom, registers, etc. storage media as is well known in the art. The storage medium is located in a memory, and a processor reads information in the memory and completes the steps of the method in combination with hardware of the processor.
The electronic device may further execute the method executed by the unit test case generation apparatus in fig. 3, and implement the function of the unit test case generation apparatus in the embodiment shown in fig. 3, which is not described herein again in this embodiment of the present application.
The embodiment of the present application further provides a computer-readable storage medium, where the computer-readable storage medium stores one or more programs, where the one or more programs include instructions, which, when executed by an electronic device including multiple application programs, enable the electronic device to perform the method performed by the apparatus for generating a unit test case in the embodiment shown in fig. 3, and are specifically configured to perform the foregoing method.
As will be appreciated by one skilled in the art, embodiments of the present application may be provided as a method, system, or computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present application may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and so forth) having computer-usable program code embodied therein.
The present application is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the application. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
In a typical configuration, a computing device includes one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
The memory may include forms of volatile memory in a computer readable medium, Random Access Memory (RAM) and/or non-volatile memory, such as Read Only Memory (ROM) or flash memory (flash RAM). Memory is an example of a computer-readable medium.
Computer-readable media, including both non-transitory and non-transitory, removable and non-removable media, may implement information storage by any method or technology. The information may be computer readable instructions, data structures, modules of a program, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), Static Random Access Memory (SRAM), Dynamic Random Access Memory (DRAM), other types of Random Access Memory (RAM), Read Only Memory (ROM), Electrically Erasable Programmable Read Only Memory (EEPROM), flash memory or other memory technology, compact disc read only memory (CD-ROM), Digital Versatile Discs (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store information that can be accessed by a computing device. As defined herein, a computer readable medium does not include a transitory computer readable medium such as a modulated data signal and a carrier wave.
It should also be noted that the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of another element in the same process, method, article, or apparatus that comprises the element.
As will be appreciated by one skilled in the art, embodiments of the present application may be provided as a method, system, or computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present application may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The above description is only an example of the present application and is not intended to limit the present application. Various modifications and changes may occur to those skilled in the art. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the scope of the claims of the present application.

Claims (10)

1. A method for generating a unit test case is characterized in that the method comprises the following steps:
matching a project to be tested with a plurality of sets of reference codes in a sample library, and determining a plurality of units to be tested of the project to be tested and an object model of each unit to be tested;
determining a test strategy of each test unit according to the object model of each tested unit;
analyzing the dependency relationship of each tested unit in the item to be tested, and generating a plurality of dependency Mock of each testing unit according to the analysis result of the dependency relationship;
and generating a plurality of unit test cases of the item to be tested according to the test strategy of each test unit and the corresponding plurality of dependent Mock, wherein the unit test cases comprise unit test scripts and Mock codes.
2. The method of claim 1, wherein matching the item to be tested with a plurality of sets of reference codes in a sample library, determining a plurality of units under test of the item to be tested and an object model of each unit under test comprises:
respectively representing the item to be tested and the sets of reference codes in the form of an abstract syntax tree, wherein the abstract syntax tree comprises a plurality of nodes;
taking the plurality of nodes of the item to be tested as a plurality of units to be tested of the item to be tested;
carrying out project frame and code structure matching on each node of the project to be tested and each node of the sets of reference codes, and determining a reference test unit corresponding to each tested unit;
and taking the object model of the benchmark test unit as the object model corresponding to the tested unit, wherein the object model comprises an underlying code object and a business logic object.
3. The method of claim 1, wherein the test policy comprises a first test policy and a second test policy;
the determining the test strategy of each test unit according to the object model of each tested unit comprises:
if the object model of one tested unit is a bottom code object, determining the test strategy of the tested unit as a first test strategy;
and if the object model of one tested unit is a business logic object, determining the test strategy of the tested unit as a second test strategy.
4. The method of claim 1, wherein the analyzing the dependency of each of the test units in the item under test comprises:
calling a dependency analysis tool, traversing the item to be tested, and determining the integral dependency relationship of the item to be tested according to the function call relationship among all tested units in the item to be tested;
and analyzing the whole dependency relationship, and determining a plurality of dependency units of each tested unit.
5. The method according to claim 4, wherein the generating a plurality of dependency Mock for each unit under test according to the dependency analysis result comprises:
performing redundancy removal processing on the determined multiple dependent units to obtain a plurality of target dependent units;
generating a Mock corresponding to each target dependent unit, if the language of the target dependent unit is C, generating the Mock corresponding to the target dependent unit based on a Microsoft faces tool, and adding an index variable in the Mock; and if the language of the target dependent unit is Java, generating a Mock corresponding to the target dependent unit based on a Mock tool.
6. The method of claim 1, wherein the generating a plurality of unit test cases of the item to be tested according to the test strategy of each test unit and a plurality of corresponding dependent Mock codes, the unit test cases comprising unit test scripts and Mock codes comprises:
if the test strategy of one tested unit is the first test strategy, confirming that the unit test case of the tested unit is not generated;
and if the test strategy of one tested unit is the second test strategy, generating a unit test script according to the service logic of the tested unit, and correspondingly storing the unit test script of the tested unit and a plurality of corresponding Mock.
7. The method according to any one of claims 1 to 6, wherein the sample library is obtained by:
representing a plurality of sets of original project codes in a code warehouse in an abstract syntax tree form to obtain abstract syntax trees of a plurality of projects;
merging and removing redundancy of abstract syntax trees of a plurality of projects adopting the same language;
and taking each processed abstract syntax tree as a set of reference code items.
8. A method for generating a unit test case is characterized in that the method comprises the following steps:
the matching unit is used for matching the item to be tested with a plurality of sets of reference codes in a sample library and determining a plurality of units to be tested of the item to be tested and an object model of each unit to be tested;
the strategy hit unit is used for determining the test strategy of each test unit according to the object model of each tested unit;
the Mock generation unit is used for analyzing the dependency relationship of each test unit and generating a plurality of dependency Mock of each test unit according to the analysis result of the dependency relationship;
and the case generating unit is used for generating a plurality of unit test cases of the item to be tested according to the test strategies of the test units and the corresponding dependence Mock, and the unit test cases comprise unit test scripts and Mock codes.
9. An electronic device, comprising:
a processor; and
a memory arranged to store computer executable instructions that, when executed, cause the processor to perform the method of claims 1 to 7.
10. A computer readable storage medium storing one or more programs which, when executed by an electronic device comprising a plurality of application programs, cause the electronic device to perform the method of claims 1-7.
CN202210679196.6A 2022-06-16 2022-06-16 Method, device, equipment and medium for generating unit test case Pending CN114968806A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210679196.6A CN114968806A (en) 2022-06-16 2022-06-16 Method, device, equipment and medium for generating unit test case

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210679196.6A CN114968806A (en) 2022-06-16 2022-06-16 Method, device, equipment and medium for generating unit test case

Publications (1)

Publication Number Publication Date
CN114968806A true CN114968806A (en) 2022-08-30

Family

ID=82962878

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210679196.6A Pending CN114968806A (en) 2022-06-16 2022-06-16 Method, device, equipment and medium for generating unit test case

Country Status (1)

Country Link
CN (1) CN114968806A (en)

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20050160322A1 (en) * 2004-01-13 2005-07-21 West John R. Method and system for conversion of automation test scripts into abstract test case representation with persistence
US20120084754A1 (en) * 2010-09-30 2012-04-05 Oracle International Corporation Streamlining Unit Testing Through Hot Code Swapping
KR20160044305A (en) * 2014-10-15 2016-04-25 삼성에스디에스 주식회사 Apparatus and method for unit test of code
CN110647459A (en) * 2018-06-27 2020-01-03 阿里巴巴集团控股有限公司 Application testing method and device
CN113010431A (en) * 2021-03-31 2021-06-22 中国工商银行股份有限公司 Java unit test case generation method and device
CN114579470A (en) * 2022-03-25 2022-06-03 创优数字科技(广东)有限公司 Unit testing method and device, electronic equipment and storage medium

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20050160322A1 (en) * 2004-01-13 2005-07-21 West John R. Method and system for conversion of automation test scripts into abstract test case representation with persistence
US20120084754A1 (en) * 2010-09-30 2012-04-05 Oracle International Corporation Streamlining Unit Testing Through Hot Code Swapping
KR20160044305A (en) * 2014-10-15 2016-04-25 삼성에스디에스 주식회사 Apparatus and method for unit test of code
CN110647459A (en) * 2018-06-27 2020-01-03 阿里巴巴集团控股有限公司 Application testing method and device
CN113010431A (en) * 2021-03-31 2021-06-22 中国工商银行股份有限公司 Java unit test case generation method and device
CN114579470A (en) * 2022-03-25 2022-06-03 创优数字科技(广东)有限公司 Unit testing method and device, electronic equipment and storage medium

Similar Documents

Publication Publication Date Title
CN109614324B (en) Test case generation method and device
US20080307006A1 (en) File mutation method and system using file section information and mutation rules
US20110145799A1 (en) Path-sensitive dataflow analysis including path refinement
CN108469955B (en) Annotation-based Android injection framework implementation method
CN114968807A (en) Code detection method and device, electronic equipment and readable storage medium
JP2018505506A (en) Machine-based instruction editing
CN110990274A (en) Data processing method, device and system for generating test case
CN111240987B (en) Method and device for detecting migration program, electronic equipment and computer readable storage medium
CN116610568A (en) Method, device, equipment and medium for identifying dependency relationship of codes
CN114490413A (en) Test data preparation method and device, storage medium and electronic equipment
WO2023207973A1 (en) Compiler test method and apparatus, case generation method and apparatus, and instruction storage structure
CN110147313B (en) Log output method and device
CN112445706A (en) Program abnormal code acquisition method and device, electronic equipment and storage medium
CN112148590A (en) Method, device and equipment for determining code coverage rate
CN114968806A (en) Method, device, equipment and medium for generating unit test case
CN116302919A (en) Multi-language extensible code dependency analysis model and analysis method
CN113296834B (en) Android closed source service type information extraction method based on reverse engineering
CN113672233B (en) Server out-of-band management method, device and equipment based on Redfish
CN115543832A (en) Software code standard detection method, system, equipment and storage medium
US11740895B2 (en) Generation of software program repair explanations
CN114579141A (en) Method and device for converting require frame into VUE frame
CN115080426A (en) Program file detection method and device, storage medium and electronic equipment
CN113360373A (en) Test method for full traversal of Activity page of mobile application
CN109857665B (en) Test execution method and device for test case
CN110688430A (en) Method and device for obtaining data bypass and electronic equipment

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