CN115509923A - Unit test case generation method and device and computer storage medium - Google Patents

Unit test case generation method and device and computer storage medium Download PDF

Info

Publication number
CN115509923A
CN115509923A CN202211207269.8A CN202211207269A CN115509923A CN 115509923 A CN115509923 A CN 115509923A CN 202211207269 A CN202211207269 A CN 202211207269A CN 115509923 A CN115509923 A CN 115509923A
Authority
CN
China
Prior art keywords
file
tested
information
test
unit test
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202211207269.8A
Other languages
Chinese (zh)
Inventor
吴玉莲
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Ping An Bank Co Ltd
Original Assignee
Ping An Bank Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Ping An Bank Co Ltd filed Critical Ping An Bank Co Ltd
Priority to CN202211207269.8A priority Critical patent/CN115509923A/en
Publication of CN115509923A publication Critical patent/CN115509923A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3684Test management for test design, e.g. generating new test cases

Abstract

The application discloses a unit test case generation method, a unit test case generation device and a computer storage medium, wherein the unit test case generation method comprises the following steps: acquiring a file to be tested and test information of the file to be tested, wherein the test information comprises a generation path of a unit test case, overtime ending process time, a case generation mode and an association state; analyzing the source code of the file to be tested to obtain file related information of the source code; and generating a unit test case of the file to be tested based on the test information and the file related information. By means of the method, the unit test case can be automatically generated by analyzing the source code of the file to be tested and based on the analysis content, operation steps of a user are reduced, and test working efficiency is improved.

Description

