Test case generation method and device

Dynamic test data is generated through digital twin technology and intelligent algorithms, and the problem of insufficient diversity and coverage of test scenarios in the existing technology is solved, real-time update and efficient automation of test data are achieved.

CN119105955APending Publication Date: 2024-12-10WUHAN FIBERHOME TECHNICAL SERVICES CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411061715.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-05
Publication Date
2024-12-10

AI Technical Summary

Technical Problem

It is difficult for the existing technology to generate high-quality, diverse and cover-based test scenarios, and traditional test data generation methods cannot achieve real-time updates, resulting in a mismatch between the test data and actual needs, increasing the risk of failure after the software is launched.

Method used

Combining digital twin technology and intelligent algorithms, by collecting operating environment and user behavior information, using particle swarm optimization algorithms and genetic algorithms to generate dynamic and variable test data, ensuring the diversity and coverage of test scenarios.

Benefits of technology

Real-time update and high-quality generation of test data are realized, the automation level and effect of tests are improved, and the complexity and inefficiency of test scenarios are solved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119105955A_ABST
    Figure CN119105955A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of software testing, in particular to a test case generation method and device. Collecting a to-be-tested operation environment and user behavior information; data coding is carried out on the operation environment and the user behavior information according to coding rules, each operation step in the operation environment and the user behavior information is converted into a vector with a fixed length, chromosomes are constructed based on the vector, and each chromosome represents a test case; representing a particle by each chromosome, calculating a fitness value of the chromosome by adopting a particle swarm optimization algorithm, and performing iteration by adopting a genetic algorithm based on the fitness value of the particle to obtain a varied chromosome; and converting the varied chromosome into a text test case according to a coding rule. According to the invention, the problem of insufficient data real-time performance is solved. By introducing a genetic particle swarm optimization algorithm, dynamic and variable test data are generated, and the problems of complex test scenes and diversity of the test scenes are solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of software testing, and in particular to a test case generation method and device. Background Art

[0002] In software or system testing, generating high-quality test scenarios often requires considering many factors, such as user behavior, device status, network environment, etc., which makes the generation of test scenarios very complex and time-consuming.

[0003] In order to comprehensively test the functions and performance of the system, it is necessary to generate test scenarios with diversity and coverage. However, it is often difficult to ensure diversity and coverage when artificially generating such scenarios.

[0004] With the continuous iteration and update of software systems, new functions and requirements continue to emerge, which requires that test data can be updated in real time to meet the test needs. However, traditional test data generation methods often cannot achieve real-time updates, resulting in a gap between test data and actual needs.

[0005] Taking a delivery system as an example, it involves multiple functional modules such as material application, material return, SAP posting, and write-off. Taking an e-commerce system as an example, it involves multiple functions such as login, product selection, shopping, ordering, and delivery. Login status, product inventory, account balance and payment method when placing an order, and logistics during delivery all have different options and status. If you only rely on a fixed test data set, it may not cover all user behaviors and abnormal situations. In addition, network delays and insufficient network bandwidth will make it impossible for testers to fully evaluate the stability and reliability of the system, increasing the risk of failure after the software is launched.

[0006] In summary, a technical solution for automatic generation of test cases is urgently needed. Summary of the invention

[0007] In view of the above problems, the present invention provides a test case generation method and device to solve the problems of difficulty in generating test data, incomplete coverage of test scenarios, and low test efficiency in the current software system testing process. By combining digital twin technology and intelligent algorithms, the method can achieve comprehensive and effective testing of software systems and improve the automation level and test effect of the test.

[0008] In a first aspect, a test case generation method is provided, the method comprising:

[0009] Collect the operating environment and user behavior information to be tested;

[0010] Encode the operating environment and user behavior information according to the encoding rules, convert each operation step in the operating environment and user behavior information into a vector of fixed length, and construct chromosomes based on the vectors. Each chromosome represents a test case.

[0011] Each chromosome is represented as a particle, and the fitness value of the chromosome is calculated by the particle swarm optimization algorithm. The genetic algorithm is used to iterate based on the fitness value of the particle to obtain the mutated chromosome.

[0012] According to the encoding rules, the mutated chromosomes are converted into text test cases.

[0013] Furthermore, the operating environment includes: operating system version, hardware configuration and network status.

[0014] Furthermore, user behavior information is a sequence of various interaction information between the user and the interface, including login, click and input.

[0015] Furthermore, the operating environment and user behavior information are encoded according to the encoding rules, including:

[0016] Convert the operating environment and user behavior information into Json format.

[0017] Furthermore, each operation step in the operating environment and user behavior information is converted into a vector of fixed length, and a chromosome is constructed based on the vector, including:

[0018] For the operation step sequence in Json format, we use embedded coding to convert discrete variables into continuous vectors, and each operation step is converted into a vector of fixed length.

[0019] Concatenate the vectors to get a complete operation step vector;

[0020] Encode the running environment as an environment vector;

[0021] Concatenate all the data items of the encoded environment vector and the operation step vector into a chromosome.

[0022] Furthermore, each chromosome is represented as a particle, and the fitness value of the chromosome is calculated using a particle swarm optimization algorithm. Based on the fitness value of the particle, a genetic algorithm is used for iteration to obtain the mutated chromosome, including:

[0023] Randomly generate positions and velocities for each particle in the search space;

[0024] For each particle, compare the fitness value of the current test case position with the fitness value of its historical best position pBest. If the current test case position is better, update pBest.

[0025] Select the global best position gBest from the pBest of all particles, that is, the test case with the highest fitness value;

[0026] According to the PSO update rules, the speed and position of each particle are updated to generate new test cases;

[0027] Based on the fitness values ​​of the particles, the particles with the top 50% fitness values ​​are selected for subsequent crossover and mutation operations;

[0028] Perform crossover operation on selected particles to generate new test cases; the crossover operation adopts single-point crossover or multi-point crossover;

[0029] Perform mutation operation on the newly generated test case, and the mutation operation randomly changes one or some input parameters in the test case.

[0030] Secondly, an intelligent testing method based on digital twin technology.

[0031] Build a digital twin system based on the network environment to be tested, operating equipment data, and user behavior data;

[0032] Based on the network environment to be tested, the running device data and the user behavior data, a test case is generated by using one of the test case generation methods mentioned above;

[0033] Use test cases to conduct testing in the digital twin system.

[0034] In a third aspect, a test case generation device includes: an information collection unit, an encoding unit, a chromosome mutation unit, and a test case generation unit;

[0035] An information collection unit, used to collect the operating environment to be tested and user behavior information;

[0036] The encoding unit is used to encode the operating environment and user behavior information according to the encoding rules, convert each operation step in the operating environment and user behavior information into a vector of fixed length, and construct chromosomes based on the vectors, each chromosome representing a test case;

[0037] The chromosome mutation unit is used to represent each chromosome as a particle, calculate the fitness value of the chromosome using the particle swarm optimization algorithm, and iterate based on the fitness value of the particle using the genetic algorithm to obtain the mutated chromosome;

[0038] The test case generation unit is used to convert the mutated chromosome into a text test case according to the encoding rules.

[0039] In a fourth aspect, an electronic device includes a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other via the communication bus;

[0040] a memory storing a computer program;

[0041] The processor is used to implement the above-mentioned test case generation method when executing the computer program stored in the memory.

[0042] In a fifth aspect, a computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the test case generation method described above is implemented.

[0043] The present disclosure has at least the following beneficial effects:

[0044] This paper uses digital twin technology to collect actual operating environment and user behavior data, solving the problem of insufficient real-time data. By introducing the genetic particle swarm optimization algorithm, dynamic and variable test data is generated, solving the problem of complex test scenarios and test scenario diversity.

[0045] Other features and advantages of the present disclosure will be described in the following description, and partly become apparent from the description, or be understood by implementing the present disclosure. The purpose and other advantages of the present disclosure can be realized and obtained by the structures indicated in the description and the drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0046] In order to more clearly illustrate the embodiments of the present disclosure or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present disclosure. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.

[0047] Figure 1 A flow chart of a method for generating test cases according to an embodiment of the present disclosure;

[0048] Figure 2 A schematic diagram of the structure of a test case generating device according to an embodiment of the present disclosure;

[0049] Figure 3 It is a schematic diagram of the structure of an electronic device;

[0050] Figure 4 Schematic diagram of the system architecture of the testing method according to the embodiment of the present disclosure. DETAILED DESCRIPTION

[0051] In order to make the purpose, technical solution and advantages of the embodiments of the present disclosure clearer, the technical solution in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the drawings in the embodiments of the present disclosure. Obviously, the described embodiments are part of the embodiments of the present disclosure, not all of the embodiments. Based on the embodiments in the present disclosure, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present disclosure.

[0052] like Figure 1 As shown, a test case generation method, the method comprising:

[0053] S101, collecting the operating environment to be tested and user behavior information;

[0054] S102, encoding the operating environment and user behavior information according to the encoding rules, converting each operation step in the operating environment and user behavior information into a vector of a fixed length, constructing a chromosome based on the vector, and each chromosome represents a test case;

[0055] S103, representing each chromosome as a particle, calculating the fitness value of the chromosome using a particle swarm optimization algorithm, and iterating based on the fitness value of the particle using a genetic algorithm to obtain a mutated chromosome;

[0056] S104, converting the mutated chromosome into a text test case according to the encoding rules.

[0057] In the specific implementation, take the e-commerce website as an example to introduce as follows:

[0058] The shopping process of e-commerce websites under certain conditions:

[0059] 1. The user obtains the test task in the test management system according to the test case ID;

[0060] 2. The user uses the Chrome browser to open the e-commerce website;

[0061] 3. The user confirms the network environment (bandwidth 10Mbps, delay 50ms);

[0062] 4. The user checks the device memory size (4GB);

[0063] 5. User login account (user name: testuser, password: testpass);

[0064] 6. The user browses the product list;

[0065] 7. The user adds the product to the shopping cart;

[0066] 8. The user checks the product information in the shopping cart;

[0067] 9. The user selects a payment method and completes the payment;

[0068] 10. User views order details;

[0069] 11. The user waits for delivery and confirms receipt.

