Test method, device, equipment and storage medium

By reading and generating multiple test scenario data from the data source, generating simulation objects and inputting the logic to be tested, the problem that JMockit can only simulate one return scenario is solved, and complex test scenarios returned by multiple logic branches and multiple interfaces is realized, which improves test coverage and reduces the test workload.

CN112540920BActive Publication Date: 2025-03-14CHINA CITIC BANK CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202011439792.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-12-10
Publication Date
2025-03-14
Estimated Expiration
2040-12-10

AI Technical Summary

Technical Problem

The existing JMockit tool can only simulate one return scenario in unit testing, resulting in the need to write multi-unit test code to test multiple return scenarios of peripheral interfaces, increasing work pressure.

Method used

By reading at least two test scene parameter groups from the data source and generating logic based on at least one scene, at least two test scene data are generated, thereby generating simulation objects of multiple test scenes, and inputting the logic to be tested to realize complex test scenes returned by multiple logic branches and multiple interfaces.

Benefits of technology

It implements complex test scenarios based on one logic to be tested, only a scene generation logic is required and multiple test cases are configured once, so that multiple logic branches and multiple interfaces can be used to realize complex test scenarios, which improves test coverage and reduces the test workload.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112540920B_ABST
    Figure CN112540920B_ABST
Patent Text Reader

Abstract

The embodiments of the present invention disclose a testing method, apparatus, device and storage medium, which relate to the field of testing technology. The method comprises: reading at least two test scenario parameter groups from a data source, and based on at least one scenario generation logic, generating at least two test scenario data according to the at least two test scenario parameter groups read; generating simulation objects of at least two test scenarios according to the at least two test scenario data; inputting the simulation objects of the at least two test scenarios into the logic to be tested; and determining the test result of the logic to be tested according to the output result of the logic to be tested. The embodiments of the present invention provide a testing method, apparatus, device and storage medium, which realize the simulation of simulation objects of multiple test scenarios based on a single scenario generation logic.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] Embodiments of the present invention relate to the field of testing technology, and in particular to a testing method, device, equipment and storage medium. Background Art

[0002] JMockit is a Java class / interface / object simulation tool. Currently, it is widely used in unit testing of Java applications. It can simulate the peripheral interfaces called by methods or modules, and achieve complete decoupling from peripheral systems during unit testing.

[0003] Although JMockit can simulate the called peripheral interface, it can only simulate one return scenario at a time. In order to improve the unit test coverage and test multiple return scenarios of the peripheral interface, it is necessary to write multiple unit test codes, which increases the work pressure. Summary of the invention

[0004] The embodiments of the present invention provide a testing method, an apparatus, a device and a storage medium to implement simulation of simulation objects of multiple test scenarios based on a single scenario generation logic.

[0005] In a first aspect, an embodiment of the present invention provides a testing method, the method comprising:

[0006] Reading at least two test scenario parameter groups from a data source, and generating at least two test scenario data according to the read at least two test scenario parameter groups based on at least one scenario generation logic;

[0007] Generate simulation objects of at least two test scenarios according to the at least two test scenario data;

[0008] Inputting the simulation objects of the at least two test scenarios into the logic to be tested;

[0009] A test result of the logic to be tested is determined according to an output result of the logic to be tested.

[0010] In a second aspect, an embodiment of the present invention further provides a testing device, the device comprising:

[0011] A scenario data generation module, used to read at least two test scenario parameter groups from a data source, and generate at least two test scenario data according to the at least two test scenario parameter groups read based on at least one scenario generation logic;

[0012] A simulation object generation module, used to generate simulation objects of at least two test scenarios according to the at least two test scenario data;

[0013] A simulation object input module, used for inputting the simulation objects of the at least two test scenarios into the logic to be tested;

[0014] The test result generating module is used to determine the test result of the logic to be tested according to the output result of the logic to be tested.

[0015] In a third aspect, an embodiment of the present invention further provides a device, the device comprising:

[0016] one or more processors;

[0017] a storage device for storing one or more programs,