Unit test case generation method and device and computer storage medium
Technical Field
The present disclosure relates to the field of software testing, and in particular, to a method and an apparatus for generating unit test cases, and a computer storage medium.
Background
With the continuous development of the internet industry, the software is larger and larger in scale and complex in structure in the software development process, and the quality of the software becomes important at the moment. In order to verify whether the software can meet the needs of the user, the software needs to be tested. There are many strategies for software testing, including unit testing, which is testing against the smallest unit in software.
In an application scenario, when a user performs unit testing on software, test case codes need to be written manually to test each tested code, and due to the fact that the tested codes are different, the test case codes need to be modified manually according to the tested codes, and therefore the testing process is very complicated. The existing unit test case generation tool cannot generate unit test cases in batch aiming at different tested codes, and the automation rate is low.
Disclosure of Invention
The technical problem mainly solved by the application is how to improve the efficiency of automatically generating the unit test cases, and therefore, the application provides a unit test case generation method, a unit test case generation device and a computer storage medium.
In order to solve the technical problem, the application adopts a technical scheme that: a method for generating unit test cases is provided, which comprises: acquiring a file to be tested and test information of the file to be tested, wherein the test information comprises a generation path of a unit test case, overtime ending process time, a case generation mode and an association state; analyzing the source code of the file to be tested to obtain file related information of the source code; and generating a unit test case of the file to be tested based on the test information and the file related information.
The method for acquiring the file to be tested and the test information of the file to be tested comprises the following steps: calling a visual plug-in to generate a configuration window; and selecting the test information of the file to be tested in the configuration window based on a first selection instruction of a user.
The method for analyzing the source code of the file to be tested to obtain the file related information of the source code comprises the following steps: and analyzing the source code of the file to be tested, and identifying method information, introduction information, statement information and calling information in the source code as file related information.
The method information comprises the following steps: methods, parameters used in methods, and types thereof, and interfaces in methods.
The unit test case of the file to be tested is generated based on the test information and the file related information, and the method comprises the following steps: generating a plurality of test sentences corresponding to the files to be tested based on the file related information; writing a plurality of test statements into corresponding files under the generated path in the test information; and generating unit test cases of the files to be tested based on the corresponding files.
Writing a plurality of test statements into corresponding files under a generated path in the test information, wherein the steps comprise: writing test method declaration sentences and execution sentences in the test sentences into java files under a generation path in the test information; writing the simulation data and assertion information used by the test statements into an xml file under a generation path in the test information; and generating a corresponding file based on the java file and the xml file.
The method comprises the following steps of analyzing the source code of the file to be tested to obtain the file related information of the source code: and configuring a simulation object of the file to be tested based on the source code, wherein the simulation object is used for simulating the functions which are not realized in the file to be tested.
Before generating a unit test case of a file to be tested based on the test information and the file related information, the method further comprises the following steps: and generating initialization configuration information of the file to be tested, wherein the initialization configuration information is used for storing prompt information before and after testing and loading a configuration file corresponding to the file to be tested.
To solve the above technical problem, another technical solution adopted by the present application is: the unit test case generation device comprises a processor and a memory, wherein the memory is coupled with the processor, the memory stores program data, and the processor is used for executing the program data to realize the unit test case generation method.
In order to solve the above technical problem, another technical solution adopted by the present application is: there is provided a computer-readable storage medium storing program data for implementing the above-described unit test case generation method when executed.
The beneficial effect of this application is: different from the situation of the prior art, the unit test case generation method provided by the application is applied to a unit test case generation device, and the unit test case generation device acquires a file to be tested and test information of the file to be tested, wherein the test information comprises a generation path of the unit test case, overtime ending process time, a case generation mode and an association state; analyzing the source code of the file to be tested to obtain file related information of the source code; and generating a unit test case of the file to be tested based on the test information and the file related information. Through the mode, compared with a conventional unit test case generation method, the method for generating the unit test case by analyzing the source code of the file to be tested and based on the analysis content can generate the complete unit test case by analyzing the source code, a user does not need to fill the test case code too much, only the parameters which accord with business logic are modified, the operation flow of the user is reduced, the test time is shortened, and the working efficiency of the user is improved. The unit test case generation method is based on the source code generation unit test cases, can generate corresponding unit test cases in batches aiming at different files to be tested, can help users to develop and test at the same time, and can quickly find problems appearing in development codes. And the automatic generation in the unit test case generation method improves the user experience, so that the user can more conveniently configure the generation of the unit test case, and the potential safety hazard that the source code is possibly invaded when a tool is used for generating the unit test case can be avoided.
Drawings
In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed to be used in the description of the embodiments are briefly introduced below, and it is obvious that the drawings in the following description are only some embodiments of the present application, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without creative efforts. Wherein:
FIG. 1 is a flowchart illustrating an embodiment of a method for generating unit test cases according to the present disclosure;
FIG. 2 is a schematic flowchart of the unit test case generating apparatus according to the present application for executing a method for generating unit test cases;
FIG. 3 is a schematic diagram of a test file generated by the unit test case generation method provided in the present application;
FIG. 4 is a schematic structural diagram of a first embodiment of a unit test case generating apparatus provided in the present application;
FIG. 5 is a schematic structural diagram of a second embodiment of a unit test case generating apparatus provided in the present application;
FIG. 6 is a schematic structural diagram of an embodiment of a computer-readable storage medium provided in the present application.
Detailed Description
The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is obvious that the described embodiments are only a part of the embodiments of the present application, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
Referring to fig. 1 and fig. 2, fig. 1 is a schematic flowchart of an embodiment of a unit test case generation method provided in the present application; fig. 2 is a schematic flow chart of the unit test case generation apparatus provided in the present application for executing the unit test case generation method.
The unit test is to divide the whole software into units, and then to detect the units, i.e. the functions realized by the modules, classes and functions in the software code, to see whether the desired expectations are met or not and whether the functional requirements are met or not. In object-oriented programs, the unit in unit testing is mainly to test the class in the code. A class is an abstraction of a class of things in real life that have common characteristics. The inside of the class encapsulates attributes and methods for manipulating the members of itself. A class is a definition of an object, having behaviors that describe what an object can do and the way it can do, and are the programs and processes that can operate on that object. It contains information about the way an object behaves, including its name, properties, methods, and events.
As shown in fig. 1 and fig. 2, the method for generating unit test cases according to the embodiment of the present application includes:
step 11: the method comprises the steps of obtaining a file to be tested and test information of the file to be tested, wherein the test information comprises a generation path of a unit test case, overtime ending process time, a case generation mode and an association state.
Specifically, a user clicks a file to be tested through a right key, an operation menu is popped up, a unit test case device is selected and used in the operation menu, and the unit test case generation device generates a unit test case based on the file to be tested selected by the user.
Specifically, the unit test case generation device calls the visual plug-in, and generates a configuration window on the display interface for the user to perform visual interface configuration on the unit test case. The unit test case generating device selects the test information of the file to be tested in the configuration window based on a first selection instruction of a user. The test information comprises a generation path of the unit test case, a timeout ending process time, a case generation mode and a correlation state. The unit test case generation device can enable a user to flexibly set the generation path, the overtime ending process time, the case generation mode and the associated state of the meta test case through the configuration window generated by the visual plug-in, the operation is simple, and the use of the user is convenient.
Alternatively, the user may select the generation path of the unit test case by selecting the path from the file list, or may determine the generation path of the unit test case by directly inputting the path. The user may also enter the timeout ending process time for the unit test case generation in the information box. The overtime ending process time is used for ending the current generation process and restarting the next generation process after the overtime time when the unit test case generation device does not normally run due to process errors, so that the problem of process blockage is avoided.
The user can select the generation mode of the test case in the test case generation mode list box in the configuration window in a check mode, wherein the test case generation mode list box comprises two test frameworks, namely PowerMockRunner and SpringGrunner. The PowerMockRunner test framework can simulate a static method, delete a static initialization program, allow simulation without relying on injection, and the PowerMock also contains some utilities, so that a user can more easily access the internal state of an object, and the SpringGrunner test framework needs to be connected with a database to perform network request. The user can also select the correlation state of the file to be tested and the database, can acquire the data in the database by connecting with the database, and substitutes the data into the unit test case to test the file to be tested.
Step 12: and analyzing the source code of the file to be tested to obtain the file related information of the source code.
Optionally, the unit test case generating device further calculates the number of classes in the unit test case to be generated according to the file to be tested, and starts a certain number of threads to start the generation process of the unit test case for the file to be tested according to the utilization rate of the device processor.
Specifically, the unit test case generation device analyzes the source code of the file to be tested, and identifies method information, introduction information, declaration information and calling information in the source code as file related information, wherein the method information comprises a plurality of methods, parameters used in the methods, types of the parameters and interfaces in the methods.
The method includes several statements which are an ordered set of steps for solving a class of problems, so that the method functions to solve a particular problem, and parameters in the method can be divided into form parameters and actual parameters, wherein the form parameters represent a function or process name, and the actual parameters are actual values used when the function or process is called in the method. An interface is a declaration of a series of methods, which is a collection of some method features.
The import information contains information in the source code that references other classes. The declaration information contains a declaration of the class and the method in the source code for indicating the existence of the class and the method. The calling information includes information for calling another method in the source code. The unit test case generation device acquires all the information obtained from the corresponding fields from the source codes as the file related information of the file to be tested, and is used for generating the corresponding test sentences later.
Step 13: and generating a unit test case of the file to be tested based on the test information and the file related information.
Specifically, the unit test case generating device generates a plurality of test sentences corresponding to the files to be tested based on the file related information. Referring to fig. 3, fig. 3 is a schematic view of a test file generated by the unit test case generation method provided by the present application.
The unit test case generation device writes test method statement sentences and execution sentences in the test sentences into java files under a generation path in the test information; writing the simulation data and assertion information used by the test statements into an xml file under a generation path in the test information; generating a corresponding file based on the java file and the xml file; and generating unit test cases of the files to be tested based on the corresponding files.
The test method declares statements and execution statements to be used for carrying out a specific test flow, the simulation data is data used in actual test, and the assertion information is used for representing and verifying results expected by a software developer, and when a program is executed to the position of the assertion, the corresponding assertion should be true. If the assertion is not true, the program may halt execution and provide an error message. The java file and the xml file generated by the unit test case generating device are combined to form the whole unit test case of the file to be tested.
Specifically, after analyzing the source code of the file to be tested based on the configuration information, the unit test case generating device also configures a simulation object of the file to be tested based on the source code, where the simulation object is used to simulate a function that is not temporarily implemented in the file to be tested, and the function is not a main content of the test that needs to be tested, but is related to the function that needs to be implemented by the file to be tested. By using the simulation object, the simulation object can be constructed by the method for realizing the effect achieved by the class or the method without calling the class or the method by using an interface, so that the configuration time of the unit test case generation device is reduced, and the working efficiency is improved.
With reference to fig. 3, the unit test case generating device generates the initialization configuration information of the document to be tested before generating the unit test case of the document to be tested based on the test information and the document related information. The initialization configuration information is used for storing prompt information before and after testing and loading a configuration file corresponding to a file to be tested. For example, prior to running a test file, a prompt may be displayed to the user as to what function the test is to test. The configuration file is the configuration parameters and initial settings of the generated unit test case and the annotation content written by the user for the unit test case.
Different from the situation of the prior art, the unit test case generation method provided by the application is applied to a unit test case generation device, and the unit test case generation device acquires a file to be tested and test information of the file to be tested, wherein the test information comprises a generation path of the unit test case, overtime ending process time, a case generation mode and an association state; analyzing the source code of the file to be tested to obtain file related information of the source code; and generating a unit test case of the file to be tested based on the test information and the file related information. Through the mode, compared with the conventional unit test case generation method, the method for generating the unit test case by analyzing the source code of the file to be tested and based on the analysis content can generate the complete unit test case by analyzing the source code, a user does not need to fill the test case code excessively, only the parameters which accord with the business logic are modified, the operation flow of the user is reduced, the test time is shortened, and the working efficiency of the user is improved. The unit test case generation method is based on the source code generation unit test cases, can generate corresponding unit test cases in batches aiming at different files to be tested, can help users to develop and test at the same time, and can quickly find problems appearing in development codes. And the automatic generation in the unit test case generation method improves the user experience, so that the user can more conveniently configure the generation of the unit test case, and the potential safety hazard that the source code is possibly invaded when a tool is used for generating the unit test case can be avoided.
The method of the foregoing embodiment may be implemented by using a unit test case generating device, which is described below with reference to fig. 4, where fig. 4 is a schematic structural diagram of a first embodiment of the unit test case generating device provided in this application.
As shown in fig. 4, the unit test case generating device 40 according to the embodiment of the present application includes an obtaining module 41, an analyzing module 42, and a case generating module 43.
The obtaining module 41 obtains a file to be tested and test information of the file to be tested, where the test information includes a generation path of a unit test case, a timeout ending process time, a case generation mode, and an association state.
And the analysis module 42 is configured to analyze the source code of the file to be tested to obtain file related information of the source code.
A case generating module 43, configured to generate unit test cases of the files to be tested based on the test information and the file related information.
The method of the foregoing embodiment may be implemented by using a unit test case generating device, and referring to fig. 5, fig. 5 is a schematic structural diagram of a second embodiment of the unit test case generating device provided in the present application, where the unit test case generating device 50 includes a memory 51 and a processor 52, the memory 51 is used for storing program data, and the processor 52 is used for executing the program data to implement the following method:
acquiring a file to be tested and test information of the file to be tested, wherein the test information comprises a generation path of a unit test case, overtime ending process time, a case generation mode and an association state; analyzing the source code of the file to be tested to obtain file related information of the source code; and generating a unit test case of the file to be tested based on the test information and the file related information.
Referring to fig. 6, fig. 6 is a schematic structural diagram of an embodiment of a computer-readable storage medium 60 provided in the present application, where the computer-readable storage medium 60 stores program data 61, and when the program data 61 is executed by a processor, the method is implemented as follows:
acquiring a file to be tested and test information of the file to be tested, wherein the test information comprises a generation path of a unit test case, overtime ending process time, a case generation mode and an association state; analyzing the source code of the file to be tested to obtain file related information of the source code; and generating a unit test case of the file to be tested based on the test information and the file related information.
Embodiments of the present application may be implemented in software functional units and may be stored in a computer readable storage medium when sold or used as a stand-alone product. Based on such understanding, the technical solution of the present application may be substantially implemented or contributed by the prior art, or all or part of the technical solution may be embodied in a software product, which is stored in a storage medium and includes instructions for causing a computer device (which may be a personal computer, a server, a network device, or the like) or a processor (processor) to execute all or part of the steps of the method according to the embodiments of the present application. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk, or an optical disk, and various media capable of storing program codes.
The above description is only for the purpose of illustrating embodiments of the present application and is not intended to limit the scope of the present application, and all modifications of equivalent structures and equivalent processes, which are made by the contents of the specification and the drawings of the present application or are directly or indirectly applied to other related technical fields, are also included in the scope of the present application.

