Unit test method, device, equipment, medium and program product

By determining the reference position and simulation scope of static final variables in Java, and using a static method without parameters for simulation, the simulation problem of static final variables in unit tests is solved, the test accuracy and efficiency are improved, and the original code is avoided. Revise.

CN118916277APending Publication Date: 2024-11-08CCB FINTECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410959812.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-17
Publication Date
2024-11-08

AI Technical Summary

Technical Problem

In Java, it is difficult to simulate static final variables, resulting in inaccurate and inefficient unit testing, and requires modification of the original code, affecting the safety and accuracy of the test.

Method used

By determining the reference position of the static final variable to be simulated, creating a simulation instance and limiting its simulation scope, and using a static method without parameters for reading and replacing, the simulation of the static final variable is realized and modification of the original code is avoided.

Benefits of technology

Effectively control the impact range of static final variables during the simulation process, improve the accuracy and efficiency of unit testing, and ensure test safety.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118916277A_ABST
    Figure CN118916277A_ABST
Patent Text Reader

Abstract

The invention provides a unit test method and device, equipment, a medium and a program product, and relates to the technical field of big data, the method comprises the following steps: determining a to-be-simulated static final variable according to a tested target unit in a unit test case; scanning a code range of the tested target unit to obtain a reference position of a code corresponding to the static final variable in the code range; when a simulation instance of the tested target unit is created, a simulation action range in the simulation instance is determined according to the reference position, and the simulation action range is used for limiting the simulation range of the static final variable; and executing a test process of the tested target unit according to the simulation instance and the simulation action range thereof to obtain a test result. By means of the method, the problem that in the prior art, due to the fact that simulation of the static final variable is achieved by modifying an original code, the test safety of the target unit (classes, methods and the like needing to be tested) is poor is solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of big data technology, and in particular to a unit testing method, device, equipment, medium and program product. Background Art

[0002] In financial institutions, a large amount of software development is required for different business needs. Unit testing, as a means of checking and verifying the smallest testable unit in software development, has been widely used in financial institutions because it can help developers find and fix errors in the code early.

[0003] Mock technology is a commonly used test method in unit testing. It simulates the behavior of real objects or systems to achieve independent testing and verification of the target test unit. However, in Java, for variables with static and final modifiers, i.e. static-final variables, due to their constant characteristics, class-level variable characteristics, single instance characteristics, etc., it is difficult to mock static-final variables, which makes the unit test process containing static-final modifiers unable to proceed normally.

[0004] In the related technology, the simulation of static-final variables is achieved by changing the values ​​of member variables modified by static-final in the original unit under test (code). This method makes it difficult to quickly identify the scope of static-final variables in the unit under test. On the one hand, it is very easy to cause assignment errors and affect the test accuracy. On the other hand, the original code needs to be changed, which will also affect the test security and test efficiency. Summary of the invention

[0005] The present application provides a unit testing method, device, equipment, medium and program product to at least solve the above technical problems.

[0006] According to a first aspect of the present application, a unit testing method is provided, comprising:

[0007] According to the target unit under test in the unit test case, determine the static final variables to be simulated;

[0008] Scanning the code range of the target unit under test to obtain the reference position of the code corresponding to the static final variable in the code range;

[0009] When creating a simulation instance of the target unit under test, determining a simulation scope in the simulation instance according to the reference position, wherein the simulation scope is used to limit a simulation range of the static final variable;

[0010] According to the simulation instance and its simulation scope, the test process of the target unit under test is executed to obtain the test result.

[0011] In one embodiment, the method further comprises:

[0012] Based on the code enhancement technology, a parameterless static method for reading the value of the static final variable is added in the target unit under test;

[0013] Determining the simulation scope in the simulation instance according to the reference position includes:

[0014] Determine, according to the reference position, a replacement position of the static method without parameters, wherein the replacement position is a position where the code corresponding to the static final variable is replaced with the static method without parameters;

[0015] According to the replacement position of the parameterless static method, a simulation scope in the simulation instance is determined.

[0016] In one embodiment, the method further comprises:

[0017] According to a preset rule, determining a substitute function access method name for the static final variable;

[0018] The replacement function access method name is used to access the static final variable, and the preset rules include exclusion rules that have semantic differences with the method name included in the target unit under test.

[0019] In one embodiment, the method further comprises:

[0020] Determine the alternative function access method name as the function name of the parameterless static method;

[0021] The method of adding a parameterless static method for reading the value of the static final variable in the target unit under test based on the code enhancement technology includes:

[0022] Based on the code enhancement technology and the function name of the parameterless static method, a parameterless static method for reading the value of the static final variable is added in the target unit under test.

[0023] In one embodiment, the method further comprises:

[0024] Based on the static method simulation tool, a simulation instance of the target unit under test is created, wherein the simulation instance includes the simulation scope and a stub for executing the parameterless static method in the simulation scope, wherein the stub is used to return a simulation object or an actually executable type for the static final variable.

