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.
The terms "first", "second" and "third" in this application are used for descriptive purposes only and are not to be construed as indicating or implying relative importance or implying any indication of the number of technical features indicated. Thus, a feature defined as "first," "second," or "third" may explicitly or implicitly include at least one of the feature. In the description of the present application, "plurality" means at least two, e.g., two, three, etc., unless explicitly specifically limited otherwise. All directional indications (such as up, down, left, right, front, and rear … …) in the embodiments of the present application are only used to explain the relative positional relationship between the components, the movement, and the like in a specific posture (as shown in the drawings), and if the specific posture is changed, the directional indication is changed accordingly. Furthermore, the terms "include" and "have," as well as any variations thereof, are intended to cover non-exclusive inclusions. For example, a process, method, system, article, or apparatus that comprises a list of steps or elements is not limited to only those steps or elements listed, but may alternatively include other steps or elements not listed, or inherent to such process, method, article, or apparatus.
Reference herein to "an embodiment" means that a particular feature, structure, or characteristic described in connection with the embodiment can be included in at least one embodiment of the application. The appearances of the phrase in various places in the specification are not necessarily all referring to the same embodiment, nor are separate or alternative embodiments mutually exclusive of other embodiments. It is explicitly and implicitly understood by one skilled in the art that the embodiments described herein can be combined with other embodiments.
Fig. 1 is a flowchart illustrating a method for automatically generating unit test cases according to a first embodiment of the present invention. It should be noted that the method of the present invention is not limited to the flow sequence shown in fig. 1 if the results are substantially the same. As shown in fig. 1, the method comprises the steps of:
step S101: and acquiring a test request of the unit to be tested, wherein the test request comprises basic parameter information of the unit to be tested.
It should be noted that the unit to be tested may be one unit in one item to be tested, and in general, one item to be generated may include a plurality of services, and one service includes a plurality of test objects. For example, a to-be-generated item may include a login service, a registration service, an upload service, and so on. If a certain service in the item to be generated is required to be tested, namely a test request of the item to be generated exists, a unit to be tested related to the test request of the item to be generated is obtained, and the test request is initiated to the unit to be tested, so that a corresponding test case of the request to be tested can be automatically generated. It should be understood that, in this embodiment, batch test case generation for multiple units to be tested can be simultaneously implemented.
Further, in this embodiment, the automatic generation method of the unit test case is implemented based on a Sirius plugin.
Specifically, the Sirius plug-in provides a visual information configuration interface for the generation process of the test case, and through the information configuration interface, a user can conveniently input a test request to generate the test case. It can be understood that, in this embodiment, automatic generation of the test case is realized by using the Sirius plugin, which does not need to use a third-party tool, so that the possibility of source code leakage is reduced, and the graphical operation interface provided by the Sirius plugin greatly optimizes the operation experience of a user, and is convenient to use and high in efficiency.
Step S102: and analyzing the file compiled by the unit to be tested based on the basic parameter information to obtain the parameter information after the file analysis.
Specifically, after a test request is obtained, basic parameter information of a unit to be tested and case parameter information of a generated case are obtained according to the test request, the basic parameter information includes a storage path of the unit to be tested, a file generated after the unit to be tested is compiled can be found through the storage path, and the parameter information required for generating the test case is obtained through analyzing the file.
Further, in some embodiments, step S102 specifically includes:
1. and acquiring the storage path of the class required by the current case generation process from the test request.
Furthermore, the number of CPU cores, the memory occupied, the classes and the number of the classes of the test cases to be generated, the storage path of the generated test cases, whether to generate the baffle information of the database, the mode information of the test cases, the default parameter information, and the like, which are required in the current case generation process, are also acquired from the test request.
Specifically, the parameters in the test request may be obtained through a script command input in advance, for example, the following script command is taken as an example for description: mvn-dmemoryiinmb ═ 2048-dcors ═ 2-Dcuts ═ xxx.xxx.xxx.xxx.xxx-xxx-DtargetFolder ═ src/test/java-dspringdunick ═ false-DtestMode ═ powermockranner sirius: clean sirius: general site: the port of the transport unit is connected with the transport unit, wherein, DmoryInMB 2048 means that the required occupied content is 2048Mb, Dcorps 2 means that the required number of CPU cores is 2, and Dcuts xxx refers to the class, the number of classes and the storage path of the class which are required to generate the test case, when a plurality of classes exist, the classes can be separated by using English commas, DtargetFolder refers to src/test/java and refers to a storage path of a generated test case, DSpringDBLock refers to whether database baffle information needs to be generated, when DSpringDBLock refers to false, i.e., no database fence information needs to be generated, when DSpringDBMock is equal to true, that is, the baffle information of the database needs to be generated, DtestMode ═ powermockranner refers to the mode of the test case, sirius: general site: export refers to the default parameters of a Sirius plug-in.
2. And acquiring the file compiled by the unit to be tested according to the class storage path, and analyzing the file to obtain the parameter information of each method to obtain the parameter information.
Specifically, according to the storage path of the acquired class, a file generated after the unit to be tested is compiled is acquired, the content in the file is identified so as to obtain import content, class declaration, method declaration, annotation, interface injection and other related information, the information is respectively extracted into corresponding sets, each method in the file, namely related information such as method parameters, parameter types, interfaces used in the method, static classes, method calls and the like, is analyzed and used as parameter information obtained through analysis.
Step S103: and based on a preset test case template, generating a test case according to the parameter information after the file analysis, and storing the test case according to a pre-specified storage path.
It should be noted that, the tester develops various test case templates in advance according to the daily test requirements, and a corresponding test case program can be generated through the test case templates. Specifically, when parameter information is obtained by analyzing the compiled file, the test case of the unit to be tested can be automatically generated by assigning the parameter information to the corresponding test case template, and then the test case is stored according to the case storage path specified in the case parameter information, so that the test case of the unit to be tested is automatically generated.
Further, in some embodiments, when the BBD unit test system is used, the BDD-based test case does not completely test our minimum code unit, and tests a similar scenario, although a jmoclit generating barrier is also used, but does not implement a static method, a Mock of a private method, and therefore, when the test request further includes database barrier information, step S103 specifically includes:
1. analyzing baffle information of the database to obtain environment debugging information;
it should be noted that the baffle test can be understood as follows: in some cross-system performance test projects, due to the limitation of objective factors (limited test hardware resources, coordination among multiple systems, and the like), a complete test environment cannot be built to complete the test work, at this time, a system to be tested is generally built, and then functions of other related systems are simulated by adopting a software program, which is generally called a baffle. Further, the baffle may also be referred to as an "off-line system," "sandbox system," or the like.
2. And generating a test case according to the parameter information, and importing the environment debugging information into a database of the test environment of the test case.
Specifically, after the database baffle information is obtained, a corresponding test baffle is generated according to the database baffle information, that is, the environment debugging information obtained from the database baffle information is written into the database of the test environment of the test case, and when the test case is executed, the test environment of the test case is generated according to the environment debugging information.
In this embodiment, the unit to be tested of the database barrier information in the test request is usually a static type or private method, so that the barrier function is realized for the static type and the private method, and the barrier can be configured in various ways according to the requirements of the user and realized by a Sirius plug-in, and the mock configuration process is more convenient and flexible due to the provided graphical interface.
Further, in some embodiments, in order to facilitate the user to manage the test case, step S103 specifically includes:
1. selecting a matched target test case template from preset test case templates according to the parameter information after file analysis;
2. assigning the parameter information to a target test case template to generate a corresponding test case;
3. and (3) writing test method declarations and executions in the test cases into the automatically generated Java file for saving, and writing simulation data and assertion information in the test cases into the automatically generated xml file for saving.
Specifically, the test case templates are preset for the testers, wherein the test case templates include case templates for different test requirements, so that in order to realize automatic generation of test cases, parameter information obtained through analysis is respectively matched with the test case templates, and an optimal test case template is selected as a target test case template to generate the test cases.
In this embodiment, after the test case is generated according to the parameter information, in order to facilitate management of the test case, data in the test case is divided into two parts for storage, where one part is a test method declaration and execution data in the test case, and the part is written into an automatically generated Java file for storage, and a simulation data and assertion information in the other part are written into an automatically generated xml file for storage, so that a user manages the test case.
The automatic generation method of the unit test case of the first embodiment of the invention obtains the test request of the unit to be tested, analyzes the file compiled by the unit to be tested based on the basic parameter information to obtain the parameter information after the file is analyzed, then generates the test case according to the parameter information after the file is analyzed, automatically queries the file compiled by the unit to be tested according to the test request, then analyzes to obtain the parameter information required by the case generation, and then automatically generates the test case of the unit to be tested according to the parameter information.
Fig. 2 is a flowchart illustrating a method for automatically generating unit test cases according to a second embodiment of the present invention. It should be noted that the method of the present invention is not limited to the flow sequence shown in fig. 2 if the results are substantially the same. As shown in fig. 2, the method comprises the steps of:
step S201: and acquiring a test request of the unit to be tested, wherein the test request comprises basic parameter information of the unit to be tested.
In this embodiment, step S201 in fig. 2 is similar to step S101 in fig. 1, and for brevity, is not described herein again.
Step S202: and analyzing the file compiled by the unit to be tested based on the basic parameter information to obtain the parameter information after the file analysis.
In this embodiment, step S202 in fig. 2 is similar to step S102 in fig. 1, and for brevity, is not described herein again.
Step S203: and when the pre-specified information needing to perform Mock exists in the parameter information after the file analysis, performing Mock operation on the information needing to perform Mock according to the Mock parameter information and returning a corresponding value.
It should be noted that, in this embodiment, the test request includes Mock parameter information set for the unit to be tested.
Specifically, when there is information that needs to be Mock operated in the parameter information after file analysis, the Mock operation needs to be performed on the information that needs to be Mock according to the Mock parameter information in the case parameters, and a corresponding value is returned for use in generating the test case.
It should be noted that Mock refers to a test method for creating a virtual object for testing some objects that are not easily constructed or easily obtained during the test process. For example, an alarm clock performs a reminding service according to time, if 5 pm later, an audio file is played to remind people of going to work, if a real object is used for testing, the test can be realized only by waiting for five pm, and in order to avoid the situation, a mock object can be used for testing, so that the control time is simulated without waiting for the clock to turn to 5 pm.
Step S204: and based on a preset test case template, generating a test case according to the parameter information after the file analysis, and storing the test case according to a pre-specified storage path.
In this embodiment, step S204 in fig. 2 is similar to step S103 in fig. 1, and for brevity, is not described herein again.
According to the automatic generation method of the unit test case, on the basis of the first embodiment, when the fact that information needing to be subjected to Mock exists in the analyzed parameter information is determined, a corresponding value is returned after Mock operation is carried out according to the Mock parameter information, and the returned value is used for generating the test case, so that the generated test case can be configured more flexibly.
Fig. 3 is a flowchart illustrating a method for automatically generating unit test cases according to a third embodiment of the present invention. It should be noted that the method of the present invention is not limited to the flow sequence shown in fig. 3 if the results are substantially the same. As shown in fig. 3, the method comprises the steps of:
step S301: and acquiring a test request of the unit to be tested, wherein the test request comprises basic parameter information of the unit to be tested.
In this embodiment, step S301 in fig. 3 is similar to step S101 in fig. 1, and for brevity, is not described herein again.
Step S302: and analyzing the file compiled by the unit to be tested based on the basic parameter information to obtain the parameter information after the file analysis.
In this embodiment, step S302 in fig. 3 is similar to step S102 in fig. 1, and for brevity, is not described herein again.
Step S303: and automatically generating a case base class, wherein the case base class stores the initial configuration of the unit to be tested.
Specifically, when a test case is generated, a base class needs to be generated to store the initialization configuration of the unit to be tested, and when a test is executed, the initialization configuration of the unit to be tested is completed according to the initialization configuration information. In this embodiment, the case base class is a baseunit base class.
Step S304: and based on a preset test case template, generating a test case according to the parameter information after the file analysis, and storing the test case according to a pre-specified storage path.
In this embodiment, step S304 in fig. 3 is similar to step S103 in fig. 1, and for brevity, is not described herein again.
The method for automatically generating the unit test case according to the third embodiment of the present invention is based on the first embodiment, and automatically generates the case base class of the test case when the test case is generated, so as to store the initialization configuration of the automatically generated test case, and automatically configures the relevant parameters in the test case according to the initialization configuration when the test case is subsequently used, without manual configuration.
Fig. 4 is a functional module diagram of an automatic unit test case generation apparatus according to an embodiment of the present invention. As shown in fig. 4, theapparatus 40 includes an obtaining module 41, an analyzingmodule 42, and agenerating module 43.
An obtaining module 41, configured to obtain a test request of a unit to be tested, where the test request includes basic parameter information of the unit to be tested;
theanalysis module 42 is configured to analyze the file compiled by the unit to be tested based on the basic parameter information to obtain parameter information after file analysis;
and the generatingmodule 43 is configured to generate a test case according to the parameter information after the file analysis based on a preset test case template, and store the test case according to a pre-specified storage path.
Optionally, the analyzingmodule 42 executes an operation of analyzing the file compiled by the unit to be tested based on the basic parameter information to obtain parameter information after file analysis, which specifically includes: obtaining a storage path of a class required by the current case generation process from the test request; and acquiring the file compiled by the unit to be tested according to the class storage path, and analyzing the file to obtain the parameter information of each method to obtain the parameter information.
Optionally, when the test request further includes database baffle information, the generatingmodule 43 executes an operation of generating a test case according to the parameter information after the file analysis, which specifically includes: analyzing baffle information of the database to obtain environment debugging information; and generating a test case according to the parameter information, and importing the environment debugging information into a database of the test environment of the test case.
Optionally, the generatingmodule 43 executes an operation of generating a test case based on a preset test case template according to the parameter information after the file analysis, and saving the test case according to a pre-specified storage path, and may further include: selecting a matched target test case template from preset test case templates according to the parameter information after file analysis; assigning the parameter information to a target test case template to generate a corresponding test case; and (3) writing test method declarations and executions in the test cases into the automatically generated Java file for saving, and writing simulation data and assertion information in the test cases into the automatically generated xml file for saving.
Optionally, the test request includes Mock parameter information set for the unit to be tested, and before the generatingmodule 43 executes the operation of generating the test case according to the parameter information after the file analysis, the generating module is further configured to: and when the pre-specified information needing to perform Mock exists in the parameter information after the file analysis, performing Mock operation on the information needing to perform Mock according to the Mock parameter information and returning a corresponding value.
Optionally, before the generatingmodule 43 performs the operation of generating the test case according to the parameter information after the file parsing, it is further configured to: and automatically generating a case base class, wherein the case base class stores the initial configuration of the unit to be tested.
Optionally, the automatic generation method of the unit test case is implemented based on a Sirius plug-in.
For other details of the technical solution implemented by each module in the automatic generation apparatus for unit test cases in the above embodiments, reference may be made to the description of the automatic generation method for unit test cases in the above embodiments, and details are not described herein again.
It should be noted that, in the present specification, the embodiments are all described in a progressive manner, each embodiment focuses on differences from other embodiments, and the same and similar parts among the embodiments may be referred to each other. For the device-like embodiment, since it is basically similar to the method embodiment, the description is simple, and for the relevant points, reference may be made to the partial description of the method embodiment.
Referring to fig. 5, fig. 5 is a schematic structural diagram of a computer device according to an embodiment of the present invention. As shown in fig. 5, thecomputer device 50 includes aprocessor 51 and amemory 52 coupled to theprocessor 51, wherein thememory 52 stores program instructions, and the program instructions, when executed by theprocessor 51, cause theprocessor 51 to execute the steps of the automatic generation method of unit test cases according to any of the embodiments.
Theprocessor 51 may also be referred to as a CPU (Central Processing Unit). Theprocessor 51 may be an integrated circuit chip having signal processing capabilities. Theprocessor 51 may also be a general purpose processor, a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field Programmable Gate Array (FPGA) or other programmable logic device, discrete gate or transistor logic, discrete hardware components. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like.
Referring to fig. 6, fig. 6 is a schematic structural diagram of a storage medium according to an embodiment of the invention. The storage medium of the embodiment of the present inventionstores program instructions 61 capable of implementing all the methods described above, where theprogram instructions 61 may be stored in the storage medium in the form of a software product, and include several instructions to enable a computer device (which may be a personal computer, a server, or a network device) or a processor (processor) to execute all or part of the steps of the methods described in the embodiments of the present application. And the aforementioned storage medium includes: various media capable of storing program codes, such as a usb disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk, or computer equipment, such as a computer, a server, a mobile phone, and a tablet.
In the several embodiments provided in the present application, it should be understood that the disclosed computer apparatus, device and method may be implemented in other ways. For example, the above-described apparatus embodiments are merely illustrative, and for example, a division of a unit is merely a logical division, and an actual implementation may have another division, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may be in an electrical, mechanical or other form.
In addition, functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may exist alone physically, or two or more units are integrated into one unit. The integrated unit can be realized in a form of hardware, and can also be realized in a form of a software functional unit. The above embodiments are merely examples and are not intended to limit the scope of the present disclosure, and all modifications, equivalents, and flow charts using the contents of the specification and drawings of the present disclosure or those directly or indirectly applied to other related technical fields are intended to be included in the scope of the present disclosure.