[0018] When the one or more programs are executed by the one or more processors, the one or more processors implement the testing method as described in any one of the embodiments of the present invention.

[0019] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements a test method as described in any one of the embodiments of the present invention.

[0020] The embodiment of the present invention reads at least two test scenario parameter groups from a data source, and based on at least one scenario generation logic, generates at least two test scenario data according to the at least two test scenario parameter groups read; based on the at least two test scenario data, generates simulation objects of at least two test scenarios, thereby achieving a complex test scenario with multiple logical branches and multiple interface returns according to one logic to be tested, only one scenario generation logic needs to be written and at least two test case configurations need to be configured, thereby improving the test coverage.

[0021] In addition, for designs based on interface programming, the technical solution of the embodiments of the present invention can test methods in all classes that inherit from a unified interface, that is, writing a scenario generation logic can test a group of logics to be tested, achieving a one-to-one code volume for the scenario generation logic and the logic to be tested, greatly reducing the testing workload. BRIEF DESCRIPTION OF THE DRAWINGS

[0022] Figure 1 A flow chart of a testing method provided in Embodiment 1 of the present invention;

[0023] Figure 2 is a flow chart of a testing method provided in Example 2 of the present application;

[0024] Figure 3 is a flow chart of a testing method provided in Example 3 of the present application;

[0025] Figure 4 is a flow chart of a testing method provided by Embodiment 4 of the present invention;

[0026] Figure 5is a structural schematic diagram of a testing device provided in Embodiment 5 of the present invention;

[0027] Figure 6 A schematic diagram of the structure of a device provided in Example 6 of the present invention. DETAILED DESCRIPTION

[0028] The present invention will be further described in detail below in conjunction with the accompanying drawings and embodiments. It is to be understood that the specific embodiments described herein are only used to explain the present invention, rather than to limit the present invention. It should also be noted that, for ease of description, only parts related to the present invention, rather than all structures, are shown in the accompanying drawings.

[0029] Embodiment 1

[0030] Figure 1 This is a flow chart of a test method provided in Embodiment 1 of the present invention. This embodiment is applicable to the case where a high coverage and low workload test is performed on the logic to be tested. The method can be executed by a test device, which can be implemented by software and / or hardware. Figure 1 , the testing method provided by the embodiment of the present invention includes:

[0031] S110, reading at least two test scenario parameter groups from a data source, and generating at least two test scenario data according to the read at least two test scenario parameter groups based on at least one scenario generation logic.

[0032] The data source is a storage medium storing at least two test scenario parameter groups. Specifically, the data source may be a file, a database, or a table.

[0033] A test scenario parameter group refers to a combination of at least one parameter describing a test scenario. Typically, the test scenario parameter group can be configured by a tester.

[0034] The at least two test scenario parameter groups include two test scenario parameter groups, three test scenario parameter groups, and more test scenario parameter groups.

[0035] The scenario generation logic is the logic for generating test scenario data.

[0036] Test scenario data refers to data that describes the test scenario.

[0037] Specifically, based on Junit, at least two test scenario parameter groups may be read from a data source, and based on at least one scenario generation logic, at least two test scenario data may be generated according to the at least two test scenario parameter groups read.

[0038] Among them, Junit is a widely used Java unit testing framework.

[0039] S120. Generate simulation objects of at least two test scenarios according to the at least two test scenario data.

[0040] Among them, the simulation object refers to the substitute of the real object during debugging.

[0041] S130: Input simulation objects of the at least two test scenarios into the logic to be tested.

[0042] S140: Determine a test result of the logic to be tested according to an output result of the logic to be tested.

[0043] Specifically, determining the test result of the logic to be tested according to the output result of the logic to be tested includes:

[0044] Compare the output result of the logic to be tested with the corresponding true value, and determine the test result of the logic to be tested according to the comparison result.

[0045] The present invention reads at least two test scenario parameter groups from a data source, and based on at least one scenario generation logic, generates at least two test scenario data according to the at least two test scenario parameter groups read; based on the at least two test scenario data, generates simulation objects of at least two test scenarios, thereby achieving a complex test scenario with multiple logic branches and multiple interface returns according to one logic to be tested, only one scenario generation logic needs to be written and at least two test case configurations need to be configured, thereby improving the test coverage.

[0046] In addition, for designs based on interface programming, the technical solution of the embodiments of the present invention can test methods in all classes that inherit from a unified interface, that is, writing a scenario generation logic can test a group of logics to be tested, achieving a one-to-one code volume for the scenario generation logic and the logic to be tested, greatly reducing the testing workload.

[0047] Embodiment 2

[0048] Figure 2 This is a flow chart of a test method provided in Example 2 of the present application. This embodiment is a further refinement of the above S110 based on the above embodiment. Figure 2 , the testing method provided in this embodiment includes:

[0049] S111. Based on Feed4Junit, read at least two test scenario parameter groups from a data source, and based on at least one scenario generation logic, generate at least two test scenario data according to the read at least two test scenario parameter groups.

[0050] Among them, Feed4Junit is a unit testing framework that separates data from code. It can completely separate test cases from test driver code, and achieve complete decoupling of test cases and test code. It is not the same framework as Junit4. Junit4 does not achieve the separation of data and test code, and data and code need to be coupled together.

[0051] S120. Generate simulation objects of at least two test scenarios according to the at least two test scenario data.

[0052] S130: Input simulation objects of the at least two test scenarios into the logic to be tested.

[0053] S140: Determine a test result of the logic to be tested according to an output result of the logic to be tested.

[0054] The technical solution of the embodiment of the present invention solves the problem of separation of data and code well through Feed4Junit. Feed4Junit is an extension of the JUnit test framework. It makes tests easier to write and maintain by operating test data from files and different data sources.

[0055] Because most of the current test logic is implemented based on the Spring framework, in order to adapt to the Spring framework, the Feed4Junit-based method reads at least two test scenario parameter groups from the data source, and generates logic based on at least one scenario, and generates at least two test scenario data according to the read at least two test scenario parameter groups, including:

[0056] Based on the new unit test framework, at least two test scenario parameter groups are read from a data source, and based on at least one scenario generation logic, at least two test scenario data are generated according to the at least two test scenario parameter groups read;

[0057] The new unit test framework is obtained by modifying the inheritance relationship of Feed4Junit, and the new unit test framework inherits from the executor of the Spring framework.

[0058] Among them, the new unit testing framework is a unit testing framework integrated with Spring, which realizes the separation of data and code, and can completely separate test cases and test driver codes, realizing the complete decoupling of test cases and test codes. It is implemented by extending the Feed4Junit source code. It is not the same framework as Junit4. Junit4 does not realize the separation of data and test code, and data and code need to be coupled together.

[0059] The Spring framework is an open source Java platform that provides a comprehensive infrastructure for easy and fast development of durable Java applications. It was created to address the complexity of enterprise application development. Currently, almost all web systems involved, from state-owned enterprises to Internet companies, from traditional private enterprises to joint-stock companies, are built using the Spring framework.

[0060] Specifically, the executor of the Spring framework may be SpringJunit4Classrunner.

[0061] Embodiment 3

[0062] Figure 3 1 is a flow chart of a test method provided in Example 3 of the present application. This embodiment is a further refinement of S120 based on the above embodiment. Figure 3 , the test method provided in the embodiment of the present application includes:

[0063] S110, reading at least two test scenario parameter groups from a data source, and generating at least two test scenario data according to the read at least two test scenario parameter groups based on at least one scenario generation logic.

[0064] S121. Based on the Mock test logic, generate simulation objects of at least two test scenarios according to the at least two test scenario data.

[0065] Among them, the mock test logic is a test method that uses a virtual object to create some objects that are not easy to construct or obtain during the test process for testing.

[0066] Specifically, the mock test logic can be implemented based on different mock frameworks, including JMockit, PowerMock, EasyMock, Mockito, and JMock.

[0067] Among them, JMockit is a tool that helps create Mock objects. It is developed based on Java and has incomparable advantages in Java testing and development environments. More importantly, it greatly simplifies the use of virtual objects.

[0068] S130: Input simulation objects of the at least two test scenarios into the logic to be tested.

[0069] S140: Determine a test result of the logic to be tested according to an output result of the logic to be tested.

[0070] The technical solution of the embodiment of the present invention generates simulation objects of at least two test scenarios based on the Mock test logic and according to the at least two test scenario data, thereby realizing the generation of the simulation objects.

[0071] Embodiment 4

[0072] Figure 4 is a flow chart of a test method provided by Embodiment 4 of the present invention. This embodiment is an optional solution proposed on the basis of the above embodiment. Figure 4 , the testing method provided in this embodiment includes:

[0073] S210. Based on the new unit test framework, at least two test scenario parameter groups are read from a data source, and based on at least one scenario generation logic, at least two test scenario data are generated according to the at least two test scenario parameter groups read.

[0074] Among them, the new unit testing framework inherits from the executor of the Spring framework.

[0075] S220. Based on the JMockit framework, generate simulation objects of at least two test scenarios according to the at least two test scenario data.

[0076] S230: Input simulation objects of the at least two test scenarios into the logic to be tested.

[0077] S240: Determine a test result of the logic to be tested according to an output result of the logic to be tested.

[0078] The key points of the embodiments of the present invention are:

[0079] 1. Since the Feed4Junit framework does not support the Spring framework, the present invention expands the Feed4Junit source code and recompiles the framework to generate a new unit test framework. The new unit test framework achieves integration with the Spring framework and solves the shortcomings of Feed4Junit.

[0080] 2. Since JMockit can only simulate one test scenario at a time, in order to improve the coverage of unit tests and test multiple test scenarios of peripheral interfaces, the embodiment of the present invention integrates the new unit test framework and JMockit, so that for one logic to be tested, only one scenario generation logic needs to be written and multiple test case configurations need to be made to realize complex test scenarios with multiple logical branches and multiple interface returns.

[0081] The technical effects of the embodiments of the present invention are:

[0082] 1. The present invention can greatly reduce the pressure of unit testing, and improve the coverage rate of unit testing and the quality of code. For example, for a simple login method, if all judgment branches are input for testing, at least 12 test methods need to be written. The checkLogin called by the login is a peripheral interface, and the peripheral interface returns 5 scenarios. If the login method needs to be fully tested, at least 17 unit test methods need to be written, that is, the code volume of the unit test method and the method being tested reaches 17:1. If the method being tested is more complex, this ratio will be even greater. If the new unit test framework of the present invention and the fusion test method of JMockit are used, only one unit test method needs to be written, and the cases of all 17 test scenarios are configured into a database or file to achieve full coverage of the login method. In addition, the login function can also be added to directly configure the test case without writing a unit test method.

[0083] 2. For designs based on interface programming, this solution can unit test all methods in classes that inherit from a unified interface, that is, writing a unit test method can test a group of methods, achieving a code volume of 1:1 between unit test code and tested code, greatly reducing unit testing workload and significantly improving test coverage.

[0084] Embodiment 5

[0085] Figure 5 Schematic diagram of the structure of a test device provided by Embodiment 5 of the present invention. Figure 5 The test device provided by the embodiment of the present invention includes: a scene data generation module 10, a simulation object generation module 20, a simulation object input module 30 and a test result generation module 40.

[0086] The scenario data generation module 10 is used to read at least two test scenario parameter groups from a data source, and based on at least one scenario generation logic, generate at least two test scenario data according to the read at least two test scenario parameter groups;

[0087] A simulation object generation module 20, configured to generate simulation objects of at least two test scenarios according to the at least two test scenario data;

[0088] A simulation object input module 30, used to input the simulation objects of the at least two test scenarios into the logic to be tested;

[0089] The test result generating module 40 is used to determine the test result of the logic to be tested according to the output result of the logic to be tested.

[0090] The embodiment of the present invention reads at least two test scenario parameter groups from a data source, and based on at least one scenario generation logic, generates at least two test scenario data according to the at least two test scenario parameter groups read; based on the at least two test scenario data, generates simulation objects of at least two test scenarios, thereby achieving a complex test scenario with multiple logical branches and multiple interface returns according to one logic to be tested, only one scenario generation logic needs to be written and at least two test case configurations need to be configured, thereby improving the test coverage.

[0091] In addition, for designs based on interface programming, the technical solution of the embodiments of the present invention can test methods in all classes that inherit from a unified interface, that is, writing a scenario generation logic can test a group of logics to be tested, achieving a one-to-one code volume for the scenario generation logic and the logic to be tested, greatly reducing the testing workload.

[0092] Furthermore, the scene data generation module includes:

[0093] The scenario data generation unit is used to read at least two test scenario parameter groups from a data source based on the Feed4Junit unit test framework, and generate at least two test scenario data according to the at least two test scenario parameter groups read based on at least one scenario generation logic.

[0094] Furthermore, the scene data generating unit is specifically used to:

[0095] Modify the inheritance relationship of Feed4Junit to obtain a new unit test framework, so that the new unit test framework inherits from the executor of the Spring framework;

[0096] Based on the new unit test framework, at least two test scenario parameter groups are read from a data source, and based on at least one scenario generation logic, at least two test scenario data are generated according to the at least two test scenario parameter groups read.

[0097] Furthermore, the simulation object generation module includes:

[0098] The simulation object generation unit is used to generate simulation objects of at least two test scenarios based on the Mock test logic and according to the at least two test scenario data.

[0099] Furthermore, the Mock test logic is the logic of creating a mock object by JMockit.

[0100] Embodiment 6

[0101] Figure 6 A schematic diagram of a device structure provided in Embodiment 6 of the present invention is shown in FIG. Figure 6As shown, the device includes a processor 60, a memory 61, an input device 62 and an output device 63; the number of processors 60 in the device can be one or more. Figure 6 A processor 60 is taken as an example; the processor 60, the memory 61, the input device 62 and the output device 63 in the device can be connected by a bus or other means. Figure 6 The example of connecting through bus is taken in the following.

[0102] The memory 61, as a computer-readable storage medium, can be used to store software programs, computer executable programs and modules, such as program instructions / modules corresponding to the test method in the embodiment of the present invention (for example, the scene data generation module 10, the simulation object generation module 20, the simulation object input module 30 and the test result generation module 40 in the test device). The processor 60 executes various functional applications and data processing of the device by running the software programs, instructions and modules stored in the memory 61, that is, implements the above-mentioned test method.

[0103] The memory 61 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system and at least one application required for a function; the data storage area may store data created according to the use of the terminal, etc. In addition, the memory 61 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other non-volatile solid-state storage device. In some instances, the memory 61 may further include a memory remotely arranged relative to the processor 60, and these remote memories may be connected to the device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.

[0104] The input device 62 may be used to receive input digital or character information and generate key signal input related to user settings and function control of the device. The output device 63 may include a display device such as a display screen.

[0105] Embodiment 7

[0106] Embodiment 7 of the present invention further provides a storage medium including computer executable instructions, wherein the computer executable instructions are used to perform a testing method when executed by a computer processor, the method comprising:

[0107] Reading at least two test scenario parameter groups from a data source, and generating at least two test scenario data according to the read at least two test scenario parameter groups based on at least one scenario generation logic;

[0108] Generate simulation objects of at least two test scenarios according to the at least two test scenario data;

[0109] Inputting the simulation objects of the at least two test scenarios into the logic to be tested;

[0110] A test result of the logic to be tested is determined according to an output result of the logic to be tested.

[0111] Of course, the computer executable instructions of the storage medium provided by the embodiment of the present invention are not limited to the above-mentioned method operations, but can also perform related operations in the test method provided by any embodiment of the present invention.