[0025] In one embodiment, determining the static final variable to be simulated according to the target unit under test in the unit test case includes:

[0026] The code range of the target unit under test is scanned to obtain all variables in the code range that are modified by both static modifiers and final modifiers, so as to determine the static final variables to be simulated.

[0027] In one embodiment, the process of executing the test of the target unit under test according to the simulation instance and its simulation scope includes:

[0028] The test process of the target unit under test is executed according to the test rules carried by the unit test case, and the simulation instance and its simulation scope; wherein the test rules include exception test rules and / or performance test rules.

[0029] According to a second aspect of the present application, a unit testing device is also provided, comprising:

[0030] an attribute determination module configured to determine static final variables to be simulated according to a target unit under test in a unit test case;

[0031] A reference acquisition module, configured to scan a code range of the target unit under test to obtain a reference position of a code corresponding to the static final variable in the code range;

[0032] A scope determination module, which is configured to determine a simulation scope in the simulation instance according to the reference position when creating a simulation instance of the target unit under test, wherein the simulation scope is used to limit a simulation range of the static final variable;

[0033] The test module is configured to execute the test process of the target unit under test according to the simulation instance and its simulation scope to obtain the test result.

[0034] In one embodiment, the device further comprises:

[0035] An enhancement module, which is configured to add a parameterless static method for reading the value of the static final variable in the target unit under test based on code enhancement technology;

[0036] The scope determination module comprises:

[0037] a replacement determination unit, configured to determine a replacement position of the parameterless static method according to the reference position, wherein the replacement position is a position where the code corresponding to the static final variable is replaced with the parameterless static method;

[0038] A determination unit is configured to determine a simulation scope in the simulation instance according to a replacement position of the parameterless static method.

[0039] In one embodiment, the device further comprises:

[0040] A function name determination module, which is configured to determine the alternative function access method name for the static final variable according to preset rules; wherein the preset rules include exclusion rules that have semantic differences with the method name contained in the target unit under test;

[0041] The enhancement module is specifically configured to add a parameterless static method for reading the value of the static final variable in the target unit under test based on the replacement function access method name based on the code enhancement technology.

[0042] According to a second aspect of the present application, there is also provided an electronic device, comprising: a processor, and a memory communicatively connected to the processor;

[0043] The memory stores computer-executable instructions;

[0044] The processor executes the computer-executable instructions stored in the memory to implement the unit testing method provided in the first aspect above.

[0045] According to a third aspect of the present application, a computer-readable storage medium is also provided, wherein the computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are executed by a processor, they are used to implement the unit testing method provided in the first aspect above.

[0046] According to a fourth aspect of the present application, a computer program product is also provided, comprising a computer program, which implements the unit testing method provided in the first aspect when executed by a processor.

[0047] The unit testing method, apparatus, equipment, medium and program product provided in the embodiments of the present application utilize the static final variables to be simulated in the target unit under test and the corresponding reference positions, determine the simulation scope corresponding to the simulation instance when creating the simulation instance, and perform simulation testing on the target unit under test for the simulation scope. The scope limitation method of the simulation scope can effectively control the influence scope of the code that references the static final variables in the simulation process based on the simulation instance, thereby solving the simulation problem of the static final variables without modifying the code in the original target unit under test, thereby improving the unit testing security, testing accuracy and testing efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0048] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.

[0049] Figure 1 A schematic diagram of a unit testing method provided in an embodiment of the present application;

[0050] Figure 2 A flowchart of another unit testing method provided in an embodiment of the present application;

[0051] Figure 3 One of the flowcharts of another unit testing method provided in the embodiment of the present application;

[0052] Figure 4 A second flow chart of another unit testing method provided in an embodiment of the present application;

[0053] Figure 5 A flowchart of another unit testing method provided in an embodiment of the present application;

[0054] Figure 6 A schematic diagram of the structure of a unit testing device provided in an embodiment of the present application;

[0055] Figure 7 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application.

[0056] The above drawings have shown clear embodiments of the present application, which will be described in more detail later. These drawings and text descriptions are not intended to limit the scope of the present application in any way, but to illustrate the concept of the present application to those skilled in the art by referring to specific embodiments. DETAILED DESCRIPTION

[0057] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with the present application. Instead, they are merely examples of devices and methods consistent with some aspects of the present application as detailed in the appended claims.

[0058] In the technical solution of this application, the collection, storage, use, processing, transmission, provision and disclosure of financial data or user data and other information involved are in compliance with the provisions of relevant laws and regulations and do not violate public order and good morals. It should be noted that in the embodiments of this application, some existing solutions in the industry such as certain software, components, models, etc. may be mentioned, and they should be considered as exemplary. Their purpose is only to illustrate the feasibility of the implementation of the technical solution of this application, but it does not mean that the applicant has or will necessarily use the solution.

[0059] Terminology explanation:

[0060] Variables (attributes) modified by static are static variables. Variables modified with the static modifier are at the class level, not the instance level. That is, the variable belongs to the class itself, not a specific object instance. All instances of the class share the same static variable; the access method is to directly access the Static variable through the class name without creating an instance of the class. For example, in Java, it can be accessed through "ClassName.variableName"; in addition, static variables are shared, and they can be used to share data between different instances of a class.

[0061] The variable (attribute) modified by final is the final variable. The variable modified with the final modifier is used to indicate that the variable cannot be modified once it is assigned a value.

[0062] Variables (attributes) modified by static-final can be understood as a combination of the above-mentioned modifications of tatic and final. Static-final variables include the following features: constant features. Static final variables are initialized when the class is loaded, and their values ​​cannot be changed after initialization. This means that they are constants and are usually used to define immutable global constants; they are class-level variables. Due to the static keyword, these variables belong to the class itself, not to instances of the class. Therefore, they can be accessed directly through the class name without creating an instance of the class; single instance. During the entire application run, the static final variable has only a single instance. No matter how many instances of the class are created, the static final variable is shared. In other words, static final variables are usually used to define class-level constants that remain unchanged throughout the application run. These characteristics of static final variables can bring some challenges when performing unit testing, especially when using a Mock framework (such as Mockito) for mocking:

[0063] 1) Due to its immutability, that is, the final keyword, the value of the corresponding static final variable cannot be changed at runtime, which means that it is difficult to simulate different test scenarios by directly modifying the values ​​of these variables; 2) Initialization at class loading, static final variables are initialized when the class is loaded, which makes it difficult to dynamically change their values ​​during testing; 3) Limitations of the Mock framework, most mock frameworks (such as Mockito) do not support mocking final variables or static methods by default. Although there are some extensions (such as PowerMockito) that can handle this situation, they may increase the complexity and maintenance cost of the test.

[0064] In the related art, for the simulation of static variables, a base class is formulated, which is inherited by all inherited classes, and the static variables are defined in the static code block of the base class; or the value of the target static variable is forcibly changed before the code under test is executed through reflection. The above related art is prone to cause the assignment behavior to have an impact beyond the expected range when the operation is improper, thereby affecting the accuracy of the unit test results.

[0065] In view of this, the embodiment of the present application provides a unit testing method, device, equipment, medium and program product, by determining the static final variable to be simulated according to the target unit to be tested in the unit test case, scanning the code range of the target unit to be tested to obtain the reference position of the code corresponding to the static final variable in the code range, and when creating a simulation instance of the target unit to be tested, determining the simulation scope in the simulation instance according to the reference position, the simulation scope is used to limit the simulation range of the static final variable, and executing the test process of the target unit to be tested according to the simulation instance and its simulation scope to obtain the test result. In this process, the static final variable to be simulated and the corresponding reference position are used to determine the simulation scope corresponding to the simulation instance when creating the simulation instance, and the unit to be tested is tested for the simulation scope. The scope limitation method of the simulation scope can effectively control the influence range of the code that references the static final variable in the simulation process based on the simulation instance, thereby solving the simulation problem of the member attribute value modified by static final without modifying the code in the original target unit to be tested, thereby improving the unit test security and test efficiency.

[0066] The technical solution of the present application and how the technical solution of the present application solves the above-mentioned technical problems are described in detail below with specific embodiments. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present application will be described below in conjunction with the accompanying drawings.

[0067] Figure 1 A schematic flow chart of a unit testing method provided in an embodiment of the present application is shown. The method can be applied to a terminal device for performing a unit test. The present embodiment does not specifically limit the specific form of the terminal device. Figure 1 As shown, the method may include the following steps S101-S104:

[0068] Step S101: Determine the static final variable to be simulated according to the target unit under test in the unit test case.

[0069] In this embodiment, the target unit under test may be any test unit such as a method under test, a function, an object, a type, etc., and the type under test (referred to as the target test type) is taken as an example.

[0070] In this embodiment, the static final variables to be simulated, that is, the stactic-final variables that need to be simulated (mock) during the test process, are all stactic-final variables in the target type.

[0071] Next, the determination of the static final variables to be simulated according to the target unit under test in the unit test case in the above steps is further introduced:

[0072] The code range of the target unit under test is scanned to obtain all variables in the code range that are modified by both static modifiers and final modifiers, so as to determine the static final variables to be simulated.

[0073] In this embodiment, in order to determine the process of static final modified attributes that need to be mocked in the target test type, the target test type is fully scanned in the code to obtain, wherein, the static modifier and the final modifier can be used at the same time. One modifier, namely static-final, can be used. By scanning the variables modified by static-final, all static final variables that need to be simulated in the target test type can be determined.

[0074] In one implementation, the code scope can be determined first, for example, by analyzing the structure, namespace, class, method, etc. of the code file. Once the code scope is determined, all code elements within the scope are traversed. During the traversal process, focus on those attributes modified with static modifiers and final modifiers (static final). These attributes are usually values ​​in a class or module, and they do not change during program execution. By identifying and determining these static final variables, important basic data can be provided for subsequent testing processes.

