Unit testing method, device, electronic device and storage medium
By generating and cross-exchanging statement collections to define the behavior of mock objects, the problem of difficult to specify the Mock object behavior in unit tests is solved, and the applicability of mock objects and the coverage of unit tests is improved.
Patent Information
- Application Number
- CN202111509601.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-10
- Publication Date
- 2025-05-23
- Estimated Expiration
- 2041-12-10
AI Technical Summary
In unit testing, it is difficult for developers to specify appropriate behavior for Mock object and cannot adapt to any test dependencies, resulting in insufficient applicability of mock objects.
By obtaining multiple statement collections, each statement collection is used to define the behavior of the mock object, mutate and cross-exchange the statements using the mutation operators, and generate a set of statements that conform to the test assertions to define the mock object.
Improve the applicability of mock objects, allowing them to more effectively adapt to various test-dependent objects, and enhance the flexibility and coverage of unit testing.
Smart Images

Figure CN114153742B_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present application relate to the technical field of software testing in financial technology (Fintech), and in particular, to a unit testing method, device, electronic device, and computer storage medium. Background Art
[0002] With the development of computer technology, more and more technologies are being applied in the financial field. Traditional finance is gradually transforming into financial technology. However, due to the security and real-time requirements of the financial industry, higher requirements are also placed on technology.
[0003] During the software development process, the software needs to go through stages such as unit testing, integration testing, system testing, and regression testing to ensure the quality of each basic component unit and each interface of the software, verify the correctness of the software functions and interfaces, and repair any errors found.
[0004] In unit testing, there are code modules that need to rely on external classes or interfaces. For some objects that are not easy to construct or obtain, it is necessary to create adaptive mock objects and define the mock objects to adapt to the required classes or interfaces for testing. In actual applications, since the results returned by the test are affected by many factors such as software, hardware, and system environment, the test phase requires a variety of normal and abnormal return results.
[0005] In the related art, a specific type of Mock object is generated for a specific field test dependency object. For general types of test dependency objects, developers usually need to manually create Mock objects, which makes it difficult to specify appropriate behaviors for Mock objects and cannot adapt to any test dependency objects. Therefore, how to improve the applicability of mock objects in unit testing has become an important issue that needs to be solved urgently. Summary of the invention
[0006] The embodiments of the present application provide a unit testing method, device, electronic device and computer storage medium, which can improve the applicability of simulation objects during unit testing.
[0007] A unit testing method provided in an embodiment of the present application includes:
[0008] Acquire multiple statement sets of the i-th population, each of the statement sets includes at least one statement; the statement is used to define the behavior of the simulation object;
[0009] According to the mutation operator corresponding to each of the statements, a mutation operation is performed on each of the statements to obtain a mutation operation result; two statement sets in the mutation operation result are cross-exchanged to obtain an i+1th population;
[0010] If it is determined that there is a statement set that meets the test assertions of the test case in the i+1th population, the statement set that meets the test assertions is used to define the simulation object; and unit testing is performed on the test case based on the simulation object.
[0011] In one implementation, the method further includes:
[0012] When i is an integer greater than 1, if it is determined that there is no statement set that satisfies the test assertion of the test case in the i-1th population, then the fitness of each statement set in the i-1th population is obtained;
[0013] According to the fitness of each sentence set, the sentence set in the i-1th population whose fitness is greater than a preset threshold is used as the sentence set of the i-th population;
[0014] Alternatively, the probability of each statement set in the i-1th population being selected is determined based on the fitness of each statement set; and the statement set of the i-th population is sampled from multiple statement sets in the i-1th population based on the probability of each statement set being selected.
[0015] In one implementation, the i-1th population includes M sets of sentences; and obtaining the fitness of each set of sentences in the i-1th population includes:
[0016] Determine at least one fitness function and a weight coefficient of each of the fitness functions; obtain the calculation results of the j-th statement set in the i-1-th population under each of the fitness functions;
[0017] Obtaining the fitness of the j-th statement set according to the calculation results of the j-th statement set under each of the fitness functions and the weight coefficient of each of the fitness functions;
[0018] When j is 1 to M in sequence, the fitness of each sentence set in the i-1th population is obtained.
[0019] In one implementation, the i-th population includes N statement sets; the cross-exchange of the two statement sets in the mutation operation result to obtain the i+1-th population includes:
[0020] Determine two statement sets from the N statement sets, cross-exchange statements of the same statement type in the two statement sets, and generate two offspring of the two statement sets;
[0021] At least one of the two statement sets and at least one of the two offspring are added to the (i+1)th population to obtain N statement sets in the (i+1)th population.
[0022] In one implementation, determining at least one fitness function includes any of the following:
[0023] Create a fitness function based on the number of statements generated in the statement set and the number of statements successfully executed;
[0024] Create a fitness function based on whether the test case can be executed to the state distribution before the test assertion;
[0025] Create a fitness function based on 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;
[0026] Create a fitness function based on the set of all method calls in the simulation object and the set of method calls in the simulation object that have specified behavior;
[0027] Create a fitness function based on the number of assertion statements in the test assertion that have been satisfied and the number of all assertion statements in the test assertion.
[0028] In one implementation, 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:
[0029] Determine at least one assertion statement of the test assertion; obtain the edit distance between the expected value and the actual value of each assertion statement;
[0030] According to the edit distance between the expected value and the actual value of each assertion statement, a normalized result of the edit distance of each assertion statement is obtained;
[0031] A weighted average of normalized results of the edit distance of each assertion statement is calculated according to the number of assertion statements in the test assertion, and a fitness function is created according to the weighted average.
[0032] In one implementation, performing a mutation operation on each of the statements according to a mutation operator corresponding to each of the statements includes any of the following:
[0033] When the statement type is a numerical type, a mutation operator is used to perform any of the following operations on the statement: increase a random value, decrease a random value;
[0034] When the statement type is a string type, a mutation operator is used to perform any of the following operations on the statement: insert a character, delete a character, or modify a character;
[0035] When the statement type is a statement containing a parameter list, the mutation operator is used to perform the following operations on the statement: replacing a randomly selected parameter with a new value;
[0036] When the statement type is a statement for matching method calls, the mutation operator is used to perform the following operations on the statement: modifying the matching parameters in the matching parameter list;
[0037] When the statement type is a behavior list statement, a mutation operator is used to perform any of the following operations on the statement: swapping the order of behaviors for each method call of the simulation object, adding behaviors, deleting behaviors, and modifying the return value of behaviors.
[0038] A unit testing device provided in an embodiment of the present application includes:
[0039] An acquisition module, used for acquiring a plurality of statement sets of the i-th population, each of the statement sets including at least one statement; the statement is used for defining the behavior of the simulation object;
[0040] A processing module, configured to perform a mutation operation on each of the statements according to a mutation operator corresponding to each of the statements to obtain a mutation operation result; and cross-exchange two statement sets in the mutation operation result to obtain an i+1th population;
[0041] The test module is used to define the simulation object by using the statement set that meets the test assertion if it is determined that there is a statement set that meets the test assertion of the test case in the i+1th population; and perform unit testing on the test case based on the simulation object.
[0042] In one implementation, the acquisition module is further used to:
[0043] When i is an integer greater than 1, if it is determined that there is no statement set that satisfies the test assertion of the test case in the i-1th population, then the fitness of each statement set in the i-1th population is obtained;
[0044] According to the fitness of each sentence set, the sentence set in the i-1th population whose fitness is greater than a preset threshold is used as the sentence set of the i-th population;
[0045] Alternatively, the probability of each statement set in the i-1th population being selected is determined based on the fitness of each statement set; and the statement set of the i-th population is sampled from multiple statement sets in the i-1th population based on the probability of each statement set being selected.
[0046] In one implementation, the i-1th population includes M sets of sentences; the acquisition module is used to acquire the fitness of each set of sentences in the i-1th population, including:
[0047] Determine at least one fitness function and a weight coefficient of each of the fitness functions; obtain the calculation results of the j-th statement set in the i-1-th population under each of the fitness functions;
[0048] Obtaining the fitness of the j-th statement set according to the calculation results of the j-th statement set under each of the fitness functions and the weight coefficient of each of the fitness functions;
[0049] When j is 1 to M in sequence, the fitness of each sentence set in the i-1th population is obtained.
[0050] In one implementation, the i-th population includes N statement sets; the processing module is used to cross-exchange two statement sets in the mutation operation result to obtain the i+1-th population, including:
[0051] Determine two statement sets from the N statement sets, cross-exchange statements of the same statement type in the two statement sets, and generate two offspring of the two statement sets;
[0052] At least one of the two statement sets and at least one of the two offspring are added to the (i+1)th population to obtain N statement sets in the (i+1)th population.
[0053] In one implementation, the acquisition module is used to determine at least one fitness function, including any of the following:
[0054] Create a fitness function based on the number of statements generated in the statement set and the number of statements successfully executed;
[0055] Create a fitness function based on whether the test case can be executed to the state distribution before the test assertion;
[0056] Create a fitness function based on 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;
[0057] Create a fitness function based on the set of all method calls in the simulation object and the set of method calls in the simulation object that have specified behavior;
[0058] Create a fitness function based on the number of assertion statements in the test assertion that have been satisfied and the number of all assertion statements in the test assertion.
[0059] In one implementation, the acquisition module is used to create 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, including:
[0060] Determine at least one assertion statement of the test assertion; obtain the edit distance between the expected value and the actual value of each assertion statement;
[0061] According to the edit distance between the expected value and the actual value of each assertion statement, a normalized result of the edit distance of each assertion statement is obtained;
[0062] A weighted average of normalized results of the edit distance of each assertion statement is calculated according to the number of assertion statements in the test assertion, and a fitness function is created according to the weighted average.
[0063] In one implementation, the processing module is used to perform a mutation operation on each of the statements according to a mutation operator corresponding to each of the statements, including any of the following:
[0064] When the statement type is a numerical type, a mutation operator is used to perform any of the following operations on the statement: increase a random value, decrease a random value;
[0065] When the statement type is a string type, a mutation operator is used to perform any of the following operations on the statement: insert a character, delete a character, or modify a character;
[0066] When the statement type is a statement containing a parameter list, the mutation operator is used to perform the following operations on the statement: replacing a randomly selected parameter with a new value;
[0067] When the statement type is a statement for matching method calls, the mutation operator is used to perform the following operations on the statement: modifying the matching parameters in the matching parameter list;
[0068] When the statement type is a behavior list statement, a mutation operator is used to perform any of the following operations on the statement: swapping the order of behaviors for each method call of the simulation object, adding behaviors, deleting behaviors, and modifying the return value of behaviors.
[0069] An embodiment of the present application provides an electronic device, which includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the program, the unit testing method provided by one or more of the above-mentioned technical solutions is implemented.
[0070] An embodiment of the present application provides a computer storage medium, which stores a computer program; after the computer program is executed, it can implement the unit testing method provided by one or more of the above-mentioned technical solutions.
[0071] Based on the unit testing method provided by the present application, multiple statement sets of the i-th population are obtained, and each statement set includes at least one statement; the statement is used to define the behavior of the simulation object. According to the mutation operator corresponding to each statement, a mutation operation is performed on each statement to obtain a mutation operation result. Therefore, statement sets of virtual objects for various application scenarios can be generated. The two statement sets in the mutation operation result are cross-interchanged to obtain the i+1-th population. Therefore, the statements in the statement set can be recombined to improve the diversity of the statement set. In the case where there is a statement set that meets the test assertion in the i+1-th population, a statement set that meets the test assertion is output, and the statement set that meets the test assertion can be used for unit testing, thereby improving the applicability of the simulation object during unit testing.
[0072] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present application. BRIEF DESCRIPTION OF THE DRAWINGS
[0073] Figure 1 An application scenario diagram of a unit testing method provided in an embodiment of the present application;
[0074] Figure 2 A schematic diagram of a unit testing method provided in an embodiment of the present application;
[0075] Figure 3 A schematic diagram of a flow chart for determining a statement set of the i-th population provided in an embodiment of the present application;
[0076] Figure 4 A schematic diagram of a process for obtaining the fitness of each statement set in a population provided in an embodiment of the present application;
[0077] Figure 5 A schematic diagram of a process for cross-exchanging two statement sets provided in an embodiment of the present application;
[0078] Figure 6 A flowchart of another unit testing method provided in an embodiment of the present application;
[0079] Figure 7 A schematic diagram of a unit testing device provided in an embodiment of the present application;
[0080] Figure 8 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0081] The present application is further described in detail below in conjunction with the accompanying drawings and examples. It should be understood that the embodiments provided herein are only used to explain the present application and are not intended to limit the present application. In addition, the embodiments provided below are partial embodiments for implementing the present application, rather than providing all embodiments for implementing the present application. In the absence of conflict, the technical solutions recorded in the embodiments of the present application can be implemented in any combination.
[0082] Unit testing is a software testing method that includes test cases and test logic. It can check and verify the smallest testable unit in the software, check whether there are code risks in each unit in the program code, and help developers verify whether the new iteration has an impact on the original business logic.
[0083] In unit testing, when testing a program module that needs to call external dependencies, since external dependencies are often difficult to construct or obtain, unit testing cannot be performed, or external dependencies are unstable, resulting in a high test failure rate and high troubleshooting costs. To this end, you can create a mock object to replace the external dependency for testing.
[0084] The unit testing method provided in the embodiment of the present application can be applied to software testing and maintenance. The unit testing method provided in the embodiment of the present application is described in detail below.
[0085] Figure 1 The following is an application scenario diagram of a unit testing method provided by an embodiment of the present application. Figure 1 The genetic variation process of a population may include the following steps: Step A101: Initialize the population (Initial population); Step A102: Calculate the fitness of each individual in the population (Fitness evaluation); Step A103: Determine whether the test result is passed, and execute Step A104 when the test result is passed, otherwise execute Step A105; Step A104: Output the individuals in the population that meet the test assertions; Step A105: Perform individual selection (Selection) on the individuals in the population; Step A106: Perform genetic variation on the individuals in the population.
[0086] It should be understood that the genetic algorithm was created by Professor Holland of the University of Michigan, USA. It is derived from the theory of biological evolution and simulates the evolutionary laws of "natural selection" and "survival of the fittest" in nature. It is a global optimization search method with good global search capabilities during the iterative search process. The genetic algorithm is mainly a "production-iteration" process, and the main links are population selection, chromosome crossover, and chromosome mutation.
[0087] In the example, when the test case depends on the Mock object, regarding the Mock object generation problem, the test case (T) using the Mock object can be modeled as follows: T =<M,S,E,A> ,Refer to Table 1, the test case is divided into the Mock object creation part (M), ,the Mock object setting part (S), the test code execution part (E), ,and the test assertion part (A).
[0088] In the example, see Table 1, based on the model T=<M,S,E,A> The four-tuple in the test case can be divided into the following parts: Mock object creation part (M), which is used to create the mock object needed in the test; Mockobject setting part (S), which is used to define the behavior of the mock object; test code execution part (E), which executes the function of calling the tested object for testing; test assertion part (A), which judges the correctness of the test running results.
[0089] In the example, referring to Table 1, in the Mock object creation part (M), the following mock objects are created: varowner1, var owner2, var logger, where var owner1 and var owner2 belong to the Logger class.
[0090] In the example, see Table 1, Mock object setting part (S), the behavior of the mock object var owner1 includes any of the following: when(owner1Pipeline1.load()).thenReturn(10000), when(owner1Pipeline1.owner()).thenReturn(owner1); the behavior of the mock object var logger includes: when(logger.isFinestEnabled()).thenReturn(true).
[0091] Table 1 Test case structure
[0092]
[0093] In the example, when the Mock object creation part (M), the test code execution part (E), and the test assertion part (A) are given as input, see Table 2, the following grammar rules can be used to generate the code of the Mock object setting part (S) so that the test case conforms to the test assertion in the test assertion part (A).
[0094] In the example, see Table 2, in the statement set of the Mock object, the non-terminal symbols include any of the following: S, Stmt, StubStmt, CallMatcher, ArgMatcherList, ArgMatcher, ReactionList, Reaction, DefStmt, Expr, Literal, NewArray, CtorCall, MethodCall, StaticMethodCall, InstanceMethodCall, MockCreation, FieldRead, Static FieldRead, InstanceFieldRead, ParaList.
[0095] It should be understood that during the compilation process, the non-terminal symbol in the program statement can be understood as a separable element, while the terminal symbol is the smallest indivisible element.
[0096] Table 2 Syntax rules of statements in the statement set
[0097]
[0098]
[0099] In the examples, see Table 2, "stub" is used to represent temporary and simple program code that simulates the functionality of multiple composite codes.
[0100] In practical applications, according to the grammatical rules of the statements in the statement set, after generating the setting statements of the Mock object, a genetic algorithm can be used to search for a statement set that meets the test assertions, and the statement set that meets the test assertions is used as the setting statements of the Mock object.
[0101] Figure 2 A schematic flow chart of a unit testing method provided in an embodiment of the present application is shown. Figure 2 The unit testing method provided in the embodiment of the present application may include the following steps:
[0102] Step A201: Obtain multiple statement sets of the i-th population, each statement set includes at least one statement; the statement is used to define the behavior of the simulation object.
[0103] Here, the functions of the simulated object may include any of the following: network request, file system, input and output, and database.
[0104] In the example, the i-th population is initialized, and the i-th population contains N sets of statements. The multiple sets of statements in the i-th population may include S 1 , S 2, S 3 ,……,S i , ……S n Among them, S i is the i-th statement set.
[0105] In the example, the i-th population can be used as the initial population, and the sentence set in the initial population can be a sentence set in which the parameters in the sentence are empty, or a sentence set generated according to a predefined sentence template. Here, the sentences in the predefined sentence template conform to the grammatical rules of the sentences in the above sentence set.
[0106] It should be understood that based on the creation of simulation objects, during unit testing, the simulation objects and test units can interact to determine whether the test unit can return expected results under normal logic, abnormal logic or stress conditions. It is not necessary to rely on real objects in the program code, thereby isolating program modules and external dependencies, which helps improve software testing efficiency.
[0107] In the example, see Table 3, the statements in the statement set can be used to define the behavior of the simulation object. The statement set can include any of the following statement types: numeric type, string literal, statement containing parameter list, statement matching method call, statement of behavior list, and association statement.
[0108] In the example, the association statement is in the form of When(CallMatcher)ReactionList, and the association statement can be used to associate the behavior of the Mock object with the method call of the Mock object.
[0109] For example, "when(logger.isFinestEnabled()).thenReturn(var0)" is used to associate the behavior "Return(var0)" of the Mockobject with the method call "logger.isFinestEnabled()" of the Mock object.
[0110] Table 3 Statements in the statement set
[0111]
[0112] Step A202: perform a mutation operation on each statement according to the mutation operator corresponding to each statement to obtain a mutation operation result; cross-exchange two statement sets in the mutation operation result to obtain the i+1th population.
[0113] In the example, the mutation operator may be a program script used when performing mutation operations on statements in a statement set. Each statement type may correspond to a type of mutation operator. Referring to Table 4, for different types of statements, mutation operations are performed on the statements using mutation operators corresponding to the statement types.
[0114] For example, for a statement of a numerical type, the statement form is Var=Expr, and the expression part Expr that affects the variable value Var can be modified to perform a mutation operation on the statement of the numerical type.
[0115] Table 4 Statement types and mutation operators corresponding to statement types
[0116]
[0117]
[0118] In the example, two sets of sentences are randomly selected from the population as two parents, and the crossover algorithm is used to crossover the sentences of the same statement type in the two parents to generate children of the two parents. At least one of the two parents and at least one of the two children are added to the new generation population to obtain the i+1th population.
[0119] Step A203: If it is determined that there is a statement set that meets the test assertions of the test case in the i+1th population, a simulation object is defined using the statement set that meets the test assertions; and unit testing is performed on the test case based on the simulation object.
[0120] In the example, during unit testing, the unit may be a function in a C language program code, a class in a Java program code, or a window or a menu in a graphical software.
[0121] In the example, when there is a certain statement set in the i+1th population that can make the test assertion pass, the genetic mutation of the statement set in the i+1th population is stopped, and a statement set that meets the test assertion is output.
[0122] In the example, an assertion statement is added to the test unit for unit testing. If the execution result of the test unit does not meet the expectations of the assertion statement, an exception prompt or an alarm prompt is thrown.
[0123] In the example, the statement set S is used i The statements in the above example set the mock object. When the test case passes the unit test, it means that the test case meets the test assertion in the test assertion part (A).
[0124] In the example, JMockit is used to perform unit testing based on a set of statements that meet the test assertions. Here, JMockit is a simulation tool for Java classes / interfaces / objects, which is widely used in unit testing of Java applications.
[0125] It should be understood that JMockit can simulate the peripheral interfaces called by methods or modules to achieve complete decoupling from peripheral systems during unit testing. During unit testing, test cases can be used to check for defects in program code, focusing on whether the test cases can implement normal interface calls and return the preset results of the test cases.
[0126] Based on the unit testing method provided by the present application, multiple statement sets of the i-th population are obtained, and each statement set includes at least one statement; the statement is used to define the behavior of the simulation object. According to the mutation operator corresponding to each statement, a mutation operation is performed on each statement to obtain a mutation operation result. Therefore, statement sets of virtual objects for various application scenarios can be generated. The two statement sets in the mutation operation result are cross-interchanged to obtain the i+1-th population. Therefore, the statements in the statement set can be recombined to improve the diversity of the statement set. In the case where there is a statement set that meets the test assertion in the i+1-th population, a statement set that meets the test assertion is output, and the statement set that meets the test assertion can be used for unit testing, thereby improving the applicability of the simulation object during unit testing.
[0127] In practical applications, the above steps A201 to A203 can be implemented using a processor, and the above processor can 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.
[0128] In one implementation, in the above unit test method, see Figure 3 , and may also include the following steps:
[0129] Step A301: When i is an integer greater than 1, if it is determined that there is no statement set of test assertions that meet the test case in the i-1th population, then the fitness of each statement set in the i-1th population is obtained.
[0130] In the example, the multiple statement sets of the i-1th population may include S 1 , S 2 , S 3 ,……,S i , ……S n Among them, S i is the i-th statement set.
[0131] It should be understood that among the multiple statement sets in the i-1th population, each statement set does not meet the test assertion, and it is determined that there is no statement set that meets the test assertion in the i-1th population.
[0132] In the example, before calculating the fitness of the statement set, the test case may be run based on the statement set, and the running data of the test case may be collected when the test case is run. See Table 5. The running data may include any of the following items:
[0133] Statement set S i The number of setup statements generated in the test case and the number of setup statements successfully executed during the test case testing process, whether the test case can be executed before the test assertion after the virtual object is defined by a statement set, the set of all method calls in the simulation object and the set of method calls with specified behaviors in the simulation object, the number of assertion statements that have been satisfied in the test assertion and the number of all assertion statements in the test assertion, 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.
[0134] Table 5 Data identification and data definition
[0135]
[0136] In the example, when a statement set is used for unit testing, the running data of the test case is obtained, and the fitness of each statement set in the i-1th population is obtained according to the running data of the test case and the fitness function.
[0137] For example, according to the statement set S i The number of statements generated in S c And the number of statements successfully executed during the test case testing process S 0 , using the fitness function Computation statement set S i The fitness of .
[0138] Step A302: Determine the statement set of the i-th population from the multiple statement sets of the i-1-th population according to the fitness of each statement set.
[0139] In the example, according to the fitness of each sentence set, the sentence set S in the i-1th population i When the fitness of is greater than the preset threshold, the statement set S i As the statement set of the i-th population. That is, the statement set in the i-1-th population whose fitness is greater than the preset threshold is taken as the statement set of the i-th population.
[0140] In this example, according to the sentence set S in the i-1 population i The fitness of the sentence set S in the i-1 population is determined i The probability of being selected. According to the probability of each sentence set being selected, the sentence set of the i-th population is sampled from multiple sentence sets of the i-1-th population.
[0141] It should be understood that the probability of each sentence set being selected is the calculation result of its fitness function. i The fitness of the sentence set S in the i-1 population is determined i The probability of being selected. Statement set S i The greater the fitness of i The greater the probability of being selected.
[0142] Therefore, the average fitness of the sentence set in the i-th population is greater than the average fitness of the sentence set in the i-1-th population.
[0143] In the example, the sentence set of the i-th population is determined from the multiple sentence sets of the i-1th population. In the random sampling process, the input is the i-1th population and the probability of each sentence set in the i-1th population being selected, and the output is the sampled sentence set.
[0144] In this example, a sampling strategy with replacement sampling is adopted. According to the probability of each sentence set being selected, a sentence set is sampled each time from multiple sentence sets in the i-1th population. The sampling process is repeated N times, and N sentence sets are output. The N sentence sets obtained by sampling are used as the sentence sets of the i-th population.
[0145] It should be understood that, when the sampling strategy of sampling with replacement is adopted, two identical samples may exist in the multiple samples obtained by sampling. Here, a sample may be a set of sentences of the i-th population.
[0146] Based on the above sampling process, the sentence set S in the i-1th population iScreening is performed to obtain the sentence set of the i-th population. Since the average fitness of the sentence set in the i-th population is greater than the average fitness of the sentence set in the i-1-th population, compared with directly using the sentence set in the i-1-th population for genetic variation, the influence of some sentence sets with lower fitness on the genetic variation process can be reduced.
[0147] In the example, when there is no statement set in the i-1th population that can make the test assertion pass, the statement set of the i-th population can be determined from multiple statement sets in the i-1th population, and the statement set in the i-th population can be genetically mutated until a statement set that meets the test assertion is output.
[0148] In one implementation, the i-1th population includes M sets of sentences; the fitness of each set of sentences in the i-1th population is obtained, see Figure 4 , which may include the following steps:
[0149] Step A401: Determine at least one fitness function and a weight coefficient of each fitness function; obtain the calculation results of the j-th statement set in the i-1-th population under each fitness function.
[0150] In the example, the fitness function is used to guide the search process of the genetic algorithm, and the fitness function includes any of the following: Setup Exception (SE), Execution Exception (EE), Mock Method Satisfactory (MS), Assertion Distances (AD), Assertion Coverage (AC).
[0151] In the example, according to the number of statements S generated in the statement set c And the number of successfully executed statements S 0 , create a fitness function, the fitness function SE is as follows:
[0152]
[0153] In the example, the test case can be executed to the state of the test assertion, which is denoted as E. 1 The state where the test case fails to execute the test assertion is recorded as E 0 , create a fitness function based on whether the test case can be executed before the test assertion. The fitness function EE is as follows:
[0154]
[0155] In the example, M S A collection of method calls with specified behaviors in the Mock object.i It is the collection of all method calls in Mockobject. The fitness function MS is as follows:
[0156]
[0157] In this example, AE is the set of assertion statements of all test assertions, d(a.expected,a.actual) is the edit distance between the expected value a.expected and the actual value a.actual of assertion statement a, a∈AE. AE is the number of assertion statements in the set AE. The fitness function AD is as follows:
[0158]
[0159] Among them, d0 = Max (la.expected, la.actual), la.expected and la.actual are the string lengths of a.expected and a.actual respectively, then d 0 It is the maximum value of la.expected and la.actual.
[0160] In the example, according to the number of assertion statements M that have been satisfied in the test assertion, 0 and the number of all assertion statements in the test assertion M AE , create a fitness function. The fitness function AC is as follows:
[0161]
[0162] In the example, the edit distance function d(x, y) is defined as follows, where Lev(x, y) is the edit distance function of the string.
[0163]
[0164] Step A402: Obtain the fitness of the jth statement set according to the calculation results of the jth statement set under each fitness function and the weight coefficient of each fitness function.
[0165] In the example, the calculation results of the statement set under each fitness function are: SE, EE, MS, AD, AC. Correspondingly, the weight coefficients of each fitness function are: γ SE , γ EE , γ MS , γ AD , γ AC .
[0166] In the example, γSE =1,γ EE =2,γ MS =4,γ AD =16,γ AC =16, according to the calculation results of the j-th sentence set under each fitness function and the weight coefficient of each fitness function, the fitness of the j-th sentence set is obtained as follows:
[0167] Fitness=SE×1+EE×2+MS×4+AD×16+AC×16 (7)
[0168] Step A403: When j is 1 to M in sequence, obtain the fitness of each statement set in the i-1th population.
[0169] In one implementation, the i-th population includes N statement sets; the two statement sets in the mutation operation result are cross-exchanged to obtain the i+1-th population, see Figure 5 , which may include the following steps:
[0170] Step A501: Determine two statement sets from the N statement sets, cross-exchange statements of the same statement type in the two statement sets, and generate two offspring of the two statement sets.
[0171] In the example, there are N statement sets in the population, where N is an integer multiple of 4. N / 4 groups of parents can be randomly selected from the population, as shown in Table 6. In each group of parents, there are two statement sets to be cross-exchanged.
[0172] Table 6 Two sets of statements to be cross-interchange
[0173]
[0174] In the example, see Table 7, for each group of parents, the statements of the same statement type in the two parents are cross-exchanged to generate children of the two parents. The two parents and the two children are added to the new generation population to obtain the i+1th population.
[0175] Table 7 Descendants of two statement sets after cross-exchange
[0176]
[0177]
[0178] In the related art, the cross-interchange algorithm uses the single-point cross-interchange in the genetic algorithm. Incorrect combinations of the generated Mockobject setting statements, the parameter generation statements for calling the application interface, and the application interface calling statements will cause the test unit to throw an exception during execution.
[0179] For example, regarding application program interface calls, when the passed-in parameters are incorrect, the application program interface will report an error when checking the parameters.
[0180] In the example, two statement sets among 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-off points for cross-exchange, and statements of the same statement type before the cut-off points in the two statement sets are cross-exchanged.
[0181] It should be understood that when performing cross-exchange, ensuring that the cut point is before the statement that throws the exception can cut off the incorrect combination formed after the cross-exchange of the two statement sets, so that the two statement sets have more opportunities to obtain the correct combination after the cross-exchange.
[0182] Step A502: Add at least one of the two statement sets and at least one of the two offspring to the (i+1)th population to obtain N statement sets in the (i+1)th population.
[0183] In the example, there are N sets of statements in the population, where N is an integer multiple of 4. N / 4 groups of parents can be randomly selected from the population. For each group of parents, the statements of the same statement type in the two parents are cross-exchanged to generate children of the two parents. The two parents and the two children are added to the new generation population to obtain the i+1th population.
[0184] In one implementation, in the above unit testing method, determining at least one fitness function may include the following steps: i The number of statements generated and the number of statements successfully executed are used to create a fitness function.
[0185] In the example, according to the statement set S i The number of statements generated in S c And the number of successfully executed statements S 0 , create a fitness function
[0186] In one implementation, in the above unit testing method, determining at least one fitness function may include the following steps: creating a fitness function according to whether the test case can be executed to the state distribution before the test assertion.
[0187] In the example, the test case can be executed to the state of the test assertion, which is denoted as E. 1 The state where the test case fails to execute the test assertion is recorded as E 0 . Create a fitness function based on whether the test case can be executed to the state distribution before the test assertion
[0188] In one implementation, in the above unit testing method, determining at least one fitness function may include the following steps: 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.
[0189] In the example, according to the test assertion, the number of statements M of assertion statements AE And the edit distance d(a.expected,a.actual) between the expected value a.expected and the actual value a.actual of each assertion statement, create a fitness function
[0190] It should be understood that the edit distance is an algorithm for calculating text similarity. If the string a.actual can be transformed into the string a.expected through operations such as addition, deletion, and modification, the fewer the related operations, the more similar the strings a.actual and a.expected are.
[0191] In one implementation, 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 may include the following steps:
[0192] Determine at least one assertion statement of the test assertion; obtain the edit distance between the expected value and the actual value of each assertion statement; obtain the normalized result of the edit distance of each assertion statement based on the edit distance between the expected value and the actual value of each assertion statement; calculate the weighted average of the normalized results of the edit distance of each assertion statement based on the number of assertion statements in the test assertion, and create a fitness function based on the weighted average.
[0193] In the example, at least one assertion statement of the test assertion is determined, and a set corresponding to the at least one assertion statement is defined as AE, in which a belongs to any assertion statement.
[0194] In the example, the edit distance d(a.expected,a.actual) between the expected value a.expected and the actual value a.actual of the assertion statement a is obtained, and the edit distance d(a.expected,a.actual) of the assertion statement a is normalized to obtain the normalized result of the edit distance of the assertion statement a: 1.0-d(a.expected,a.actual) / d o .
[0195] In the example, according to the test assertion, the number of statements M of assertion statements AE , calculate the weighted average of the normalized edit distance of each assertion statement Create a fitness function based on the weighted average of the normalized results
[0196] It should be understood that 1.0-d(a.expected,a.actual) / d 0 The larger the value, the smaller the edit distance d(a.expected,a.actual). Correspondingly, the fitness function The greater the corresponding fitness.
[0197] In one implementation, in the above unit testing method, determining at least one fitness function may include the following steps: creating a fitness function based on the test case execution to assert all methods called on the Mock object and methods with specified behaviors on the Mock object.
[0198] In the example, the fitness function is created based on the test case execution to assert all the methods called on the Mock object and the methods with specified behavior on the Mockobject.
[0199] In one implementation, in the above unit testing method, determining at least one fitness function may include the following steps: creating a fitness function according to the number of assertion statements that have been satisfied in the test assertion and the number of all assertion statements in the test assertion.
[0200] In the example, according to the number of assertion statements M that have been satisfied in the test assertion, o and the number of all assertion statements in the test assertion M AE , create a fitness function
[0201] The following describes in detail the mutation operators defined in the embodiments of the present application for different types of expressions.
[0202] In one implementation, a mutation operation is performed on each statement according to a mutation operator corresponding to each statement, including:
[0203] When the statement type is a numeric type, the mutation operator is used to perform any of the following operations on the statement: increase a random value, decrease a random value.
[0204] In the example, for a statement of a numeric type, the statement form is Var=Expr. For a literal of a numeric type, a random value can be added / subtracted in the expression part Expr of Var.
[0205] In one implementation, a mutation operation is performed on each statement according to a mutation operator corresponding to each statement, including:
[0206] When the statement type is string type, use the mutation operator to perform any of the following operations on the statement: insert characters, delete characters, and modify characters.
[0207] In the example, for string type statements, the statement form is Primitive String Null Class. For string literals, you can insert / delete / modify random characters in the string and reverse the order of characters in the string.
[0208] In one implementation, a mutation operation is performed on each statement according to a mutation operator corresponding to each statement, including:
[0209] When the statement type is a statement containing a parameter list, the mutation operator is used to perform the following operations on the statement: a randomly selected parameter is replaced with a new value.
[0210] In the example, for a statement containing a parameter list, the statement form is ParaList Var|∈. For the statement containing a parameter list, a parameter can be randomly selected from the parameter list and replaced with a new value.
[0211] In one implementation, a mutation operation is performed on each statement according to a mutation operator corresponding to each statement, including:
[0212] When the statement type is a statement for matching method calls, the mutation operator is used to perform the following operations on the statement: Modify the matching parameters in the matching parameter list.
[0213] In the example, for the statement that matches the method call, the statement format is when(CallMatcher)ReactionList. For the CallMatcher part, the ArgMatcherList in CallMatcher can be modified.
[0214] For example, a single parameter ArgMatcher in the matching parameter list ArgMatcherList is randomly selected, and the following modifications are randomly performed with equal probability: the single parameter ArgMatcher in the matching parameter list is switched between any() and eq(Var).
[0215] For example, when a single argument ArgMatcher in the matching argument list ArgMatcherList is eq(Var), Var is replaced with the new variable.
[0216] In one implementation, a mutation operation is performed on each statement according to a mutation operator corresponding to each statement, including:
[0217] When the statement type is a behavior list statement, the mutation operator is used to perform any of the following operations on the statement: swap the order of each behavior of each method call of the simulation object, add behaviors, delete behaviors, and modify the return value of the behavior.
[0218] In the example, for the behavior association statement, the statement format is when(CallMatcher)ReactionList. For the ReactionList part, the following modifications can be made randomly with equal probability:
[0219] Swap the order of Reactions in the ReactList, randomly add / delete a Reaction, replace the return value or exception of a randomly selected Reaction with a new variable, and randomly generate the return value of a Reaction.
[0220] In the example, for the Mock object creation part, the return value / exception is randomly specified for the Mock object method call.
[0221] It should be understood that in actual applications, the mutation operators used by the statement set may be added or deleted; the fitness calculation functions used by the statement set may be added or deleted.
[0222] Based on the same technical concept as the above embodiments, see Figure 6 The unit testing method provided in the embodiment of the present application may include:
[0223] Step A601: Initialize the initial population, which includes N statement sets.
[0224] Step A602: Determine that there is no statement set in the population that meets the test assertion, and obtain the fitness of each statement set in the population.
[0225] In the example, the fitness function is used to calculate the fitness of each statement set in the population.
[0226] Step A603: Determine the statement set of the i-th population from the multiple statement sets of the i-1-th population according to the fitness of each statement set.
[0227] In the example, i is an integer greater than 1, and when i=2, the i-1th population is the 1st population, and the 1st population can be used as the initial population.
[0228] Step A604: Perform a mutation operation on each statement according to the mutation operator corresponding to each statement to obtain a mutation operation result.
[0229] In the example, for each statement in each statement set in the i-th population, a mutation operation is performed on each statement according to a mutation operator corresponding to each statement to obtain a mutation operation result.
[0230] Step A605: Cross-exchange the two statement sets in the mutation operation result to generate offspring of the two statement sets.
[0231] In the example, two statement sets are selected as parents in the mutation operation results, and statements of the same statement type in the two statement sets are cross-exchanged to generate children of each statement set.
[0232] In the example, at least one statement set in two parent generations and at least one statement set in two child generations are added to the i+1th population to obtain the statement set of the i+1th population.
[0233] Step A606: Determine whether there is a statement set that meets the test assertion in the (i+1)th population.
[0234] In the example, if there is a set of statements that meet the test assertion in the i+1th population, the following step A607 is performed. If there is no set of statements that meet the test assertion in the i+1th population, the following step A609 is performed.
[0235] Step A607: Output the set of statements in the (i+1)th population that meet the test assertion.
[0236] Step A608: Report generation statement set is successful.
[0237] In the example, in the case where the report generation statement set is successful, the following step A611 is performed.
[0238] Step A609: Determine whether the number of population iterations is less than a preset value.
[0239] In the example, the population iteration number is recorded, and when the iteration number from the initial population to the i+1th population is less than a preset value, the above step A602 is performed. When the iteration number from the initial population to the i+1th population is greater than or equal to a preset value, the following step A611 is performed.
[0240] Step A610: Report generation statement set failure.
[0241] Step A610: End.
[0242] In the related technology, the Mock object construction technology is integrated into EvoSuite, and a Mock object is generated when generating unit tests to improve the code coverage of the test. Starting from the test case, through the Capture-And-Replay method, the behavior of the test dependent object is recorded when running the test case, and the recorded behavior is applied to the Mock object.
[0243] Based on the same technical concept as the above embodiments, see Figure 7 The unit testing device provided in the embodiment of the present application may include:
[0244] The acquisition module 701 is used to acquire multiple statement sets of the i-th population, each of which includes at least one statement; the statement is used to define the behavior of the simulation object;
[0245] The processing module 702 is used to perform a mutation operation on each of the statements according to the mutation operator corresponding to each of the statements to obtain a mutation operation result; cross-exchange two statement sets in the mutation operation result to obtain an i+1th population;
[0246] The testing module 703 is used to define the simulation object by using the statement set that meets the test assertion if it is determined that there is a statement set that meets the test assertion of the test case in the i+1th population; and perform unit testing on the test case based on the simulation object.
[0247] In one implementation, the acquisition module 701 is further configured to:
[0248] When i is an integer greater than 1, if it is determined that there is no statement set that satisfies the test assertion of the test case in the i-1th population, then the fitness of each statement set in the i-1th population is obtained;
[0249] According to the fitness of each sentence set, the sentence set in the i-1th population whose fitness is greater than a preset threshold is used as the sentence set of the i-th population;
[0250] Alternatively, the probability of each statement set in the i-1th population being selected is determined based on the fitness of each statement set; and the statement set of the i-th population is sampled from multiple statement sets in the i-1th population based on the probability of each statement set being selected.
[0251] In one implementation, the i-1th population includes M sets of sentences; the acquisition module 701 is used to acquire the fitness of each set of sentences in the i-1th population, including:
[0252] Determine at least one fitness function and a weight coefficient of each of the fitness functions; obtain the calculation results of the j-th statement set in the i-1-th population under each of the fitness functions;
[0253] Obtaining the fitness of the j-th statement set according to the calculation results of the j-th statement set under each of the fitness functions and the weight coefficient of each of the fitness functions;
[0254] When j is 1 to M in sequence, the fitness of each sentence set in the i-1th population is obtained.
[0255] In one implementation, the i-th population includes N statement sets; the processing module 702 is used to cross-exchange two statement sets in the mutation operation result to obtain the i+1-th population, including:
[0256] Determine two statement sets from the N statement sets, cross-exchange statements of the same statement type in the two statement sets, and generate two offspring of the two statement sets;
[0257] At least one of the two statement sets and at least one of the two offspring are added to the (i+1)th population to obtain N statement sets in the (i+1)th population.
[0258] In one implementation, the acquisition module 701 is used to determine at least one fitness function, including any of the following:
[0259] Create a fitness function based on the number of statements generated in the statement set and the number of statements successfully executed;
[0260] Creating a fitness function according to whether the test case can be executed before the test assertion;
[0261] 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;
[0262] Create a fitness function based on the set of all method calls in the simulation object and the set of method calls in the simulation object that have specified behavior;
[0263] Create a fitness function based on the number of assertion statements in the test assertion that have been satisfied and the number of all assertion statements in the test assertion.
[0264] In one implementation, the acquisition module 701 is used to create 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, including:
[0265] Determine at least one assertion statement of the test assertion; obtain the edit distance between the expected value and the actual value of each assertion statement;
[0266] According to the edit distance between the expected value and the actual value of each assertion statement, a normalized result of the edit distance of each assertion statement is obtained;
[0267] A weighted average of normalized results of the edit distance of each assertion statement is calculated according to the number of assertion statements in the test assertion, and a fitness function is created according to the weighted average.
[0268] In one implementation, the processing module 702 is configured to perform a mutation operation on each of the statements according to a mutation operator corresponding to each of the statements, including any of the following:
[0269] When the statement type is a numerical type, a mutation operator is used to perform any of the following operations on the statement: increase a random value, decrease a random value;
[0270] When the statement type is a string type, a mutation operator is used to perform any of the following operations on the statement: insert a character, delete a character, or modify a character;
[0271] When the statement type is a statement containing a parameter list, the mutation operator is used to perform the following operations on the statement: replacing a randomly selected parameter with a new value;
[0272] When the statement type is a statement for matching method calls, the mutation operator is used to perform the following operations on the statement: modifying the matching parameters in the matching parameter list;
[0273] When the statement type is a behavior list statement, a mutation operator is used to perform any of the following operations on the statement: swapping the order of behaviors for each method call of the simulation object, adding behaviors, deleting behaviors, and modifying the return value of behaviors.
[0274] In practical applications, the acquisition module 701, the processing module 702 and the test module 703 can all be implemented using a processor of an electronic device. The above processor can 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 embodiments of the present application are not limited to this.
[0275] In some embodiments, the functions or modules included in the device provided in the embodiments of the present application can be used to execute the method described in the above method embodiments. The specific implementation can refer to the description of the above method embodiments. For the sake of brevity, it will not be repeated here.
[0276] Based on the same technical concept as the above embodiments, see Figure 8 The electronic device 800 provided in the embodiment of the present application may include: a memory 810 and a processor 820; wherein,
[0277] Memory 810, for storing computer programs and data;
[0278] The processor 820 is used to execute the computer program stored in the memory to implement any one of the unit testing methods in the aforementioned embodiments.
[0279] In practical applications, the memory 810 may be a volatile memory, for example a RAM; or a non-volatile memory, for example a ROM, a flash memory, a hard disk drive (HDD) or a solid-state drive (SSD); or a combination of the above-mentioned types of memory. The memory 810 may provide instructions and data to the processor 820.
[0280] The above description of the various embodiments tends to emphasize the differences between the various embodiments. The same or similar parts can be referenced to each other. For the sake of brevity, this article will not repeat them.
[0281] The methods disclosed in the various method embodiments provided in this application can be arbitrarily combined without conflict to obtain new method embodiments.
[0282] The features disclosed in the various product embodiments provided in this application can be arbitrarily combined without conflict to obtain new product embodiments.
[0283] The features disclosed in the various method or device embodiments provided in this application can be arbitrarily combined without conflict to obtain new method embodiments or device embodiments.
[0284] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. The device embodiments described above are only schematic. For example, the division of units is only a logical function division. There may be other division methods in actual implementation, such as: multiple units or components can be combined, or can be integrated into another system, or some features can be ignored or not executed. In addition, the coupling, direct coupling, or communication connection between the components shown or discussed can be through some interfaces, and the indirect coupling or communication connection of devices or units can be electrical, mechanical or other forms.
[0285] The units described above as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place or distributed on multiple grid units; some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0286] 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 configured as a unit, or two or more units may be integrated into one unit; the above-mentioned integrated units may be implemented in the form of hardware or in the form of hardware plus software functional units.
[0287] A person skilled in the art can understand that all or part of the steps of implementing the above method embodiment can be completed by hardware related to program instructions, and the aforementioned program can be stored in a computer-readable storage medium, which, when executed, executes the steps including the above method embodiment.
[0288] The above are only specific implementations of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A unit testing method, It is characterized in that include: Acquire multiple statement sets of the i-th population, each of the statement sets includes at least one statement; the statement is used to define the behavior of the simulation object; The statement set includes any of the following statement types: numeric type, string literal, statement containing parameter list, statement matching method call, statement of behavior list, and association statement; the statement of behavior list is used to specify the behavior of each method call of the simulation object; The association statement is used to associate the behavior of the simulation object with the method call of the simulation object; According to the mutation operator corresponding to each of the statements, a mutation operation is performed on each of the statements to obtain a mutation operation result; Cross-exchange the two statement sets in the mutation operation result to obtain the i+1th population; If it is determined that there is a statement set that meets the test assertion of the test case in the i+1th population, the statement set that meets the test assertion is used to define the simulation object; Performing unit testing on the test case based on the simulation object specifically includes: Based on the creation of simulation objects, the simulation objects and test units interact during unit testing to determine whether the test unit returns expected results under normal logic, abnormal logic or stress conditions.
2. The method according to claim 1, It is characterized in that The method further comprises: When i is an integer greater than 1, if it is determined that there is no statement set that meets the test assertion of the test case in the i-1th population, then the fitness of each statement set in the i-1th population is obtained. According to the fitness of each sentence set, the sentence set in the i-1th population whose fitness is greater than a preset threshold is used as the sentence set of the i-th population; Alternatively, the probability of each statement set in the i-1th population being selected is determined based on the fitness of each statement set; and the statement set of the i-th population is sampled from multiple statement sets in the i-1th population based on the probability of each statement set being selected.
3. The method according to claim 2, It is characterized in that The i-1th population includes M sets of sentences; and obtaining the fitness of each set of sentences in the i-1th population includes: Determine at least one fitness function and a weight coefficient of each of the fitness functions; obtain the calculation results of the j-th statement set in the i-1-th population under each of the fitness functions; Obtaining the fitness of the j-th statement set according to the calculation results of the j-th statement set under each of the fitness functions and the weight coefficient of each of the fitness functions; When j is 1 to M in sequence, the fitness of each sentence set in the i-1th population is obtained.
4. The method according to claim 1, It is characterized in that The i-th population includes N sets of sentences; The two statement sets in the mutation operation result are cross-exchanged to obtain the i+1th population, including: Determine two statement sets from the N statement sets, cross-exchange statements of the same statement type in the two statement sets, and generate two offspring of the two statement sets; At least one of the two statement sets and at least one of the two offspring are added to the (i+1)th population to obtain N statement sets in the (i+1)th population.
5. The method according to claim 3, It is characterized in that Determining at least one fitness function includes any of the following: Create a fitness function based on the number of statements generated in the statement set and the number of statements successfully executed; Create a fitness function based on whether the test case can be executed to the state distribution before the test assertion; Create a fitness function based on 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; Create a fitness function based on the set of all method calls in the simulation object and the set of method calls in the simulation object that have specified behavior; Create a fitness function based on the number of assertion statements in the test assertion that have been satisfied and the number of all assertion statements in the test assertion.
6. The method according to claim 5, It is characterized in that The step of 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 comprises: Determine at least one assertion statement of the test assertion; obtain the edit distance between the expected value and the actual value of each assertion statement; According to the edit distance between the expected value and the actual value of each assertion statement, a normalized result of the edit distance of each assertion statement is obtained; A weighted average of normalized results of the edit distance of each assertion statement is calculated according to the number of assertion statements in the test assertion, and a fitness function is created according to the weighted average.
7. The method according to claim 1, It is characterized in that The performing a mutation operation on each of the statements according to the mutation operator corresponding to each of the statements includes any of the following: When the statement type is a numerical type, a mutation operator is used to perform any of the following operations on the statement: increase a random value, decrease a random value; When the statement type is a string type, a mutation operator is used to perform any of the following operations on the statement: insert a character, delete a character, or modify a character; When the statement type is a statement containing a parameter list, the mutation operator is used to perform the following operations on the statement: replacing a randomly selected parameter with a new value; When the statement type is a statement for matching method calls, the mutation operator is used to perform the following operations on the statement: modifying the matching parameters in the matching parameter list; When the statement type is a behavior list statement, a mutation operator is used to perform any of the following operations on the statement: swapping the order of behaviors for each method call of the simulation object, adding behaviors, deleting behaviors, and modifying the return value of behaviors.
8. A unit testing device, It is characterized in that include: An acquisition module, used for acquiring a plurality of statement sets of the i-th population, each of the statement sets including at least one statement; the statement is used for defining the behavior of the simulation object; The statement set includes any of the following statement types: numeric type, string literal, statement containing parameter list, statement matching method call, statement of behavior list, and association statement; the statement of behavior list is used to specify the behavior of each method call of the simulation object; The association statement is used to associate the behavior of the simulation object with the method call of the simulation object; A processing module performs a mutation operation on each of the statements according to a mutation operator corresponding to each of the statements to obtain a mutation operation result; Cross-exchange the two statement sets in the mutation operation result to obtain the i+1th population; A test module, for defining the simulation object by using the statement set that meets the test assertion if it is determined that there is a statement set that meets the test assertion of the test case in the i+1th population; Performing unit testing on the test case based on the simulation object specifically includes: Based on the creation of simulation objects, the simulation objects and test units interact during unit testing to determine whether the test unit returns expected results under normal logic, abnormal logic or stress conditions.
9. The unit testing device according to claim 8, It is characterized in that The acquisition module is also used for: If it is determined that there is no statement set that meets the test assertion in the i-1th population, then the fitness of each statement set in the i-1th population is obtained; According to the fitness of each sentence set, the sentence set in the i-1th population whose fitness is greater than a preset threshold is used as the sentence set of the i-th population; Alternatively, the probability of each statement set in the i-1th population being selected is determined based on the fitness of each statement set; and the statement set of the i-th population is sampled from multiple statement sets in the i-1th population based on the probability of each statement set being selected.
10. An electronic device, It is characterized in that The electronic device comprises a memory, a processor and a computer program stored in the memory and executable on the processor, and the processor implements the unit testing method according to any one of claims 1 to 7 when executing the program.
11. A computer storage medium, wherein the storage medium stores a computer program; It is characterized in that The computer program can implement the unit testing method according to any one of claims 1 to 7 after being executed.
Citation Information
Patent Citations
Mutant-based Test Problem Generating Apparatus and Method thereof
KR1020180126856A
Automated testing of application program interfaces using genetic algorithms
US20130014089A1