[0070] The operating environment and user behavior information are collected through the digital twin system: the operating environment includes the operating system version, hardware configuration, network status, etc. User behavior information is the user's various interactive information on the WEB page, such as entering the user name and password in the Input box of the login page, clicking the Button of the login module, etc., and the sequence of user operations is recorded.

[0071] Dataset example:

[0072] 1. User operation behavior data:

[0073]

[0074]

[0075] 2. Operating environment information

[0076]

[0077] Convert to Json format:

[0078]

[0079]

[0080]

[0081]

[0082] Encode the data and convert it into the input of genetic particle swarm optimization algorithm.

[0083] A new test case set is output through the Genetic Particle Swarm Optimization (GAPSO) algorithm.

[0084] Several new test cases are generated, as follows:

[0085] Test case 1: Browser compatibility test:

[0086] Users obtain test tasks in the test management system based on the test case ID;

[0087] The user opens the e-commerce website using the Safari browser (variation: trying different browsers);

[0088] The user confirms the network environment (bandwidth 10Mbps, latency 50ms);

[0089] The user checks the device memory size (4GB);

[0090] User login account (use standard login information);

[0091] The user browses the product list;

[0092] The user attempts to use all major features of the website (e.g., search, filter, compare, etc.);

[0093] Users check that the page layout and elements are rendered correctly;

[0094] The user logs out and closes the browser.

[0095] Test case 2: Network condition test:

[0096] Users obtain test tasks in the test management system based on the test case ID;

[0097] The user opens the e-commerce website using the Chrome browser;

[0098] The user confirms the network environment (bandwidth 2Mbps, latency 200ms) (variation: simulate slower network conditions);

[0099] The user checks the device memory size (4GB);

[0100] User login account (use standard login information);

[0101] The user attempts to perform high-load operations (such as uploading a large number of pictures or videos);

[0102] Check whether the website performance is affected (such as page loading speed, response time, etc.);

[0103] The user checks if any timeouts or errors occur;

[0104] The user logs out and closes the browser.

[0105] Test case 3: Shopping cart function test:

[0106] Users obtain test tasks in the test management system based on the test case ID;

[0107] The user opens the e-commerce website using the Chrome browser;

[0108] The user confirms the network environment (standard network environment);

[0109] The user checks the device memory size (4GB);

[0110] User login account (use standard login information);

[0111] The user browses the product list and adds multiple products to the shopping cart;

[0112] The user checks whether the quantity, price, discount and other information of the items in the shopping cart are correct;

[0113] The user attempts to modify the quantity of items in the shopping cart, remove items, etc.

[0114] The user checks whether the shopping cart updates are reflected on the page in real time;

[0115] The user selects a payment method and completes payment to confirm the order status.

[0116] Test case 4: Order processing flow test:

[0117] Users obtain test tasks in the test management system based on the test case ID;

[0118] The user uses the Chrome browser to open the e-commerce website;

[0119] The user confirms the network environment (standard network environment);

[0120] The user checks the device memory size (4GB);

[0121] User login account (use standard login information);

[0122] The user views the historical order list;

[0123] The user selects a completed order and checks the order details (such as product information, price, delivery status, etc.);

[0124] The user attempts to modify the order status (such as canceling the order, returning the product for a refund, etc., if allowed);

[0125] The user checks whether the system has correctly processed the change in order status;

[0126] The user logs out and closes the browser.

[0127] The mutation points of test case 3 (shopping cart function test) are:

[0128] Variations in the number and types of items: In the original test case, the user may only add one or two items to the shopping cart. In the variant version, the user may add multiple items, including items of different types and price ranges, to test the performance of the shopping cart under different combinations of items.

[0129] Application of promotional activities: If the website has promotional activities (such as discounts on purchases above a certain amount, discount coupons, etc.), mutation testing can include testing the correct application of these promotional activities in the shopping cart, such as checking whether the discounts are calculated correctly and whether the discounts are superimposed.

[0130] Inventory check: Add some low-stock items to the shopping cart to test whether the system can handle it correctly and give correct prompts when the stock is insufficient.

[0131] Simulation of abnormal situations: For example, try to add a delisted product to the shopping cart to check whether the system can give correct feedback.

[0132] Mutation points of test case 4 (order processing flow test):

[0133] Diversity of order status: In the original test case, only completed orders may be tested. In the variant version, orders in different states can be tested, such as pending payment, pending shipment, shipped, received, etc., to verify the correctness of the system throughout the order life cycle.

[0134] Diversity of payment methods: If the website supports multiple payment methods (such as online payment, cash on delivery, etc.), you can test the impact of different payment methods on order status.

[0135] Changes to delivery information: During order processing, users can modify delivery information (such as address, phone number, etc.). Test whether these changes affect order processing and delivery.

[0136] Refund and return simulation: Test whether the system can correctly process and update the order status when the user applies for a refund or return, and whether the refund and return process is correct.

[0137] Genetic Particle Swarm Optimization (GPSO) algorithm details:

[0138] 1. Initialization phase:

[0139] Setting parameters:

[0140] Particle swarm size (N): Assume that we set the particle swarm size to 30, that is, N=30.

[0141] Iteration number (Tmax): Set the maximum number of iterations to 100, that is, Tmax=100.

[0142] The crossover probability (Pc) of the genetic algorithm is set to 0.8, and the mutation probability (Pm) is set to 0.05.

