Test case generation method, system, device and medium based on genetic algorithm
Patent Information
- Application Number
- CN202111540606.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-16
- Publication Date
- 2025-02-21
- Estimated Expiration
- 2041-12-16
AI Technical Summary
The existing software test case generation method based on genetic algorithms is not effective in actual application scenarios. It requires more test cases to cover more comprehensive application scenarios. In addition, a large amount of redundant data is generated during the automatic generation of test cases, which is a test efficiency. low.
By obtaining seed test cases in the object to be tested, extracting feature information and feature values, building the initial population, and using the fitness function of the genetic algorithm for iterative optimization until the preset convergence conditions are met, the target test case set is output.
It realizes that the more comprehensive scenarios are covered with fewer test cases, reduces the generation of redundant data, and improves the efficiency of software testing.
Smart Images

Figure CN114185800B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software testing, and in particular to a test case generation method, system, device and medium based on genetic algorithm. Background Art
[0002] Genetic algorithm is a search algorithm used in computational mathematics to solve optimization problems. It is a type of evolutionary algorithm. Evolutionary algorithms were originally developed based on some phenomena in evolutionary biology, including inheritance, mutation, natural selection, and hybridization. Genetic algorithms are usually implemented as a computer simulation. For an optimization problem, a population of abstract representations (called chromosomes) of a certain number of candidate solutions (called individuals) evolves towards better solutions. Traditionally, solutions are represented in binary (i.e., strings of 0s and 1s), but other representations can also be used. Evolution starts with a population of completely random individuals and then occurs generation after generation. In each generation, the fitness of the entire population is evaluated, and multiple individuals are randomly selected from the current population (based on their fitness) to generate a new population of life through natural selection and mutation, which becomes the current population in the next iteration of the algorithm.
[0003] In the software test case generation method based on genetic algorithm optimization provided by the prior art, the genetic operator in the genetic algorithm is improved, and the fitness function is designed considering the influence of branch distance and branch weight, but it is not combined with the actual application scenario, resulting in the need for more test cases to cover more comprehensive application scenarios. A large amount of redundant data is generated during the automatic generation of test cases, and the test efficiency is low. Summary of the invention
[0004] In view of the problems in the prior art, the purpose of the present invention is to provide a test case generation method, system, device and medium based on genetic algorithm, so as to cover more comprehensive scenarios with fewer test cases, thereby improving software testing efficiency.
[0005] To achieve the above object, the present invention provides a test case generation method based on genetic algorithm, the method comprising the following steps:
[0006] The server obtains the object to be tested, and obtains a seed test case from the object to be tested; the seed test case includes feature information and corresponding feature values;
[0007] The server constructs an initial population based on the feature information and the corresponding feature values;
[0008] The server constructs a fitness function of the genetic algorithm, and uses the initial population as input of the genetic algorithm to obtain a descendant population;
[0009] The server iterates based on the offspring population evolution until a preset convergence condition is met, and outputs a target test case set.
[0010] Optionally, obtaining a seed test case from the object to be tested includes:
[0011] The server parses the object to be tested to obtain metadata information; the metadata information includes test samples;
[0012] The server traverses the metadata information to obtain the test sample, and uses the test sample as a seed test case.
[0013] Optionally, the server constructs a primary population based on the feature information and the corresponding feature value, including:
[0014] The server extracts feature information from the seed test case and feature values corresponding to the feature information;
[0015] The server constructs an initial feature pool based on the feature information and the corresponding feature values; each feature value in the initial feature pool corresponds to a feature information;
[0016] The server matches the initial feature pool with the metadata information to obtain target feature values that match each feature information;
[0017] The server combines the feature information and the corresponding target feature values to form a target feature pool.
[0018] Optionally, the server constructs a primary population based on the feature information and the corresponding feature value, including:
[0019] The server constructs an initial population based on the target feature pool.
[0020] Optionally, the fitness function is the ratio of the number of newly added test code coverage lines after each round of iteration of the genetic algorithm to the number of uncovered code lines after the previous round of iteration.
[0021] Optionally, the fitness function is:
[0022]
[0023] f(x) represents the fitness value of the offspring population generated during the xth iteration, N(a) represents the number of lines in the code line set a, and N(b) represents the number of lines in the code line set b, where a = {M x -[M x ∩(M1∪M2∪M3∪…M x-1 )]}, b={SUM-(M1∪M2∪M3∪…M x-1 )};
[0024] M x It represents the set of code lines covered after the xth iteration, and SUM represents the set of code lines of the object to be tested.
[0025] Optionally, the step of iterating based on the offspring population evolution until a preset convergence condition is met and outputting a target test case set includes:
[0026] The server obtains the initial test case set corresponding to the offspring population generated during each round of iteration;
[0027] The server combines all the initial test case sets into a target test case set and outputs it.
[0028] Optionally, the iterative evolution based on the offspring population includes:
[0029] The server evaluates the offspring population based on the fitness function, and retains individuals in the offspring population whose fitness values are greater than a first preset threshold;
[0030] The server performs crossover and mutation operations based on the individuals retained in the offspring population to obtain the parent individuals for the next iteration;
[0031] The server iterates based on the parent individual evolution.
[0032] Optionally, the server constructs a primary population based on the target feature pool, including:
[0033] The server determines whether the target feature pool meets the preset conditions;
[0034] If satisfied, the server constructs an initial population based on the target feature pool.
[0035] Optionally, the preset condition is that the amount of feature information in the target feature pool is greater than a second preset threshold, and the amount of target feature values corresponding to each feature information is greater than a third preset threshold.
[0036] Optionally, the method further comprises:
[0037] The server calls the automated test execution function, performs tests based on the target test case set, and stores the test results in the graph database.
[0038] The present invention also provides a test case generation system based on a genetic algorithm, which is used to implement the test case generation method based on a genetic algorithm, and the system comprises:
[0039] The seed test case acquisition module, the server acquires the object to be tested, and obtains the seed test case from the object to be tested; the seed test case contains feature information and corresponding feature values;
[0040] A primary population construction module, in which the server constructs a primary population based on the characteristic information and the corresponding characteristic values;
[0041] The first generation evolution module, the server constructs the fitness function of the genetic algorithm, and uses the first generation population as the input of the genetic algorithm to obtain the offspring population;
[0042] The evolutionary iteration module, the server evolves and iterates based on the offspring population until the preset convergence conditions are met, and outputs the target test case set.
[0043] The present invention also provides a test case generation device based on a genetic algorithm, comprising:
[0044] processor;
[0045] a memory in which an executable program of the processor is stored;
[0046] The processor is configured to execute any one of the steps of the above-mentioned test case generation method based on genetic algorithm by executing the executable program.
[0047] The present invention also provides a computer-readable storage medium for storing a program, wherein when the program is executed by a processor, the program implements the steps of any one of the above-mentioned test case generation methods based on genetic algorithms.
[0048] Compared with the prior art, the present invention has the following advantages and outstanding effects:
[0049] The test case generation method, system, device and medium based on genetic algorithm provided by the present invention obtain feature information and corresponding feature values based on seed test cases obtained from the object to be tested, and use them to cover more comprehensive test scenarios, thereby improving the richness of test cases in the initial generation population, thereby achieving more comprehensive scenarios coverage with fewer test cases, which is beneficial to improving test efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0050] Other features, objectives and advantages of the present invention will become more apparent from a reading of the detailed description of non-limiting embodiments made with reference to the following accompanying drawings.
[0051] Figure 1 A schematic diagram of a test case generation method based on a genetic algorithm disclosed in an embodiment of the present invention;
[0052] Figure 2 A flowchart of step S120 in a test case generation method based on a genetic algorithm disclosed in an embodiment of the present invention;
[0053] Figure 3A flowchart of step S140 in a test case generation method based on a genetic algorithm disclosed in an embodiment of the present invention;
[0054] Figure 4 A schematic diagram of a test case generation method based on a genetic algorithm disclosed in another embodiment of the present invention;
[0055] Figure 5 A schematic diagram of the structure of a test case generation system based on a genetic algorithm disclosed in an embodiment of the present invention;
[0056] Figure 6 A schematic diagram of the structure of an initial population building module in a test case generation system based on a genetic algorithm disclosed in an embodiment of the present invention;
[0057] Figure 7 A schematic diagram of the structure of an evolutionary iteration module in a test case generation system based on a genetic algorithm disclosed in an embodiment of the present invention;
[0058] Figure 8 A schematic diagram of the structure of a test case generation system based on a genetic algorithm disclosed in another embodiment of the present invention;
[0059] Fig. 9 A schematic diagram of the structure of a test case generation device based on a genetic algorithm disclosed in an embodiment of the present invention;
[0060] Fig.10 A schematic diagram of the structure of a computer-readable storage medium disclosed in one embodiment of the present invention. DETAILED DESCRIPTION
[0061] Example embodiments will now be described more fully with reference to the accompanying drawings. However, example embodiments can be implemented in a variety of forms and should not be construed as limited to the embodiments set forth herein. Rather, these embodiments are provided so that the present invention will be comprehensive and complete and fully convey the concepts of the example embodiments to those skilled in the art. The same reference numerals in the figures represent the same or similar structures, and thus their repeated description will be omitted.
[0062] like Figure 1 As shown, an embodiment of the present invention discloses a test case generation method based on a genetic algorithm, the method comprising the following steps:
[0063] S110, the server obtains the object to be tested, and obtains a seed test case from the object to be tested. The seed test case contains feature information and corresponding feature values. Specifically, in this embodiment, this step includes:
[0064] S111, the server parses the object to be tested to obtain metadata information. The metadata information includes test samples. And
[0065] S112, the server traverses the metadata information to obtain test samples, and uses the test samples as seed test cases.
[0066] Specifically, the object to be tested is the code to be tested, such as an interface, and the code contains test samples such as unit test samples or test samples provided by annotations. The code is composed of multiple structures, and the object to be tested is parsed, such as extracting the structure in the code, and automatically generating metadata information corresponding to the object to be tested. In addition to test samples, the metadata information may also include the method of the object to be tested, method input parameters, input parameter types, return parameters, and return parameter types.
[0067] S120, the server constructs the primary population based on the above feature information and the corresponding feature values. Figure 2 As shown, the step includes:
[0068] S121, the server extracts feature information from the seed test case and feature values corresponding to the feature information.
[0069] S122, the server constructs an initial feature pool based on the feature information and the corresponding feature values. Each feature value in the initial feature pool corresponds to a feature information.
[0070] S123, the server matches the initial feature pool with the metadata information to obtain target feature values that match each feature information.
[0071] S124: The server combines the above feature information and the corresponding target feature values to form a target feature pool.
[0072] S125, the server determines whether the target feature pool meets the preset conditions.
[0073] If satisfied, execute step S126: construct an initial population based on the target feature pool.
[0074] If not, execute step S127: recombine and filter all feature information in the target feature pool according to the corresponding target feature values based on Cartesian product to obtain a new use case set, and use the new use case set as the initial population.
[0075] Specifically, in this embodiment, all feature values corresponding to each feature information form a feature dictionary of the feature information, and each feature information and its corresponding feature dictionary form a feature library, and the collection of all feature libraries is the initial feature pool. The feature dictionary can cover a variety of test scenarios, such as normal, abnormal, and security test scenarios. This improves the richness of test cases in the initial population, which is conducive to covering more comprehensive scenarios with fewer test cases.
[0076] Since one feature information in the initial feature pool may correspond to multiple feature values, and the metadata information also contains different feature information and the feature values corresponding thereto, each feature information in the metadata information may correspond to one or two feature values. Then, step S123 is to obtain the common intersection of the feature information of the initial feature pool and the metadata information, and the intersection of the feature values corresponding to the obtained common feature information.
[0077] In this embodiment, illustratively, the preset condition may be that the number of feature information in the target feature pool is greater than the second preset threshold, and the number of target feature values corresponding to each feature information is greater than the third preset threshold. The present application is not limited thereto. The second preset threshold may be in the range of 10-100, and the third preset threshold may be in the range of 100-200.
[0078] In step S126, a target feature value is randomly selected from the target feature pool according to the name of each feature information, and combined to generate a primary population. Each combination of feature information and a target feature value in the primary population can be called an individual, and the number of individuals in the primary population can range from 1000 to 3000.
[0079] In step S127, Cartesian products are performed on the feature information and the target feature value to generate some new feature information and new target feature values, which can improve the richness of the test cases of the initial population, help to cover more comprehensive scenarios with fewer test cases, and help improve test efficiency. Among them, filtering can be based on a preset rule as a filtering condition. This application does not limit this.
[0080] S130, the server constructs a fitness function of the genetic algorithm, and uses the initial population as the input of the genetic algorithm to obtain a progeny population. In the present application, the fitness function is the ratio of the number of new test code coverage lines after each iteration of the genetic algorithm to the number of uncovered code lines after the previous iteration. Exemplarily, the fitness function is:
[0081]
[0082] f(x) represents the fitness value of the offspring population generated during the xth iteration, N(a) represents the number of lines in the code line set a, and N(b) represents the number of lines in the code line set b, where a = {M x -[M x ∩(M1∪M2∪M3∪…M x-1 )]}, b={SUM-(M1∪M2∪M3∪…M x-1 )};
[0083] M x represents the set of lines of code covered after the xth iteration, SUM represents the set of lines of code of the object to be tested. x represents the current evolutionary generation, (M1∪M2∪M3∪…M x-1 ) represents the union of the set of covered code lines after each iteration from the first iteration to the (x-1)th iteration, which helps to exclude repeated covered code lines. [M x ∩(M1∪M2∪M3∪…M x-1 )] represents the intersection of the set of covered code lines after the xth iteration and the above union result, which can exclude repeated covered code lines and make the fitness value calculation more accurate.
[0084] {M x -[M x ∩(M1∪M2∪M3∪…M x-1 )]} represents the set of code lines remaining after removing the above intersection result code lines from the set of covered code lines after the xth iteration.
[0085] {SUM-(M1∪M2∪M3∪…M x-1 )} represents the total number of code lines of the object under test combined with the remaining code lines after removing the above union result.
[0086] This embodiment can use the evolution process of the existing genetic algorithm. After the genetic algorithm is calculated, the offspring population is obtained. Subsequently, the individuals in the offspring population need to be evaluated in order to determine the parent individuals for subsequent iterations.
[0087] S140, the server iterates based on the above-mentioned offspring population evolution until the preset convergence condition is met, and outputs the target test case set. Figure 3 As shown, this step may include:
[0088] S141, the server evaluates the above-mentioned offspring population based on the above-mentioned fitness function, and retains the individuals in the above-mentioned offspring population whose fitness values are greater than the first preset threshold. That is, the fitness values of all individuals in the offspring population are calculated. The larger the fitness value, the higher the contribution of the test cases of the population to the code coverage rate and the higher the contribution to the test effect.
[0089] S142, the server performs crossover and mutation operations based on the individuals retained in the offspring population to obtain parent individuals for the next round of iteration.
[0090] S143, the server iterates based on the above parent individual evolution.
[0091] S144, obtaining an initial test case set corresponding to the offspring population generated in each round of iteration.
[0092] S145, after reaching the preset convergence condition, all the initial test case sets are combined to form a target test case set and output.
[0093] Specifically, the present application collects the test case sets obtained after each round of iteration and finally combines them to form the final target test case set. This helps to enrich the test case set, test more usage scenarios, and help to achieve more comprehensive usage scenarios with fewer test cases.
[0094] For example, in this embodiment, the preset convergence condition is that all individual fitness values in the obtained offspring population are greater than or equal to a fourth preset threshold, the first preset threshold may be 80%, and the fourth preset threshold may be 95%. This application does not limit this.
[0095] In another embodiment of the present application, another test case generation method based on genetic algorithm is disclosed. This method is based on the above embodiment. Figure 4 As shown, the steps include:
[0096] S150, the server calls the automated test execution function, performs a test based on the target test case set, and stores the test results in the graph database. The automated test execution function can be preset, and the present embodiment stores the test results in the graph database, which is conducive to visually viewing the test effects of each test case and improving the distribution of test cases.
[0097] It should be noted that all the above embodiments disclosed in this application can be freely combined, and the technical solutions obtained after the combination are also within the protection scope of this application.
[0098] like Figure 5 As shown, an embodiment of the present invention further discloses a test case generation system 5 based on a genetic algorithm, the system comprising:
[0099] In the seed test case acquisition module 51, the server acquires the object to be tested and obtains a seed test case from the object to be tested. The seed test case includes feature information and corresponding feature values.
[0100] The initial population construction module 52, the server constructs the initial population based on the feature information and the corresponding feature values.
[0101] In the primary evolution module 53, the server constructs a fitness function of a genetic algorithm and uses the primary population as an input of the genetic algorithm to obtain a descendant population.
[0102] The evolutionary iteration module 54, the server evolves and iterates based on the offspring population until a preset convergence condition is met, and outputs a target test case set.
[0103] In one embodiment of the present application, Figure 6 As shown, the above-mentioned primary population construction module 52 includes:
[0104] The feature extraction unit 521, the server extracts the feature information in the seed test case and the feature value corresponding to the feature information.
[0105] Initial feature pool construction unit 522: The server constructs an initial feature pool based on the feature information and the corresponding feature values. Each feature value in the initial feature pool corresponds to a feature information.
[0106] The target feature value acquisition unit 523, the server matches the initial feature pool with the metadata information to obtain the target feature value that matches each feature information.
[0107] The target feature pool construction unit 524, the server combines the above feature information and the corresponding target feature values to form a target feature pool.
[0108] The feature pool condition judgment unit 525, the server judges whether the above-mentioned target feature pool meets the preset conditions.
[0109] The first construction unit 526 of the primary population, when the target feature pool meets the preset conditions, the server constructs the primary population based on the target feature pool.
[0110] The first-generation population second construction unit 527, when the target feature pool does not meet the preset conditions, the server recombine and filter all feature information in the above target feature pool according to the corresponding target feature values based on the Cartesian product to obtain a new use case set, and use the above new use case set as the first-generation population.
[0111] In one embodiment of the present application, Figure 7 As shown, the above-mentioned evolutionary iteration module 54 includes:
[0112] The offspring population evaluation unit 541, the server evaluates the offspring population based on the fitness function, and retains the individuals in the offspring population whose fitness values are greater than the first preset threshold. That is, the fitness values of all individuals in the offspring population are calculated. The larger the fitness value, the higher the contribution of the test cases of the population to the code coverage rate and the higher the contribution to the test effect.
[0113] The crossover and mutation unit 542, the server performs a crossover operation and a mutation operation based on the individuals retained in the offspring population to obtain the parent individuals for the next round of iteration.
[0114] Iteration unit 543, the server iterates based on the above parent individual evolution.
[0115] The test case collection unit 544 obtains the initial test case set corresponding to the offspring population generated in each round of iteration.
[0116] The convergence unit 545 combines all the initial test case sets into a target test case set and outputs it after reaching a preset convergence condition.
[0117] like Figure 8 As shown, an embodiment of the present invention further discloses a test case generation system 8 based on a genetic algorithm. Based on the above embodiment, the system further includes:
[0118] The test execution module 55, the server calls the automated test execution function, performs the test based on the target test case set, and stores the test results in the graph database.
[0119] It can be understood that the test case generation system based on genetic algorithm of the present invention also includes other existing functional modules that support the operation of the test case generation system based on genetic algorithm. Figure 5 The test case generation system based on genetic algorithm shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0120] The test case generation system based on genetic algorithm in this embodiment is used to implement the above-mentioned test case generation method based on genetic algorithm. Therefore, the specific implementation steps of the test case generation system based on genetic algorithm can refer to the above-mentioned description of the test case generation method based on genetic algorithm, which will not be repeated here.
[0121] An embodiment of the present invention also discloses a test case generation device based on a genetic algorithm, comprising a processor and a memory, wherein the memory stores an executable program of the processor; the processor is configured to execute the steps in the above-mentioned test case generation method based on the genetic algorithm by executing the executable program. Fig. 9Schematic diagram of the structure of the test case generation device based on genetic algorithm disclosed in the present invention. Fig. 9 The electronic device 600 according to this embodiment of the present invention is described. Fig. 9 The electronic device 600 shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0122] like Fig. 9 As shown, the electronic device 600 is in the form of a general computing device. The components of the electronic device 600 may include, but are not limited to: at least one processing unit 610, at least one storage unit 620, a bus 630 connecting different platform components (including the storage unit 620 and the processing unit 610), a display unit 640, etc.
[0123] The storage unit stores program codes, which can be executed by the processing unit 610, so that the processing unit 610 executes the steps of various exemplary embodiments of the present invention described in the above-mentioned test case generation method based on genetic algorithm. For example, the processing unit 610 can execute the following steps: Figure 1 Follow the steps shown in .
[0124] The storage unit 620 may include a readable medium in the form of a volatile storage unit, such as a random access memory unit (RAM) 6201 and / or a cache memory unit 6202 , and may further include a read-only memory unit (ROM) 6203 .
[0125] The storage unit 620 may also include a program / utility 6204 having a set (at least one) of program modules 6205, such program modules 6205 including but not limited to: an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment.
[0126] Bus 630 may represent one or more of several types of bus structures, including a memory unit bus or memory unit controller, a peripheral bus, an accelerated graphics port, a processing unit, or a local bus using any of a variety of bus architectures.
[0127] The electronic device 600 may also communicate with one or more external devices 700 (e.g., keyboards, pointing devices, Bluetooth devices, etc.), may also communicate with one or more devices that enable a user to interact with the electronic device 600, and / or communicate with any device that enables the electronic device 600 to communicate with one or more other computing devices (e.g., routers, modems, etc.). Such communication may be performed via an input / output (I / O) interface 650. Furthermore, the electronic device 600 may also communicate with one or more networks (e.g., a local area network (LAN), a wide area network (WAN), and / or a public network, such as the Internet) via a network adapter 660. The network adapter 660 may communicate with other modules of the electronic device 600 via a bus 630. It should be understood that, although not shown in the figure, other hardware and / or software modules may be used in conjunction with the electronic device 600, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage platforms, etc.
[0128] The present invention also discloses a computer-readable storage medium for storing a program, which implements the steps in the test case generation method based on a genetic algorithm when executed. In some possible implementations, various aspects of the present invention can also be implemented in the form of a program product, which includes a program code. When the program product is run on a terminal device, the program code is used to enable the terminal device to execute the steps of various exemplary implementations of the present invention described in the test case generation method based on a genetic algorithm in the present specification.
[0129] As shown above, when the program of the computer-readable storage medium of this embodiment is executed, feature information and corresponding feature values are obtained based on the seed test cases obtained from the object to be tested, and the feature information and corresponding feature values are used to cover a more comprehensive test scenario, thereby improving the richness of test cases of the initial generation population, thereby achieving coverage of more comprehensive scenarios with fewer test cases, which is conducive to improving test efficiency.
[0130] Fig.10 Schematic diagram of the structure of the computer-readable storage medium of the present invention. Fig.10 As shown, a program product 800 for implementing the above method according to an embodiment of the present invention is described, which can adopt a portable compact disk read-only memory (CD-ROM) and include program code, and can be run on a terminal device, such as a personal computer. However, the program product of the present invention is not limited thereto. In this document, a readable storage medium can be any tangible medium containing or storing a program, which can be used by or in combination with an instruction execution system, an apparatus or a device.
[0131] The program product may use any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. The readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of readable storage media (a non-exhaustive list) include: an electrical connection with one or more wires, a portable disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above.
[0132] Computer readable storage media may include data signals propagated in baseband or as part of a carrier wave, wherein readable program codes are carried. Such propagated data signals may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. The readable storage medium may also be any readable medium other than a readable storage medium, which may send, propagate, or transmit a program for use by or in conjunction with an instruction execution system, device, or device. The program codes contained on the readable storage medium may be transmitted using any suitable medium, including but not limited to wireless, wired, optical cable, RF, etc., or any suitable combination thereof.
[0133] Program code for performing the operations of the present invention may be written in any combination of one or more programming languages, including object-oriented programming languages such as Java, C++, etc., and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user computing device, partially on the user device, as a separate software package, partially on the user computing device and partially on a remote computing device, or entirely on a remote computing device or server. In the case of a remote computing device, the remote computing device may be connected to the user computing device through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computing device (e.g., through the Internet using an Internet service provider).
[0134] The test case generation method, system, device and medium based on genetic algorithm provided by the embodiment of the present invention obtain feature information and corresponding feature values from the seed test cases obtained from the object to be tested, and use them to cover a more comprehensive test scenario, thereby improving the richness of the test cases of the initial population, thereby achieving coverage of a more comprehensive scenario with fewer test cases, which is conducive to improving the test efficiency;
[0135] This application uses the ratio of the number of newly added test code coverage lines after each round of iteration to the number of uncovered code lines after the previous round of iteration as a fitness function, which reduces the redundancy of the offspring population use case set and ultimately achieves the goal of covering more comprehensive scenarios with fewer test cases.
[0136] The above contents are further detailed descriptions of the present invention in combination with specific preferred embodiments, and it cannot be determined that the specific implementation of the present invention is limited to these descriptions. For ordinary technicians in the technical field to which the present invention belongs, several simple deductions or substitutions can be made without departing from the concept of the present invention, which should be regarded as falling within the protection scope of the present invention.
Claims
1. A test case generation method based on genetic algorithm, characterized in that: The following steps are involved: The server obtains the object to be tested, and obtains a seed test case from the object to be tested; The seed test case includes feature information and corresponding feature values; The server constructs an initial population based on the feature information and the corresponding feature values; The server constructs a fitness function of the genetic algorithm, and uses the initial population as the input of the genetic algorithm to obtain a descendant population, wherein the fitness function is the ratio of the number of new test code coverage lines after each iteration of the genetic algorithm to the number of uncovered code lines after the previous iteration; The server evaluates the offspring population based on the fitness function, and retains individuals in the offspring population whose fitness values are greater than a first preset threshold; The server performs crossover and mutation operations based on the individuals retained in the offspring population to obtain the parent individuals for the next round of iteration; the server iterates based on the evolution of the parent individuals, and obtains the initial test case set corresponding to the offspring population generated during each round of iteration until the preset convergence conditions are met. The server combines all the initial test case sets to form a target test case set and outputs it.
2. The test case generation method based on genetic algorithm as claimed in claim 1, characterized in that: The step of obtaining a seed test case from the object to be tested includes: The server parses the object to be tested to obtain metadata information; the metadata information includes test samples; The server traverses the metadata information to obtain the test sample, and uses the test sample as a seed test case.
3. The test case generation method based on genetic algorithm as claimed in claim 2, characterized in that: The server constructs a primary population based on the feature information and the corresponding feature value, including: The server extracts feature information from the seed test case and feature values corresponding to the feature information; The server constructs an initial feature pool based on the feature information and the corresponding feature values; each feature value in the initial feature pool corresponds to a feature information; The server matches the initial feature pool with the metadata information to obtain target feature values that match each feature information; The server combines the feature information and the corresponding target feature values to form a target feature pool.
4. The test case generation method based on genetic algorithm as claimed in claim 3, characterized in that: The server constructs a primary population based on the feature information and the corresponding feature value, including: The server constructs an initial population based on the target feature pool.
5. The test case generation method based on genetic algorithm as claimed in claim 1, characterized in that: The fitness function is: f(x) represents the fitness value of the offspring population generated during the xth iteration, N(a) represents the number of lines in the code line set a, and N(b) represents the number of lines in the code line set b, where a = {M x -[M x ∩(M1∪M2∪M3∪…M x-1 )]}, b={SUM-(M1∪M2∪M3∪…M x-1 )}; M x It represents the set of code lines covered after the xth iteration, and SUM represents the set of code lines of the object to be tested.
6. The test case generation method based on genetic algorithm as claimed in claim 4, characterized in that: The server constructs a primary population based on the target feature pool, including: The server determines whether the target feature pool meets the preset conditions; If satisfied, the server constructs an initial population based on the target feature pool.
7. The test case generation method based on genetic algorithm as claimed in claim 6, characterized in that: The preset condition is that the amount of feature information in the target feature pool is greater than a second preset threshold, and the amount of target feature values corresponding to each feature information is greater than a third preset threshold.
8. The test case generation method based on genetic algorithm as claimed in claim 1, characterized in that: The method further comprises: The server calls the automated test execution function, performs tests based on the target test case set, and stores the test results in the graph database.
9. A test case generation system based on genetic algorithm, used to implement the test case generation method based on genetic algorithm as claimed in claim 1, characterized in that: The system comprises: The seed test case acquisition module, the server acquires the object to be tested, and obtains the seed test case from the object to be tested; the seed test case contains feature information and corresponding feature values; A primary population construction module, in which the server constructs a primary population based on the characteristic information and the corresponding characteristic values; In the primary evolution module, the server constructs a fitness function of the genetic algorithm and uses the primary population as the input of the genetic algorithm to obtain a progeny population. The fitness function is the ratio of the number of new test code coverage lines after each iteration of the genetic algorithm to the number of uncovered code lines after the previous iteration. In the evolutionary iteration module, the server evaluates the offspring population based on the fitness function, and retains the individuals in the offspring population whose fitness values are greater than a first preset threshold value; the server performs crossover and mutation operations based on the individuals retained in the offspring population to obtain the parent individuals for the next round of iteration; the server performs evolutionary iteration based on the parent individuals, and the server obtains the initial test case set corresponding to the offspring population generated during each round of iteration until the preset convergence condition is met, and the server combines all the initial test case sets to form a target test case set and outputs it.
10. A test case generation device based on genetic algorithm, characterized in that: include: processor; a memory in which an executable program of the processor is stored; Wherein, the processor is configured to execute the steps of the test case generation method based on genetic algorithm in any one of claims 1 to 8 by executing the executable program.
11. A computer-readable storage medium for storing a program, characterized in that: When the program is executed by a processor, the steps of the test case generation method based on genetic algorithm described in any one of claims 1 to 8 are implemented.
Citation Information
Patent Citations
Test case obtaining method, system and device and computer readable storage medium
CN112579462A