[0075] It can be understood that the variables or attributes in this embodiment refer to member variables of a class.

[0076] Step S102: Scan the code range of the target unit under test to obtain the reference position of the code corresponding to the static final variable in the code range.

[0077] The code range for scanning the target unit under test is exemplified above, and the related description can be found in the above content, which will not be repeated here.

[0078] In this embodiment, the code range to be executed in the unit test of the target test type code is scanned, and all reference locations to the above code are recorded. By obtaining where these static final variables are used in the code, that is, the reference locations, it is convenient to determine the simulation scope in the subsequent steps.

[0079] Step S103: when creating a simulation instance of the target unit under test, determine a simulation scope in the simulation instance according to the reference position, wherein the simulation scope is used to limit a simulation range of the static final variable.

[0080] In this embodiment, a static method simulation tool mockStatic can be used in the unit test code to create a simulation instance. During the creation process, its simulation scope is determined based on the reference location, that is, the simulation scope is the location of all references to static simulation variables, so that the simulation instance simulates the static final variables in the simulation scope without out-of-scope simulation, which would lead to assignment errors and other problems.

[0081] Step S104: Execute the test process of the target unit under test according to the simulation instance and its simulation scope to obtain the test result.

[0082] In this embodiment, in addition to the simulation scope, the simulation instance may also include data that needs to be simulated for static final variables, and assign values ​​to related codes in the simulation scope, thereby realizing the test process of the target under test.

[0083] Optionally, in the above steps, the test process of the target unit under test is executed according to the simulation instance and its simulation scope, and the following method can be adopted: according to the test rules carried by the unit test case, and the simulation instance and its simulation scope, the test process of the target unit under test is executed; wherein the test rules include exception test rules and / or performance test rules.

[0084] In this embodiment, the exception test rules can be used to verify whether the behavior of the tested code meets expectations under abnormal conditions, such as inputting illegal data, simulating network failures or resource unavailability. Performance test rules can be used to evaluate the performance of the tested code under different load conditions, such as indicators such as response time, throughput and resource consumption. In the process of executing code testing, the required simulation instance is first initialized according to the description in the unit test case. Specifically, the test steps can be executed step by step according to the requirements of the test rules. For the exception test rules, the system can introduce various abnormal conditions, observe the response of the tested code, and record whether its behavior meets expectations. For example, the system can simulate a network timeout to check whether the tested code can correctly handle the timeout exception and return appropriate error information. For performance test rules, the system will run the tested code under different load conditions, collect performance data, and compare it with the predetermined performance standards to determine whether its performance meets the standards. Through the above process, the reliability and performance of the tested code under various conditions can be comprehensively evaluated to ensure that it can run stably and meet performance requirements in actual applications. This systematic unit testing method not only helps to discover potential defects and performance bottlenecks, but also improves the quality and stability of the code, providing a solid foundation for subsequent development and maintenance.

[0085] Through the above technical solution, in the unit test execution code, the mockStatic scope of the above specific static method is limited by scope limitation, which can effectively control the scope of influence of the above code, thereby solving the simulation problem of member attribute values ​​modified by static final and improving test accuracy and efficiency.

[0086] Figure 2 This is a flow chart of another unit testing method provided by an embodiment of the present application. On the basis of the above embodiment, in order to further solve the simulation complexity problem of the static-final modifier, this embodiment combines code enhancement technology to add a parameterless static method for reading the value of the static final variable in the tested code to solve the simulation difficulty problem of the static final variable. Specifically, in addition to the above steps S101-S104, the method provided by this embodiment also includes step S201, and further divides step S103 into step S103a and step S103b:

[0087] Step S201: Based on code enhancement technology, a parameterless static method for reading the value of the static final variable is added in the target unit under test.

[0088] Code Enhancement Techniques refers to the use of various methods and tools to improve and optimize the quality, performance and maintainability of computer program codes. Among them, code enhancement techniques (such as bytecode manipulation or dynamic proxy) can be used to dynamically modify or extend the behavior of existing codes to improve the quality and maintainability of codes.

[0089] In the current code testing technology, reading the value of a static final variable may require a complex reflection mechanism or other indirect methods, which may affect the readability and maintainability of the code, and may cause test performance problems in some cases. This embodiment automatically generates a parameterless static method by utilizing code enhancement technology, and inserts the parameterless static method into the target unit under test, thereby achieving the increase of the parameterless static method. Specifically, based on the code enhancement technology, a parameterless static method is added. For example, a bytecode operation library such as ASM, Javassist or CGLIB in Java can be used to dynamically add a parameterless static method to the target unit under test (i.e., the class or module that needs to be tested).

[0090] It can be understood that a parameterless static method refers to a method in programming that does not require any parameters and belongs to the class itself rather than an instance of the class. The main function of this method is to read and return the value of a static final variable. By adding a parameterless static method for reading static property values ​​in the target test type code, the form of this method directly defines the scope of the target static property.

[0091] Step S103a, when creating a simulation instance of the target unit under test, determine the replacement position of the static method without parameters according to the reference position, and the replacement position is to replace the code corresponding to the static final variable with the position of the static method without parameters.

[0092] Step S103b: Determine the simulation scope in the simulation instance according to the replacement position of the static method without parameters.

[0093] In this embodiment, a non-parameter static method is a method that does not require any parameter input and belongs to the class itself, rather than to an instance of the class. In other words, a non-parameter static method can be called without creating a class instance. The replacement position of the non-parameter static method is determined by utilizing the reference position, so that the code that references the static final variable is replaced with the non-parameter static method.

[0094] For example, after adding a parameterless static method, you can use the newly added static method execution statement, similar to replacing the existing reference "a" to the target static final attribute (i.e., static final variable) in the code with a(). The format is usually to replace a with a(), and no other changes are made.

[0095] Continuing to refer to the above example, in this embodiment, the simulation scope of the simulation instance is replaced with all the positions of a().

[0096] Through the above technical solution, using the replacement method of the non-parameter static method, in the simulation process, only the call of the non-parameter static method (reading the simulated value) is needed to realize the simulation of the static final variable without modifying the original tested code. In other words, without changing the initial code, it is possible to rely on limited and accurate rules to realize the safe and reliable mock implementation of the target attribute. Compared with the above method, it has obvious advantages in unit test security and can improve test efficiency.

[0097] Figure 3 This is one of the flow charts of another unit testing method provided in the embodiment of the present application. Figure 1 On the basis of the corresponding embodiment, the determination of the replacement function access method name is added to facilitate the access to the static final variable, so as to improve the accuracy and reliability of the test. Specifically, in addition to the above steps, this embodiment can also include the following step S301.

[0098] Step S301, according to preset rules, determine an alternative function access method name for the static final variable; wherein the alternative function access method name is used to access the static final variable, and the preset rules include exclusion rules that have semantic differences with the method name contained in the target unit under test.

[0099] Understandably, the exclusion rule is that the replacement function access method name is clearly distinguished from the definition of the existing method in the target test type in accordance with Java syntax.

[0100] In this embodiment, the process of determining the alternative function access method name of the static-final variable may include: naming specifications, the alternative function access method name follows specific naming specifications to distinguish it from other method names. These specifications may include the use of prefixes or suffixes, capitalization rules, etc. For example, all alternative function access method names may start with "access_"; and have semantic differences, the alternative function access method name should be semantically distinct from the method name of the target unit under test. This means that the name of the alternative function needs to clearly indicate that its purpose is to access static final variables, rather than perform other logical operations. This can be achieved by using specific words or phrases. In addition, consistency checks can be performed in code testing to ensure that all alternative function access method names comply with preset rules. This can be achieved through static code analysis tools or custom scripts to automatically detect and report names that do not comply with the rules.

[0101] In this embodiment, by determining the substitute function access method name of the static final variable, the test process of the target unit under test can be executed in combination with the access method name, the simulation instance and the simulation scope. Specifically, in the simulation process of the static final variable, the static final variable in the simulation scope can be accessed and simulated based on the substitute function access method name. The substitute function access method name is different from the method name used by the target unit under test, which can ensure the uniqueness and clarity of the substitute function access method name, and can also avoid potential naming conflicts and semantic confusion, thereby improving the maintainability and readability of the code. In the actual code testing process, the application of these rules can significantly reduce errors caused by naming problems and ensure the accuracy and reliability of the test results.

[0102] Figure 4 This is another flow chart of a unit testing method provided in the present application embodiment. Figure 1-Figure 3 On the basis of the corresponding embodiment, this embodiment uses the alternative function access method name to determine the function name of the non-parameter static method to determine the non-parameter static method. In the simulation process, the non-parameter static method can be called through the alternative function access method name, thereby realizing the simulation and testing process of the static final variable. Specifically, in addition to the above steps S101-S104, the method provided by this embodiment can also include the following step S401, and further divide the above step S201 into step S201a.

[0103] Step S401: Determine the replacement function access method name as the function name of the static method without parameters.

[0104] Step S201a: Based on the code enhancement technology and the function name of the parameterless static method, a parameterless static method for reading the value of the static final variable is added to the target unit under test.

[0105] In the present embodiment, the substitute function access method name can be used to replace or expand the existing parameterless static method. The name of this substitute function should be consistent with the function name of the parameterless static method to be added, so as to ensure that the value of the static final variable can be correctly accessed and read when called (during the test). Specifically, the substitute function access method name can be determined as the function name of the parameterless static method, and the substitute function access method name can be directly used as the function name of the parameterless static method, so as to realize the call of the parameterless static method using the substitute function access method name during the simulation test process. To ensure that the substitute function access method can accurately correspond to the parameterless static method, so as to achieve the consistency of function replacement and call.

[0106] Through the above technical solution, a method of the same name of a specific type is added to the target test type to implement access to the property. At the same time, through code enhancement technology, all references to the target static-final property are converted into the execution of the target member variable, thereby simulating the static-final property without directly modifying the original code.

[0107] Figure 5 It is a flow chart of another unit testing method provided by an embodiment of the present application. On the basis of the above embodiment, this embodiment creates a simulation instance in the form of a stub to simulate the static final variables in the scope when the static method without parameters is executed. Specifically, in addition to the above steps S101-S140, the method provided by this embodiment can also include the following step S501.

[0108] Step S501: Based on the static method simulation tool, create a simulation instance of the target unit under test, the simulation instance includes the simulation scope, and a stub for executing the parameterless static method in the simulation scope, the stub is used to return a simulation object or an actually executable type for the static final variable.

[0109] As you can understand, stub is a technology used to simulate the behavior of certain components or modules. Stubs are often used in unit testing to isolate the code to be tested so that it does not depend on the actual external system or complex dependencies, which makes it easier to test specific parts of the code.

[0110] In the present embodiment, the stub is used to provide the simulation data of the simulation instance, which can return the simulation object and can also return the actual executable type, wherein the simulation object is a plurality of different mocked instances (different values ​​of simulation) for the simulation instance, and the actual execution type can be a specific value. Specifically, the static method simulation tool (MockStatic) can be used to create a MockStatic simulation instance of the target test type, and by limiting its scope, within the scope, a stub for the execution of the a() method is added, so that during the test process, the target method to be tested can be executed within the scope of MockStatic, the test results can be obtained, and test verification, etc. can be performed. Based on this, efficient simulation of static final variables and testing of the entire unit under test can be achieved.

[0111] In some embodiments, the test results can also be reported to the target terminal, which can be a terminal with which developers interact. Developers can verify, analyze, modify, update or determine whether the test needs to be performed again based on the test results, and whether the test process needs to be optimized to improve test performance.

[0112] In the related art, there are some deficiencies in the mock behavior when the static member variable of the type modified by static final is a complex type. It can be understood that complex types usually refer to those types that are not basic data types (such as int, boolean, char, etc.), such as custom classes, any user-defined classes; collection classes: such as List, etc.; arrays, such as int[], etc.; generic classes, such as Optional, etc.

[0113] Through the above technical solution, combined with code enhancement technology, by adding a specific type of the same-name method in the existing type, the access to the property is realized. At the same time, through code enhancement technology, all references to the target static-final property are converted into the execution of the target member variable; in addition, in the unit test execution code, the scope of mockStatic is limited by scope limitation, which can effectively control the scope of influence of the above code. Based on this, the mock problem of the member property value modified by static final can be effectively solved, thereby ensuring the efficient execution of unit testing.

[0114] Figure 6 A schematic diagram of the structure of a unit test device provided in an embodiment of the present application is shown in FIG. Figure 6 As shown, the device includes an attribute determination module 61, a reference acquisition module 62, a scope determination module 63 and a testing module 64, wherein:

[0115] An attribute determination module 61, which is configured to determine the static final variable to be simulated according to the target unit under test in the unit test case;

[0116] A reference acquisition module 62, which is configured to scan the code range of the target unit under test to obtain the reference position of the code corresponding to the static final variable in the code range;

[0117] A scope determination module 63, which is configured to determine a simulation scope in the simulation instance according to the reference position when creating a simulation instance of the target unit under test, wherein the simulation scope is used to limit the simulation range of the static final variable;

[0118] The testing module 64 is configured to execute the testing process of the target unit under test according to the simulation instance and its simulation scope to obtain the testing result.

[0119] In one embodiment, the device further comprises:

[0120] An enhancement module, which is configured to add a parameterless static method for reading the value of the static final variable in the target unit under test based on code enhancement technology;

[0121] The scope determination module 63 includes:

[0122] a replacement determination unit, configured to determine a replacement position of the parameterless static method according to the reference position, wherein the replacement position is a position where the code corresponding to the static final variable is replaced with the parameterless static method;

[0123] A determination unit is configured to determine a simulation scope in the simulation instance according to a replacement position of the parameterless static method.

[0124] In one embodiment, the device further comprises:

[0125] A function name determination module, which is configured to determine the alternative function access method name for the static final variable according to preset rules; wherein the preset rules include exclusion rules that have semantic differences with the method name contained in the target unit under test;

[0126] The enhancement module is specifically configured to add a parameterless static method for reading the value of the static final variable in the target unit under test based on the replacement function access method name based on the code enhancement technology.

[0127] In one embodiment, the device further comprises:

[0128] Based on the static method simulation tool, a simulation instance of the target unit under test is created, wherein the simulation instance includes the simulation scope and a stub for executing the parameterless static method in the simulation scope, wherein the stub is used to return a simulation object or an actually executable type for the static final variable.

[0129] In one embodiment, the property determination module 61 is configured to scan the code range of the target unit under test, obtain all variables in the code range modified with both static modifiers and final modifiers, and determine the static final variables to be simulated.

[0130] In one embodiment, the test module is specifically configured to execute the test process of the target unit under test according to the test rules carried by the unit test case, and the simulation instance and its simulation scope; wherein the test rules include exception test rules and / or performance test rules.

[0131] The above-mentioned device provided in the embodiment of the present application can be used to execute the technical solution of the unit testing method in the above-mentioned embodiment. Its implementation principle and technical effect are similar and will not be repeated here.

[0132] It should be noted that it should be understood that the division of the various modules of the above device is only a division of logical functions. In actual implementation, they can be fully or partially integrated into one physical entity, or they can be physically separated. And these modules can all be implemented in the form of software called by processing elements; they can also be all implemented in the form of hardware; some modules can also be implemented in the form of software called by processing elements, and some modules can be implemented in the form of hardware. For example, the attribute determination module 61 can be a separately established processing element, or it can be integrated in a chip of the above device. In addition, it can also be stored in the memory of the above device in the form of program code, and called and executed by a processing element of the above device. The function of the above attribute determination module 61. The implementation of other modules is similar. In addition, all or part of these modules can be integrated together, or they can be implemented independently. The processing element here can be an integrated circuit with signal processing capabilities. In the implementation process, each step of the above method or each module above can be completed by an integrated logic circuit of hardware in the processor element or instructions in the form of software.

[0133] Figure 7 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Figure 7 As shown, the electronic device may include: a transceiver 71 , a processor 72 , and a memory 73 .

[0134] The processor 72 executes the computer execution instructions stored in the memory, so that the processor 72 executes the unit test method in the above embodiment. The processor 72 can be a general-purpose processor, including a central processing unit CPU, a network processor (NP), etc.; it can also be a digital signal processor DSP, an application-specific integrated circuit ASIC, a field programmable gate array FPGA or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components.

[0135] The memory 73 is connected to the processor 72 via a system bus and completes communication between them. The memory 73 is used to store computer program instructions.

[0136] The transceiver 71 may be used to receive or send data to an external device.

[0137] The system bus can be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus, etc. The system bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus. The transceiver is used to realize the communication between the database access device and other computers (such as clients, read-write libraries, and read-only libraries). The memory may include random access memory (RAM) and may also include non-volatile memory.

[0138] The electronic device provided in the embodiment of the present application may be the terminal device of the above embodiment, which can be used to execute the technical solution of the unit testing method in the above embodiment, and its implementation principle and technical effect are similar, which will not be repeated here.

[0139] The embodiment of the present application also provides a computer-readable storage medium, in which computer-executable instructions are stored. When the computer-executable instructions are executed by a processor, they are used to implement the unit testing method provided in the above method embodiment.

[0140] The storage medium provided in the embodiment of the present application can be used to execute the technical solution of the unit testing method in the above embodiment. Its implementation principle and technical effect are similar and will not be repeated here.

[0141] The embodiment of the present application also provides a computer program product, including a computer program, which implements the unit testing method corresponding to the above method embodiment when executed by a processor.

[0142] The program product provided in the embodiment of the present application can be used to execute the technical solution of the unit testing method in the above embodiment. Its implementation principle and technical effect are similar and will not be repeated here.

[0143] The present application also provides a chip for executing instructions, which is used to execute the technical solution of the unit test method in the above embodiment. Its implementation principle and technical effect are similar and will not be repeated here.

[0144] In the several embodiments provided in the present 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 only schematic, for example, the division of modules is only a logical function division, and there may be other division methods in actual implementation, such as multiple modules 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 an indirect coupling or communication connection through some interfaces, devices or modules, which can be electrical, mechanical or other forms.

[0145] The modules described as separate components may or may not be physically separated, and the components shown as modules may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to implement the solution of this embodiment.

[0146] In addition, each functional module in each embodiment of the present application can be integrated into one processing unit, or each module can exist physically separately, or two or more modules can be integrated into one unit. The above-mentioned module-composed unit can be implemented in the form of hardware or in the form of hardware plus software functional units.

[0147] The above-mentioned integrated module implemented in the form of a software function module can be stored in a computer-readable storage medium. The above-mentioned software function module is stored in a storage medium, including a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor to perform some steps of the methods of various embodiments of the present application.

[0148] It should be understood that the processor may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), etc. A general-purpose processor may be a microprocessor or any conventional processor. The steps of the method disclosed in the invention may be directly implemented as being executed by a hardware processor, or may be implemented by a combination of hardware and software modules in the processor.

[0149] The memory may include a high-speed RAM memory, and may also include a non-volatile storage NVM, such as at least one disk memory, and may also be a USB flash drive, a mobile hard disk, a read-only memory, a magnetic disk or an optical disk, etc.

[0150] The bus may be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, the bus in the drawings of the present application is not limited to only one bus or one type of bus.

[0151] The above storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk. The storage medium can be any available medium that can be accessed by a general or special purpose computer.

[0152] An exemplary storage medium is coupled to a processor so that the processor can read information from the storage medium and write information to the storage medium. Of course, the storage medium can also be a component of the processor. The processor and the storage medium can be located in an application specific integrated circuit (ASIC). Of course, the processor and the storage medium can also exist as discrete components in an electronic control unit or a main control device.

[0153] Those skilled in the art can understand that all or part of the steps of implementing the above-mentioned method embodiments can be completed by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When the program is executed, the steps of the above-mentioned method embodiments are executed; and the aforementioned storage medium includes: ROM, RAM, disk or optical disk and other media that can store program codes.

[0154] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present application.

Claims

1. A unit testing method, characterized in that: include: According to the target unit under test in the unit test case, determine the static final variables to be simulated; Scanning the code range of the target unit under test to obtain the reference position of the code corresponding to the static final variable in the code range; When creating a simulation instance of the target unit under test, determining a simulation scope in the simulation instance according to the reference position, wherein the simulation scope is used to limit a simulation range of the static final variable; According to the simulation instance and its simulation scope, the test process of the target unit under test is executed to obtain the test result.

2. The method according to claim 1, characterized in that Also includes: Based on the code enhancement technology, a parameterless static method for reading the value of the static final variable is added in the target unit under test; Determining the simulation scope in the simulation instance according to the reference position includes: Determine, according to the reference position, a replacement position of the static method without parameters, wherein the replacement position is a position where the code corresponding to the static final variable is replaced with the static method without parameters; According to the replacement position of the parameterless static method, a simulation scope in the simulation instance is determined.

3. The method according to claim 2, characterized in that Also includes: According to a preset rule, determining a substitute function access method name for the static final variable; The replacement function access method name is used to access the static final variable, and the preset rules include exclusion rules that have semantic differences with the method name included in the target unit under test.

4. The method according to claim 3, characterized in that Also includes: Determine the alternative function access method name as the function name of the parameterless static method; The method of adding a parameterless static method for reading the value of the static final variable in the target unit under test based on the code enhancement technology includes: Based on the code enhancement technology and the function name of the parameterless static method, a parameterless static method for reading the value of the static final variable is added in the target unit under test.

5. The method according to any one of claims 1 to 4, characterized in that: Also includes: Based on the static method simulation tool, a simulation instance of the target unit under test is created, wherein the simulation instance includes the simulation scope and a stub for executing the parameterless static method in the simulation scope, wherein the stub is used to return a simulation object or an actually executable type for the static final variable.

6. The method according to any one of claims 1 to 4, characterized in that: The step of determining the static final variable to be simulated according to the target unit under test in the unit test case includes: The code range of the target unit under test is scanned to obtain all variables in the code range that are modified by both static modifiers and final modifiers, so as to determine the static final variables to be simulated.

7. The method according to any one of claims 1 to 4, characterized in that: The process of executing the test of the target unit under test according to the simulation instance and its simulation scope includes: The test process of the target unit under test is executed according to the test rules carried by the unit test case, and the simulation instance and its simulation scope; wherein the test rules include exception test rules and / or performance test rules.

8. A unit testing device, characterized in that: include: an attribute determination module configured to determine static final variables to be simulated according to a target unit under test in a unit test case; A reference acquisition module, configured to scan a code range of the target unit under test to obtain a reference position of a code corresponding to the static final variable in the code range; A scope determination module, which is configured to determine a simulation scope in the simulation instance according to the reference position when creating a simulation instance of the target unit under test, wherein the simulation scope is used to limit a simulation range of the static final variable; The test module is configured to execute the test process of the target unit under test according to the simulation instance and its simulation scope to obtain the test result.

9. The device according to claim 8, characterized in that Also includes: An enhancement module, which is configured to add a parameterless static method for reading the value of the static final variable in the target unit under test based on code enhancement technology; The scope determination module comprises: a replacement determination unit, configured to determine a replacement position of the parameterless static method according to the reference position, wherein the replacement position is a position where the code corresponding to the static final variable is replaced with the parameterless static method; A determination unit is configured to determine a simulation scope in the simulation instance according to a replacement position of the parameterless static method.

10. The device according to claim 9, characterized in that Also includes: A function name determination module, which is configured to determine the alternative function access method name for the static final variable according to preset rules; wherein the preset rules include exclusion rules that have semantic differences with the method name contained in the target unit under test; The enhancement module is specifically configured to add a parameterless static method for reading the value of the static final variable in the target unit under test based on the replacement function access method name based on the code enhancement technology.

11. An electronic device, characterized in that: include: A processor, and a memory communicatively connected to the processor; The memory stores computer-executable instructions; The processor executes the computer-executable instructions stored in the memory to implement the unit method according to any one of claims 1 to 7.

12. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, which are used to implement the method according to any one of claims 1 to 7 when executed by a processor.

13. A computer program product, characterized in that The invention comprises a computer program, which implements the unit testing method according to any one of claims 1 to 7 when being executed by a processor.