Claims (10)

1. A method for generating unit test cases, the method comprising:
acquiring a file to be tested and test information of the file to be tested, wherein the test information comprises a generation path of the unit test case, overtime ending process time, a case generation mode and an association state;
analyzing the source code of the file to be tested to obtain file related information of the source code;
and generating a unit test case of the file to be tested based on the test information and the file related information.
2. The generation method according to claim 1,
the acquiring of the file to be tested and the test information of the file to be tested includes:
calling a visual plug-in to generate a configuration window;
and selecting the test information of the file to be tested in the configuration window based on a first selection instruction of a user.
3. The generation method according to claim 1,
the analyzing the source code of the file to be tested to obtain the file related information of the source code comprises:
analyzing the source code of the file to be tested, and identifying method information, introduction information, statement information and calling information in the source code as the related information of the file.
4. The generation method according to claim 3,
the method information comprises the following steps:
methods, parameters used in the methods and their types, and interfaces in the methods.
5. The generation method according to claim 3,
the generating the unit test case of the file to be tested based on the test information and the file related information comprises:
generating a plurality of test sentences corresponding to the files to be tested based on the file related information;
writing the test statements into corresponding files under the path generated in the test information;
and generating unit test cases of the files to be tested based on the corresponding files.
6. The generation method according to claim 5,
the writing the test statements into the corresponding files under the path generated in the test information includes:
writing test method declaration sentences and execution sentences in the test sentences into java files under a generation path in the test information;
writing the simulation data and assertion information used by the test statements into an xml file under a generation path in the test information;
and generating the corresponding file based on the java file and the xml file.
7. The generation method according to claim 1,
after analyzing the source code of the file to be tested to obtain the file related information of the source code, the method further comprises the following steps:
and configuring a simulation object of the file to be tested based on the source code, wherein the simulation object is used for simulating functions which are not realized in the file to be tested temporarily.
8. The generation method according to claim 1,
before generating the unit test case of the file to be tested based on the test information and the file related information, the method further comprises the following steps:
and generating initialization configuration information of the file to be tested, wherein the initialization configuration information is used for storing prompt information before and after testing and loading a configuration file corresponding to the file to be tested.
9. A unit test case generation apparatus, comprising a memory and a processor coupled to the memory;
wherein the memory is adapted to store program data and the processor is adapted to execute the program data to implement the unit test case generation method of any of claims 1 to 8.
10. A computer storage medium for storing program data for implementing a method for generating unit test cases as claimed in any one of claims 1 to 8 when executed by a computer.
CN202211207269.8A 2022-09-29 2022-09-29 Unit test case generation method and device and computer storage medium Pending CN115509923A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202211207269.8A CN115509923A (en) 2022-09-29 2022-09-29 Unit test case generation method and device and computer storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202211207269.8A CN115509923A (en) 2022-09-29 2022-09-29 Unit test case generation method and device and computer storage medium