[0143] The inertia weight (w) of PSO is set to 0.9, and the individual learning factor (c1) and social learning factor (c2) are both set to 2.

[0144] Initialize the particle swarm:

[0145] The test case consists of a series of input parameters (as described in the data above), and each particle represents a test case, that is, a combination of input parameters. The position and speed are randomly generated for each particle in the search space.

[0146] 2. Calculate fitness:

[0147] Evaluate particles:

[0148] Code coverage is used as the evaluation metric of the fitness function.

[0149] This function evaluates the quality of the test case based on the results after the test case is executed.

[0150] The fitness function is used to calculate the fitness value of each particle.

[0151] 3. Evolution and update stage:

[0152] Particle Swarm Optimization (PSO):

[0153] For each particle, the fitness value of its current position (i.e., the current test case) is compared with the fitness value of its historical best position (pBest). If the current position is better, pBest is updated.

[0154] Select the global best position (gBest) from the pBest of all particles, that is, the test case with the highest fitness value.

[0155] According to the PSO update rule, the speed and position of each particle are updated to generate new test cases.

[0156] Genetic Algorithm (GA):

[0157] Based on the fitness values ​​of the particles, a portion of the particles (such as the top 50% with higher fitness values) are selected for subsequent crossover and mutation operations.

[0158] Perform crossover operation on the selected particles to generate new test cases. The crossover operation can be performed in single-point crossover, multi-point crossover and other ways, depending on the specific problem.

[0159] Perform mutation operations on the newly generated test cases to increase the diversity of the population. The mutation operation can randomly change one or some input parameters in the test case.

[0160] 4. Termination conditions:

[0161] If the preset number of iterations (Tmax) is reached, that is, 100 iterations are completed, or other termination conditions are met (such as the fitness value reaches the preset threshold), the algorithm stops and outputs the optimal solution, that is, the test case with the highest fitness value as the optimized test case.

[0162] 5. Test results and application:

[0163] The optimized test cases are applied to the actual test of the software system, and the test efficiency and coverage are observed to verify the effectiveness of the genetic particle swarm optimization algorithm in test case generation.

[0164] Through the above examples, by combining the global search capability of GA and the fast convergence ability of PSO, high-quality test cases can be generated more efficiently, improving the test efficiency and coverage of the software system.

[0165] An intelligent testing method based on digital twin technology.

[0166] Build a digital twin system based on the network environment to be tested, operating equipment data, and user behavior data;

[0167] Based on the network environment to be tested, the running device data and the user behavior data, a test case is generated by using one of the test case generation methods mentioned above;

[0168] Use test cases to conduct testing in the digital twin system.

[0169] like Figure 2 As shown, a test case generating device includes: an information collecting unit 201, an encoding unit 202, a chromosome variation unit 203 and a test case generating unit 204;

[0170] The information collection unit 201 is used to collect the operating environment to be tested and the user behavior information;

[0171] The encoding unit 202 is used to perform data encoding on the operating environment and the user behavior information according to the encoding rules, convert each operation step in the operating environment and the user behavior information into a vector of a fixed length, and construct a chromosome based on the vector, where each chromosome represents a test case;

[0172] The chromosome mutation unit 203 is used to represent each chromosome as a particle, calculate the fitness value of the chromosome using a particle swarm optimization algorithm, and iterate based on the fitness value of the particle using a genetic algorithm to obtain a mutated chromosome;

[0173] The test case generating unit 204 is used to convert the mutated chromosome into a text test case according to the encoding rules.

[0174] like Figure 3As shown, the present disclosure provides an electronic device, including a processor 301, a communication interface 302, a memory 303 and a communication bus 304, wherein the processor 301, the communication interface 302 and the memory 303 communicate with each other through the communication bus 304;

[0175] Memory 303, storing computer programs;

[0176] The processor 301 is configured to implement the above method when executing the computer program stored in the memory 303 .

[0177] The present disclosure provides a computer-readable storage medium storing a computer program, wherein the computer program implements the above method when executed by a processor.

[0178] The computer-readable storage medium may be included in the device / apparatus described in the above embodiment; or it may exist independently without being assembled into the device / apparatus. The above computer-readable storage medium carries one or more programs, and when the above one or more programs are executed, the method according to the embodiment of the present disclosure is implemented.

[0179] According to an embodiment of the present disclosure, a computer-readable storage medium may be a non-volatile computer-readable storage medium, for example, may include but is not limited to: a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program that may be used by or in conjunction with an instruction execution system, apparatus, or device.

[0180] In order to enable those skilled in the art to better understand the present disclosure, the principles of the present disclosure are described as follows in conjunction with the accompanying drawings:

[0181] like Figure 4 As shown in the figure, the process of the entire technical solution, in which the algorithm part is carried out after the data is collected to build the digital twin system. The input and output of the algorithm: the collected operating environment information and user operation behavior information.

[0182] Data encoding is described as follows:

[0183] 1. Data preprocessing:

[0184] Numeric data:

[0185] -Bandwidth: 10Mbps can be directly encoded as the value 10.

[0186] - Delay: 50ms can be directly encoded as the value 50.

[0187] -Memory size: 4GB can be directly encoded as the value 4.

