Unit test code generation method and device, electronic device, and storage medium

By obtaining and updating parameter properties in the unit test code generation framework, the low efficiency problem caused by manual parameter changes is solved, and automated and efficient unit testing is achieved.

CN115033484BActive Publication Date: 2025-09-23CHINA PING AN LIFE INSURANCE CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202210688631.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-06-17
Publication Date
2025-09-23
Estimated Expiration
2042-06-17

AI Technical Summary

Technical Problem

The existing unit test code generation framework requires manual modification of parameter properties, resulting in low testing efficiency.

Method used

By obtaining the operation units and parameter attributes of the source code file to be tested, the parameter attributes are updated using the request parameters to generate unit test code that meets the test requirements.

Benefits of technology

There is no need to manually change parameter properties, which improves the efficiency and accuracy of unit testing.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115033484B_ABST
    Figure CN115033484B_ABST
Patent Text Reader

Abstract

The present application provides a unit test code generation method and device, electronic device, and storage medium, which belong to the field of unit test technology. The unit test code generation method includes: obtaining a source code file to be tested, parsing the source code file to be tested, obtaining an operation unit corresponding to the source code file to be tested and a first parameter attribute corresponding to the operation unit; obtaining a request parameter; updating the first parameter attribute according to the request parameter to obtain a second parameter attribute corresponding to the operation unit; obtaining a unit test coding rule, and generating a unit test code corresponding to the operation unit according to the unit test coding rule and the second parameter attribute, which can automatically fill in the parameter attributes that meet the test requirements and improve the efficiency of the unit test.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of unit testing technology, and in particular to a unit test code generation method and device, electronic equipment, and storage medium. Background Art

[0002] In the related art, the unit test code generation framework generates test code for the corresponding test interface by scanning the test interface and setting the parameter attributes of the test interface to default values ​​or random values. However, this test code generation method requires manual modification of parameter attributes, resulting in low unit test efficiency. Summary of the Invention

[0003] The main purpose of the embodiments of the present application is to propose a unit test code generation method and device, electronic device, and storage medium that can automatically fill in parameter attributes that meet test requirements and improve the efficiency of unit testing.

[0004] To achieve the above objectives, a first aspect of an embodiment of the present application provides a method for generating unit test code, the method comprising:

[0005] Acquire a source code file to be tested, parse the source code file to be tested, and obtain an operation unit corresponding to the source code file to be tested and a first parameter attribute corresponding to the operation unit;

[0006] Get request parameters;

[0007] Update the first parameter attribute according to the request parameter to obtain the second parameter attribute corresponding to the operation unit;

[0008] A unit test coding rule is obtained, and a unit test code corresponding to the operation unit is generated according to the unit test coding rule and the second parameter attribute.

[0009] In some embodiments, obtaining a source code file to be tested, parsing the source code file to be tested, and obtaining an operation unit corresponding to the source code file to be tested and a first parameter attribute corresponding to the operation unit include:

[0010] Obtaining a directory rule of a project to be tested, wherein the project to be tested includes at least one source code file to be tested;

[0011] Obtain the source code files to be tested in sequence according to the directory rules;

[0012] Setting a corresponding programming language reflection mechanism according to the programming language of the source code file to be tested;

[0013] The source code file to be tested is parsed according to the programming language reflection mechanism to obtain the operation unit corresponding to the source code file to be tested and the first parameter attribute corresponding to the operation unit.

[0014] In some embodiments, parsing the source code file to be tested according to the programming language reflection mechanism to obtain the operation unit corresponding to the source code file to be tested and the first parameter attribute corresponding to the operation unit includes:

[0015] Analyzing the file structure of the source code file to be tested;

[0016] The source code file to be tested is parsed from top to bottom according to the file structure through the programming language reflection mechanism to obtain multiple operation units of the source code file to be tested and the first parameter attribute corresponding to each operation unit.

[0017] In some embodiments, obtaining request parameters includes:

[0018] Call the first aspect component;

[0019] intercepting the test request of the operation unit according to the first aspect component to obtain an update request;

[0020] Get the request parameters of the update request.

[0021] In some embodiments, obtaining request parameters includes:

[0022] Get log files;

[0023] Extract the log file according to the request parameter keyword to obtain a log string corresponding to the request parameter keyword;

[0024] Assemble the log string to obtain the request parameters.

[0025] In some embodiments, obtaining the log file includes:

[0026] Call the second aspect component;

[0027] According to the second aspect component, the test request of the operating unit is output to a log file in a preset format.

[0028] In some embodiments, generating the unit test code corresponding to the operation unit according to the unit test coding rule and the second parameter attribute includes:

[0029] Determining the marking content in the unit test coding rules;

[0030] The tag content is replaced with the second parameter attribute to generate unit test code corresponding to the operation unit.

[0031] A second aspect of the embodiments of the present application provides a unit test code generation device, the device comprising:

[0032] A first acquisition module is configured to acquire a source code file to be tested, parse the source code file to be tested, and obtain an operation unit corresponding to the source code file to be tested and a first parameter attribute corresponding to the operation unit;

[0033] The second acquisition module is used to obtain request parameters;

[0034] a parameter updating module, configured to update the first parameter attribute according to the request parameter, and obtain a second parameter attribute corresponding to the operation unit;

[0035] The test code generation module is used to obtain unit test coding rules and generate unit test code corresponding to the operation unit according to the unit test coding rules and the second parameter attribute.

[0036] A third aspect of an embodiment of the present application proposes an electronic device, comprising a memory and a processor, wherein a program is stored in the memory, and when the program is executed by the processor, the processor is used to execute the unit test code generation method as described in any one of the embodiments of the first aspect of the present application.

[0037] The fourth aspect of the embodiments of the present application proposes a storage medium, which is a computer-readable storage medium. The storage medium stores computer-executable instructions, and the computer-executable instructions are used to enable a computer to execute the unit test code generation method as described in any one of the embodiments of the first aspect of the present application.

[0038] The unit test code generation method and device, electronic device, and storage medium proposed in the embodiments of the present application obtain the source code file to be tested, parse the source code file to be tested, obtain the operation unit corresponding to the source code file to be tested and the first parameter attribute corresponding to the operation unit, obtain the request parameter, update the first parameter attribute according to the request parameter, obtain the second parameter attribute corresponding to the operation unit, obtain the unit test coding rule, and generate the unit test code corresponding to the operation unit according to the unit test coding rule and the second parameter attribute. The embodiments of the present application update the first parameter attribute by obtaining the request parameter so that the first parameter attribute can meet the test requirements. There is no need to manually change the first parameter attribute, thereby improving the efficiency of unit testing. BRIEF DESCRIPTION OF THE DRAWINGS

[0039] Figure 1 This is a first flow chart of the unit test code generation method provided by an embodiment of the present application;

[0040] Figure 2 yes Figure 1 Flowchart of step S110 in FIG.

[0041] Figure 3 yes Figure 2 Flowchart of step S240 in FIG.

[0042] Figure 4 yes Figure 1 The first flow chart of step S120 in FIG.

[0043] Figure 5 yes Figure 1 A second flow chart of step S120 in FIG.

[0044] Figure 6 yes Figure 5 Flowchart of step S510 in FIG.

[0045] Figure 7 yes Figure 1 Flowchart of step S140 in FIG.

[0046] Figure 8 This is a second flow chart of the unit test code generation method provided in an embodiment of the present application;

[0047] Figure 9 This is a block diagram of the module structure of the unit test code generation device provided in an embodiment of the present application;

[0048] Figure 10 This is a schematic diagram of the hardware structure of the electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0049] In order to make the purpose, technical solutions and advantages of the present invention more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not intended to limit the present application.

[0050] It should be noted that although the device schematics illustrate functional module divisions and the flowcharts illustrate logical sequences, in certain circumstances, the steps shown or described may be performed in a sequence that differs from the module divisions in the device or the sequence in the flowcharts. The terms "first," "second," and so on, in the specification, claims, and drawings, are used to distinguish similar items and are not necessarily used to describe a specific sequence or precedence.

[0051] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by those skilled in the art to which this application pertains. The terms used herein are for the purpose of describing the embodiments of this application only and are not intended to limit this application.

[0052] In addition, the described features, structures or characteristics may be combined in any suitable manner in one or more embodiments. In the following description, many specific details are provided to provide a full understanding of the embodiments of the present disclosure. However, those skilled in the art will appreciate that the technical solutions of the present disclosure can be practiced without one or more of the specific details, or other methods, components, devices, steps, etc. can be adopted. In other cases, well-known methods, devices, implementations or operations are not shown or described in detail to avoid blurring various aspects of the present disclosure.

[0053] The block diagrams shown in the accompanying drawings are merely functional entities and do not necessarily correspond to physically separate entities. That is, these functional entities may be implemented in software, in one or more hardware modules or integrated circuits, or in different networks and / or processor devices and / or microcontroller devices.

[0054] The flowcharts shown in the accompanying drawings are for illustrative purposes only and do not necessarily include all contents and operations / steps, nor must they be executed in the order described. For example, some operations / steps may be decomposed, while others may be combined or partially combined. Therefore, the actual execution order may vary depending on the actual situation.

[0055] First, let’s analyze some of the terms used in this application:

[0056] Unit Testing: This is a code testing method that examines and verifies the smallest testable unit of software. A unit is a manually defined, minimal functional module to be tested. Its specific meaning depends on the specific situation. For example, in C, a unit refers to a function; in Java, a unit refers to a class; and in graphical software, a unit refers to a window or menu.

[0057] Reflection mechanism: refers to the ability of a program to obtain its own information during operation. For example, the Java language reflection mechanism refers to the ability of a Java program to dynamically obtain class information and call object methods during operation. That is, a Java program can dynamically obtain the properties and methods of any class and call the properties and methods of any object during operation.

[0058] Spring MVC: It is a lightweight web framework implemented in Java. It is based on the MVC design pattern of the Model, View and Controller layered structure, which divides web applications into the model layer, the control layer and the view layer. The model layer refers to Java Bean, which is used to process data; the control layer refers to servlet, which is used to receive request and response data; the view layer refers to HTML or JSP pages, which is used to interact with the user end to display data. The MVC design pattern has the advantages of low coupling, high portability and layer-by-layer separation, and has become the mainstream model for web application development.

[0059] Aspect-Oriented Programming (AOP) is a programming philosophy that complements and improves object-oriented programming. It uses aspects to modularize cross-cutting concerns to achieve code reuse and module decoupling. AOP separates the core functions from the non-core functions of application software, with the core functions being the core concerns and the non-core functions being the cross-cutting concerns. Non-core functions such as logging statistics and exception handling are separated from the application and modularized into specific classes. Cross-cutting concerns are modularized into aspects, which are then woven into the target object class at specific connection points and proxy objects are created. When the caller invokes the target method, the proxy object is triggered to intercept the target method and execute the aspect logic, implementing interception, logging statistics, exception handling, etc.

[0060] The unit test code generation framework can automatically generate basic test code for interfaces or methods, which can effectively reduce the cost of manual code maintenance. In the related art, the operating mechanism of the unit test code generation framework is generally to generate the test code corresponding to the test interface after scanning the test interface. For example, JUnitGenerator uses an empty test method to automatically generate the default test code for the specified interface; Squaretest and Randoop further generate corresponding test logic code for the interface method, among which Squaretest uses the method of setting the parameter attributes of the test interface to the default value to generate the test code, and Randoop uses the method of setting the parameter attributes of the test interface to the default value to generate the test code. This operating mechanism makes the parameter attributes of the test interface constructed by the unit test code generation framework generally set to default values ​​or random values, and it is impossible to construct test parameters other than those close to the real business. Most of the generated test codes cannot be directly used for testing business interfaces. It is still necessary to manually maintain the real parameters or supplement test cases according to the business scenario, resulting in low unit testing efficiency.

[0061] Based on this, the embodiments of the present application propose a unit test code generation method and device, electronic device, and storage medium. By obtaining the request parameters sent to the test interface, the parameter attributes of the test interface are updated according to the request parameters. According to the updated parameter attributes, test parameters that meet the business parameter requirements can be constructed, so that the generated test code can be directly used for testing the test interface, without the need for manual maintenance of real parameters or supplementary test cases according to the business scenario, thereby improving the efficiency of unit testing.

[0062] The unit test code generation method and device, electronic device, and storage medium provided in the embodiments of the present application are specifically illustrated through the following embodiments. First, the unit test code generation method in the embodiments of the present application is described.

[0063] The unit test code generation method provided in the embodiment of the present application relates to the field of unit test technology. The unit test code generation method provided in the embodiment of the present application can be applied to a terminal, can also be applied to a server side, and can also be software running in a terminal or a server side. In some embodiments, the terminal can be a smart phone, a tablet computer, a laptop computer, a desktop computer or a smart watch, etc.; the server side can be configured as an independent physical server, or can be configured as a server cluster or a distributed system composed of multiple physical servers, or can be configured as a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms; the software can be an application that implements the unit test code generation method, etc., but is not limited to the above forms.

[0064] Embodiments of the present application can be used in numerous general or special computer system environments or configurations. For example: personal computers, server computers, handheld or portable devices, tablet devices, multiprocessor systems, microprocessor-based systems, set-top boxes, programmable consumer electronics, network PCs, minicomputers, mainframe computers, distributed computing environments including any of the above systems or devices, and the like. The present application can be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, and the like that perform specific tasks or implement specific abstract data types. The present application can also be practiced in distributed computing environments, in which tasks are performed by remote processing devices connected via a communication network. In a distributed computing environment, program modules can be located in local and remote computer storage media including storage devices.

[0065] Reference Figure 1According to the unit test code generation method of the first aspect of the embodiment of the present application, it includes but is not limited to steps S110 to S140.

[0066] Step S110: Acquire a source code file to be tested, parse the source code file to be tested, and obtain an operation unit corresponding to the source code file to be tested and a first parameter attribute corresponding to the operation unit;

[0067] Step S120, obtaining request parameters;

[0068] Step S130: updating the first parameter attribute according to the request parameter to obtain the second parameter attribute corresponding to the operation unit;

[0069] Step S140: Obtain unit test coding rules, and generate unit test code corresponding to the operation unit according to the unit test coding rules and the second parameter attribute.

[0070] In step S110 of some embodiments, the source code file to be tested may be a C language source code file, a Java language source code file, a Python language source code file, etc., and the operation unit is a program unit that implements a function in the source code file to be tested, i.e., a test interface. A C language source code file is obtained, and the C language source code file is parsed according to the C language reflection mechanism to obtain the operation unit corresponding to the C language source code file and the first parameter attribute corresponding to the operation unit, wherein the operation unit is a function, and the first parameter attribute may be the name of the function, the input parameters of the function, the type of the input parameters, the return value, etc. A Java language source code file is obtained, and the Java language source code file is parsed according to the Java language reflection mechanism to obtain the operation unit corresponding to the Java language source code file and the first parameter attribute corresponding to the operation unit, wherein the operation unit is a class, and the first parameter attribute may be the name of the class, the name of the method in the class, the method parameters, etc. Get a Python language source code file, parse the Python language source code file according to the Python language reflection mechanism, and obtain the operation unit corresponding to the Python language source code file and the first parameter attribute corresponding to the operation unit, where the operation unit can be a module, a function, or a class, and the first parameter attribute can be a module name, function name, class name, input parameter, parameter type, return value, etc.

[0071] In step S120 of some embodiments, an HTTP request sent by a business end to an operation unit is obtained, where the business end may be a front-end web end, a back-end business service application end, etc. The HTTP request includes request parameters, namely, HttpServletRequest information, including the request method, request path, request body, etc. If the source code file to be tested is a Java language source code file, the operation unit may be a test interface defined using the Spring MVC specification. This test interface is capable of receiving HTTP requests, and the test interface information and its corresponding first parameter attribute are stored in the Controller class, i.e., the control layer.

[0072] In step S130 of some embodiments, the business end sends an HTTP request to the test interface corresponding to the request path according to the request path in the request parameters, so that the test interface can receive the HTTP request. Since the initial value of the first parameter attribute of the test interface is a default value or an empty value, it cannot meet the business test requirements. In order to construct test parameters close to real business scenarios, after the test interface obtains the request parameters of the HTTP request, if the request method in the request parameters is post, it is determined whether the parameter name field in the request body of the request parameter matches the parameter name field of the first parameter attribute. If the judgment result is that the two parameter name fields match, the first parameter attribute of the test interface in the Controller class is updated according to the parameter attributes such as the parameter type and parameter value of the request parameters, and the second parameter attribute corresponding to the test interface is obtained, wherein the second parameter attribute is the updated first parameter attribute. If the request method in the request parameters is get, the test interface information and its corresponding parameter attribute are sent to the business end, wherein the parameter attribute is the first parameter attribute or the second parameter attribute. It can be understood that if the request method is get, and the parameter attribute of the test interface is the initial value and has not been updated, the first parameter attribute will be sent to the business end; if the initial value of the parameter attribute of the test interface has been updated from the first parameter attribute to the second parameter attribute, the second parameter attribute will be sent to the business end.

[0073] In step S140 of some embodiments, a unit test coding rule is obtained, wherein the unit test coding rule is a test class template, and a template engine is used to fill the second parameter attribute into a predefined flag in the test class template, and a unit test code corresponding to the operation unit is generated, and the corresponding operation unit in the source code file to be tested is tested according to the unit test code. The unit test code is executed to obtain the actual test result of the operation unit, and the actual test result is matched with the expected test result. If the matching result is that the actual test result is consistent with the expected test result, it means that the code logic of the operation unit is correct. If the matching result is that the actual test result is inconsistent with the expected test result, it means that the code logic of the operation unit is wrong, and an error exception message is output, which can realize automated testing of the operation unit, avoid test errors caused by human factors, and improve the efficiency and accuracy of unit testing. The template engine can be a velocity template engine, which is edited using the velocity template language.

[0074] It should be noted that the test class template defines the naming rules for the test class name, the creation rules for the test class, the creation rules for the test function, etc. For example, the naming rules for the test class name are as follows: use "UnitTest" as the class name of the test class; the creation rules for the test class are as follows: define the access rights of the test class as public through the public keyword, and the test class provides a public no-parameter constructor; the creation rules for the test function are as follows: if the name of the operation unit is "A()", add the prefix "test" before the name of the operation unit "A()" to generate "testA()", and use "testA()" as the function name of the test function to distinguish the function name of the test function from the name of the operation unit; add a comment "@test" to the line above the function name of the test function to declare that the content below the comment line is a test function; and declare the test function as void, that is, the test function has no return value and no input parameters.

[0075] The unit test code generation method proposed in the embodiment of the present application obtains the source code file to be tested, parses the source code file to be tested, obtains the operation unit corresponding to the source code file to be tested and the first parameter attribute corresponding to the operation unit, obtains the request parameter, updates the first parameter attribute according to the request parameter, obtains the second parameter attribute corresponding to the operation unit, obtains the unit test coding rule, and generates the unit test code corresponding to the operation unit according to the unit test coding rule and the second parameter attribute. The embodiment of the present application updates the first parameter attribute by obtaining the request parameter so that the first parameter attribute can meet the test requirements. There is no need to manually change the first parameter attribute, thereby improving the efficiency of the unit test.

[0076] In some embodiments, as Figure 2As shown, step S110 specifically includes but is not limited to steps S210 to S240.

[0077] Step S210, obtaining the directory rules of the project to be tested;

[0078] Step S220, sequentially obtain the source code files to be tested according to the directory rules;

[0079] Step S230, setting a corresponding programming language reflection mechanism according to the programming language of the source code file to be tested;

[0080] Step S240 , parsing the source code file to be tested according to the programming language reflection mechanism to obtain the operation unit corresponding to the source code file to be tested and the first parameter attribute corresponding to the operation unit.

[0081] In step S210 of some embodiments, a directory rule of the project to be tested is obtained, wherein the project to be tested includes at least one source code file to be tested, and the directory rule is the arrangement order of the source code files to be tested in the project to be tested. It is understandable that the arrangement order can be obtained based on the size of the storage space occupied by the source code files to be tested, the modification time of the source code files to be tested, the name of the source code files to be tested, etc. For example, the source code files to be tested are sorted from small to large according to the size of the storage space they occupy, and the arrangement order of the source code files to be tested in the project to be tested is obtained; the source code files to be tested are sorted from front to back according to the time of their modification, and the arrangement order of the source code files to be tested in the project to be tested is obtained; if the source code files to be tested are named in natural numbers, the source code files to be tested are sorted from small to large according to the size of the numbers contained in their names, and the arrangement order of the source code files to be tested in the project to be tested is obtained.

[0082] In step S220 of some embodiments, the source code files to be tested are obtained sequentially from top to bottom according to the order in which they are arranged in the project to be tested. If the order is to sort the source code files to be tested from small to large according to the size of the storage space occupied by the source code files to be tested, the storage space occupied by the obtained source code files to be tested gradually increases.

[0083] In step S230 of some embodiments, the programming language includes C language, Java language, Python language, etc. If the programming language of the source code file to be tested is C language, the set programming language reflection mechanism is the C language reflection mechanism. If the programming language of the source code file to be tested is Java language, the set programming language reflection mechanism is the Java language reflection mechanism. If the programming language of the source code file to be tested is Python language, the set programming language reflection mechanism is the Python language reflection mechanism.

[0084] In step S240 of some embodiments, the source code file to be tested, whose programming language is C, is parsed according to the C language reflection mechanism to obtain multiple C operation units and the first parameter attribute corresponding to each C operation unit. The source code file to be tested, whose programming language is Java, is parsed according to the Java language reflection mechanism to obtain multiple Java operation units and the first parameter attribute corresponding to each Java operation unit. The source code file to be tested, whose programming language is Python, is parsed according to the Python language reflection mechanism to obtain multiple Python operation units and the first parameter attribute corresponding to each Python operation unit.

[0085] In steps S210 to S240 of some embodiments, the source code files to be tested are obtained in sequence according to the arrangement order of the source code files to be tested in the project to be tested, a corresponding programming language reflection mechanism is set according to the programming language of the source code files to be tested, and the source code files to be tested are parsed according to the programming language reflection mechanism to obtain multiple operation units and the first parameter attributes corresponding to each operation unit. The corresponding programming language reflection mechanism can be selected according to the different programming languages ​​of the source code files to be tested, thereby supporting the parsing of source code files to be tested in multiple programming languages.

[0086] In some embodiments, as Figure 3 As shown, step S240 specifically includes but is not limited to steps S310 to S320.

[0087] S310, analyzing the file structure of the source code file to be tested;

[0088] S320 , parsing the source code file to be tested from top to bottom according to the file structure through a programming language reflection mechanism to obtain multiple operation units of the source code file to be tested and a first parameter attribute corresponding to each operation unit.

[0089] In some embodiments, in steps S310 to S320, if the programming language of the source code file to be tested is Java, and the source code file to be tested includes multiple controller classes, in order to implement sequential parsing of the source code file to be tested, the positional order of the controller classes in the source code file to be tested is obtained by analyzing the file structure of the source code file to be tested. The source code file to be tested is parsed from top to bottom according to the positional order to obtain the operation units in the controller classes and their corresponding first parameter attributes. For example, if the source code file to be tested includes two controller classes, controllerA and controllerB, the file structure of the source code file to be tested is analyzed to obtain the positional order of controllerA as the first position and the positional order of controllerB as the second position. The Java language reflection mechanism is used to first parse controllerA according to the positional order to obtain the operation unit classA and its first parameter attribute classInfoA, where classInfoA includes methodInfoA and methodInfoB, where methodInfoA includes paramInfo, and then parse controllerB to obtain the operation unit classB and its first parameter attribute classInfoB. It's understood that the operation unit class is the carrier entity class parsed from the controller class, and classInfo contains information about the carrier entity class, including attributes such as the package name, class name, and class path. Because a class may contain multiple methods, classInfo also includes a method information list, or methodInfo. methodInfo is the carrier entity class of the parsed method information, including attributes such as the method name and method parameter paramInfo.

[0090] In some embodiments, as Figure 4 As shown, step S120 specifically includes but is not limited to steps S410 to S430.

[0091] Step S410, calling the first aspect component;

[0092] Step S420: intercept the test request of the operation unit according to the first aspect component to obtain an update request;

[0093] Step S430: Obtain request parameters of the update request.

[0094] In steps S410 to S430 of some embodiments, the first aspect component is called, wherein the first aspect component is implemented based on AOP and is used to horizontally intercept the HTTP test request sent by the business end to the operation unit, and use the intercepted request as an update request and obtain the request parameters of the update request, wherein the request parameters are HttpServletRequest information, including request method, request path, request body, etc., so as to update the input parameters of the test case in the test class template according to the request parameters, so that the generated test case can be directly used for testing the unit test code.

[0095] In some embodiments, as Figure 5 As shown, step S120 specifically includes but is not limited to steps S510 to S530.

[0096] Step S510, obtaining a log file;

[0097] Step S520: extract the log file according to the request parameter keyword to obtain the log string corresponding to the request parameter keyword;

[0098] Step S530: assemble the log character string to obtain request parameters.

[0099] In steps S510 to S530 of some embodiments, in order to solve the problem that the HTTP request cannot reach the corresponding operation unit, the log file is read through I / O, and the log file is extracted according to the request parameter keywords such as request body, request path, request method, etc., and the log string corresponding to the request parameter keyword is obtained, and the log string is assembled to obtain the request parameters so that the HTTP request can reach the local corresponding operation unit.

[0100] In some embodiments, as Figure 6 As shown, step S510 specifically includes but is not limited to steps S610 to S620.

[0101] S610, calling the second aspect component;

[0102] S620: Output the test request of the operating unit to a log file according to a preset format according to the second aspect component.

[0103] In steps S610 to S620 of some embodiments, the second aspect component is called, which is also implemented using AOP. The log printing format is defined in the second aspect component, which can enable the HTTP request sent by the business end to the operation unit to be output to the log file in a preset log printing format, wherein the preset log printing format can be to output the HTTP request to the log file in the order of request method, request path, and request body, so that the log file can be subsequently parsed according to the request parameter keyword to obtain the request parameters.

[0104] In some embodiments, as Figure 7 As shown, step S140 specifically includes but is not limited to steps S710 to S720.

[0105] S710, determining the marking content in the unit test coding rules;

[0106] S720: Replace the markup content with the second parameter attribute to generate a unit test code corresponding to the operation unit.

[0107] In step S710 of some embodiments, the marking content in the unit test coding rule is a flag bit predefined in the test class template.

[0108] In step S720 of some embodiments, a predefined flag in the test class template is replaced with the second parameter attribute to obtain a target test class, and unit test code corresponding to the operation unit is obtained based on the target test class. The target test class includes multiple unit test cases, and the unit test cases include logic such as construction parameters, request interfaces, and result verification, which can verify the correctness of the code in the source code file to be tested.

[0109] By executing steps S710 to S720, parameter attributes that are close to the business scenario can be constructed, and unit test code that meets the business-side testing requirements can be generated, which solves the problem of low efficiency of unit testing caused by the need to manually modify test cases when the input parameters of the operation unit are default values ​​or random values.

[0110] In the related technology, the Java unit test code generation framework obtains the methods, properties and other data of the target class by parsing the target class, and then generates the target test class through the template engine combined with the test class template. However, the input parameters of the test cases in the target test class are usually the default values, empty values ​​or random values ​​of the Java basic data types, resulting in the generated code being unable to be used for actual business testing.

[0111] To solve the above problems, refer to Figure 8 Another embodiment of the present application proposes a unit test code generation method, which provides a service for generating unit test code in the form of a Java code package, including but not limited to steps S810 to S850.

[0112] S810, scan the controller class in the project to be tested;

[0113] S820, parse the controller class according to the Java language reflection mechanism to obtain classInfo, and store the classInfo in the memory cache pool;

[0114] S830: When the project to be tested receives an HTTP request, the first aspect component generatorAspect is called to intercept the HTTP request and obtain the request parameters; if the project to be tested cannot receive the HTTP request, the second aspect component loggerAspect is called to obtain the request parameters from the log;

[0115] S840, update the paramInfo of the corresponding methodInfo in classInfo according to the request parameters;

[0116] S850: Obtain updated classInfo from the memory cache pool according to the velocity template engine, fill the updated classInfo into the test class template class.vm, and generate a test class file.

[0117] In step S810 of some embodiments, the java code package is developed based on the springboot microservice framework. After loading in the bean, the @PostConstruct annotated method is called to automatically scan the controller class in the project to be tested when the java code package starts executing.

[0118] In step S830 of some embodiments, by intercepting and obtaining request parameters through aspect or grabbing request parameters from logs, the request parameters in the actual business scenario can be filled into the unit test case of the test class template. The unit test code generated in this way does not need to manually fill in the test parameters, which can improve the efficiency of maintaining the unit test code, and the test case is close to the real business application scenario. In addition, multiple groups of request parameters can be obtained based on multiple requests, so that the generated test case includes multiple groups of input parameters, which is conducive to improving the coverage of the unit test code and ensuring the correctness of the business logic code. It is understandable that since the unit test code generation method is generally run locally in the project to be tested, in order to solve the problem that online environments such as testing and production cannot send requests to the local, the request parameters are obtained by log parsing.

[0119] In step S840 of some embodiments, the initial value of paramInfo is a default value or a random value, which causes the generated test case to be unable to be directly used for business logic code testing. ParamInfo is updated according to the request parameters to make paramInfo closer to the parameter attributes of the business scenario, thereby facilitating the generation of valid test cases.

[0120] By executing steps S810 to S850, the unit test code generation method of the embodiment of the present application can solve the problem that the traditional Java unit test code generation framework cannot construct valid test case input parameters, and the generated test class file, i.e., the unit test code, can be directly used for testing business logic code.

[0121] The present application also provides a unit test code generation device, such as Figure 9 As shown, the above-mentioned unit test code generation method can be implemented. The device includes: a first acquisition module 910, a second acquisition module 920, a parameter updating module 930, and a test code generation module 940. The first acquisition module 910 is used to obtain the source code file to be tested, parse the source code file to be tested, and obtain the operation unit corresponding to the source code file to be tested and the first parameter attribute corresponding to the operation unit; the second acquisition module 920 is used to obtain the request parameter; the parameter updating module 930 is used to update the first parameter attribute according to the request parameter to obtain the second parameter attribute corresponding to the operation unit; the test code generation module 940 is used to obtain the unit test coding rules and generate the unit test code corresponding to the operation unit according to the unit test coding rules and the second parameter attribute.

[0122] The unit test code generation device proposed in the embodiment of the present application obtains the source code file to be tested through the first acquisition module, parses the source code file to be tested, obtains the operation unit corresponding to the source code file to be tested and the first parameter attribute corresponding to the operation unit, the second acquisition module obtains the request parameter, the parameter update module updates the first parameter attribute according to the request parameter, obtains the second parameter attribute corresponding to the operation unit, the test code generation module obtains the unit test coding rule, and generates the unit test code corresponding to the operation unit according to the unit test coding rule and the second parameter attribute. The embodiment of the present application updates the first parameter attribute by obtaining the request parameter so that the first parameter attribute can meet the test requirements, and there is no need to manually change the first parameter attribute, thereby improving the efficiency of the unit test.

[0123] The unit test code generation device of the embodiment of the present application is used to execute the unit test code generation method in the above embodiment. Its specific processing process is the same as the unit test code generation method in the above embodiment, and will not be repeated here.

[0124] An embodiment of the present application further provides an electronic device, including:

[0125] at least one processor, and

[0126] a memory communicatively connected to at least one processor; wherein,

[0127] The memory stores instructions, which are executed by at least one processor so that the at least one processor implements a method as described in any one of the embodiments of the first aspect of the present application when executing the instructions.

[0128] The electronic device proposed in the embodiment of the present application obtains the source code file to be tested, parses the source code file to be tested, obtains the operation unit corresponding to the source code file to be tested and the first parameter attribute corresponding to the operation unit, obtains the request parameter, updates the first parameter attribute according to the request parameter, obtains the second parameter attribute corresponding to the operation unit, obtains the unit test coding rule, and generates the unit test code corresponding to the operation unit according to the unit test coding rule and the second parameter attribute. The embodiment of the present application updates the first parameter attribute by obtaining the request parameter so that the first parameter attribute can meet the test requirements, and there is no need to manually change the first parameter attribute, thereby improving the efficiency of the unit test.

[0129] The following combination Figure 10 The hardware structure of the electronic device is described in detail. The electronic device includes: a processor 1010 , a memory 1020 , an input / output interface 1030 , a communication interface 1040 , and a bus 1050 .

[0130] The processor 1010 can be implemented as a general-purpose CPU (Central Processing Unit), a microprocessor, an application-specific integrated circuit (ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided in the embodiments of the present application.

[0131] The memory 1020 can be implemented in the form of ROM (Read Only Memory), static storage device, dynamic storage device, or RAM (Random Access Memory). The memory 1020 can store an operating system and other application programs. When the technical solutions provided in the embodiments of this specification are implemented through software or firmware, the relevant program code is stored in the memory 1020 and is called by the processor 1010 to execute the unit test code generation method of the embodiments of this application;

[0132] Input / output interface 1030, used to implement information input and output;

[0133] Communication interface 1040, used to implement communication interaction between this device and other devices, which can be achieved through wired means (such as USB, network cable, etc.) or wireless means (such as mobile network, Wi-Fi, Bluetooth, etc.); and

[0134] bus 1050 , which transmits information between various components of the device (e.g., processor 1010 , memory 1020 , input / output interface 1030 , and communication interface 1040 );

[0135] The processor 1010 , the memory 1020 , the input / output interface 1030 , and the communication interface 1040 are connected to each other in communication within the device via a bus 1050 .

[0136] An embodiment of the present application further provides a storage medium, which is a computer-readable storage medium. The computer-readable storage medium stores computer-executable instructions, and the computer-executable instructions are used to enable a computer to execute the unit test code generation method of the embodiment of the present application.

[0137] The storage medium proposed in the embodiment of the present application obtains the source code file to be tested, parses the source code file to be tested, obtains the operation unit corresponding to the source code file to be tested and the first parameter attribute corresponding to the operation unit, obtains the request parameter, updates the first parameter attribute according to the request parameter, obtains the second parameter attribute corresponding to the operation unit, obtains the unit test coding rule, and generates the unit test code corresponding to the operation unit according to the unit test coding rule and the second parameter attribute. The embodiment of the present application updates the first parameter attribute by obtaining the request parameter so that the first parameter attribute can meet the test requirements. There is no need to manually change the first parameter attribute, thereby improving the efficiency of the unit test.

[0138] The memory, as a non-transient computer-readable storage medium, can be used to store non-transient software programs and non-transient computer executable programs. In addition, the memory may include a high-speed random access memory and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some embodiments, the memory may optionally include a memory remotely arranged relative to the processor, and these remote memories may be connected to the processor 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.

[0139] The embodiments described in the embodiments of this application are intended to more clearly illustrate the technical solutions of the embodiments of this application and do not constitute a limitation on the technical solutions provided by the embodiments of this application. Those skilled in the art will appreciate that with the evolution of technology and the emergence of new application scenarios, the technical solutions provided in the embodiments of this application are also applicable to similar technical problems.

[0140] It will be understood by those skilled in the art that Figures 1 to 8 The technical solutions shown in the figures do not constitute a limitation on the embodiments of the present application, and may include more or fewer steps than those shown in the figures, or a combination of certain steps, or different steps.

[0141] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, i.e., they may be located in one place or distributed across multiple network units. Some or all of the modules may be selected based on actual needs to achieve the objectives of this embodiment.

[0142] Those skilled in the art will appreciate that all or some of the steps in the methods, systems, and functional modules / units in the devices disclosed above may be implemented as software, firmware, hardware, or appropriate combinations thereof.

[0143] The terms "first", "second", "third", "fourth", etc. (if any) in the specification of the present application and the above-mentioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequential order. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0144] It should be understood that in this application, "at least one (item)" means one or more, and "plurality" means two or more. "And / or" is used to describe the association relationship of associated objects, indicating that three relationships may exist. For example, "A and / or B" can mean: only A exists, only B exists, and A and B exist at the same time, where A and B can be singular or plural. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. "At least one of the following items" or similar expressions refers to any combination of these items, including any combination of single items or plural items. For example, at least one of a, b or c can mean: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, c can be single or multiple.

[0145] In the several embodiments provided in this application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are merely schematic. For example, the division of the units is merely a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0146] The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of these units may be selected to achieve the purpose of this embodiment according to actual needs.

[0147] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.

[0148] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes multiple instructions for enabling an electronic device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store programs, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0149] The preferred embodiments of the present invention are described above with reference to the accompanying drawings, but are not intended to limit the scope of the present invention. Any modifications, equivalent substitutions, and improvements made by those skilled in the art without departing from the scope and essence of the present invention should be within the scope of the present invention.

Claims

1. A unit test code generation method, characterized in that: The method comprises: Obtaining a source code file to be tested, parsing the source code file to be tested, and obtaining an operation unit corresponding to the source code file to be tested and a first parameter attribute corresponding to the operation unit; an initial value of the first parameter attribute is a default value or a null value; Get request parameters; Update the first parameter attribute according to the request parameter to obtain the second parameter attribute corresponding to the operation unit; Obtaining a unit test coding rule, and generating a unit test code corresponding to the operation unit according to the unit test coding rule and the second parameter attribute; The acquisition request parameters include: Invoke a first aspect component, which is implemented based on aspect-oriented programming; intercept the test request sent by the business end to the operation unit horizontally according to the first aspect component to obtain an update request; and obtain request parameters of the update request; If the operation unit cannot receive the test request, the second aspect component is called. The second aspect component is implemented based on the programming idea of ​​aspect-oriented programming, and the second aspect component defines a log printing format; the test request is output according to the log printing format through the second aspect component to obtain a log file; the log file is extracted according to the request parameter keyword to obtain a log string corresponding to the request parameter keyword; the log string is assembled to obtain the request parameter.

2. The unit test code generation method according to claim 1, characterized in that: The step of obtaining a source code file to be tested, parsing the source code file to be tested, and obtaining an operation unit corresponding to the source code file to be tested and a first parameter attribute corresponding to the operation unit includes: Obtaining a directory rule of a project to be tested, wherein the project to be tested includes at least one source code file to be tested; Obtain the source code files to be tested in sequence according to the directory rules; Setting a corresponding programming language reflection mechanism according to the programming language of the source code file to be tested; The source code file to be tested is parsed according to the programming language reflection mechanism to obtain the operation unit corresponding to the source code file to be tested and the first parameter attribute corresponding to the operation unit.

3. The unit test code generation method according to claim 2, characterized in that: The parsing of the source code file to be tested according to the programming language reflection mechanism to obtain the operation unit corresponding to the source code file to be tested and the first parameter attribute corresponding to the operation unit includes: Analyzing the file structure of the source code file to be tested; The source code file to be tested is parsed from top to bottom according to the file structure through the programming language reflection mechanism to obtain multiple operation units of the source code file to be tested and the first parameter attribute corresponding to each operation unit.

4. The unit test code generation method according to any one of claims 1 to 3, characterized in that: Generating the unit test code corresponding to the operation unit according to the unit test coding rule and the second parameter attribute includes: Determining the marking content in the unit test coding rules; The tag content is replaced with the second parameter attribute to generate unit test code corresponding to the operation unit.

5. A unit test code generating device, characterized in that: The device comprises: a first acquisition module, configured to acquire a source code file to be tested, parse the source code file to be tested, and obtain an operation unit corresponding to the source code file to be tested and a first parameter attribute corresponding to the operation unit; an initial value of the first parameter attribute is a default value or a null value; The second acquisition module is used to obtain request parameters; a parameter updating module, configured to update the first parameter attribute according to the request parameter, and obtain a second parameter attribute corresponding to the operation unit; A test code generation module, configured to obtain a unit test coding rule and generate a unit test code corresponding to the operation unit according to the unit test coding rule and the second parameter attribute; The device is also used for: Invoke a first aspect component, which is implemented based on aspect-oriented programming; intercept the test request sent by the business end to the operation unit horizontally according to the first aspect component to obtain an update request; and obtain request parameters of the update request; If the operation unit cannot receive the test request, the second aspect component is called. The second aspect component is implemented based on the programming idea of ​​aspect-oriented programming, and the second aspect component defines a log printing format; the test request is output according to the log printing format through the second aspect component to obtain a log file; the log file is extracted according to the request parameter keyword to obtain a log string corresponding to the request parameter keyword; the log string is assembled to obtain the request parameter.

6. An electronic device, characterized in that include: at least one memory; at least one processor; at least one program; The program is stored in the memory, and the processor executes at least one of the programs to implement: The unit test code generation method according to any one of claims 1 to 4.

7. Storage medium, the storage medium is a computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, and the computer-executable instructions are used to cause a computer to execute: The unit test code generation method according to any one of claims 1 to 4.

Citation Information

Patent Citations

  • Unit test code generation method and device

    CN102591777A