Publications (1)

Publication Number Publication Date
CN115509923A true CN115509923A (en) 2022-12-23

Family

ID=84507914

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202211207269.8A Pending CN115509923A (en) 2022-09-29 2022-09-29 Unit test case generation method and device and computer storage medium

Country Status (1)

Country Link
CN (1) CN115509923A (en)

Similar Documents

Publication Publication Date Title
US10642721B2 (en) Generation of automated testing scripts by converting manual test cases
Memon Automatically repairing event sequence-based GUI test suites for regression testing
US8429614B2 (en) Method and apparatus of effective functional test data generation for web service testing
US8924937B1 (en) Method and system for generating verification information and tests for software
CN110716870B (en) Automatic service testing method and device
CN102667730B (en) Design time debugging
US6061643A (en) Method for defining durable data for regression testing
CN110008113B (en) Test method and device and electronic equipment
US9465718B2 (en) Filter generation for load testing managed environments
US7895575B2 (en) Apparatus and method for generating test driver
Sincero et al. Approaching non-functional properties of software product lines: Learning from products
CN111158656B (en) Test code generation method and device based on fruit tree method
CN110196809B (en) Interface testing method and device
US8661414B2 (en) Method and system for testing an order management system
Li et al. ADAutomation: An activity diagram based automated GUI testing framework for smartphone applications
CN117493188A (en) Interface testing method and device, electronic equipment and storage medium
CN111143228B (en) Test code generation method and device based on decision table method
US10169217B2 (en) System and method for test generation from software specification models that contain nonlinear arithmetic constraints over real number ranges
CN112181852A (en) Interface automatic testing method and device, computer equipment and storage medium
CN109240923B (en) Interface test script generation method and computer readable storage medium
CN111679977A (en) Jest-based React project unit testing method, equipment and storage medium
CN115509923A (en) Unit test case generation method and device and computer storage medium
CN114064469A (en) Interface automation test method and storage medium
CN114297961A (en) Chip test case processing method and related device
CN113220586A (en) Automatic interface pressure test execution method, device and system

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination