CN114153742A - Unit testing method, unit testing device, electronic equipment and storage medium - Google Patents

Unit testing method, unit testing device, electronic equipment and storage medium Download PDF

Info

Publication number
CN114153742A
CN114153742A CN202111509601.1A CN202111509601A CN114153742A CN 114153742 A CN114153742 A CN 114153742A CN 202111509601 A CN202111509601 A CN 202111509601A CN 114153742 A CN114153742 A CN 114153742A
Authority
CN
China
Prior art keywords
statement
population
test
assertion
sets
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
CN202111509601.1A
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.)
WeBank Co Ltd
Hong Kong University of Science and Technology HKUST
Original Assignee
WeBank Co Ltd
Hong Kong University of Science and Technology HKUST
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 WeBank Co Ltd, Hong Kong University of Science and Technology HKUST filed Critical WeBank Co Ltd
Priority to CN202111509601.1A priority Critical patent/CN114153742A/en
Publication of CN114153742A publication Critical patent/CN114153742A/en
Priority to PCT/CN2022/124385 priority patent/WO2023103574A1/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/3688Test management for test execution, e.g. scheduling of test suites
    • 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
    • G06NCOMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
    • G06N3/00Computing arrangements based on biological models
    • G06N3/12Computing arrangements based on biological models using genetic models
    • G06N3/126Evolutionary algorithms, e.g. genetic algorithms or genetic programming

Abstract

The embodiment of the application provides a unit testing method, a unit testing device, electronic equipment and a storage medium, wherein the method comprises the following steps: acquiring a plurality of statement sets of an ith population, wherein each statement set comprises at least one statement; the statements are used for defining the behavior of the simulation object; performing mutation operation on each statement according to the mutation operator corresponding to each statement to obtain a mutation operation result; performing cross interchange on the two statement sets in the variation operation result to obtain an i +1 th population; if the statement set which accords with the test assertion of the test case exists in the (i + 1) th population, adopting the statement set which accords with the test assertion to define a simulation object; and performing unit test on the test case based on the simulation object. Based on the unit testing method provided by the application, the applicability of the simulation object is improved during unit testing.

Description

Unit testing method, unit testing device, electronic equipment and storage medium
Technical Field
The embodiment of the application relates to the technical field of software testing of financial technology (Fintech), in particular to a unit testing method and device, electronic equipment and a computer storage medium.
Background
With the development of computer technology, more and more technologies are applied in the financial field, and the traditional financial industry is gradually changing to financial technology, but higher requirements are also put forward on the technologies due to the requirements of the financial industry on safety and real-time performance.
In the software research and development process, software needs to undergo stages of unit testing, integration testing, system testing, regression testing and the like so as to ensure the quality of each basic component unit and each interface of the software, verify the correctness of software functions and interfaces, and repair found errors.
In unit testing, a code module needs to rely on an external class or interface, and for some objects which are not easy to construct or obtain, an adaptive simulation object needs to be created, and the simulation object is defined to be adapted to the required class or interface for testing. In practical application, since the test return result is influenced by software, hardware, system environment and other aspects, the test stage needs various return results which are normal and abnormal.
In the related art, a certain type Mock object is generated for a specific domain test dependent object. For a general type of test dependent object, a developer usually needs to create the Mock object manually, it is difficult to specify proper behavior for the Mock object, and it is impossible to adapt to any test dependent object. Therefore, how to improve the applicability of the simulation object in the unit test becomes an important issue to be solved urgently.
Disclosure of Invention
The embodiment of the application provides a unit testing method and device, electronic equipment and a computer storage medium, which can improve the applicability of a simulation object during unit testing.
The unit testing method provided by the embodiment of the application comprises the following steps:
acquiring a plurality of statement sets of an ith population, wherein each statement set comprises at least one statement; the statement is used for defining the behavior of a simulation object;
performing mutation operation on each statement according to the mutation operator corresponding to each statement to obtain a mutation operation result; performing cross interchange on the two statement sets in the variation operation result to obtain an i +1 th population;
if the statement set which conforms to the test assertion of the test case exists in the (i + 1) th population, adopting the statement set which conforms to the test assertion to define the simulation object; and performing unit test on the test case based on the simulation object.
In one implementation, the method further comprises:
under the condition that i is an integer larger than 1, if it is determined that no statement set which meets the test assertion of the test case exists in the (i-1) th population, acquiring the fitness of each statement set in the (i-1) th population;
according to the fitness of each statement set, taking the statement set with the fitness larger than a preset threshold value in the ith-1 population as the statement set of the ith population;
or, determining the probability of drawing each statement set in the i-1 th population according to the fitness of each statement set; and sampling a plurality of statement sets of the i-1 th population according to the probability of each statement set being sampled to obtain the statement set of the i-th population.
In one implementation, the (i-1) th population includes M statement sets; the obtaining the fitness of each statement set in the i-1 th population comprises:
determining at least one fitness function and a weight coefficient for each fitness function; obtaining a calculation result of the j statement set in the i-1 th population under each fitness function;
acquiring the fitness of the jth statement set according to the calculation result of the jth statement set under each fitness function and the weight coefficient of each fitness function;
and under the condition that j sequentially takes 1 to M, acquiring the fitness of each statement set in the (i-1) th population.
In one implementation, the ith population includes N statement sets; performing cross interchange on the two statement sets in the variation operation result to obtain an i +1 th population, including:
determining two statement sets in the N statement sets, and performing cross interchange on statements with the same statement types in the two statement sets to generate two filial generations of the two statement sets;
and adding at least one of the two statement sets and at least one of the two descendants into the (i + 1) th population to obtain N statement sets in the (i + 1) th population.
In one implementation, the determining at least one fitness function includes any one of:
creating a fitness function according to the number of the sentences generated in the sentence set and the number of the sentences successfully executed;
establishing a fitness function according to the state distribution of whether the test case can be executed before test assertion;
creating a fitness function according to the statement number of the assertion statements in the test statement and the editing distance of the expected value and the actual value of each assertion statement;
creating a fitness function according to a set of all method calls in the simulation object and a set of method calls for which a behavior has been specified in the simulation object;
and creating a fitness function according to the number of the satisfied assertion statements in the test assertion and the number of all assertion statements in the test assertion.
In one implementation, the creating a fitness function according to the number of assertion statements in the test assertion and the edit distance between the expected value and the actual value of each assertion statement includes:
determining at least one predicate statement of the test predicate; acquiring an expected value and an editing distance of an actual value of each assertion statement;
acquiring a normalization result of the editing distance of each assertion statement according to the editing distance of the expected value and the actual value of each assertion statement;
and calculating the weighted average value of the normalization result of the editing distance of each assertion statement according to the statement number of the assertion statement in the test statement, and creating a fitness function according to the weighted average value.
In one implementation, the mutating each statement according to its corresponding mutation operator includes any one of:
when the statement type is a numerical type, adopting a variant operator to perform any one of the following operations on the statement: increasing a random number value and decreasing a random number value;
when the statement type is a character string type, adopting a variant operator to perform any one of the following operations on the statement: inserting characters, deleting characters and modifying characters;
when the statement type is a statement containing a parameter list, adopting a variant operator to perform the following operations on the statement: replacing a randomly selected one of the parameters with a new value;
when the statement type is the statement called by the matching method, adopting a variant operator to execute the following operations on the statement: modifying the matching parameters in the matching parameter list;
when the statement type is a statement of a behavior list, adopting a variant operator to perform any one of the following operations on the statement: and exchanging the sequence of each behavior of each method call of the simulation object, adding the behavior, deleting the behavior and modifying the return value of the behavior.
The unit testing device provided by the embodiment of the application comprises:
the acquisition module is used for acquiring a plurality of statement sets of the ith population, wherein each statement set comprises at least one statement; the statement is used for defining the behavior of a simulation object;
the processing module is used for carrying out mutation operation on each statement according to the mutation operator corresponding to each statement to obtain a mutation operation result; performing cross interchange on the two statement sets in the variation operation result to obtain an i +1 th population;
the test module is used for defining the simulation object by adopting the statement set conforming to the test assertion if the statement set conforming to the test assertion of the test case exists in the (i + 1) th population; and performing unit test on the test case based on the simulation object.
In one implementation, the obtaining module is further configured to:
under the condition that i is an integer larger than 1, if it is determined that no statement set which meets the test assertion of the test case exists in the (i-1) th population, acquiring the fitness of each statement set in the (i-1) th population;
according to the fitness of each statement set, taking the statement set with the fitness larger than a preset threshold value in the ith-1 population as the statement set of the ith population;
or, determining the probability of drawing each statement set in the i-1 th population according to the fitness of each statement set; and sampling a plurality of statement sets of the i-1 th population according to the probability of each statement set being sampled to obtain the statement set of the i-th population.
In one implementation, the (i-1) th population includes M statement sets; the obtaining module is configured to obtain fitness of each statement set in the i-1 th population, and includes:
determining at least one fitness function and a weight coefficient for each fitness function; obtaining a calculation result of the j statement set in the i-1 th population under each fitness function;
acquiring the fitness of the jth statement set according to the calculation result of the jth statement set under each fitness function and the weight coefficient of each fitness function;
and under the condition that j sequentially takes 1 to M, acquiring the fitness of each statement set in the (i-1) th population.
In one implementation, the ith population includes N statement sets; the processing module is configured to perform cross-exchange on the two statement sets in the mutation operation result to obtain an i +1 th population, and includes:
determining two statement sets in the N statement sets, and performing cross interchange on statements with the same statement types in the two statement sets to generate two filial generations of the two statement sets;
and adding at least one of the two statement sets and at least one of the two descendants into the (i + 1) th population to obtain N statement sets in the (i + 1) th population.
In one implementation, the obtaining module is configured to determine at least one fitness function, and includes any one of:
creating a fitness function according to the number of the sentences generated in the sentence set and the number of the sentences successfully executed;
establishing a fitness function according to the state distribution of whether the test case can be executed before test assertion;
creating a fitness function according to the statement number of the assertion statements in the test statement and the editing distance of the expected value and the actual value of each assertion statement;
creating a fitness function according to a set of all method calls in the simulation object and a set of method calls for which a behavior has been specified in the simulation object;
and creating a fitness function according to the number of the satisfied assertion statements in the test assertion and the number of all assertion statements in the test assertion.
In one implementation, the obtaining module is configured to create a fitness function according to the number of the assertion statements in the test assertion and the edit distance between the expected value and the actual value of each assertion statement, and includes:
determining at least one predicate statement of the test predicate; acquiring an expected value and an editing distance of an actual value of each assertion statement;
acquiring a normalization result of the editing distance of each assertion statement according to the editing distance of the expected value and the actual value of each assertion statement;
and calculating the weighted average value of the normalization result of the editing distance of each assertion statement according to the statement number of the assertion statement in the test statement, and creating a fitness function according to the weighted average value.
In an implementation manner, the processing module is configured to perform a mutation operation on each statement according to a mutation operator corresponding to each statement, and includes any one of:
when the statement type is a numerical type, adopting a variant operator to perform any one of the following operations on the statement: increasing a random number value and decreasing a random number value;
when the statement type is a character string type, adopting a variant operator to perform any one of the following operations on the statement: inserting characters, deleting characters and modifying characters;
when the statement type is a statement containing a parameter list, adopting a variant operator to perform the following operations on the statement: replacing a randomly selected one of the parameters with a new value;
when the statement type is the statement called by the matching method, adopting a variant operator to execute the following operations on the statement: modifying the matching parameters in the matching parameter list;
when the statement type is a statement of a behavior list, adopting a variant operator to perform any one of the following operations on the statement: and exchanging the sequence of each behavior of each method call of the simulation object, adding the behavior, deleting the behavior and modifying the return value of the behavior.
The embodiment of the present application provides an electronic device, where the electronic device includes a memory, a processor, and a computer program stored in the memory and capable of running on the processor, and when the processor executes the computer program, the unit testing method provided by one or more of the foregoing technical solutions is implemented.
The embodiment of the application provides a computer storage medium, wherein a computer program is stored in the computer storage medium; the computer program can implement the unit testing method provided by one or more of the above technical solutions after being executed.
Based on the unit testing method provided by the application, a plurality of statement sets of the ith population are obtained, wherein each statement set comprises at least one statement; statements are used to define the behavior of a simulated object. And carrying out mutation operation on each statement according to the mutation operator corresponding to each statement to obtain a mutation operation result. Thus, a set of statements of virtual objects for a variety of different application scenarios may be generated. And performing cross interchange on the two statement sets in the mutation operation result to obtain the (i + 1) th population. Therefore, the sentences in the sentence sets can be recombined, and the diversity of the sentence sets is improved. And under the condition that the statement set conforming to the test assertion exists in the (i + 1) th population, outputting the statement set conforming to the test assertion, and performing unit test by adopting the statement set conforming to the test assertion, so that the applicability of the simulation object is improved during the unit test.
It is to be understood that both the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the application.
Drawings
Fig. 1 is an application scenario diagram of a unit testing method according to an embodiment of the present application;
FIG. 2 is a schematic flow chart illustrating a unit testing method according to an embodiment of the present disclosure;
fig. 3 is a schematic flowchart of determining a statement set of an ith population according to an embodiment of the present application;
fig. 4 is a schematic flowchart of a process for obtaining fitness of each statement set in a population according to an embodiment of the present application;
fig. 5 is a schematic flowchart of cross-interchanging two sentence sets according to an embodiment of the present application;
FIG. 6 is a schematic flow chart illustrating another unit testing method according to an embodiment of the present disclosure;
FIG. 7 is a schematic diagram of a unit testing apparatus according to an embodiment of the present disclosure;
fig. 8 is a schematic structural diagram of an electronic device according to an embodiment of the present application.
Detailed Description
The present application will be described in further detail below with reference to the accompanying drawings and examples. It should be understood that the examples provided herein are merely illustrative of the present application and are not intended to limit the present application. In addition, the following examples are provided as partial examples for implementing the present application, not all examples for implementing the present application, and the technical solutions described in the examples of the present application may be implemented in any combination without conflict.
The unit test is a software test method, comprises a test case and test logic, can check and verify the minimum testable unit in software, and checks whether each unit in a program code has a code risk, thereby helping developers verify whether new iteration influences the original business logic.
In the unit test, when a program module requiring external dependency is to be called for testing, the unit test cannot be performed because the external dependency is often difficult to construct or obtain, or the external dependency is unstable, so that the test failure rate is high and the troubleshooting cost is high. To this end, a Mock object (Mock object) may be created to test in place of the external dependency.
The unit testing method provided by the embodiment of the application can be applied to software testing and maintenance. Hereinafter, the unit testing method provided in the embodiments of the present application will be described in detail.
Fig. 1 shows an application scenario diagram of a unit testing method provided in an embodiment of the present application. Referring to fig. 1, the process of genetic variation of a population may include the following steps: step A101: initializing a population (Initial population); step A102: calculating the Fitness (Fitness evaluation) of each individual in the population; step A103: judging whether the test result passes, and executing the step A104 when the test result passes, otherwise executing the step A105; step A104: outputting individuals in the population, which accord with the test assertion; step A105: individual Selection (Selection) of individuals in the population; step A106: genetic variation is performed on individuals in the population.
It should be understood that the genetic algorithm is created by the professor hollander of michigan university, is derived from the biological evolution theory, simulates the evolution rules of "natural selection" and "preferential elimination" in the nature, is a global optimization search method, and has good global search capability in the iterative search process; the genetic algorithm is mainly a 'production-iteration' process, and the main links are population selection, chromosome crossing and chromosome variation.
In an example, when a test case depends on a Mock object, with respect to Mock object generation problem, a test case (T) using Mock object can be modeled as follows: referring to table 1, the test case is divided into a Mock object creation part (M), a Mock object setting part (S), a test code execution part (E), and a test assertion part (a).
In an example, referring to table 1, based on the quadruple in the model T ═ < M, S, E, a >, one test case can be divided into the following parts: a Mock object creation part (M) for creating a simulation object to be used in the test; a Mock object setting section (S) for defining a behavior of the simulation object; a test code execution part (E) for executing a test by calling a function of the object to be tested; and a test assertion section (A) for judging the correctness of the test operation result.
In an example, referring to table 1, in the Mock object creation section (M), the following simulated objects are created: var owner1, var owner2 and var Logger, wherein var owner1 and var owner2 belong to the Logger class.
In an example, referring to table 1, Mock object setup section (S), the behavior of the Mock object var owner1 includes any of: while (ower1Pipeline 1.load ()), the Return (10000), while (ower1Pipeline 1. inner ()), the Return (inner 1); the behavior of the simulated object var logger comprises the following steps: while (logger. isfinestenabled ()).
TABLE 1 test case composition
Figure BDA0003405265200000091
In an example, given the Mock object creation section (M), the test code execution section (E), and the test assertion section (a) as inputs, referring to table 2, the code of the Mock object setting section (S) can be generated using the following syntax rules so that the test case conforms to the test assertion in the test assertion section (a).
In an example, referring to table 2, in the statement set of Mock object, the non-terminal includes any of: s, Stmt, StubStmt, CallMatcher, ArgMatcher List, ArgMatcher, reactionList, Reaction, DefStmt, Expr, Literal, NewArray, CtorCall, MethodCall, StaticMethodCall, instanceMethodMethodCall, MockCreation, FieldRead, Static FieldRead, Instance FieldRead, ParaList.
It should be understood that during compilation, a non-terminal in a program statement can be understood as a splittable element, while a terminal is the smallest element that is not splittable.
TABLE 2 grammar rules for statements in a statement set
Figure BDA0003405265200000101
Figure BDA0003405265200000111
In an example, referring to Table 2, "stub" is used to represent temporary and simple program code that simulates the function of multiple compound codes.
In practical application, after generating the Mock object setting statement according to the grammar rule of the statements in the statement set, a genetic algorithm can be adopted to search the statement set conforming to the test assertion, and the statement set conforming to the test assertion is adopted as the Mock object setting statement.
Fig. 2 shows a schematic flowchart of a unit testing method provided in an embodiment of the present application. Referring to fig. 2, the unit testing method provided in the embodiment of the present application may include the following steps:
step A201: acquiring a plurality of statement sets of an ith population, wherein each statement set comprises at least one statement; statements are used to define the behavior of a simulated object.
Here, the function of the simulation object may include any one of: network requests, file systems, input output, databases.
In an example, an ith population is initialized, the ith population containing N sets of statements. The plurality of statement sets of the ith group may include S1、S2、S3、……、Si、……Sn. Wherein S isiIs the ith statement set.
In an example, the ith population may serve as an initial population, and the statement set in the initial population may be a statement set with parameters in the statement being empty, or a statement set generated according to a predefined statement template. Here, the sentences in the predefined sentence template conform to the grammar rules of the sentences in the sentence set.
It should be appreciated that based on creating the simulation object, it may be determined whether the test cell may return the expected result under normal logic, abnormal logic, or stress conditions based on interaction of the simulation object and the test cell during the cell test. Real objects in the program codes are not required to be relied on, so that the program modules and external dependence are isolated, and the software testing efficiency is improved.
In an example, referring to table 3, the statements in the set of statements may be used to define the behavior of a simulation object. Any of the following statement types may be included in the statement set: numerical type, character string font size, statements containing parameter lists, statements matching method calls, statements of behavior lists, and associated statements.
In an example, the association statement is in the form of a when (callmatch) ReactionList, which can be used to associate the behavior of Mock object with its method invocation.
For example, "while (loader. isfienstenabled (),. thenReturn (var 0)" is used to indicate that the behavior "Return (var 0)" of Mock object is associated with the method call "loader. isfienstenabled ()" of Mock object.
TABLE 3 statements in the statement set
Figure BDA0003405265200000131
Step A202: performing mutation operation on each statement according to the mutation operator corresponding to each statement to obtain a mutation operation result; and performing cross interchange on the two statement sets in the mutation operation result to obtain the (i + 1) th population.
In an example, the mutation operator may be a program script used in performing mutation operations on statements in the set of statements. Each statement type may correspond to a type of variant operator. Referring to table 4, for different types of statements, a mutation operator corresponding to the statement type is used to perform a mutation operation on the statement.
For example, for a numerical type statement, the statement is in the form of Var ═ Expr, and the expression portion Expr that affects the variable value Var is modified, so that a mutation operation can be performed on the numerical type statement.
TABLE 4 statement type and variant operator corresponding to statement type
Figure BDA0003405265200000132
Figure BDA0003405265200000141
In an example, two sentence sets are randomly selected from the group as two parents, and sentences of the same sentence type in the two parents are subjected to cross interchange by adopting a cross interchange algorithm to generate offspring of the two parents. And adding at least one of the two parents and at least one of the two descendants into the new generation population to obtain the (i + 1) th population.
Step A203: if the statement set which accords with the test assertion of the test case exists in the (i + 1) th population, adopting the statement set which accords with the test assertion to define a simulation object; and performing unit test on the test case based on the simulation object.
In an example, at the time of unit test, a unit may be a function in C language program code, a class in Java program code, and a window or a menu in graphical software.
In an example, when a certain statement set exists in the (i + 1) th population so that the test assertion passes through, genetic mutation on the statement set in the (i + 1) th population is stopped, and the statement set conforming to the test assertion is output.
In an example, an assertion statement is added in a test unit for unit test, and if the execution result of the test unit does not meet the expectation of the assertion statement, an exception prompt or an alarm prompt is thrown.
In the example, a set of statements S is employediThe Mock object in (1) is set to indicate that the test case conforms to the test assertion in the test assertion section (a) when the test case can pass the unit test.
In an example, a unit test is performed with jmoclit based on a set of statements conforming to a test predicate. Here, jmoclit is a Java class/interface/object simulation tool, and is widely used in unit testing of Java applications.
It should be understood that jmoclit can simulate the peripheral interface called by a method or module, and realize complete decoupling with the peripheral system in the unit test process. The test case can be used for detecting defects in the program code during unit test, paying attention to whether the test case can realize normal interface calling and returning a preset result of the test case.
Based on the unit testing method provided by the application, a plurality of statement sets of the ith population are obtained, wherein each statement set comprises at least one statement; statements are used to define the behavior of a simulated object. And carrying out mutation operation on each statement according to the mutation operator corresponding to each statement to obtain a mutation operation result. Thus, a set of statements of virtual objects for a variety of different application scenarios may be generated. And performing cross interchange on the two statement sets in the mutation operation result to obtain the (i + 1) th population. Therefore, the sentences in the sentence sets can be recombined, and the diversity of the sentence sets is improved. And under the condition that the statement set conforming to the test assertion exists in the (i + 1) th population, outputting the statement set conforming to the test assertion, and performing unit test by adopting the statement set conforming to the test assertion, so that the applicability of the simulation object is improved during the unit test.
In practical applications, the steps a201 to a203 may be implemented by a Processor, and the Processor may be at least one of an Application Specific Integrated Circuit (ASIC), a Digital Signal Processor (DSP), a Digital Signal Processing Device (DSPD), a Programmable Logic Device (PLD), a Field Programmable Gate Array (FPGA), a Central Processing Unit (CPU), a controller, a microcontroller, and a microprocessor.
In one implementation, in the unit testing method, referring to fig. 3, the following steps may be further included:
step A301: under the condition that i is an integer larger than 1, if it is determined that no statement set which meets the test assertion of the test case exists in the (i-1) th population, the fitness of each statement set in the (i-1) th population is obtained.
In an example, the plurality of statement sets of the (i-1) th population may include S1、S2、S3、……、Si、……Sn. Wherein S isiIs the ith statement set.
It should be appreciated that in the plurality of statement sets of the (i-1) th population, each statement set is not compliant with the test predicate, and it is determined that there is no statement set in the (i-1) th population that is compliant with the test predicate.
In an example, before calculating the fitness of the statement set, a test case may be run based on the statement set, and run data of the test case may be collected when the test case is run, where the run data may include any one of the following:
sentence set SiThe number of the generated setting statements and the number of the successfully executed setting statements in the test case testing process, whether the test case can be executed before the test assertion after the virtual object is defined by the statement set, the set of all method calls in the simulation object and the set of the method calls of which the behavior is specified in the simulation object, the number of the statements of the assertion statements which are satisfied in the test assertion and the number of the statements of all the assertion statements in the test assertion, the number of the statements of the assertion statements in the test assertion, and the editing distance of the expected value and the actual value of each assertion statement.
TABLE 5 identification of data and definition of data
Figure BDA0003405265200000161
In the example, the operation data of the test case is obtained when the statement sets are adopted for unit testing, and the fitness of each statement set in the (i-1) th population is obtained according to the operation data of the test case and the fitness function.
For example, according to the sentence set SiNumber of sentences S generated incAnd the number S of successfully executed statements in the test case test process0Using fitness function
Figure BDA0003405265200000162
Computing a set of statements SiThe fitness of (2).
Step A302: and determining the sentence sets of the ith population from the plurality of sentence sets of the ith-1 th population according to the fitness of each sentence set.
In the example, the sentence sets S are in the i-1 th population according to the fitness of each sentence setiIs greater than a preset threshold, the statement set S is collectediAs a set of statements for the ith population. That is, the fitness in the i-1 th population is larger than that in the i-1 th populationAnd (4) setting a statement set of a preset threshold value as a statement set of the ith population.
In the example, the set S of statements in the i-1 th population is usediDetermining the statement set S in the i-1 th populationiProbability of being drawn. And sampling a plurality of statement sets of the i-1 th group according to the probability of each statement set being sampled to obtain the statement set of the i-th group.
It should be understood that the probability of each statement set being drawn is the result of the computation of its fitness function. Statement set S in i-1 th populationiThe fitness of the (i-1) th group is determined, and a statement set S in the (i-1) th group is determinediProbability of being drawn. Sentence set SiThe larger the fitness of (A), the sentence set SiThe greater the probability of being drawn.
Therefore, the average value of the fitness of the statement set in the ith population is larger than the average value of the fitness of the statement set in the ith-1 population.
In an example, a statement set for an ith population is determined among a plurality of statement sets for an ith-1 population. In the random sampling process, the input is the (i-1) th population and the probability of being sampled in each statement set in the (i-1) th population, and the output is the statement set obtained by sampling.
In the example, a sampling strategy of putting back samples is adopted, and one statement set is obtained in each sampling in a plurality of statement sets of the (i-1) th group according to the probability that each statement set is sampled. Repeating the sampling process for N times, outputting N statement sets, and taking the N statement sets obtained by sampling as statement sets of the ith population.
It should be appreciated that with the sampling strategy of put-back sampling, there may be two identical samples among the multiple samples obtained from the sampling. Here, one sample may be one sentence set of the ith population.
Based on the sampling process, the statement set S in the i-1 th population is collectediAnd screening to obtain a statement set of the ith population. Because the average value of the fitness of the statement sets in the ith population is larger than the average value of the fitness of the statement sets in the (i-1) th population, compared with the method of directly adopting the statement sets in the (i-1) th population to carry out genetic variation, partial adaptation can be reducedThe impact of less highly clustered sentences on the process of genetic variation.
In an example, when a certain statement set does not exist in the (i-1) th population can enable the test assertion to pass, the statement set of the (i) th population can be determined in a plurality of statement sets of the (i-1) th population, and genetic mutation on the statement set in the (i) th population is continued until the statement set conforming to the test assertion is output.
In one implementation, the (i-1) th population includes M statement sets; the fitness of each statement set in the i-1 th population is obtained, and referring to fig. 4, the method may include the following steps:
step A401: determining at least one fitness function and a weight coefficient of each fitness function; and acquiring a calculation result of the jth statement set in the ith-1 population under each fitness function.
In an example, a fitness function is used to guide the search process of the genetic algorithm, the fitness function including any one of: setup initiation (SE), initiation (EE), Mock Method software factor (MS), initiation Distances (AD), and initiation code (AC).
In an example, the number S of sentences generated from a set of sentencescAnd the number of successfully executed statements S0A fitness function is created, the fitness function SE being as follows:
Figure BDA0003405265200000181
in an example, the state that a test case can execute to a test assertion is denoted as E1The state that the test case fails to execute to the test assertion is denoted as E0And creating a fitness function according to whether the test case can be executed before the test assertion. The fitness function EE is shown below:
Figure BDA0003405265200000182
in an example, MSFor the party with the specified behavior in the Mock objectSet of method calls, MiIs the set of all method calls in the Mock object. The fitness function MS is as follows:
Figure BDA0003405265200000183
in an example, AE is the set of predicate statements of all test predicates, d (a.expected, a.actual) is the edit distance of the expected value a.expected and the actual value a.actual of predicate statement a, a ∈ AE. MAEThe number of statements that are predicate statements in the set AE. The fitness function AD is as follows:
Figure BDA0003405265200000184
where d0 is Max (la.expected, la.actual), and la.expected and la.actual are the string length of a.expected and the string length of a.actual, respectively, then d is0Is the maximum value in la.expected, la.actual.
In an example, the number of statements M according to the predicate statements that have been satisfied in the test assertion0And the number M of statements testing all assertion statements in the assertionAEAnd creating a fitness function. The fitness function AC is as follows:
Figure BDA0003405265200000191
in the example, the definition of the edit distance function d (x, y) is as follows, and in the edit distance function d (x, y), Lev (x, y) is an edit distance function of a character string.
Figure BDA0003405265200000192
Step A402: and acquiring the fitness of the jth statement set according to the calculation result of the jth statement set under each fitness function and the weight coefficient of each fitness function.
In an example, the calculation results of the statement set under each fitness function are respectively: SE, EE, MS, AD, AC. Accordingly, the weight coefficient of each fitness function is: gamma raySE、γEE、γMS、γAD、γAC
In an example, γSE=1、γEE=2、γMS=4、γAD=16、γACAnd (16) acquiring the fitness of the jth statement set according to the calculation result of the jth statement set under each fitness function and the weight coefficient of each fitness function, as follows:
Fitness=SE×1+EE×2+MS×4+AD×16+AC×16 (7)
step A403: and under the condition that j sequentially takes 1 to M, acquiring the fitness of each statement set in the i-1 th population.
In one implementation, the ith population includes N statement sets; performing cross-exchange on two statement sets in the mutation operation result to obtain an i +1 th population, referring to fig. 5, which may include the following steps:
step A501: determining two statement sets in the N statement sets, and performing cross interchange on statements with the same statement types in the two statement sets to generate two filial generations of the two statement sets.
In an example, there are N sets of statements in the population, where N is an integer multiple of 4, and N/4 sets of parents can be randomly selected in the population, see table 6, where there are two sets of statements to be cross-interchanged in each set of parents.
TABLE 6 two sets of statements to be cross-interchanged
Figure BDA0003405265200000201
In an example, referring to Table 7, for each set of parents, statements of the same statement type in both parents are cross-swapped, resulting in children of both parents. And adding the two parents and the two offspring into the new generation population to obtain the (i + 1) th population.
TABLE 7 progeny of two sets of statements after cross-exchange
Figure BDA0003405265200000202
Figure BDA0003405265200000211
In the related art, the cross interchange algorithm adopts single-point cross interchange in the genetic algorithm, the generated Mock object setting statement, and the incorrect combination of the parameter generation statement for calling the application program interface and the application program interface calling statement will cause the test unit to throw an exception in the execution process.
For example, with respect to application program interface calls, when an incoming parameter is incorrect, the application program interface may report an error at the time of parameter checking.
In an example, two statement sets in the N statement sets are determined, abnormal statements in the two statement sets are identified, the abnormal statements in the two statement sets are used as cut points of cross interchange, and the statements which are in the two statement sets and have the same statement type before the cut points are cross interchanged.
It should be understood that when performing cross-over, ensuring that the tangent point is before throwing the abnormal statement can cut off the incorrect combination formed after the cross-over of the two statement sets, so that the two statement sets have more chances to get the correct combination after the cross-over.
Step A502: and adding at least one of the two statement sets and at least one of the two descendants into the (i + 1) th population to obtain N statement sets in the (i + 1) th population.
In an example, where there are N sets of statements in the population, where N is an integer multiple of 4, N/4 sets of parents can be randomly selected in the population, and for each set of parents, the statements of the same statement type in the two parents are cross-interchanged, resulting in children of the two parents. And adding the two parents and the two offspring into the new generation population to obtain the (i + 1) th population.
In one implementation, in the unit testing method, determining at least one fitness function may include: according to a sentence set SiAnd (4) creating a fitness function according to the number of the sentences generated in the step (1) and the number of the sentences successfully executed.
In an example, according to statement set SiNumber of sentences S generated incAnd the number of successfully executed statements S0Creating a fitness function
Figure BDA0003405265200000221
In one implementation, in the unit testing method, determining at least one fitness function may include: and creating a fitness function according to the state distribution of whether the test case can be executed before the test assertion.
In an example, the state that a test case can execute to a test assertion is denoted as E1The state that the test case fails to execute to the test assertion is denoted as E0. According to the state distribution of whether the test case can be executed before the test assertion, a fitness function is established
Figure BDA0003405265200000222
In one implementation, in the unit testing method, determining at least one fitness function may include: and creating a fitness function according to the statement number of the assertion statements in the test statement and the editing distance of the expected value and the actual value of each assertion statement.
In an example, the number of statements M according to the predicate statements in the test assertionAEAnd an edit distance d (a.expected, a.actual) of the expected value a.expected and the actual value a.actual of each assertion statement, creating a fitness function
Figure BDA0003405265200000223
It should be understood that the edit distance is an algorithm for calculating the similarity of the text, if the character string a.actual can be transformed into the character string a.expected by adding, deleting, modifying, etc., and if the number of related operations is smaller, it indicates that the character string a.actual and the character string a.expected are more similar.
In one implementation, creating a fitness function according to the number of predicates in the test predicate and the edit distance between the expected value and the actual value of each predicated predicate may include the following steps:
determining at least one assertion statement that tests an assertion; acquiring an expected value and an editing distance of an actual value of each assertion statement; acquiring a normalization result of the editing distance of each assertion statement according to the editing distance of the expected value and the actual value of each assertion statement; and calculating the weighted average of the normalization result of the editing distance of each assertion statement according to the statement number of the assertion statement in the test statement, and creating a fitness function according to the weighted average.
In an example, at least one assertion statement for testing assertion is determined, and a set corresponding to the at least one assertion statement is defined as AE, wherein a belongs to any assertion statement in the set AE.
In the example, the edit distance d (a.expected, a.actual) of the expected value a.expected and the actual value a.actual of the predicate statement a is obtained, and the edit distance d (a.expected, a.actual) of the predicate statement a is normalized to obtain the normalization result 1.0-d (a.expected, a.actual)/d of the edit distance of the predicate statement ao
In an example, the number of statements M according to the predicate statements in the test assertionAECalculating a weighted average of the normalized results of the edit distance of each assertion statement
Figure BDA0003405265200000231
Creating a fitness function from a weighted average of the normalized results
Figure BDA0003405265200000232
It is understood that 1.0-d (a.expected/a.actual)/d0The larger the editing distance d (a.actual) is, the smaller the fitness function is, and accordingly
Figure BDA0003405265200000233
The greater the corresponding fitness.
In one implementation, in the unit testing method, determining at least one fitness function may include: and creating a fitness function according to a method called by the test case execution on all the Mock objects and a method of specified behaviors on the Mock objects.
In an example, a fitness function is created according to a method of test case execution to assert all Mock objects called and a method of specified behavior on Mock objects
Figure BDA0003405265200000234
In one implementation, in the unit testing method, determining at least one fitness function may include: and creating a fitness function according to the number of the satisfied assertion statements in the test assertion and the number of all assertion statements in the test assertion.
In an example, the number of statements M according to the predicate statements that have been satisfied in the test assertionoAnd the number M of statements testing all assertion statements in the assertionAECreating a fitness function
Figure BDA0003405265200000241
Hereinafter, the variant operators defined in the embodiments of the present application will be described in detail with respect to different types of expressions.
In one implementation, performing a mutation operation on each statement according to a mutation operator corresponding to each statement includes:
when the statement type is a numerical type, a variant operator is adopted to perform any one of the following operations on the statement: increase a random number value, decrease a random number value.
In the example, for a statement of a numeric type, the statement is in the form of Var ═ Expr, and for the literal quantity of the numeric type, a random number may be added/subtracted to the expression portion Expr of Var.
In one implementation, performing a mutation operation on each statement according to a mutation operator corresponding to each statement includes:
when the statement type is a character string type, a variant operator is adopted to execute any one of the following operations on the statement: insert characters, delete characters, modify characters.
In the example, for the statement of the character String type, the statement is in the form of Primitive String Null Class, and for the word size of the character String, random characters can be inserted/deleted/modified in the character String, and the character sequence in the character String is turned.
In one implementation, performing a mutation operation on each statement according to a mutation operator corresponding to each statement includes:
when the statement type is a statement containing a parameter list, adopting a variant operator to execute the following operations on the statement: one of the parameters selected at random is replaced with a new value.
In the example, for a statement containing a parameter list, the statement is in the form of ParaList Var e, and for a statement containing a parameter list, a parameter can be randomly selected from the parameter list, and the randomly selected parameter is replaced with a new value.
In one implementation, performing a mutation operation on each statement according to a mutation operator corresponding to each statement includes:
when the statement type is the statement called by the matching method, a variant operator is adopted to execute the following operations on the statement: and modifying the matching parameters in the matching parameter list.
In an example, for statements matching a method call, the statements are in the form of while (CallMatcher) ReactionList, and for the CallMatcher portion, the ArgMatcher List in CallMatcher may be modified.
For example, a single parameter, argmather, in the matching parameter list, argmatherrst, is randomly selected, modified with equal probability as follows: the single parameter argmather of the matching parameter list is switched between any () and eq (var).
For example, in the case where the single parameter argmather in the matching parameter list argmatherlist is eq (Var), Var is replaced with a new variable.
In one implementation, performing a mutation operation on each statement according to a mutation operator corresponding to each statement includes:
when the statement type is a statement of a behavior list, adopting a variant operator to execute any one of the following operations on the statement: and exchanging the sequence of each behavior of each method call of the simulation object, adding the behavior, deleting the behavior and modifying the return value of the behavior.
In an example, for a behavior association statement, the statement form is when (callmatch) ReactionList, and for the ReactionList portion, the following modifications can be made at equal probability:
exchanging the order of the respective Reaction in the Reaction List, randomly adding/deleting one Reaction, replacing the return value or abnormality of the randomly selected Reaction with a new variable, and randomly generating the return value of the Reaction.
In an example, for the Mock object creation part, a return value/exception is randomly specified for the Mock object method call.
It should be understood that in practical applications, variant operators used by a set of sentences may be added or deleted; the fitness computation function used by the statement set is added or deleted.
Based on the same technical concept as the foregoing embodiment, referring to fig. 6, the unit testing method provided in the embodiment of the present application may include:
step A601: initializing an initial population, wherein the initial population comprises N statement sets.
Step A602: and determining that no statement set conforming to the test assertion exists in the population, and acquiring the fitness of each statement set in the population.
In an example, fitness is calculated using a fitness function to calculate each set of statements in the population.
Step A603: and determining the sentence sets of the ith population from the plurality of sentence sets of the ith-1 th population according to the fitness of each sentence set.
In an example, when i is an integer greater than 1, and i ═ 2, the i-1 th population is the 1 st population, and the 1 st population may be the initial population.
Step A604: and carrying out mutation operation on each statement according to the mutation operator corresponding to each statement to obtain a mutation operation result.
In an example, for a statement in each statement set in the ith population, performing mutation operation on each statement according to a mutation operator corresponding to each statement to obtain a mutation operation result.
Step A605: and performing cross interchange on the two statement sets in the mutation operation result to generate offspring of the two statement sets.
In the example, two sets of sentences are selected as parents in the mutation operation result, and the sentences with the same sentence type in the two sets of sentences are subjected to cross interchange to generate the offspring of each set of sentences.
In an example, at least one statement set in two parents and at least one statement set in two children are added into the (i + 1) th population, so as to obtain a statement set of the (i + 1) th population.
Step A606: it is determined whether a set of statements exist in the (i + 1) th population that meet the test predicate.
In an example, where there is a set of statements in the (i + 1) th population that meet the test predicate, the following step a607 is performed. In case there is no set of statements in the (i + 1) th population that fits the test predicate, the following step a609 is performed.
Step A607: and outputting a statement set which accords with the test assertion in the (i + 1) th population.
Step A608: report generation statement set success.
In an example, in case the report generation statement set is successful, the following step a611 is performed.
Step A609: and determining whether the iteration times of the population are smaller than a preset value.
In an example, the number of iterations of the population is recorded, and the step a602 is performed when the number of iterations from the initial population to the i +1 th population is less than a preset value. When the number of times of the initial population to the i +1 th population is greater than or equal to the preset value, the following step a611 is performed.
Step A610: report generation statement set failure.
Step A610: and (6) ending.
In the related art, the Mock object construction technology is integrated into the EvoSuite, and the Mock object is generated when a unit test is generated, so as to improve the code coverage rate of the test. Starting from the test case, recording the behavior of the test dependent object when the test case is operated in a Capture-And-Replay mode, And applying the recorded behavior to the Mock object.
Based on the same technical concept of the foregoing embodiments, referring to fig. 7, the unit testing apparatus provided in the embodiments of the present application may include:
an obtaining module 701, configured to obtain multiple statement sets of an ith population, where each statement set includes at least one statement; the statement is used for defining the behavior of a simulation object;
a processing module 702, configured to perform a mutation operation on each statement according to a mutation operator corresponding to each statement, so as to obtain a mutation operation result; performing cross interchange on the two statement sets in the variation operation result to obtain an i +1 th population;
a testing module 703, configured to define the simulation object by using a statement set conforming to a test assertion if it is determined that the statement set conforming to the test assertion exists in the (i + 1) th population; and performing unit test on the test case based on the simulation object.
In an implementation manner, the obtaining module 701 is further configured to:
under the condition that i is an integer larger than 1, if it is determined that no statement set which meets the test assertion of the test case exists in the (i-1) th population, acquiring the fitness of each statement set in the (i-1) th population;
according to the fitness of each statement set, taking the statement set with the fitness larger than a preset threshold value in the ith-1 population as the statement set of the ith population;
or, determining the probability of drawing each statement set in the i-1 th population according to the fitness of each statement set; and sampling a plurality of statement sets of the i-1 th population according to the probability of each statement set being sampled to obtain the statement set of the i-th population.
In one implementation, the (i-1) th population includes M statement sets; the obtaining module 701 is configured to obtain a fitness of each statement set in the i-1 th population, and includes:
determining at least one fitness function and a weight coefficient for each fitness function; obtaining a calculation result of the j statement set in the i-1 th population under each fitness function;
acquiring the fitness of the jth statement set according to the calculation result of the jth statement set under each fitness function and the weight coefficient of each fitness function;
and under the condition that j sequentially takes 1 to M, acquiring the fitness of each statement set in the (i-1) th population.
In one implementation, the ith population includes N statement sets; the processing module 702 is configured to perform cross-exchange on the two statement sets in the mutation operation result to obtain an i +1 th population, where the processing module includes:
determining two statement sets in the N statement sets, and performing cross interchange on statements with the same statement types in the two statement sets to generate two filial generations of the two statement sets;
and adding at least one of the two statement sets and at least one of the two descendants into the (i + 1) th population to obtain N statement sets in the (i + 1) th population.
In one implementation, the obtaining module 701 is configured to determine at least one fitness function, where the at least one fitness function includes any one of:
creating a fitness function according to the number of the sentences generated in the sentence set and the number of the sentences successfully executed;
creating a fitness function according to whether the test case can be executed before the test assertion;
creating a fitness function according to the statement number of the assertion statements in the test statement and the editing distance of the expected value and the actual value of each assertion statement;
creating a fitness function according to a set of all method calls in the simulation object and a set of method calls for which a behavior has been specified in the simulation object;
and creating a fitness function according to the number of the satisfied assertion statements in the test assertion and the number of all assertion statements in the test assertion.
In one implementation manner, the obtaining module 701 is configured to create a fitness function according to the number of the assertion statements in the test assertion and the edit distance between the expected value and the actual value of each assertion statement, where the creating module includes:
determining at least one predicate statement of the test predicate; acquiring an expected value and an editing distance of an actual value of each assertion statement;
acquiring a normalization result of the editing distance of each assertion statement according to the editing distance of the expected value and the actual value of each assertion statement;
and calculating the weighted average value of the normalization result of the editing distance of each assertion statement according to the statement number of the assertion statement in the test statement, and creating a fitness function according to the weighted average value.
In an implementation manner, the processing module 702 is configured to perform a mutation operation on each statement according to a mutation operator corresponding to each statement, where the mutation operation includes any one of:
when the statement type is a numerical type, adopting a variant operator to perform any one of the following operations on the statement: increasing a random number value and decreasing a random number value;
when the statement type is a character string type, adopting a variant operator to perform any one of the following operations on the statement: inserting characters, deleting characters and modifying characters;
when the statement type is a statement containing a parameter list, adopting a variant operator to perform the following operations on the statement: replacing a randomly selected one of the parameters with a new value;
when the statement type is the statement called by the matching method, adopting a variant operator to execute the following operations on the statement: modifying the matching parameters in the matching parameter list;
when the statement type is a statement of a behavior list, adopting a variant operator to perform any one of the following operations on the statement: and exchanging the sequence of each behavior of each method call of the simulation object, adding the behavior, deleting the behavior and modifying the return value of the behavior.
In practical applications, the obtaining module 701, the processing module 702, and the testing module 703 may all be implemented by a processor of an electronic device, where the processor may be at least one of an ASIC, a DSP, a DSPD, a PLD, an FPGA, a CPU, a controller, a microcontroller, and a microprocessor, and the embodiment of the present application is not limited thereto.
In some embodiments, functions of or modules included in the apparatus provided in the embodiments of the present application may be used to execute the method described in the above method embodiments, and specific implementation thereof may refer to the description of the above method embodiments, and for brevity, will not be described again here.
Based on the same technical concept as the foregoing embodiment, referring to fig. 8, an electronic device 800 provided in an embodiment of the present application may include: a memory 810 and a processor 820; wherein the content of the first and second substances,
a memory 810 for storing computer programs and data;
a processor 820 for executing the computer program stored in the memory to implement any one of the unit testing methods in the foregoing embodiments.
In practical applications, the memory 810 may be a volatile memory (volatile memory), such as RAM; or a non-volatile memory (non-volatile memory), illustratively a ROM, a flash memory, a Hard Disk Drive (HDD) or a Solid-State Drive (SSD); or a combination of the above types of memories. The memory 810 may provide instructions and data to the processor 820.
The foregoing descriptions of the various embodiments are intended to highlight different aspects of the various embodiments, which have the same or similar aspects, and thus, for brevity, detailed descriptions thereof are omitted
The methods disclosed in the method embodiments provided by the present application can be combined arbitrarily without conflict to obtain new method embodiments.
Features disclosed in various product embodiments provided by the application can be combined arbitrarily to obtain new product embodiments without conflict.
The features disclosed in the various method or apparatus embodiments provided herein may be combined in any combination to arrive at new method or apparatus embodiments without conflict.
In the several embodiments provided in the present application, it should be understood that the disclosed apparatus and method may be implemented in other ways. The above-described device embodiments are merely illustrative, and for example, the division of the unit is only one logical function division, and there may be other division ways in actual implementation, such as: multiple units or components may be combined, or may be integrated into another system, or some features may be omitted, or not implemented. In addition, the coupling, direct coupling or communication between the components shown or discussed may be through some interfaces, and the indirect coupling or communication between the devices or units may be electrical, mechanical or other forms.
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 grid units; some or all of the units can be selected according to actual conditions to achieve the purpose of the scheme of the embodiment.
In addition, all functional units in the embodiments of the present application may be integrated into one processing module, or each unit may be separately regarded as one unit, or two or more units may be integrated into one unit; the integrated unit can be realized in a form of hardware, or in a form of hardware plus a software functional unit.
Those of ordinary skill in the art will understand that: all or part of the steps for implementing the method embodiments may be implemented by hardware related to program instructions, and the program may be stored in a computer readable storage medium, and when executed, the program performs the steps including the method embodiments.
The above description is only for the specific embodiments of the present application, but the scope of the present application is not limited thereto, and any person skilled in the art can easily conceive of the changes or substitutions within the technical scope of the present application, and shall be covered by the scope of the present application. Therefore, the protection scope of the present application shall be subject to the protection scope of the claims.

Claims (11)

1. A method of unit testing, comprising:
acquiring a plurality of statement sets of an ith population, wherein each statement set comprises at least one statement; the statement is used for defining the behavior of a simulation object;
performing mutation operation on each statement according to the mutation operator corresponding to each statement to obtain a mutation operation result; performing cross interchange on the two statement sets in the variation operation result to obtain an i +1 th population;
if the statement set which conforms to the test assertion of the test case exists in the (i + 1) th population, adopting the statement set which conforms to the test assertion to define the simulation object; and performing unit test on the test case based on the simulation object.
2. The method of claim 1, further comprising:
under the condition that i is an integer larger than 1, if the statement set which meets the test assertion of the test case does not exist in the (i-1) th population, acquiring the fitness of each statement set in the (i-1) th population,
according to the fitness of each statement set, taking the statement set with the fitness larger than a preset threshold value in the ith-1 population as the statement set of the ith population;
or, determining the probability of drawing each statement set in the i-1 th population according to the fitness of each statement set; and sampling a plurality of statement sets of the i-1 th population according to the probability of each statement set being sampled to obtain the statement set of the i-th population.
3. The method of claim 2, wherein M sets of statements are included in the i-1 th population; the obtaining the fitness of each statement set in the i-1 th population comprises:
determining at least one fitness function and a weight coefficient for each fitness function; obtaining a calculation result of the j statement set in the i-1 th population under each fitness function;
acquiring the fitness of the jth statement set according to the calculation result of the jth statement set under each fitness function and the weight coefficient of each fitness function;
and under the condition that j sequentially takes 1 to M, acquiring the fitness of each statement set in the (i-1) th population.
4. The method of claim 1, wherein the ith population includes N sets of statements; performing cross interchange on the two statement sets in the variation operation result to obtain an i +1 th population, including:
determining two statement sets in the N statement sets, and performing cross interchange on statements with the same statement types in the two statement sets to generate two filial generations of the two statement sets;
and adding at least one of the two statement sets and at least one of the two descendants into the (i + 1) th population to obtain N statement sets in the (i + 1) th population.
5. The method of claim 3, wherein determining at least one fitness function comprises any one of:
creating a fitness function according to the number of the sentences generated in the sentence set and the number of the sentences successfully executed;
establishing a fitness function according to the state distribution of whether the test case can be executed before test assertion;
creating a fitness function according to the statement number of the assertion statements in the test statement and the editing distance of the expected value and the actual value of each assertion statement;
creating a fitness function according to a set of all method calls in the simulation object and a set of method calls for which a behavior has been specified in the simulation object;
and creating a fitness function according to the number of the satisfied assertion statements in the test assertion and the number of all assertion statements in the test assertion.
6. The method of claim 5, wherein creating a fitness function based on the number of predicates in the test predicate and the edit distance of the expected value and the actual value of each predicated statement comprises:
determining at least one predicate statement of the test predicate; acquiring an expected value and an editing distance of an actual value of each assertion statement;
acquiring a normalization result of the editing distance of each assertion statement according to the editing distance of the expected value and the actual value of each assertion statement;
and calculating the weighted average value of the normalization result of the editing distance of each assertion statement according to the statement number of the assertion statement in the test statement, and creating a fitness function according to the weighted average value.
7. The method according to claim 1, wherein the mutating each statement according to its corresponding mutation operator comprises any one of:
when the statement type is a numerical type, adopting a variant operator to perform any one of the following operations on the statement: increasing a random number value and decreasing a random number value;
when the statement type is a character string type, adopting a variant operator to perform any one of the following operations on the statement: inserting characters, deleting characters and modifying characters;
when the statement type is a statement containing a parameter list, adopting a variant operator to perform the following operations on the statement: replacing a randomly selected one of the parameters with a new value;
when the statement type is the statement called by the matching method, adopting a variant operator to execute the following operations on the statement: modifying the matching parameters in the matching parameter list;
when the statement type is a statement of a behavior list, adopting a variant operator to perform any one of the following operations on the statement: and exchanging the sequence of each behavior of each method call of the simulation object, adding the behavior, deleting the behavior and modifying the return value of the behavior.
8. A unit testing apparatus, comprising:
the acquisition module is used for acquiring a plurality of statement sets of the ith population, wherein each statement set comprises at least one statement; the statement is used for defining the behavior of a simulation object;
the processing module is used for carrying out mutation operation on each statement according to the mutation operator corresponding to each statement to obtain a mutation operation result; performing cross interchange on the two statement sets in the variation operation result to obtain an i +1 th population;
the test module is used for defining the simulation object by adopting the statement set conforming to the test assertion if the statement set conforming to the test assertion of the test case exists in the (i + 1) th population; and performing unit test on the test case based on the simulation object.
9. The apparatus of claim 1, wherein the obtaining module is further configured to:
if it is determined that no statement set conforming to the test assertion exists in the (i-1) th population, acquiring the fitness of each statement set in the (i-1) th population;
according to the fitness of each statement set, taking the statement set with the fitness larger than a preset threshold value in the ith-1 population as the statement set of the ith population;
or, determining the probability of drawing each statement set in the i-1 th population according to the fitness of each statement set; and sampling a plurality of statement sets of the i-1 th population according to the probability of each statement set being sampled to obtain the statement set of the i-th population.
10. An electronic device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, the processor implementing the unit testing method of any one of claims 1 to 7 when executing the program.
11. A computer storage medium storing a computer program; characterized in that the computer program is capable of implementing the unit testing method of any one of claims 1 to 7 when executed.
CN202111509601.1A 2021-12-10 2021-12-10 Unit testing method, unit testing device, electronic equipment and storage medium Pending CN114153742A (en)

Priority Applications (2)

Application Number Priority Date Filing Date Title
CN202111509601.1A CN114153742A (en) 2021-12-10 2021-12-10 Unit testing method, unit testing device, electronic equipment and storage medium
PCT/CN2022/124385 WO2023103574A1 (en) 2021-12-10 2022-10-10 Unit test method and apparatus, electronic device, storage medium, and program

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202111509601.1A CN114153742A (en) 2021-12-10 2021-12-10 Unit testing method, unit testing device, electronic equipment and storage medium

Publications (1)

Publication Number Publication Date
CN114153742A true CN114153742A (en) 2022-03-08

Family

ID=80450597

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202111509601.1A Pending CN114153742A (en) 2021-12-10 2021-12-10 Unit testing method, unit testing device, electronic equipment and storage medium

Country Status (2)

Country Link
CN (1) CN114153742A (en)
WO (1) WO2023103574A1 (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2023103574A1 (en) * 2021-12-10 2023-06-15 深圳前海微众银行股份有限公司 Unit test method and apparatus, electronic device, storage medium, and program

Family Cites Families (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103294595B (en) * 2013-05-23 2016-06-22 西安电子科技大学 A kind of software repair based on genetic algorithm
CN103559129B (en) * 2013-10-31 2016-08-17 中国矿业大学 Statistical regression test data generating method based on genetic algorithm
CN106528433B (en) * 2016-12-12 2018-10-02 西安邮电大学 A kind of priorities of test cases sort method for white-box testing
JP7331681B2 (en) * 2019-12-18 2023-08-23 富士通株式会社 Test execution program, test execution method, and test execution device
CN112346997B (en) * 2020-12-04 2022-05-31 福州理工学院 Automatic test case generation method and terminal
CN112559344A (en) * 2020-12-10 2021-03-26 平安养老保险股份有限公司 Remote mock testing method and system
CN114153742A (en) * 2021-12-10 2022-03-08 深圳前海微众银行股份有限公司 Unit testing method, unit testing device, electronic equipment and storage medium

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2023103574A1 (en) * 2021-12-10 2023-06-15 深圳前海微众银行股份有限公司 Unit test method and apparatus, electronic device, storage medium, and program

Also Published As

Publication number Publication date
WO2023103574A1 (en) 2023-06-15

Similar Documents

Publication Publication Date Title
CN110908640B (en) Method for realizing service function and script engine
JP2012059026A (en) Source code conversion method and source code conversion program
US11269760B2 (en) Systems and methods for automated testing using artificial intelligence techniques
CN109857641A (en) The method and device of defects detection is carried out to program source file
Oliveira et al. Advanced stochastic petri net modeling with the mercury scripting language
Lu et al. Nonconformity resolving recommendations for product line configuration
Lee et al. Exhaustive testing of safety-critical software for reactor protection system
KR102546424B1 (en) Machine learning data generating apparatus, apparatus and method for analyzing errors in source code
CN114153742A (en) Unit testing method, unit testing device, electronic equipment and storage medium
Amorim et al. A new word embedding approach to evaluate potential fixes for automated program repair
US10324829B2 (en) Application testing
US20210286706A1 (en) Graph-based method for inductive bug localization
CN114185800A (en) Test case generation method, system, equipment and medium based on genetic algorithm
JP2012181666A (en) Information processing device, information processing method and information processing program
KR100777103B1 (en) Apparatus and method for generation of test driver
US11914507B2 (en) Software test apparatus and software test method
CN113051582B (en) Computer software technology development and debugging system
CN115437942A (en) Interface testing method and related device thereof
CN113282495A (en) Java software fault positioning method based on track monitoring
KR101160482B1 (en) Apparatus and method for generating test suite
CN114153447B (en) Automatic AI training code generation method
CN115576854B (en) Android automation unit testing method
CN117609015A (en) Unit test method, unit test large model adjusting method and unit test large model adjusting device
Smith et al. Overview of the SAPHIRE Probabilistic Risk Analysis Software
CN110321280B (en) Data processing method and related 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