Test case generation method and related device

Through the test case generation method based on group intelligence algorithm, the weight of various code coverage in the fitness function is dynamically adjusted, which solves the problem of insufficient coverage of the mini program's functional test and achieves more efficient testing results.

CN119512958APending Publication Date: 2025-02-25HUNAN HAPPLY SUNSHINE INTERACTIVE ENTERTAINMENT MEDIA CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411649183.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-18
Publication Date
2025-02-25

AI Technical Summary

Technical Problem

The existing technology is difficult to ensure the coverage of mini program functional tests, and traditional testing methods cannot effectively cover the operating environment of mini program in host applications and mobile phone systems.

Method used

The test case generation method based on group intelligence algorithm is adopted, and the test case optimization process is performed iteratively, and the weight of the code coverage in the fitness function is dynamically adjusted to optimize the code coverage of the test case.

Benefits of technology

It improves the overall code coverage of test cases, enhances the test effect, and ensures the comprehensiveness and accuracy of mini program functional testing.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119512958A_ABST
    Figure CN119512958A_ABST
Patent Text Reader

Abstract

The invention discloses a test case generation method and a related device, and relates to the technical field of software test.The method comprises the steps that a fitness function is obtained, and the fitness of each meta-case individual is calculated according to the fitness function; whether a preset termination condition is met or not is judged, the target meta-use case individuals are meta-use case individuals meeting a preset fitness condition, and the fitness condition includes that the fitness is larger than a preset fitness threshold value. If the termination condition is met, all target meta-case individuals are obtained from the meta-case individual population, and a test case set is obtained. Wherein the fitness function is a weighted summation function of multiple code coverage rates, and the weight of any code coverage rate is related to the comprehensive coverage rate of the meta-use case individuals in the last iteration. According to the scheme, by dynamically adjusting the weight of the coverage rate of each code in the fitness function, the influence degree of the coverage rate of each code on the fitness is adjusted, the coverage rate of each code of the test case is improved, and the optimization effect of the test case is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of software testing technology, and in particular to a test case generation method and related devices. Background Art

[0002] Currently, mini-program applications are increasingly widely used. Mini-programs are "light applications" that run on host applications (such as WeChat, Douyin, Baidu, etc.). Since mini-programs are highly dependent on the host application environment, the code logic of mini-programs runs in the container environment provided by the host application and cannot directly control the host application and mobile phone system. This makes it impossible to ensure that the coverage of mini-program functions tested by traditional testing methods meets the coverage requirements. Summary of the invention

[0003] In view of the above problems, the present application provides a method for generating a test case and a related device to achieve the purpose of improving the test case coverage. The specific scheme is as follows:

[0004] The first aspect of the present application provides a method for generating a test case, including an iterative test case optimization process, wherein the tth iteration includes:

[0005] Based on a preset swarm intelligence algorithm, a meta-case individual population is updated, wherein the meta-case individual population includes a plurality of meta-case individuals, and a meta-case individual includes parameter values ​​of a set of input parameters of a test case;

[0006] For each meta-use case individual in the meta-use case individual population, a fitness function is obtained, wherein the fitness function is a weighted sum function of multiple code coverage rates, wherein the weight of any code coverage rate is related to the comprehensive coverage rate of the meta-use case individual, and the comprehensive coverage rate is the arithmetic sum of multiple code coverage rates;

[0007] Calculating the fitness of each meta-case individual according to the fitness function;

[0008] Determining whether a preset termination condition is met, wherein the termination condition includes that the number of target meta-use case individuals is greater than a preset use case number threshold, and the target meta-use case individual is a meta-use case individual that satisfies a preset fitness condition, wherein the fitness condition includes that the fitness is greater than a preset fitness threshold;

[0009] If the termination condition is met, all target meta-case individuals are obtained from the meta-case individual population to obtain a test case set;

[0010] If the termination condition is not met, execute the t+1th iteration;

[0011] The t is greater than 1.

[0012] In one possible implementation, the first iteration includes:

[0013] Acquire test information, where the test information includes input parameter information of the test case, where the input parameter information includes the dimension of the input parameter and the value range of each input parameter;

[0014] Based on the input parameter information of the test case, a preset target number of initial meta-case individuals are randomly generated to form an initial meta-case individual population;

[0015] For each meta-use case individual in the initial meta-use case individual population, an initial fitness function is obtained, and the weights of each code coverage in the initial fitness function are corresponding initial weights.

[0016] In a possible implementation, in the tth iteration, based on a preset swarm intelligence algorithm, the meta-use case individual population is updated, including:

[0017] Determine a target search mode based on a loudness value, wherein the loudness value is inversely correlated with the number of iterations, and the target search mode is one of a global search and a local search;

[0018] For each meta-use case individual, the position of the meta-use case individual is updated in the target search manner.

[0019] In a possible implementation, in the tth iteration, determining the target search mode based on the loudness value includes:

[0020] Obtaining the loudness value of the t-th iteration, where the loudness value of the t-th iteration is the product of a preset attenuation coefficient and the loudness value of the t-1-th iteration, and the attenuation coefficient is less than 1;

[0021] If the loudness value of the t-th iteration is not less than a preset loudness threshold, determining that the search mode is the global search;

[0022] If the loudness value of the t-th iteration is less than the loudness threshold, the search mode is determined to be the local search.

[0023] In a possible implementation, the code coverage includes statement coverage, branch coverage, and path coverage. In the t-th iteration, obtaining the fitness function of the meta-use case individual includes:

[0024] Calculate the arithmetic sum of the statement coverage, branch coverage and path coverage of the individual meta-use cases to obtain the comprehensive coverage;

[0025] If the comprehensive coverage of the individual meta-use cases is within a preset numerical range, the initial weights corresponding to the various code coverages are used as the weights of the various code coverages, the minimum value of the first numerical range is a preset first numerical value, and the maximum value of the first numerical range is a preset second numerical value;

[0026] If the comprehensive coverage of the individual meta-use cases is less than the first preset value, the initial weight of the statement coverage is increased by a preset first ratio to obtain a first statement dynamic weight, and the initial weights of the branch coverage and the path coverage are respectively reduced by a preset second ratio to obtain a first branch dynamic weight and a first path dynamic weight; the first statement dynamic weight, the first branch dynamic weight and the first path dynamic weight are respectively used as the weights of the statement coverage, the branch coverage and the path coverage, to construct the fitness function;

[0027] If the comprehensive coverage rate of the meta-use case individuals is greater than the second value, the initial weight of the statement coverage rate is reduced by a preset third ratio to obtain a second statement dynamic weight, and the initial weights of the branch coverage rate and the path coverage rate are respectively increased by a preset fourth ratio to obtain a second branch dynamic weight and a second path dynamic weight; the second statement dynamic weight, the second branch dynamic weight and the second path dynamic weight are used as the weights of the statement coverage rate, the branch coverage rate and the path coverage rate, respectively, to construct the fitness function.

[0028] In a possible implementation, the termination condition also includes: the number of iterations is greater than a preset iteration number threshold, and the fitness condition also includes that the coverage of each code item is greater than the corresponding coverage threshold.

[0029] A second aspect of the present application provides a test case generation device, the test case generation device is used to iteratively execute a test case optimization process, the test case generation device includes a t-th iteration unit for executing a t-th iteration, the t-th iteration unit includes:

[0030] A test case updating unit, configured to update a meta-case individual population based on a preset swarm intelligence algorithm, wherein the meta-case individual population includes a plurality of meta-case individuals, and a meta-case individual includes parameter values ​​of a set of input parameters of a test case;

[0031] A fitness function construction unit, used for obtaining a fitness function for each meta-use case individual in the meta-use case individual population, wherein the fitness function is a weighted sum function of multiple code coverage rates, wherein the weight of any code coverage rate is related to the comprehensive coverage rate of the meta-use case individual, and the comprehensive coverage rate is the arithmetic sum of multiple code coverage rates;

[0032] A fitness calculation unit, used to calculate the fitness of each meta-case individual according to the fitness function;

[0033] A termination condition judgment unit, used to judge whether a preset termination condition is met, wherein the termination condition includes that the number of target meta-use case individuals is greater than a preset use case number threshold, and the target meta-use case individual is a meta-use case individual that meets a preset fitness condition, wherein the fitness condition includes that the fitness is greater than a preset fitness threshold;

[0034] An iteration result generating unit, configured to obtain all target meta-case individuals from the meta-case individual population to obtain a test case set if the termination condition is met; and to execute the t+1th iteration if the termination condition is not met;

[0035] The t is greater than 1.

[0036] A third aspect of the present application provides a computer program product, comprising computer-readable instructions. When the computer-readable instructions are executed on an electronic device, the electronic device implements the method for generating test cases of the above-mentioned first aspect or any implementation manner of the first aspect.

[0037] A fourth aspect of the present application provides an electronic device, comprising at least one processor and a memory connected to the processor, wherein:

[0038] The memory is used to store computer programs;

[0039] The processor is used to execute the computer program so that the electronic device can implement the method for generating a test case of the above-mentioned first aspect or any implementation manner of the first aspect.

[0040] A fifth aspect of the present application provides a computer storage medium, which carries one or more computer programs. When the one or more computer programs are executed by an electronic device, the electronic device can generate a test case according to the first aspect or any implementation method of the first aspect.

[0041] By means of the above technical solution, the present application provides a method for generating a test case and a related device. The method includes an iterative test case optimization process. The tth iteration (t is greater than 1) includes updating the meta-case individual population based on a preset swarm intelligence algorithm. The meta-case individual population includes multiple meta-case individuals. A meta-case individual includes a set of parameter values ​​of input parameters of the test case. For each meta-case individual in the meta-case individual population, a fitness function is obtained, and the fitness of each meta-case individual is calculated according to the fitness function. It is judged whether the preset termination condition is met. The termination condition includes that the number of target meta-case individuals is greater than the preset case number threshold. The target meta-case individual is a meta-case individual that meets the preset fitness condition. The fitness condition includes that the fitness is greater than the preset fitness threshold. If the termination condition is met, all target meta-case individuals are obtained from the meta-case individual population to obtain a test case set. If the termination condition is not met, the t+1th iteration is executed. The fitness function is a weighted sum function of multiple code coverage rates, where the weight of any code coverage rate is related to the comprehensive coverage rate of the meta-case individual, and the comprehensive coverage rate is the arithmetic sum of multiple code coverage rates. Therefore, this solution dynamically adjusts the weights of each code coverage rate in the fitness function to adjust the degree of influence of each code coverage rate on the fitness, thereby improving the fitness function's representation of the optimization objectives of each code coverage rate of the test case, so that in the iterative process, the code coverage rates of each test case are improved in a targeted manner, thereby improving the overall code coverage rate of the test case and improving the optimization effect of the test case. BRIEF DESCRIPTION OF THE DRAWINGS

[0042] The above and other features, advantages and aspects of the embodiments of the present disclosure will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. Throughout the accompanying drawings, the same or similar reference numerals represent the same or similar elements. It should be understood that the drawings are schematic and the originals and elements are not necessarily drawn to scale.

[0043] Figure 1 A structural diagram of a system architecture provided for this application;

[0044] Figure 2 An optional hardware structure diagram of the terminal 100 is shown;

[0045] Figure 3 An optional hardware structure diagram of the server 200 is shown;

[0046] Figure 4 A flowchart of a method for generating a test case provided in an embodiment of the present application;

[0047] Figure 5 A flowchart of a specific implementation method of a test case generation method provided in an embodiment of the present application;

[0048] Figure 6 A schematic diagram of the structure of a test case generation device provided in an embodiment of the present application;

[0049] Figure 7 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0050] The following describes the embodiments of the present application in conjunction with the drawings in the embodiments of the present application. The terms used in the implementation method section of the present application are only used to explain the specific embodiments of the present application, and are not intended to limit the present application.

[0051] The embodiments of the present application are described below in conjunction with the accompanying drawings. Those skilled in the art will appreciate that, with the development of technology and the emergence of new scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.

[0052] The terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and need not be used to describe a specific order or sequential order. It should be understood that the terms used in this way can be interchangeable under appropriate circumstances, which is only to describe the distinction mode adopted by the objects of the same attributes when describing in the embodiments of the present application. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, so that the process, method, system, product or equipment comprising a series of units need not be limited to those units, but may include other units that are not clearly listed or inherent to these processes, methods, products or equipment.

[0053] At present, the testing methods for mini programs mainly include the following:

[0054] 1. Use traditional manual testing to test the UI function of the mini program. This method is relatively inefficient and belongs to black box testing. It cannot test the internal code logic of the mini program, resulting in inadequate testing.

[0055] 2. Use a packet capture tool to capture the packet test interface of the mini program. However, due to network security restrictions on the host application, it is often impossible to capture the packet to view the request.

[0056] 3. The mini-program compliance sandbox testing method is based on JS Hook injection debugging. However, there may be differences between different mini-program frameworks and versions, resulting in injection code incompatibility and application security issues.

[0057] In order to solve the above problems, the present application provides a test case generation method, which intelligently generates test cases based on a meta-heuristic algorithm, that is, a test case set is obtained by real-time iterative calculation of a constructed code coverage fitness function, and the overall code coverage of the test cases is improved by dynamically adjusting the weights of various types of code coverage in the fitness function during the iteration process of the intelligent algorithm.

[0058] This application can be applied, but not limited to, to applications with software testing functions or cloud services provided by cloud-side servers, etc., with the aim of increasing the coverage of test cases and further improving the test results. Optionally, a test case generation method provided in this application can be applied to a test case generation system. Next, the system architecture of an optional test case generation system for implementing a test case generation method provided in this application is introduced:

[0059] See also Figure 1 , Figure 1 A schematic diagram of a system architecture is shown. The system may include a terminal 100 and a server 200. The server 200 may include one or more servers ( Figure 1 In the example, a server is included, and the server 200 can provide the method provided in the embodiment of the present application for one or more terminals.

[0060] Among them, a test case generation application can be installed on the terminal 100. The above application and web page can provide an interface. The terminal 100 can receive relevant parameters entered by the user on the test case generation interface and send the above parameters to the server 200. The server 200 can obtain processing results based on the received parameters and return the processing results to the terminal 100.

[0061] It should be understood that in some optional implementations, the terminal 100 can also complete the action of obtaining the processing result based on the received parameters by itself without the cooperation of the server, and the embodiments of the present application are not limited to this.

[0062] Next describe Figure 1 The product form of the mid-terminal 100;

[0063] The terminal 100 in the embodiment of the present application can be a mobile phone, a tablet computer, a wearable device, a vehicle-mounted device, an augmented reality (AR) / virtual reality (VR) device, a laptop computer, an ultra-mobile personal computer (UMPC), a netbook, a personal digital assistant (PDA), etc., and the embodiment of the present application does not impose any limitation on this.

[0064] Figure 2 An optional hardware structure diagram of the terminal 100 is shown.

[0065] refer to Figure 2 As shown, the terminal 100 may include a radio frequency unit 110, a memory 120, an input unit 130, a display unit 140, a camera 150 (optional), an audio circuit 160 (optional), a speaker 161 (optional), a microphone 162 (optional), an earphone jack 163 (optional), a processor 170, an external interface 180, a power supply 190 and other components. Those skilled in the art will appreciate that Figure 2 These are merely examples of terminals or multi-function devices and do not constitute limitations on the terminals or multi-function devices, which may include more or fewer components than those shown in the figures, or combinations of certain components, or different components.

[0066] The input unit 130 can be used to receive input digital or character information, and generate key signal input related to the user settings and function control of the portable multifunctional device. Specifically, the input unit 130 may include a touch screen 131 (optional) and / or other input devices 132. The touch screen 131 can collect the user's touch operations on or near it (such as the user's operation on or near the touch screen using any suitable object such as fingers, joints, stylus, etc.), and drive the corresponding connection device according to a pre-set program. The touch screen can detect the user's touch action on the touch screen, convert the touch action into a touch signal and send it to the processor 170, and can receive and execute the command sent by the processor 170; the touch signal at least includes the touch point coordinate information. The touch screen 131 can provide an input interface and an output interface between the terminal 100 and the user. In addition, the touch screen can be implemented using multiple types such as resistive, capacitive, infrared and surface acoustic wave. In addition to the touch screen 131, the input unit 130 can also include other input devices. Specifically, other input devices 132 may include, but are not limited to, one or more of a physical keyboard, function keys (such as a volume control key, a switch key, etc.), a trackball, a mouse, a joystick, and the like.

[0067] Among them, the input device 132 can receive input data and the like.

[0068] The display unit 140 may be used to display information input by the user or provided to the user, various menus of the terminal 100, interactive interfaces, file displays, and / or playback of any multimedia file. In an embodiment of the present application, the display unit 140 may be used to display an interface of the test case generation process, processing results, etc.

[0069] The memory 120 can be used to store instructions and data. The memory 120 can mainly include an instruction storage area and a data storage area. The data storage area can store various data, such as multimedia files, texts, etc.; the instruction storage area can store software units such as operating systems, applications, instructions required for at least one function, or their subsets and extensions. It can also include a non-volatile random access memory; provide the processor 170 with hardware, software and data resources including management of computing and processing equipment, and support control software and applications. It is also used for the storage of multimedia files, and the storage of running programs and applications.

[0070] The processor 170 is the control center of the terminal 100. It uses various interfaces and lines to connect various parts of the entire terminal 100. By running or executing instructions stored in the memory 120 and calling data stored in the memory 120, it executes various functions of the terminal 100 and processes data, thereby controlling the terminal device as a whole. Optionally, the processor 170 may include one or more processing units; preferably, the processor 170 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface and application program, and the modem processor mainly processes wireless communication. It is understandable that the above-mentioned modem processor may not be integrated into the processor 170. In some embodiments, the processor and the memory may be implemented on a single chip, and in some embodiments, they may also be implemented separately on separate chips. The processor 170 may also be used to generate corresponding operation control signals, send them to corresponding components of the computing and processing device, read and process data in the software, especially read and process data and programs in the memory 120, so that each functional module therein performs corresponding functions, thereby controlling the corresponding components to act according to the requirements of the instructions.

[0071] Among them, the memory 120 can be used to store software codes related to the test case generation method, the processor 170 can execute the steps of the test case generation method, and can also schedule other units (such as the above-mentioned input unit 130 and display unit 140) to implement corresponding functions.

[0072] The radio frequency unit 110 (optional) can be used for receiving and sending information or receiving and sending signals during a call, for example, after receiving the downlink information of the base station, it is sent to the processor 170 for processing; in addition, the designed uplink data is sent to the base station. Generally, the RF circuit includes but is not limited to an antenna, at least one amplifier, a transceiver, a coupler, a low noise amplifier (Low Noise Amplifier, LNA), a duplexer, etc. In addition, the radio frequency unit 110 can also communicate with network devices and other devices through wireless communication. The wireless communication can use any communication standard or protocol, including but not limited to Global System of Mobile communication (Global System of Mobile communication, GSM), General Packet Radio Service (General Packet Radio Service, GPRS), Code Division Multiple Access (Code Division Multiple Access, CDMA), Wideband Code Division Multiple Access (Wideband Code Division Multiple Access, WCDMA), Long Term Evolution (Long Term Evolution, LTE), email, Short Messaging Service (SMS), etc.

[0073] In this embodiment of the present application, the RF unit 110 can send data to the server 200 and receive processing results sent by the server 200.

[0074] It should be understood that the radio frequency unit 110 is optional and can be replaced by other communication interfaces, such as a network port.

[0075] The terminal 100 also includes a power supply 190 (such as a battery) for supplying power to various components. Preferably, the power supply can be logically connected to the processor 170 through a power management system, so that the power management system can manage functions such as charging, discharging, and power consumption.

[0076] The terminal 100 further includes an external interface 180 , which may be a standard Micro USB interface or a multi-pin connector, and may be used to connect the terminal 100 to communicate with other devices, or to connect a charger to charge the terminal 100 .

[0077] Although not shown, the terminal 100 may also include a flashlight, a wireless fidelity (WiFi) module, a Bluetooth module, sensors with different functions, etc., which are not described in detail here. Some or all of the methods described below may be applied in the following embodiments. Figure 2 In the terminal 100 shown.

[0078] Next describe Figure 1 The product form of the server 200;

[0079] Figure 3 An optional hardware structure diagram of the server 200 is shown. Figure 3 As shown, the server 200 includes a bus 201, a processor 202, a communication interface 203, and a memory 204. The processor 202, the memory 204, and the communication interface 203 communicate with each other via the bus 201.

[0080] The bus 201 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 3 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0081] The processor 202 may be any one or more of a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), or a digital signal processor (DSP).

[0082] The memory 204 may include a volatile memory, such as a random access memory (RAM). The memory 204 may also include a non-volatile memory, such as a read-only memory (ROM), a flash memory, a hard disk drive (HDD), or a solid state drive (SSD).

[0083] The memory 204 may be used to store software codes related to the test case generation method, the processor 202 may execute the steps of the test case generation method for the chip, and may also schedule other units to implement corresponding functions.

[0084] It should be understood that the above-mentioned terminal 100 and server 200 can be centralized or distributed devices, and the processors in the above-mentioned terminal 100 and server 200 (such as processor 170 and processor 202) can be hardware circuits (such as application specific integrated circuit (ASIC), field-programmable gate array (FPGA), general-purpose processor, digital signal processor (DSP), microprocessor or microcontroller, etc.), or a combination of these hardware circuits. For example, the processor can be a hardware system with an instruction execution function, such as a CPU, DSP, etc., or a hardware system without an instruction execution function, such as an ASIC, FPGA, etc., or a combination of the above-mentioned hardware systems without an instruction execution function and hardware systems with an instruction execution function.

[0085] The embodiment of the present application provides a method for generating a test case. The method for generating a test case of the embodiment of the present application is described in detail below with reference to the accompanying drawings.

[0086] Reference Figure 4 , Figure 4 A flow chart of a method for generating a test case provided in an embodiment of the present application is shown as follows: Figure 4 As shown, a test case generation method provided in an embodiment of the present application includes an iterative test case optimization process, wherein the tth (t is greater than 1) iteration may include steps S401 to S405, and these steps are described in detail below.

[0087] S401. Update the meta-use case individual population based on a preset swarm intelligence algorithm.

[0088] In this embodiment, the meta-use case individual population includes a plurality of meta-use case individuals, and one meta-use case individual includes parameter values ​​of a group of input parameters of a test case.

[0089] S402. For each meta-use case individual in the meta-use case individual population, obtain a fitness function.

[0090] In this embodiment, the fitness function is a weighted sum function of multiple code coverage rates, wherein the weight of any code coverage rate is related to the comprehensive coverage rate of the meta-use case individual, and the comprehensive coverage rate is the arithmetic sum of multiple code coverage rates.

[0091] S403: Calculate the fitness of each meta-use case individual according to the fitness function.

[0092] S404: Determine whether a preset termination condition is met.

[0093] In this embodiment, the termination condition includes that the number of target meta-use case individuals is greater than a preset use case number threshold, the target meta-use case individual is a meta-use case individual that meets a preset fitness condition, and the fitness condition includes that the fitness is greater than a preset fitness threshold.

[0094] In an optional embodiment, the termination condition also includes that the number of iterations is greater than a preset iteration number threshold, and the fitness condition also includes that the coverage of each code item is greater than the corresponding coverage threshold.

[0095] S405: If the termination condition is met, all target meta-case individuals are obtained from the meta-case individual population to obtain a test case set. If the termination condition is not met, the t+1th iteration is executed.

[0096] It can be seen from the above technical scheme that a method for generating a test case provided by an embodiment of the present application, after updating the meta-case individual population based on the swarm intelligence algorithm, obtains a fitness function for each meta-case individual in the meta-case individual population, and calculates the fitness of each meta-case individual based on the fitness function. It is judged whether a preset termination condition is met, and the termination condition includes that the number of target meta-case individuals is greater than a preset case number threshold, and the target meta-case individual is a meta-case individual that meets the preset fitness condition, and the fitness condition includes that the fitness is greater than the preset fitness threshold. Until the termination condition is met, all target meta-case individuals are obtained from the meta-case individual population to obtain a test case set. Among them, the meta-case individuals in the meta-case individual population represent the parameter values ​​of a set of input parameters of the test case. The fitness function is a weighted sum function of multiple code coverage rates, wherein the weight of any code coverage rate is related to the comprehensive coverage rate of the meta-use case individual in the previous iteration, and the comprehensive coverage rate is the arithmetic sum of multiple code coverage rates, that is, the fitness of the target meta-use case individual is not only related to the current code coverage rates, but also to the code coverage rate in the previous iteration. Therefore, this scheme adjusts the influence of each code coverage rate on the fitness by dynamically adjusting the weight of each code coverage rate in the fitness function, thereby improving the representation degree of the fitness function for the optimization objectives of each code coverage rate of the test case, so that, in the iteration process, the code coverage rates of each test case are improved in a targeted manner, thereby improving the overall code coverage rate of the test case and improving the optimization effect of the test case.

[0097] It should be noted that the above embodiment is only a specific implementation of the test case generation method provided by the present application. The present application can also be implemented through other specific processes. For example, the termination condition also includes that the number of iterations is greater than a preset iteration number threshold, and the fitness condition also includes that the coverage of each code is greater than the corresponding coverage threshold. For another example, A1~A4 is only an optional method for obtaining a fitness function.

[0098] Figure 5 A flowchart of a specific implementation method of a test case generation method provided in an embodiment of the present application is shown in FIG. Figure 5 As shown, this method specifically includes:

[0099] S501. Obtain test information.

[0100] In this embodiment, the test information includes the input parameter information of the test case, and the input parameter information includes the dimension of the input parameter and the value range of each input parameter. The dimension of the input parameter corresponds to the number of input parameters of the function to be tested. For example, under the commodity transaction function, the dimension of the input parameter includes: commodity ID, purchase quantity and user address. The value range corresponding to each input parameter is used to specify the maximum and minimum values ​​of the parameter value. For example, the value range of the purchase quantity is [01, 100].

[0101] S502: Based on the input parameter information of the test case, a preset target number of initial meta-case individuals are randomly generated to form an initial meta-case individual population.

[0102] In this embodiment, the algorithm parameters of the swarm intelligence algorithm are preconfigured, including but not limited to a preset population size, a maximum number of iterations, a frequency range, an initial loudness, and a loudness attenuation factor, wherein the population size is used to indicate the target number.

[0103] In this embodiment, the meta-case individual population includes multiple meta-case individuals, one meta-case individual includes parameter values ​​of a set of input parameters of a test case, the meta-case individual population is initialized according to the algorithm parameters, and the parameter values ​​of the input parameters included in each initial meta-case individual included in the initialized meta-case individual population all represent initial positions obtained based on the value range of the input parameters.

[0104] S503: For each meta-use case individual in the initial meta-use case individual population, obtain an initial fitness function.

[0105] In this embodiment, the weights of each code coverage in the initial fitness function are corresponding initial weights.

[0106] In this embodiment, the fitness function is represented as the weighted sum of the code coverage of each preset type, wherein the preset types of code coverage include sentence coverage, branch coverage, and path coverage. The initial fitness function is a weighted addition function that uses the preset initial weights of each code coverage as the weight of the code coverage.

[0107] It should be noted that various code coverage rates can reflect the coverage of the code by test cases from different dimensions. The initial fitness function is constructed by linearly combining various code coverage rates. For example, the fitness function is expressed as:

[0108]

[0109] Among them, SC represents statement coverage, BC represents branch coverage, and PC represents path coverage. Represent the weights of SC, BC and PC respectively. In the first iteration, Represent the preset initial weights of SC, BC and PC respectively.

[0110] Optionally, P represents a complexity penalty term, and R represents a historical execution reward term.

[0111] Among them, the complexity penalty term is designed for complex code blocks that are not covered by test cases. Generally speaking, complex code blocks (such as deeply nested conditional statements, complex loop structures, etc.) are more prone to defects and should be given higher priority. The role of the complexity penalty term is to impose a certain penalty on the uncovered complex code blocks and reduce the fitness score of the corresponding test cases. This can encourage the search algorithm to prioritize the generation of test cases that can cover complex code blocks. The calculation method of the complexity penalty term is usually based on indicators such as the cyclomatic complexity and nesting depth of the code block, assigning a complexity value to each code block, and then accumulating penalties for the uncovered complex code blocks.

[0112] The historical execution bonus is designed based on the performance of the test case in the historical execution. If a test case covers some important or difficult-to-cover code blocks in the previous execution, then we can give this test case a certain bonus to improve its fitness score. The advantage of this is that high-quality test cases that have covered important code blocks can be retained to prevent them from being eliminated in subsequent iterations. At the same time, it can also prompt the search algorithm to search in the direction of expanding and supplementing existing high-quality test cases, thereby improving the quality of the overall test case collection. The calculation method of the historical execution bonus is usually to maintain a historical execution counter for each code block, recording the number of times it has been covered in the historical execution. For the code block covered by the test case, the corresponding bonus score is given according to the value of its historical execution counter.

[0113] It should be noted that S501 to S503 are the first iteration process of the test case generation method provided in the embodiment of the present application, which is used to generate the initial meta-case individual population and the initial fitness function. Next, the non-first iteration process is introduced by taking the tth iteration process as an example:

[0114] S504: Update the meta-use case individual population based on a preset swarm intelligence algorithm.

[0115] In this embodiment, the individual population of meta-use cases is updated based on various algorithm parameters of the swarm intelligence algorithm. The specific method includes:

[0116] 1. Update the frequency of the meta-case individuals. The frequency is used to indicate the search step and search direction.

[0117] In this embodiment, the formula for updating the step size parameter is: .

[0118] in represents the frequency of the i-th meta-case individual, and Indicates the lower and upper bounds of the frequency. The lower bound is the parameter dimension of the meta-case individual. For example, the input parameters of the test case include three dimensions (product ID, purchase quantity, and user address). Therefore, the lower bound of the frequency is 3, and the upper bound is the preset multiple of the parameter dimension. For example, if it is 10 times, the upper bound is 30. β is a preset random number in [0, 1].

[0119] It should be noted that the frequency affects the step size of the meta-use case individual in each iteration. The higher the frequency, the larger the step size and the wider the search range. The lower the frequency, the smaller the step size and the narrower the search range, but the local area can be searched more finely.

[0120] 2. Update the speed of the meta-use case individual. The speed indicates the size and direction of the position change of the meta-use case individual during each iteration. It is used to indicate the moving step of the meta-use case individual in the search space, which directly affects the convergence speed and optimization ability of the algorithm.

[0121] In this embodiment, the update speed formula is: .

[0122] in represents the velocity of the i-th meta-use case individual in the t+1-th iteration. represents the velocity of the i-th meta-use case individual in the t-th iteration. Represents the position of the i-th meta-use case individual in the t-th iteration. Indicates the location of the current global optimal solution. Represents the frequency of the i-th meta-case individual.

[0123] It should be noted that the initial speed of each meta-use case is preset to 0.

[0124] 3. Get the loudness value of the tth iteration.

[0125] In this embodiment, the loudness value of the t-th iteration is the product of a preset attenuation coefficient and the loudness value of the t-1-th iteration, and the attenuation coefficient is less than 1. The formula for updating the loudness value is expressed as: ,in, represents the loudness of the i-th meta-case individual at the t+1th iteration, represents the loudness of the i-th meta-case individual at the t-th iteration. α is the attenuation factor, and 0.9,1).

[0126] 4. If the loudness value of the tth iteration is not less than the preset loudness threshold, the position of the meta-use case individual is updated by global search.

[0127] In this embodiment, the formula for updating the position of the meta-use case individual based on the global search method is: ,

[0128] in, represents the position of the ith meta-case individual in the t+1th iteration, represents the position of the ith meta-use case individual in the tth iteration, Represents the speed of the i-th meta-use case individual in the t-th iteration, that is, the position of the meta-use case individual will be updated according to the current speed.

[0129] It should be noted that global search is achieved through large step size mutation. Global search is performed in the entire solution space, aiming to explore the solution space on a large scale to avoid falling into the local optimal solution. This search method is suitable for situations where the solution space is large or the problem is more complex. The fitness is calculated by continuously updating the frequency, speed, and position of the individual meta-use cases.

[0130] 5. If the loudness value of the tth iteration is less than the loudness threshold, update the position of the meta-case individual by global search.

[0131] In this embodiment, the formula for updating the position of the meta-use case individual based on the local search method is: new_position = global_best['position'] + alpha * random.uniform(-1, 1), where global_best['position'] represents the position of the meta-use case individual with the highest response rate, alpha is a constant, which is usually used to control the range of variation (can also be regarded as the step size), and random.uniform(-1, 1) generates a random floating point number between -1 and 1, indicating the direction and amplitude of the variation.

[0132] It should be noted that local search is to achieve local exploration through slight variations in input parameters to find a better solution. This search method is suitable for situations where the optimal solution is already close, and the solution can be optimized through slight adjustments.

[0133] In summary, this step first determines the target search method based on the loudness value, and the target search method is one of the global search and the local search. For each meta-case individual, the position of the meta-case individual is updated in the target search method.

[0134] S505. For each meta-use case individual in the meta-use case individual population, obtain a fitness function.

[0135] In this embodiment, the fitness function is a weighted sum function of multiple code coverage rates, wherein the weight of any code coverage rate is related to the comprehensive coverage rate of the meta-use case individual, and the comprehensive coverage rate is the arithmetic sum of multiple code coverage rates.

[0136] It can be understood that obtaining the fitness function in the non-first iteration is to update each weight in the initial fitness function to obtain an updated fitness function.

[0137] In an optional embodiment, the method for obtaining the fitness function includes:

[0138] A1. Calculate the arithmetic sum of the statement coverage, branch coverage, and path coverage of the individual meta-use cases to obtain the comprehensive coverage.

[0139] A2. If the comprehensive coverage of the individual meta-use cases is within the preset numerical range, the initial weights corresponding to each code coverage are used as the weights of each code coverage. The minimum value of the first numerical range is the preset first numerical value, and the maximum value of the first numerical range is the preset second numerical value.

[0140] Optionally, the first value is 0.5 and the second value is 0.9, that is, if 0.5≤(SC+BC+PC)≤0.9, = , = = ,in, represents the weight of the statement coverage obtained in the tth iteration, represents the weight of the branch coverage obtained in the tth iteration, Represents the weight of the path coverage obtained in the tth iteration.

[0141] A3. If the comprehensive coverage of the individual meta-use cases is less than the first value, the initial weight of the statement coverage is increased by a preset first ratio to obtain the first statement dynamic weight, and the initial weights of the branch coverage and the path coverage are respectively reduced by a preset second ratio to obtain the first branch dynamic weight and the first path dynamic weight. The first statement dynamic weight, the first branch dynamic weight and the first path dynamic weight are used as the weights of the statement coverage, the branch coverage and the path coverage, respectively, to construct a fitness function.

[0142] Optionally, when (SC+BC+PC)<0.5, the initial weight of the sentence coverage is increased by the first ratio of 1.1 to obtain the first sentence dynamic weight = , the initial weights of the branch coverage and the path coverage are reduced by the second ratio of 0.9, and the first branch dynamic weight and the first path dynamic weight are obtained, which are expressed as .

[0143] A4. If the comprehensive coverage of the individual meta-use cases is greater than the second value, the initial weight of the statement coverage is reduced by a preset third ratio to obtain a second statement dynamic weight, and the initial weights of the branch coverage and the path coverage are increased by a preset fourth ratio to obtain a second branch dynamic weight and a second path dynamic weight. The second statement dynamic weight, the second branch dynamic weight, and the second path dynamic weight are used as the weights of the statement coverage, the branch coverage, and the path coverage, respectively, to construct a fitness function.

[0144] Optionally, when SC+BC+PC)>0.9, the initial weight of the sentence coverage is reduced by a third ratio of 0.8 to obtain the dynamic weight of the two sentences = , the initial weights of branch coverage and path coverage are increased by the second ratio of 1.1, and the second branch dynamic weight and the second path dynamic weight are obtained, which are expressed as .

[0145] It should be noted that because the difficulty of statement coverage is relatively low, while the difficulty of branch coverage and path coverage is relatively high, therefore, when the total coverage is small, the statement coverage value is increased while the branch coverage and path coverage are reduced, and the focus is on optimizing the statement coverage first. When the total coverage is large, the statement coverage value is reduced while the branch coverage and path coverage are increased. When the total coverage is too large, more in-depth branch coverage and path coverage optimization is performed by adjusting the weights of increasing the branch coverage and path coverage.

[0146] S506: Calculate the fitness of each meta-use case individual according to the fitness function.

[0147] In this embodiment, the statement coverage, branch coverage, and path coverage of each meta-use case individual are substituted into the updated fitness function respectively, and the fitness of the meta-use case individual is calculated.

[0148] S507: Determine whether a preset termination condition is met.

[0149] In this embodiment, the termination condition includes that the number of target meta-use case individuals is greater than a preset use case number threshold, wherein the target meta-use case individual is a meta-use case individual that satisfies a preset fitness condition, and the fitness condition includes that the fitness is greater than a preset fitness threshold.

[0150] In this embodiment, it is determined whether the fitness of each meta-use case individual is greater than a fitness threshold. If so, the meta-use case individual is added to the optimal meta-use case set as a target meta-use case individual.

[0151] S508: If the termination condition is met, all target meta-case individuals are obtained from the meta-case individual population to obtain a test case set. If the termination condition is not met, the t+1th iteration is executed.

[0152] In this embodiment, the optimal meta-case set is used as the test case set, wherein the position of the target meta-case individual is the input parameter of the test case.

[0153] It can be seen from the above technical scheme that the test case generation method provided in the embodiment of the present application iteratively updates the optimal meta-case set based on the swarm intelligence algorithm, and finally obtains a test case set after reaching the termination condition. Since the fitness function is a weighted sum function of multiple code coverage rates, wherein the weight of any code coverage rate is related to the comprehensive coverage rate of the meta-case individual in the previous iteration, and the comprehensive coverage rate is the arithmetic sum of multiple code coverage rates, that is, the fitness of the target meta-case individual is not only related to the current code coverage rates, but also to the code coverage rate in the previous iteration. Therefore, this scheme adjusts the influence of each code coverage rate on the fitness by dynamically adjusting the weights of each code coverage rate in the fitness function, thereby improving the representation degree of the fitness function on the optimization objectives of each code coverage rate of the test case, thereby, in the iterative process, the code coverage rates of each test case are targetedly improved, thereby improving the overall code coverage rate of the test case and improving the optimization effect of the test case.

[0154] Furthermore, in order to balance the global search and the local search during the population update process, the loudness value is adaptively adjusted and the search method is adjusted based on the loudness value. Since the loudness value is inversely correlated with the number of iterations, that is, in the initial stage of the algorithm, the loudness is usually large, which is conducive to extensive exploration in the entire search space and finding a better initial solution. As the number of iterations increases, the loudness gradually decreases, which is conducive to a more refined search of the local area and improves the accuracy of the solution.

[0155] Furthermore, taking into account the importance and implementation difficulty of each type of code coverage, when the total coverage rate is small, the statement coverage rate is increased and the branch coverage rate and path coverage rate are reduced, and the statement coverage rate is optimized first. When the total coverage rate is large, the statement coverage rate is reduced and the branch coverage rate and path coverage rate are increased. When the total coverage rate is too large, more in-depth branch coverage and path coverage rate optimization is performed by adjusting the weights of increasing branch coverage and path coverage rates to improve the optimization effect.

[0156] In summary, this method randomly generates a certain number of meta-use case individuals as the initial population according to the boundary value range of the input parameters, and then starts to update the frequency, speed, and position attributes of the meta-use case individuals. Repeat the iteration to update the frequency, speed, and position, perform local search or global search, calculate the fitness to update the global optimal solution, and after each iteration, calculate the fitness score of the new meta-use case individual, retain the individuals with higher scores, and gradually optimize in the direction of maximizing coverage. According to the fitness function, the position of each meta-use case individual is calculated, and the global optimal meta-use case individual set is updated. After each iteration, the fitness score of the new meta-use case individual is calculated, and the individuals with higher scores are retained, and gradually optimize in the direction of maximizing coverage. During the iteration process, local search and global search operations are randomly selected in the use case population. Local search is to achieve local exploration through small mutations of input parameters to find better solutions. Global search is achieved through large step mutation. Global search is carried out in the entire solution space, aiming to explore the solution space on a large scale, so as to avoid falling into the local optimal solution. In order to balance global search and local search, adaptive adjustment of loudness is used for adjustment.

[0157] A method for generating a test case provided in an embodiment of the present application is introduced above, and a device for executing the above-mentioned method for generating a test case will be introduced below.

[0158] See also Figure 6 , Figure 6 A schematic diagram of the structure of a test case generation device provided in an embodiment of the present application. Figure 6 As shown, the test case generation device includes a t-th iteration unit 600 for performing a t-th iteration, and the t-th iteration unit includes:

[0159] A test case updating unit 601 is used to update a meta-case individual population based on a preset swarm intelligence algorithm, wherein the meta-case individual population includes a plurality of meta-case individuals 602, and a meta-case individual includes parameter values ​​of a group of input parameters of a test case;

[0160] A fitness function construction unit 603 is used to obtain a fitness function for each meta-use case individual in the meta-use case individual population, wherein the fitness function is a weighted sum function of multiple code coverage rates, wherein the weight of any code coverage rate is related to the comprehensive coverage rate of the meta-use case individual, and the comprehensive coverage rate is the arithmetic sum of multiple code coverage rates;

[0161] A fitness calculation unit 604, used to calculate the fitness of each meta-case individual according to the fitness function;

[0162] The termination condition judgment unit 605 is used to judge whether a preset termination condition is met, wherein the termination condition includes that the number of target meta-use case individuals is greater than a preset use case number threshold, and the target meta-use case individual is a meta-use case individual that satisfies a preset fitness condition, wherein the fitness condition includes that the fitness is greater than a preset fitness threshold;

[0163] The iteration result generating unit 606 is used to obtain all target meta-case individuals from the meta-case individual population to obtain a test case set if the termination condition is met; if the termination condition is not met, perform the t+1th iteration; t is greater than 1.

[0164] In a possible implementation, the test case generation device further includes a first iteration unit for performing a first iteration, and the first iteration unit includes:

[0165] A test information acquisition unit, used to acquire test information, wherein the test information includes input parameter information of the test case, and the input parameter information includes the dimension of the input parameter and the value range of each input parameter;

[0166] A case initialization unit, used to randomly generate a preset target number of initial meta-case individuals based on the input parameter information of the test case to form an initial meta-case individual population;

[0167] The fitness function acquisition unit is used to acquire an initial fitness function for each meta-use case individual in the initial meta-use case individual population, wherein the weights of each code coverage in the initial fitness function are corresponding initial weights.

[0168] In a possible implementation, the use case updating unit in the t-th iteration unit is used to update the meta-use case individual population based on a preset swarm intelligence algorithm, specifically to:

[0169] Determine a target search mode based on a loudness value, wherein the loudness value is inversely correlated with the number of iterations, and the target search mode is one of a global search and a local search;

[0170] For each meta-use case individual, the position of the meta-use case individual is updated in the target search manner.

[0171] In a possible implementation, when the use case updating unit in the t-th iteration unit is used to determine the target search mode based on the loudness value, it is specifically used to:

[0172] Obtaining the loudness value of the t-th iteration, where the loudness value of the t-th iteration is the product of a preset attenuation coefficient and the loudness value of the t-1-th iteration, and the attenuation coefficient is less than 1;

[0173] If the loudness value of the t-th iteration is not less than a preset loudness threshold, determining that the search mode is the global search;

[0174] If the loudness value of the t-th iteration is less than the loudness threshold, the search mode is determined to be the local search.

[0175] In a possible implementation, the code coverage includes statement coverage, branch coverage, and path coverage. The fitness function construction unit in the t-th iteration unit is used to obtain the fitness function of the meta-use case individual, including:

[0176] Calculate the arithmetic sum of the statement coverage, branch coverage and path coverage of the individual meta-use cases to obtain the comprehensive coverage;

[0177] If the comprehensive coverage of the individual meta-use cases is within a preset numerical range, the initial weights corresponding to the various code coverages are used as the weights of the various code coverages, the minimum value of the first numerical range is a preset first numerical value, and the maximum value of the first numerical range is a preset second numerical value;

[0178] If the comprehensive coverage of the individual meta-use cases is less than the first preset value, the initial weight of the statement coverage is increased by a preset first ratio to obtain a first statement dynamic weight, and the initial weights of the branch coverage and the path coverage are respectively reduced by a preset second ratio to obtain a first branch dynamic weight and a first path dynamic weight; the first statement dynamic weight, the first branch dynamic weight and the first path dynamic weight are respectively used as the weights of the statement coverage, the branch coverage and the path coverage, to construct the fitness function;

[0179] If the comprehensive coverage rate of the meta-use case individuals is greater than the second value, the initial weight of the statement coverage rate is reduced by a preset third ratio to obtain a second statement dynamic weight, and the initial weights of the branch coverage rate and the path coverage rate are respectively increased by a preset fourth ratio to obtain a second branch dynamic weight and a second path dynamic weight; the second statement dynamic weight, the second branch dynamic weight and the second path dynamic weight are used as the weights of the statement coverage rate, the branch coverage rate and the path coverage rate, respectively, to construct the fitness function.

[0180] In a possible implementation, the termination condition also includes: the number of iterations is greater than a preset iteration number threshold, and the fitness condition also includes that the coverage of each code item is greater than the corresponding coverage threshold.

[0181] The present application also provides an electronic device in an embodiment. Figure 7As shown, it shows a schematic diagram of the structure of an electronic device suitable for implementing the embodiment of the present application. The electronic device in the embodiment of the present application may include but is not limited to fixed terminals such as mobile phones, laptops, PDAs (personal digital assistants), PADs (tablet computers), desktop computers, etc. Figure 7 The electronic device shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.

[0182] like Figure 7 As shown, the electronic device may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 701, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 702 or a program loaded from a storage device 708 to a random access memory (RAM) 703. When the electronic device is powered on, various programs and data required for the operation of the electronic device are also stored in the RAM 703. The processing device 701, the ROM 702, and the RAM 703 are connected to each other via a bus 704. An input / output (I / O) interface 705 is also connected to the bus 704.

[0183] Typically, the following devices may be connected to the I / O interface 705: an input device 706 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 707 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 708 including, for example, a memory card, a hard disk, etc.; and a communication device 709. The communication device 709 may allow the electronic device to communicate with other devices wirelessly or by wire to exchange data. Although Figure 7 An electronic device having various devices is shown, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.

[0184] An embodiment of the present application also provides a computer program product including computer-readable instructions. When the computer-readable instructions are executed on an electronic device, the electronic device implements any test case generation method provided in the embodiment of the present application.

[0185] A computer-readable storage medium is also provided in an embodiment of the present application. The storage medium carries one or more computer programs. When the one or more computer programs are executed by an electronic device, the electronic device can implement any test case generation method provided in the embodiment of the present application.

[0186] It should also be noted that the device embodiments described above are merely schematic, wherein the units described 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 they may be distributed over multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. In addition, in the drawings of the device embodiments provided by the present application, the connection relationship between the modules indicates that there is a communication connection between them, which may be specifically implemented as one or more communication buses or signal lines.

[0187] Through the description of the above implementation mode, the technicians in the field can clearly understand that the present application can be implemented by means of software plus necessary general hardware, and of course, it can also be implemented by special hardware including special integrated circuits, special CPUs, special memories, special components, etc. In general, all functions completed by computer programs can be easily implemented by corresponding hardware, and the specific hardware structure used to implement the same function can also be various, such as analog circuits, digital circuits or special circuits. However, for the present application, software program implementation is a better implementation mode in more cases. Based on such an understanding, the technical solution of the present application is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a readable storage medium, such as a computer floppy disk, a U disk, a mobile hard disk, a ROM, a RAM, a disk or an optical disk, etc., including a number of instructions to enable a computer device (which can be a personal computer, a training device, or a network device, etc.) to execute the methods described in each embodiment of the present application.