[0112] Through the above description of the implementation methods, the technicians in the relevant field can clearly understand that the present invention can be implemented by means of software and necessary general hardware, and of course it can also be implemented by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as a computer floppy disk, read-only memory (ROM), random access memory (RAM), flash memory (FLASH), hard disk or optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, server, or network device, etc.) to execute the methods described in each embodiment of the present invention.

[0113] It is worth noting that in the embodiment of the above-mentioned search device, the various units and modules included are only divided according to functional logic, but are not limited to the above-mentioned division, as long as the corresponding functions can be achieved; in addition, the specific names of the functional units are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of the present invention.

[0114] Note that the above are only preferred embodiments of the present invention and the technical principles used. Those skilled in the art will understand that the present invention is not limited to the specific embodiments described herein, and that various obvious changes, readjustments and substitutions can be made by those skilled in the art without departing from the scope of protection of the present invention. Therefore, although the present invention has been described in more detail through the above embodiments, the present invention is not limited to the above embodiments, and may include more other equivalent embodiments without departing from the concept of the present invention, and the scope of the present invention is determined by the scope of the appended claims.

Claims

1. A testing method, characterized in that: include: Reading at least two test scenario parameter groups from a data source, and generating at least two test scenario data according to the read at least two test scenario parameter groups based on at least one scenario generation logic, including: Based on the Feed4Junit unit test framework, at least two test scenario parameter groups are read from a data source, and based on at least one scenario generation logic, at least two test scenario data are generated according to the at least two test scenario parameter groups read; Generating simulation objects of at least two test scenarios according to the at least two test scenario data, including: Based on the Mock test logic, generating simulation objects of at least two test scenarios according to the at least two test scenario data; Inputting the simulation objects of the at least two test scenarios into the logic to be tested; A test result of the logic to be tested is determined according to an output result of the logic to be tested.

2. The method according to claim 1, characterized in that: The method of reading at least two test scenario parameter groups from a data source based on Feed4Junit and generating at least two test scenario data according to the read at least two test scenario parameter groups based on at least one scenario generation logic includes: Based on the new unit test framework, at least two test scenario parameter groups are read from a data source, and based on at least one scenario generation logic, at least two test scenario data are generated according to the at least two test scenario parameter groups read; The new unit test framework is obtained by modifying the inheritance relationship of Feed4Junit, and the new unit test framework inherits from the executor of the Spring framework.

3. The method according to claim 1, characterized in that The mock test logic is the logic for JMockit to create mock objects.

4. A testing device, characterized in that: include: A scenario data generation module, used to read at least two test scenario parameter groups from a data source, and generate at least two test scenario data according to the at least two test scenario parameter groups read based on at least one scenario generation logic; The scene data generation module comprises: A scenario data generation unit, configured to read at least two test scenario parameter groups from a data source based on a Feed4Junit unit test framework, and generate at least two test scenario data according to the read at least two test scenario parameter groups based on at least one scenario generation logic; A simulation object generation module, used to generate simulation objects of at least two test scenarios according to the at least two test scenario data; The simulation object generation module comprises: A simulation object generation unit, configured to generate simulation objects of at least two test scenarios based on the Mock test logic and according to the at least two test scenario data; A simulation object input module, used for inputting the simulation objects of the at least two test scenarios into the logic to be tested; The test result generating module is used to determine the test result of the logic to be tested according to the output result of the logic to be tested.

5. The device according to claim 4, characterized in that The scene data generating unit is specifically used for: Based on the new unit test framework, at least two test scenario parameter groups are read from a data source, and based on at least one scenario generation logic, at least two test scenario data are generated according to the at least two test scenario parameter groups read; The new unit test framework is obtained by modifying the inheritance relationship of Feed4Junit, and the new unit test framework inherits from the executor of the Spring framework.

6. An electronic device, characterized in that: The electronic device comprises: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the testing method according to any one of claims 1 to 3.

7. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the testing method according to any one of claims 1 to 3 is implemented.

Citation Information

Patent Citations

  • Function test method of application system, terminal equipment and medium

    CN109783354A