[0188] Categorizing Data:

[0189] - Browser: Chrome can be tab coded as 1 (assuming other browsers like Firefox is 2, IE is 3, etc.).

[0190] - Network conditions: One-hot encoding can be used. For example, a bandwidth of 10 Mbps is encoded as [1,0,0], 20 Mbps is encoded as [0,1,0], and 30 Mbps is encoded as [0,0,1].

[0191] 2. Coding scheme:

[0192] Sequence data encoding:

[0193] For the sequence of operation steps, we use embedding, which is a method of converting discrete variables into continuous vectors. For example, each operation step can be converted into a vector of fixed length.

[0194] Example: Operation steps code:

[0195] The following steps are available:

[0196] {

[0197] "Component": "Username Input Box",

[0198] "Type":"Input box",

[0199] "DOM_ID":"username-input",

[0200] "Operation":"Input",

[0201] "value":"testuser123"

[0202] }

[0203] Create an embedding vector for each attribute:

[0204] -Component: Use an embedding layer to convert the “username input box” into a vector, such as [0.1, 0.2, 0.3].

[0205] -Type: Convert the "input box" to another vector, such as [0.4, 0.5, 0.6].

[0206] -DOM_ID: Convert "username-input" to a vector, e.g. [0.7, 0.8, 0.9].

[0207] - Operation: Convert "input" to a vector, e.g. [1.0, 1.1, 1.2].

[0208] -values: Convert "testuser123" to a vector, e.g. [1.3, 1.4, 1.5].

[0209] Concatenate these vectors to get a complete operation step vector:

[0210] Operation step vector = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5].

[0211] 3. Construct chromosomes:

[0212] Concatenate all encoded data items into a chromosome. For example:

[0213] Chromosome = [bandwidth encoding, delay encoding, memory size encoding, browser encoding, network condition encoding, operation step encoding 1, operation step encoding 2, ...];

[0214] Chromosome = [10,50,4,1,1,0,0,operation step vector 1,operation step vector 2,operation step vector 3];

[0215] Each chromosome is an input data of the genetic algorithm;

[0216] Generate a new test case set, as described below:

[0217] Genetic Particle Swarm Optimization (GPSO) algorithm details:

[0218] 1. Initialization phase:

[0219] Setting parameters:

[0220] Particle swarm size (N): Assume that we set the particle swarm size to 30, that is, N=30.

[0221] Iteration number (Tmax): Set the maximum number of iterations to 100, that is, Tmax=100.

[0222] The crossover probability (Pc) of the genetic algorithm is set to 0.8, and the mutation probability (Pm) is set to 0.05.

[0223] The inertia weight (w) of PSO is set to 0.9, and the individual learning factor (c1) and social learning factor (c2) are both set to 2.

[0224] Initialize the particle swarm:

[0225] The test case consists of a series of input parameters (such as the chromosomes mentioned above), and each particle represents a test case, that is, a combination of input parameters. The position and speed are randomly generated for each particle in the search space.

[0226] 2. Calculate fitness:

[0227] Evaluate particles:

[0228] Code coverage is used as the evaluation metric of the fitness function.

[0229] This function evaluates the quality of the test case based on the results after the test case is executed.

[0230] The fitness function is used to calculate the fitness value of each particle.

[0231] 3. Evolution and update stage:

[0232] Particle Swarm Optimization (PSO):

[0233] For each particle, the fitness value of its current position (i.e., the current test case) is compared with the fitness value of its historical best position (pBest). If the current position is better, pBest is updated.

[0234] Select the global best position (gBest) from the pBest of all particles, that is, the test case with the highest fitness value.

[0235] According to the PSO update rule, the speed and position of each particle are updated to generate new test cases.

[0236] Genetic Algorithm (GA):

[0237] Based on the fitness values ​​of the particles, a portion of the particles (such as the top 50% with higher fitness values) are selected for subsequent crossover and mutation operations.

[0238] Perform crossover operation on the selected particles to generate new test cases. The crossover operation can be performed in single-point crossover, multi-point crossover and other ways, depending on the specific problem.

[0239] Perform mutation operations on the newly generated test cases to increase the diversity of the population. The mutation operation can randomly change one or some input parameters in the test case.

[0240] Here I explain in detail how to generate new test cases:

[0241] Chromosome example:

[0242] Chromosome = [10,50,4,1,1,0,0,operation step vector 1,operation step vector 2,operation step vector 3];

[0243] choose:

[0244] Suppose there is a population containing multiple chromosomes, each with a fitness score. We select two (actually there will be many, two for ease of understanding) chromosomes with higher fitness for crossover.

[0245] cross:

[0246] The choice is to crossover after the numerical data and the categorical data, that is, between [10,50,4,1,1,0,0] and the operation step vector 1. Suppose the two chromosomes we choose are:

[0247] Chromosome A=[10,50,4,1,1,0,0,[0.1,0.2,0.3],[0.4,0.5,0.6],[0.7,0.8,0.9]];

[0248] Chromosome B = [20,60,5,0,0,1,1,[1.0,1.1,1.2],[1.3,1.4,1.5],[1.6,1.7,1.8]];

[0249] Choose to cross between the numerical data and the operation step vector, that is, between [10,50,4,1,1,0,0] and the operation step vector 1.

[0250] Chromosome A'=[10,50,4,1,1,0,0,[1.0,1.1,1.2],[1.3,1.4,1.5],[1.6,1.7,1.8]];

[0251] Chromosome B' = [20, 60, 5, 0, 0, 1, 1, [0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]];

[0252] The numerical data is kept unchanged. Thus, the new chromosomes A' and B' inherit the characteristics of the parent chromosomes, but introduce new combinations.

[0253] Mutations:

[0254] Mutate the newly generated chromosome. For example, randomly select a gene to mutate, such as the second operation step vector of chromosome A':

[0255] Before mutation:

[0256] Chromosome A'=[10,50,4,1,1,0,0,[1.0,1.1,1.2],[1.3,1.4,1.5],[1.6,1.7,1.8]];

[0257] After mutation:

[0258] Chromosome A”=[10,50,4,1,1,0,0,[1.0,1.1,1.2],[1.3,1.4,1.5+0.1],[1.6,1.7,1.8]];

[0259] In this way, new test cases are generated through genetic algorithms, which contain the genetic information of the original chromosomes.

[0260] Finally, according to the encoding rules, the mutated chromosomes are converted into text test cases.

[0261] 4. Termination conditions:

[0262] If the preset number of iterations (Tmax) is reached, that is, 100 iterations are completed, or other termination conditions are met (such as the fitness value reaches the preset threshold), the algorithm stops and outputs the optimal solution, that is, the test case with the highest fitness value as the optimized test case.

[0263] 5. Test results and application:

[0264] The optimized test cases are applied to the actual test of the software system, and the test efficiency and coverage are observed to verify the effectiveness of the genetic particle swarm optimization algorithm in test case generation.

[0265] Through the above examples, by combining the global search capability of GA and the fast convergence ability of PSO, high-quality test cases can be generated more efficiently, improving the test efficiency and coverage of the software system.

[0266] The pseudo code example is as follows:

[0267] #Step 1: Initialization phase

[0268] def initialize_parameters():

[0269] #Set parameters

[0270] N=30# Particle swarm size

[0271] Tmax=100#maximum number of iterations

[0272] Pc=0.8#Crossover probability of genetic algorithm

[0273] Pm=0.05#Genetic algorithm mutation probability

[0274] w=0.9#PSO inertia weight

[0275] c1=2#PSO individual learning factor

[0276] c2=2#PSO's social learning factor

[0277] # Initialize particle swarm

[0278] particles = [initialize_particle() for_in range(N)] # Initialize N particles

[0279] pBest = [None] * N # the best historical position of each particle

[0280] gBest=None#global best position

[0281] return N,Tmax,Pc,Pm,w,c1,c2,particles,pBest,gBest

[0282] # Calculate fitness

[0283] def calculate_fitness(particle,test_environment):

[0284] #Assume that the function calculates the fitness value based on the results of the test case after execution in the test environment

[0285] #This is simplified to return a random value as an example

[0286] fitness_value=evaluate_test_case(particle,test_environment)

[0287] return fitness_value

[0288] #Particle Swarm Optimization (PSO)

[0289] def update_pso(particles,pBest,gBest,w,c1,c2):

[0290] for iin range(len(particles)):

[0291] position,velocity=particles[i]

[0292] # Update individual best position

[0293] if calculate_fitness(position,test_environment)>calculate_fitness(pBest[i],test_environment):

[0294] pBest[i]=position

[0295] # Update the global best position

[0296] if pBest[i]is not None and(gBest is None or calculate_fitness(pBest[i],test_environment)>calculate_fitness(gBest,test_environment)):

[0297] gBest=pBest[i]

[0298] #Update the particle's speed and position

[0299] velocity=w*velocity+c1*random()*(pBest[i]-position)+c2*random()*(gBest-position)

[0300] position+=velocity

[0301] particles[i]=position,velocity

[0302] #Genetic Algorithm (GA)

[0303] def update_ga(particles,pBest,Pc,Pm):

[0304] #Select particles with higher fitness for crossover and mutation

[0305] selected_particles=select_particles(particles,pBest,Pc)

[0306] new_particles = []

[0307] for iin range(0,len(selected_particles),2):

[0308] # Crossover Operation

[0309] child1,child2=crossover(selected_particles[i],selected_particles[i+1])

[0310] #Mutation Operation

[0311] child1=mutate(child1,Pm)

[0312] child2=mutate(child2,Pm)

[0313] new_particles.extend([child1,child2])

[0314] #Replace some old particles

[0315] particles=replace_particles(particles,new_particles)

[0316] return particles

[0317] #Main algorithm

[0318] def hybrid_ga_pso(test_environment):

[0319] # Initialize parameters and particle swarm

[0320] N,Tmax,Pc,Pm,w,c1,c2,particles,pBest,gBest=initialize_parameters()

[0321] for t in range(Tmax):

[0322] # Calculate fitness

[0323] for i in range(N):

[0324] pBest[i]=particles[i][0]if pBest[i]is None else pBest[i]

[0325] fitness=calculate_fitness(particles[i][0],test_environment)

[0326] #Particle Swarm Optimization (PSO)