[0188] In the above embodiments, all or part of the embodiments may be implemented by software, hardware, firmware or any combination thereof. When implemented by software, all or part of the embodiments may be implemented in the form of a computer program product.

[0189] The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions may be transmitted from a website site, a computer, a training device, or a data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, training device, or data center. The computer-readable storage medium may be any available medium that a computer can store or a data storage device such as a training device, a data center, etc. that includes one or more available media integrations. The available medium may be a magnetic medium, (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive (SSD)), etc.

Claims

1. A method for generating a test case, characterized in that: The test case optimization process includes iterative execution. The tth iteration includes: Based on a preset swarm intelligence algorithm, a meta-case individual population is updated, wherein the meta-case individual population includes a plurality of meta-case individuals, and a meta-case individual includes parameter values ​​of a set of input parameters of a test case; For each meta-use case individual in the meta-use case individual population, a fitness function is obtained, wherein the fitness function is a weighted sum function of multiple code coverage rates, wherein the weight of any code coverage rate is related to the comprehensive coverage rate of the meta-use case individual, and the comprehensive coverage rate is the arithmetic sum of multiple code coverage rates; Calculating the fitness of each meta-case individual according to the fitness function; Determining whether a preset termination condition is met, wherein the termination condition includes that the number of target meta-use case individuals is greater than a preset use case number threshold, and the target meta-use case individual is a meta-use case individual that satisfies a preset fitness condition, wherein the fitness condition includes that the fitness is greater than a preset fitness threshold; If the termination condition is met, all target meta-case individuals are obtained from the meta-case individual population to obtain a test case set; If the termination condition is not met, execute the t+1th iteration; The t is greater than 1.

2. The method for generating a test case according to claim 1, characterized in that: Iteration 1 includes: Acquire test information, where the test information includes input parameter information of the test case, where the input parameter information includes the dimension of the input parameter and the value range of each input parameter; Based on the input parameter information of the test case, a preset target number of initial meta-case individuals are randomly generated to form an initial meta-case individual population; For each meta-use case individual in the initial meta-use case individual population, an initial fitness function is obtained, and the weights of each code coverage in the initial fitness function are the corresponding initial weights.

3. The method for generating a test case according to claim 2, characterized in that: In the t-th iteration, based on the preset swarm intelligence algorithm, the meta-use case individual population is updated, including: Determine a target search mode based on a loudness value, wherein the loudness value is inversely correlated with the number of iterations, and the target search mode is one of a global search and a local search; For each meta-use case individual, the position of the meta-use case individual is updated in the target search manner.

4. The method for generating a test case according to claim 3, characterized in that: In the t-th iteration, the target search method is determined based on the loudness value, including: Obtaining the loudness value of the t-th iteration, where the loudness value of the t-th iteration is the product of a preset attenuation coefficient and the loudness value of the t-1-th iteration, and the attenuation coefficient is less than 1; If the loudness value of the t-th iteration is not less than a preset loudness threshold, determining that the search mode is the global search; If the loudness value of the t-th iteration is less than the loudness threshold, the search mode is determined to be the local search.

5. The method for generating a test case according to claim 2, characterized in that: The code coverage includes statement coverage, branch coverage and path coverage. In the t-th iteration, the fitness function of the meta-use case individual is obtained including: Calculate the arithmetic sum of the statement coverage, branch coverage and path coverage of the individual meta-use cases to obtain the comprehensive coverage; If the comprehensive coverage of the individual meta-use cases is within a preset numerical range, the initial weights corresponding to the various code coverages are used as the weights of the various code coverages, the minimum value of the first numerical range is a preset first numerical value, and the maximum value of the first numerical range is a preset second numerical value; If the comprehensive coverage of the individual meta-use cases is less than the first preset value, the initial weight of the statement coverage is increased by a preset first ratio to obtain a first statement dynamic weight, and the initial weights of the branch coverage and the path coverage are respectively reduced by a preset second ratio to obtain a first branch dynamic weight and a first path dynamic weight; the first statement dynamic weight, the first branch dynamic weight and the first path dynamic weight are respectively used as the weights of the statement coverage, the branch coverage and the path coverage, to construct the fitness function; If the comprehensive coverage rate of the meta-use case individuals is greater than the second value, the initial weight of the statement coverage rate is reduced by a preset third ratio to obtain a second statement dynamic weight, and the initial weights of the branch coverage rate and the path coverage rate are respectively increased by a preset fourth ratio to obtain a second branch dynamic weight and a second path dynamic weight; the second statement dynamic weight, the second branch dynamic weight and the second path dynamic weight are used as the weights of the statement coverage rate, the branch coverage rate and the path coverage rate, respectively, to construct the fitness function.

6. The method for generating a test case according to claim 2, characterized in that: The termination condition also includes: the number of iterations is greater than a preset iteration number threshold, and the fitness condition also includes that the coverage of each code is greater than the corresponding coverage threshold.

7. A test case generation device, characterized in that: The test case generation device is used to iteratively execute the test case optimization process, and the test case generation device includes a t-th iteration unit for executing the t-th iteration, and the t-th iteration unit includes: A test case updating unit, configured to update a meta-case individual population based on a preset swarm intelligence algorithm, wherein the meta-case individual population includes a plurality of meta-case individuals, and a meta-case individual includes parameter values ​​of a set of input parameters of a test case; A fitness function construction unit, used for obtaining a fitness function for each meta-use case individual in the meta-use case individual population, wherein the fitness function is a weighted sum function of multiple code coverage rates, wherein the weight of any code coverage rate is related to the comprehensive coverage rate of the meta-use case individual, and the comprehensive coverage rate is the arithmetic sum of multiple code coverage rates; A fitness calculation unit, used to calculate the fitness of each meta-case individual according to the fitness function; A termination condition judgment unit, used to judge whether a preset termination condition is met, wherein the termination condition includes that the number of target meta-use case individuals is greater than a preset use case number threshold, and the target meta-use case individual is a meta-use case individual that meets a preset fitness condition, wherein the fitness condition includes that the fitness is greater than a preset fitness threshold; An iteration result generating unit, configured to obtain all target meta-case individuals from the meta-case individual population to obtain a test case set if the termination condition is met; and to execute the t+1th iteration if the termination condition is not met; The t is greater than 1.

8. A computer program product, characterized in that The method comprises computer-readable instructions, and when the computer-readable instructions are executed on an electronic device, the electronic device implements the test case generation method as claimed in any one of claims 1 to 6.

9. An electronic device, characterized in that: The method comprises at least one processor and a memory connected to the processor, wherein: The memory is used to store computer programs; The processor is used to execute the computer program so that the electronic device can implement the test case generation method as described in any one of claims 1 to 6.

10. A computer storage medium, characterized in that: The storage medium carries one or more computer programs, and when the one or more computer programs are executed by an electronic device, the electronic device can implement the test case generation method as described in any one of claims 1 to 6.