[0327] update_pso(particles,pBest,gBest,w,c1,c2)

[0328] #Genetic Algorithm (GA)

[0329] particles=update_ga(particles,pBest,Pc,Pm)

[0330] # Check termination conditions

[0331] if termination_condition_met(t,gBest):

[0332] break

[0333] # Output the optimal solution

[0334] return gBest

[0335] #Helper Function

[0336] #Generate random positions (test case)

[0337] def generate_random_position():

[0338] #Assuming that the test case consists of a set of parameters, here it is simplified to return a random list

[0339] #According to the actual situation, a more complex test case structure can be generated

[0340] return [random() for_in range(num_parameters)] #num_parameters is the number of parameters in the test case

[0341] #Generate random speed

[0342] def generate_random_velocity():

[0343] #Assume that the speed is also a set of random values

[0344] return [random()-0.5for_in range(num_parameters)] #Speed ​​is usually a bounded value, here it is simplified to [-0.5,0.5]

[0345] #Evaluation test case (sample function, needs to be written according to actual situation)

[0346] def evaluate_test_case(test_case,test_environment):

[0347] #Here the fitness value should be calculated based on the execution results of the test case in the test environment

[0348] #Assume that a random value is returned as an example

[0349] return random()#In actual applications, this value should be based on test coverage or other evaluation indicators

[0350] #Select particles (based on fitness value)

[0351] def select_particles(particles,pBest,Pc):

[0352] #Select particles with higher fitness values ​​for crossover operation

[0353] #This is simplified to random selection (Pc% of particles)

[0354] selected_indices=random.sample(range(len(particles)),int(len(particles)*Pc))

[0355] return[particles[i]for iin selected_indices]

[0356] #Cross operation (sample function, needs to be written according to actual situation)

[0357] def crossover(parent1,parent2):

[0358] #Generate new particles based on a certain crossover strategy (test case)

[0359] #This is simplified to return two identical sub-particles as an example

[0360] return parent1,parent2#In actual applications, crossover strategies should be implemented, such as single-point crossover, multi-point crossover, etc. #Mutation operation (example function, needs to be written according to actual conditions)

[0361] def mutate(particle,Pm):

[0362] #Mutate particles (test cases)

[0363] #This is simplified to return unchanged particles as an example

[0364] return particle#In practical applications, one or some parameters of the particle should be randomly changed

[0365] #Replace particles (sample function, need to be written according to actual situation)

[0366] def replace_particles(particles,new_particles):

[0367] #Replace some old particles with newly generated particles

[0368] #Here is simplified to random replacement as an example

[0369] replace_indices=random.sample(range(len(particles)),len(new_particles))

[0370] for i,new_particle in zip(replace_indices,new_particles):

[0371] particles[i] = new_particle

[0372] return particles

[0373] # Check termination conditions (sample function, need to be written according to actual situation)

[0374] def termination_condition_met(t,gBest):

[0375] #Check whether the preset number of iterations or other termination conditions have been reached

[0376] #This is simplified to just check the number of iterations

[0377] return t>=Tmax

[0378] #Variable and parameter explanation

[0379] #-N: particle swarm size, that is, the number of test cases

[0380] #-Tmax: Maximum number of iterations, the maximum number of rounds the algorithm runs

[0381] #-Pc: The crossover probability of the genetic algorithm determines the proportion of particles that perform the crossover operation

[0382] #-Pm: The mutation probability of the genetic algorithm, which determines the probability of mutation in the particles undergoing mutation operation

[0383] #-w: PSO's inertia weight, which controls the degree to which the particle velocity is maintained

[0384] #-c1, c2: PSO's individual learning factor and social learning factor, which control the speed at which particles move to the individual optimal position and the global optimal position respectively.

[0385] #-particles: particle group, a list containing position and velocity information

[0386] #-pBest: List of the best historical positions of each particle

[0387] #-gBest: The global best position, that is, the position with the highest fitness value among all particles

[0388] #-test_environment: A collection of operating environment and user behavior information used to evaluate the fitness value of the test case

[0389] #-num_parameters: The number of parameters in the test case, determined according to the specific situation

[0390] The pseudo code is interpreted as follows:

[0391] Initialization: First, the particle swarm (i.e., test case set) needs to be initialized, and each particle contains position and velocity information. At the same time, the historical best position (pBest) and global best position (gBest) of each particle are initialized.

[0392] Evaluation: Each particle in the initial particle swarm (i.e., test case) is evaluated in the test environment (test_environment) and its fitness value is calculated.

[0393] Iteration: Perform a certain number of iterations, each of which includes the following steps:

[0394] a. Update speed and position: Update the speed and position of each particle according to the PSO formula.

[0395] b. Evaluate the new position: Evaluate the updated particle position and calculate its fitness value.

[0396] c. Update the historical best position: If the fitness value of the new position is better than the historical best position of the particle (pBest), update pBest.

[0397] d. Update the global best position: Among the pBest of all particles, find the one with the highest fitness value as the global best position (gBest).

[0398] e. Genetic operation: According to the idea of ​​genetic algorithm, some particles are selected for crossover and mutation operations to generate new particles and replace some old particles.

[0399] Termination condition: Check whether the termination condition is met (such as reaching the maximum number of iterations). If so, stop the iteration.

[0400] Output: Output the global best position (gBest), which is usually the best solution or best test case found.

[0401] Function calling sequence and input and output:

[0402] initialization:

[0403] Input: particle swarm size N, number of test case parameters num_parameters, parameters of PSO and GA (such as w, c1, c2, Pc, Pm), etc.

[0404] Output: initialized particle swarm (including position and velocity information), pBest list and gBest.

[0405] Evaluate:

[0406] Input: particles (ie test cases), test environment (test_environment).

[0407] Output: The fitness value of the particle.

[0408] Update speed and position (PSO part):

[0409] Input: current particle swarm (including position and velocity), pBest, gBest, PSO parameters (w, c1, c2).

[0410] Output: Updated particle swarm (including new position and velocity).

[0411] Genetic operation (GA part):

[0412] Input: particle swarm, pBest, Pc, Pm.

[0413] Output: A new particle swarm (or just new particles) generated by the crossover and mutation operations.

[0414] Replace particles:

[0415] Input: old particle group, new particles.

[0416] Output: A new particle group after replacing some old particles.

[0417] Check termination conditions:

[0418] Input: current number of iterations t, gBest, maximum number of iterations Tmax.

[0419] Output: A Boolean value indicating whether the termination condition is met.

[0420] Output:

[0421] Input: gBest.

[0422] Output: The global best position (i.e. the optimal solution or the best test case).

[0423] Although the present disclosure has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present disclosure.

Claims

1. A test case generation method, characterized in that: The method comprises: Collect the operating environment and user behavior information to be tested; Encode the operating environment and user behavior information according to the encoding rules, convert each operation step in the operating environment and user behavior information into a vector of fixed length, and construct chromosomes based on the vectors. Each chromosome represents a test case. Each chromosome is represented as a particle, and the fitness value of the chromosome is calculated by the particle swarm optimization algorithm. The genetic algorithm is used to iterate based on the fitness value of the particle to obtain the mutated chromosome. According to the encoding rules, the mutated chromosomes are converted into text test cases.

2. A test case generation method according to claim 1, characterized in that: Operating environment, including: operating system version, hardware configuration and network status.

3. A test case generation method according to claim 1, characterized in that: User behavior information is a sequence of various interaction information between the user and the interface, including login, click, and input.

4. A test case generation method according to claim 1, characterized in that: Encode the operating environment and user behavior information according to the encoding rules, including: Convert the operating environment and user behavior information into Json format.

5. A test case generation method according to claim 4, characterized in that: Each operation step in the operating environment and user behavior information is converted into a vector of fixed length, and a chromosome is constructed based on the vector, including: For the operation step sequence in Json format, we use embedded coding to convert discrete variables into continuous vectors, and each operation step is converted into a vector of fixed length. Concatenate the vectors to get a complete operation step vector; Encode the running environment as an environment vector; Concatenate all the data items of the encoded environment vector and the operation step vector into a chromosome.

6. A test case generation method according to claim 1, characterized in that: Each chromosome is represented as a particle. The fitness value of the chromosome is calculated by the particle swarm optimization algorithm. The genetic algorithm is used to iterate based on the fitness value of the particle to obtain the mutated chromosome, including: Randomly generate positions and velocities for each particle in the search space; For each particle, compare the fitness value of the current test case position with the fitness value of its historical best position pBest. If the current test case position is better, update pBest. Select the global best position gBest from the pBest of all particles, that is, the test case with the highest fitness value; According to the PSO update rules, the speed and position of each particle are updated to generate new test cases; Based on the fitness values ​​of the particles, the particles with the top 50% fitness values ​​are selected for subsequent crossover and mutation operations; Perform crossover operation on selected particles to generate new test cases; the crossover operation adopts single-point crossover or multi-point crossover; Perform mutation operation on the newly generated test case, and the mutation operation randomly changes one or some input parameters in the test case.

7. An intelligent testing method based on digital twin technology, characterized in that: Build a digital twin system based on the network environment to be tested, operating equipment data, and user behavior data; Based on the network environment to be tested, the running device data and the user behavior data, a test case generation method according to any one of claims 1 to 6 is adopted to generate a test case; Use test cases to conduct testing in the digital twin system.

8. A test case generating device, characterized in that: include: Information collection unit, encoding unit, chromosome mutation unit and test case generation unit; An information collection unit, used to collect the operating environment to be tested and user behavior information; The encoding unit is used to encode the operating environment and user behavior information according to the encoding rules, convert each operation step in the operating environment and user behavior information into a vector of fixed length, and construct chromosomes based on the vectors, each chromosome representing a test case; The chromosome mutation unit is used to represent each chromosome as a particle, calculate the fitness value of the chromosome using the particle swarm optimization algorithm, and iterate based on the fitness value of the particle using the genetic algorithm to obtain the mutated chromosome; The test case generation unit is used to convert the mutated chromosome into a text test case according to the encoding rules.

9. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other via the communication bus; a memory storing a computer program; A processor, used to implement a test case generation method according to any one of claims 1 to 6 when executing a computer program stored in a memory.

10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, a test case generation method according to any one of claims 1 to 6 is implemented.