Unit testing method and device and storage medium

By decoupling test cases and environment through user configuration and controlling the behavior of objective functions, the problem of insufficient reusability of gtest framework in virtual environments and test cases is solved, and the efficiency and flexibility of C/C++ code unit testing is improved.

CN119088707APending Publication Date: 2024-12-06MOORE THREADS TECHNOLOGY (SHANGHAI) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411217086.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-30
Publication Date
2024-12-06

AI Technical Summary

Technical Problem

Traditional C/C++ code unit testing frameworks such as Google Test cannot support virtual environment operation, test cases are not reusable, test cases are not configurable, and additional code is required to simulate interface output in each test case code, resulting in efficiency and flexibility. insufficient.

Method used

By determining the user configuration, decoupling the test cases from the test environment, making the test cases run in a virtual environment, and controlling the behavior of the objective functions through user configuration, implementing different logic processing, making the test cases reusable.

Benefits of technology

It realizes running test cases in a virtual test environment under the gtest framework, reduces the difficulty and maintenance cost of test cases, improves test coverage and flexibility, and supports the reuse of test cases in different environments.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119088707A_ABST
    Figure CN119088707A_ABST
Patent Text Reader

Abstract

The invention relates to a unit testing method and device and a storage medium. The method comprises the following steps: determining user configuration, wherein the user configuration is used for determining a test case and a test environment; running a test code associated with the test case in the test environment; and in the process of running the test code, calling the target function in response to the test code, controlling the behavior of the target function according to the user configuration, and returning a function execution result. According to the embodiment of the invention, various required test environments can be simulated, and the dependence of the test case and the test environment is decoupled, so that the test case can be operated in a virtual test environment configured by a user during operation under a gtest framework, and the writing and maintenance of the test case are facilitated. Different processing logics can be realized through different configurations, so that a set of test cases can be operated in different test environments by loading different configurations, various test environments can be covered through one set of test cases, and the effect that the test cases can be reused is achieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of computer software technology, and in particular to a unit testing method, device and storage medium. Background Art

[0002] In the traditional software development process, unit testing of C / C++ code often relies on manually writing test cases and using custom test frameworks for testing. In recent years, with the development of the software development industry, some excellent automated testing frameworks have emerged, among which Google Test (gtest) is one of them. gtest is a C++ unit testing framework developed by Google, which can greatly simplify the unit testing process of C / C++ code and improve the efficiency and quality of testing. However, the gtest framework has problems such as not being able to support running in a virtual environment, test cases that are not reusable, test cases that are not configurable, and the need to add additional code to each test case code to simulate the output of certain interfaces. Summary of the invention

[0003] In view of this, the present disclosure proposes a unit testing method, device and storage medium.

[0004] According to one aspect of the present disclosure, a unit testing method is provided. The method comprises:

[0005] Determine user configuration, which is used to determine test cases and test environment;

[0006] Run the test code associated with the test case in the test environment;

[0007] In the process of running the test code, in response to the test code calling the target function, the behavior of the target function is controlled according to the user configuration, and the function execution result is returned.

[0008] In a possible implementation, during the process of running the test code, in response to the test code calling the target function, controlling the behavior of the target function according to the user configuration, and returning the function execution result include:

[0009] In the process of running the test code, in response to the test code calling the target function, the custom function corresponding to the target function is run according to the user configuration, and the function execution result is returned.

[0010] In a possible implementation, the user configuration for the test case includes the configuration for the behavior of the objective function and the expected output value of the objective function.

[0011] In the process of running the test code, in response to the test code calling the target function, the custom function corresponding to the target function is run according to the user configuration, and the function execution result is returned, including:

[0012] In the process of running the test code, in response to the test code calling the target function, the input parameters of the custom function are determined according to the configuration for the behavior of the target function;

[0013] Based on the input parameters of the custom function, the custom function corresponding to the target function is run to determine the output value of the target function;

[0014] Returns the function execution result based on the output value of the objective function and the expected output value.

[0015] In a possible implementation, during the process of running the test code, in response to the test code calling the target function, running the custom function corresponding to the target function according to the user configuration, and returning the function execution result, including:

[0016] During the process of running the test code, in response to the test code calling the target function and the existence of a custom function with a function signature consistent with the target function in the custom function library, the custom function with the function signature consistent with the target function is run according to the user configuration, and the function execution result is returned.

[0017] In a possible implementation, during the process of running the test code, in response to the test code calling the target function, running the custom function corresponding to the target function according to the user configuration, and returning the function execution result, including:

[0018] In the process of running the test code, in response to the test code calling the target function and the target function including a preset jump instruction, determining the custom function based on the preset jump instruction;

[0019] Run the custom function corresponding to the preset jump instruction according to the user configuration and return the function execution result.

[0020] In a possible implementation, during the process of running the test code, in response to the test code calling the target function, controlling the behavior of the target function according to the user configuration, and returning the function execution result, further includes:

[0021] In the process of running the test code, in response to the test code calling the target function, the input parameters of the target function are modified, the output value of the target function is modified, and any one or more operations in the target function are skipped according to the user configuration, and the function execution result is returned.

[0022] In a possible implementation, the method further includes:

[0023] Start the test case process;

[0024] In response to the test case process being started, loading a custom function library, the custom function library including one or more custom functions;

[0025] In response to the completion of loading the custom function library, loading the received user configuration;

[0026] Run the test code associated with the test case in the test environment, including:

[0027] In response to the user configuration loading being completed, the test code associated with the test case is run in the test environment.

[0028] In a possible implementation, the user configuration includes any one or more forms of a user configuration file, configuration information stored in a database, and a visual interface configuration.

[0029] In a possible implementation, the target function includes an interface function of the system and an interface function of the test code.

[0030] According to another aspect of the present disclosure, a unit testing device is provided. The device comprises:

[0031] A determination module is used to determine user configuration, and the user configuration is used to determine test cases and test environment;

[0032] The run module is used to run the test code associated with the test case in the test environment;

[0033] The control module is used to call the target function in response to the test code during the running of the test code, control the behavior of the target function according to the user configuration, and return the function execution result.

[0034] In a possible implementation, the control module is used to:

[0035] In the process of running the test code, in response to the test code calling the target function, the custom function corresponding to the target function is run according to the user configuration, and the function execution result is returned.

[0036] In a possible implementation, the user configuration for the test case includes the configuration for the behavior of the objective function and the expected output value of the objective function.

[0037] In the process of running the test code, in response to the test code calling the target function, the custom function corresponding to the target function is run according to the user configuration, and the function execution result is returned, including:

[0038] In the process of running the test code, in response to the test code calling the target function, the input parameters of the custom function are determined according to the configuration for the behavior of the target function;

[0039] Based on the input parameters of the custom function, the custom function corresponding to the target function is run to determine the output value of the target function;

[0040] Returns the function execution result based on the output value of the objective function and the expected output value.

[0041] In a possible implementation, during the process of running the test code, in response to the test code calling the target function, running the custom function corresponding to the target function according to the user configuration, and returning the function execution result, including:

[0042] During the process of running the test code, in response to the test code calling the target function and the existence of a custom function with a function signature consistent with the target function in the custom function library, the custom function with the function signature consistent with the target function is run according to the user configuration, and the function execution result is returned.

[0043] In a possible implementation, during the process of running the test code, in response to the test code calling the target function, running the custom function corresponding to the target function according to the user configuration, and returning the function execution result, including:

[0044] In the process of running the test code, in response to the test code calling the target function and the target function including a preset jump instruction, determining the custom function based on the preset jump instruction;

[0045] Run the custom function corresponding to the preset jump instruction according to the user configuration and return the function execution result.

[0046] In a possible implementation, the control module is further configured to:

[0047] In the process of running the test code, in response to the test code calling the target function, the input parameters of the target function are modified, the output value of the target function is modified, and any one or more operations in the target function are skipped according to the user configuration, and the function execution result is returned.

[0048] In a possible implementation, the device further includes:

[0049] Startup module, used to start the test case process;

[0050] A first loading module, configured to load a custom function library in response to the start of a test case process, wherein the custom function library includes one or more custom functions;

[0051] A second loading module, configured to load the received user configuration in response to completion of loading of the custom function library;

[0052] Run modules for:

[0053] In response to the user configuration loading being completed, the test code associated with the test case is run in the test environment.

[0054] In a possible implementation, the user configuration includes any one or more forms of a user configuration file, configuration information stored in a database, and a visual interface configuration.

[0055] In a possible implementation, the target function includes an interface function of the system and an interface function of the test code.

[0056] According to another aspect of the present disclosure, a unit testing device is provided, comprising: a processor; and a memory for storing processor-executable instructions; wherein the processor is configured to implement the above method when executing the instructions stored in the memory.

[0057] According to another aspect of the present disclosure, a non-volatile computer-readable storage medium is provided, on which computer program instructions are stored, wherein the computer program instructions implement the above method when executed by a processor.

[0058] According to another aspect of the present disclosure, a computer program product is provided, including a computer-readable code, or a non-volatile computer-readable storage medium carrying the computer-readable code. When the computer-readable code runs in a processor of an electronic device, the processor in the electronic device executes the above method.

[0059] According to the embodiment of the present application, by determining the user configuration, the user configuration is used to determine the test case and the test environment, and various required test environments can be simulated to decouple the test case from the test environment, so that when running under the gtest framework, it can be run in the virtual test environment configured by the user. Through the user configuration, the test code associated with the test case can also be separated from the environment configuration for executing the test, which is convenient for the writing and maintenance of the test case. By running the test code associated with the test case in the test environment, and in the process of running the test code, responding to the test code to call the target function, controlling the behavior of the target function according to the user configuration, and returning the function execution result, different processing logics can be implemented through different configurations, so that a set of test cases can be run in different test environments by loading different configurations, and various test environments can be covered by a set of test cases, so as to achieve the effect of test case reusability.

[0060] Further features and aspects of the present disclosure will become apparent from the following detailed description of exemplary embodiments with reference to the attached drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0061] The accompanying drawings, which are incorporated in and constitute a part of the specification, illustrate exemplary embodiments, features, and aspects of the disclosure and, together with the description, serve to explain the principles of the disclosure.

[0062] Figure 1A schematic diagram showing an application scenario according to an embodiment of the present application.

[0063] Figure 2 A schematic structural diagram of a unit testing system according to an embodiment of the present application is shown.

[0064] Figure 3 A flow chart of a unit testing method according to an embodiment of the present application is shown.

[0065] Figure 4 A schematic diagram of the overall process of a unit testing method according to an embodiment of the present application is shown.

[0066] Figure 5 A flow chart of a unit testing method according to an embodiment of the present application is shown.

[0067] Figure 6 A schematic diagram showing a hook method for a target function according to an embodiment of the present application is shown.

[0068] Figure 7 A schematic flow chart of a unit testing method according to an embodiment of the present application is shown.

[0069] Figure 8 A structural diagram of a unit testing device according to an embodiment of the present application is shown.

[0070] Fig. 9 It is a block diagram of a device 1900 for unit testing according to an exemplary embodiment. DETAILED DESCRIPTION

[0071] Various exemplary embodiments, features and aspects of the present disclosure will be described in detail below with reference to the accompanying drawings. The same reference numerals in the accompanying drawings represent elements with the same or similar functions. Although various aspects of the embodiments are shown in the accompanying drawings, the drawings are not necessarily drawn to scale unless otherwise specified.

[0072] The word “exemplary” is used exclusively herein to mean “serving as an example, example, or illustration.” Any embodiment described herein as “exemplary” is not necessarily to be construed as preferred or advantageous over other embodiments.

[0073] In addition, in order to better illustrate the present disclosure, numerous specific details are given in the following specific embodiments. It should be understood by those skilled in the art that the present disclosure can also be implemented without certain specific details. In some examples, methods, means, components and circuits well known to those skilled in the art are not described in detail in order to highlight the subject matter of the present disclosure.

[0074] In the traditional software development process, unit testing of C / C++ code often relies on manually writing test cases and using custom test frameworks for testing. In recent years, with the development of the software development industry, some excellent automated testing frameworks have emerged, among which Google Test (gtest) is one of them. gtest is a C++ unit testing framework developed by Google, which can greatly simplify the unit testing process of C / C++ code and improve the efficiency and quality of testing. However, the gtest framework has problems such as not being able to support running in a virtual environment, test cases that are not reusable, test cases that are not configurable, and the need to add additional code to each test case code to simulate the output of certain interfaces.

[0075] In view of this, the present application provides a unit test method, device and storage medium. The method of the embodiment of the present application can simulate various required test environments by determining user configuration, and user configuration is used to determine test cases and test environments, so that the test cases can be run in the virtual test environment configured by the user when running under the gtest framework, and the test code associated with the test case can also be separated from the environment configuration for executing the test through user configuration, which is convenient for the writing and maintenance of test cases. By running the test code associated with the test case in the test environment, and in the process of running the test code, the target function is called in response to the test code, the behavior of the target function is controlled according to the user configuration, and the function execution result is returned. Different processing logics can be implemented through different configurations, so that a set of test cases can be run in different test environments by loading different configurations, and various test environments can be covered by a set of test cases, so as to achieve the effect of test case reusability.

[0076] Figure 1 A schematic diagram of an application scenario according to an embodiment of the present application is shown. The unit testing system of the embodiment of the present application can be used in a scenario where a unit test of a C / C++ code is performed based on a gtest framework, such as Figure 1 As shown, the unit test system of the embodiment of the present application can receive the configuration input by the user, select the test case and the test environment by the user through the user input configuration, execute the test case process based on the test case and the test environment selected by the user, execute the test code corresponding to the test case (which can be c / c++ code) in this process, and obtain the execution result of the test case based on the expected output configured by the user. The test results of the test case under the gtest framework can include detailed information, coverage information, running time, etc. of each test case as failed / passed, failed test cases, so as to realize the unit test of c / c++ code.

[0077] The unit test system of the embodiment of the present application can be deployed in a terminal device or a server. Among them, the terminal device can be any one or more of a mobile phone, a foldable electronic device, a tablet computer, a desktop computer, a laptop computer, a handheld computer, a notebook computer, an ultra-mobile personal computer (ultra-mobile personal computer, UMPC), a netbook, a cellular phone, a personal digital assistant (personal digital assistant, PDA), and a vehicle-mounted device. The embodiment of the present disclosure does not impose any special restrictions on the specific type of the terminal device, and it can have a wired or wireless communication function.

[0078] The server can be located locally or in the cloud, and can be a physical device or a virtual device, such as a virtual machine, a container, etc., and has a wireless communication function, wherein the wireless communication function can be set in the chip (system) or other parts or components of the server. The wireless communication function can be realized, for example, through mobile communication technologies such as 2G / 3G / 4G / 5G, as well as Wi-Fi, Bluetooth, frequency modulation (FM), digital radio, satellite communication, etc. Communication can also be carried out through wired connection to achieve interaction with other devices.

[0079] Figure 2 FIG. 2 shows a schematic diagram of the structure of a unit test system according to an embodiment of the present application. Figure 2 As shown, the unit test system may include a main control module, a hook library, a function behavior control module, and a data configuration module. The main control module may be used to receive and process the configuration input by the user, and control the execution of the unit test process; the hook library may include a custom function for implementing the hook operation on the target function; the function behavior control module may be used to control the behavior of the function based on the configuration input by the user; and the data configuration module may be used to parse the configuration input by the user to obtain the function behavior configuration data associated with the custom function.

[0080] The following Figure 2 Based on this, the unit testing method of this application is introduced.

[0081] Figure 3 FIG. 1 is a flow chart showing a unit testing method according to an embodiment of the present application. The method can be used in the above-mentioned unit testing system, such as Figure 3 As shown, the method may include:

[0082] Step S301, determine user configuration.

[0083] The user configuration may be a configuration written by the user according to the test requirements, and the user configuration may be received by the main control module. The user configuration includes any one or more forms of a user configuration file, configuration information stored in a database, and a visual interface configuration. The user configuration file may be a text file stored in a file system in any format, and the configuration information stored in the database may be stored in the database in the form of a table.

[0084] Among them, user configuration can be used to determine test cases and test environments. As a result, users can reduce the difficulty of test case development, reduce the number of test cases, and improve test coverage. At the same time, it can reduce maintenance costs, so that test cases and environment configurations can be maintained separately, and the configuration file does not need to be recompiled to modify the entire configuration case library. And because the user configuration includes the configuration for the test environment, it can reduce the investment in the physical test environment. Through the configuration file, various test environments can be simulated, reducing the demand for the physical environment.

[0085] The user configuration for the test case may include configuration for the behavior of the objective function and the expected output value of the objective function.

[0086] By making the user configuration for the test case include the expected output value, the return value of the function can also be configured in the user configuration, so that the function interface can return the specified content when it is executed, and in this process, the output of the simulation interface can be realized without adding additional code in the target function.

[0087] The target function includes the interface function of the system and the interface function of the test code. The target function can be one or more functions preset in the test code associated with the test case. The configuration of the target function behavior can be used to perform hook operations corresponding to certain functions during the execution of the test case to change its behavior or record its call status. It can include information such as selecting the target function to be hooked, the processing logic after the hook, whether to enable or disable certain hooks, etc., so that the behavior of the target function can be flexibly controlled during the test process to adapt to different test requirements and scenarios.

[0088] In the user configuration of the test case, the expected output result of each test case can be set. The expected output value of the target function can include the expected return value, the value of the output parameter, the function call status, etc., so that the actual output and the expected output value of the corresponding target function can be compared after executing the test case to verify and judge the functional correctness of the code under test.

[0089] Figure 4 The overall flow chart of the unit testing method according to an embodiment of the present application is shown. Figure 4 As shown, the overall process of the unit testing method in the embodiment of the present application includes:

[0090] Step S41, receiving and processing user configuration in the main process;

[0091] Step S42, start the test case process;

[0092] Step S43, loading and initializing the hook library in the test case process;

[0093] The hook library can be used to implement hooking of functions.

[0094] Step S44, executing the test case;

[0095] The test code associated with the test case can be run, and the function behavior configuration file (ie user configuration) is loaded when the target function is run, and the behavior of the target function is controlled by means of the hook function.

[0096] After the test case is executed, the execution result can be returned to end the test case process and then end the main process.

[0097] exist Figure 4 Based on the overall process introduced, you can refer to Figure 5 , shows a flow chart of a unit testing method according to an embodiment of the present application. Figure 5 As shown, after step S301, the method may further include:

[0098] Step S501, start the test case process.

[0099] The test case process can be started by the main control module, and the test case process can be determined by the user according to the test requirements. This step can correspond to Figure 4 Step S41.

[0100] Step S502, in response to the test case process starting, loading the custom function library.

[0101] The custom function library includes one or more custom functions. The custom function library can be the above-mentioned hook library, and the hook library can be loaded into the process of the test case by calling the interface provided by the gtest framework. The hook library can also be initialized by configuring the parameters of the hook operation. This step can correspond to Figure 4 Step S43.

[0102] Step S503, in response to the completion of loading the custom function library, load the received user configuration.

[0103] The received user configuration can be loaded by the function behavior control module and used as function behavior data. The function behavior data is loaded into the form of a configuration file through the data configuration module and then returned to the function behavior control module, and then returned to the test case process through the hook library.

[0104] This will start running the test cases.

[0105] Return to see Figure 3 :

[0106] Step S302: running the test code associated with the test case in the test environment.

[0107] The test environment can be determined based on user configuration. This step can be performed during the test case process, see Figure 5 , the step S302 may include:

[0108] Step S504: in response to the completion of loading the user configuration, running the test code associated with the test case in the test environment.

[0109] Return to see Figure 3 :

[0110] Step S303, in the process of running the test code, in response to the test code calling the target function, the behavior of the target function is controlled according to the user configuration, and the function execution result is returned.

[0111] Among them, controlling the behavior of the target function according to the user configuration may include controlling the parameters and return values ​​of the target function according to the user configuration, which may be achieved by hooking. The function execution result may include the execution status of each test case, such as whether the test case is failed / passed, detailed information of the failed test case, coverage information, running time, etc.

[0112] According to the embodiment of the present application, by determining the user configuration, the user configuration is used to determine the test case and the test environment, and various required test environments can be simulated to decouple the test case from the test environment, so that when running under the gtest framework, it can be run in the virtual test environment configured by the user. Through the user configuration, the test code associated with the test case can also be separated from the environment configuration for executing the test, which is convenient for the writing and maintenance of the test case. By running the test code associated with the test case in the test environment, and in the process of running the test code, responding to the test code to call the target function, controlling the behavior of the target function according to the user configuration, and returning the function execution result, different processing logics can be implemented through different configurations, so that a set of test cases can be run in different test environments by loading different configurations, and various test environments can be covered by a set of test cases, so as to achieve the effect of test case reusability.

[0113] In step S303, you can:

[0114] In the process of running the test code, in response to the test code calling the target function, the input parameters of the target function are modified, the output value of the target function is modified, and any one or more operations in the target function are skipped according to the user configuration, and the function execution result is returned.

[0115] For example, the user configuration can specify the input parameters and output values ​​of the modified objective function, functions to be skipped, etc.

[0116] Thus, the behavior of the objective function can be controlled according to user configuration to flexibly simulate various required test cases and test environments.

[0117] In step S303, you can:

[0118] In the process of running the test code, in response to the test code calling the target function, the custom function corresponding to the target function is run according to the user configuration, and the function execution result is returned.

[0119] When the test code calls the target function, you can enter the custom function through the hook method, run the custom function, and control the target function parameters and return values ​​based on the user configuration through the custom processing logic in the custom function. For example, the user configuration can specify the parameter values ​​and expected output values ​​of the target function.

[0120] Among them, you can:

[0121] In the process of running the test code, in response to the test code calling the target function, the input parameters of the custom function are determined according to the configuration of the target function behavior; based on the input parameters of the custom function, the custom function corresponding to the target function is run to determine the output value of the target function, and the function execution result is returned based on the output value of the target function and the expected output value.

[0122] For example, in the configuration of the objective function behavior, the input parameter value of the objective function can be specified, the input parameter value can be passed as the input parameter of the custom function, and the custom function can be run to obtain the running result (ie, the output value) as the output value of the objective function.

[0123] The output value of the objective function can be the output value obtained by executing the code corresponding to the objective function in the custom function. The expected output value can be the expected output result for each test case (that is, the result when the test code functions correctly), which can include the expected return value, the value of the output parameter, the function call status, etc.

[0124] The expected output value of the test case can be described in a user-configured configuration file. When the test case is executed on different test environments, the expected output of the objective function can be controlled by loading different configuration files.

[0125] The expected output value and the output value of the target function can be compared to determine the accuracy of the test result to return the function execution result.

[0126] See also Figure 6 , showing a schematic diagram of a hook method for a target function according to an embodiment of the present application. Figure 6 As shown, the solid arrow indicates the process of running the test code when the hook operation corresponding to the target function is not executed, and the dotted arrow indicates the process of running the test code when the hook operation corresponding to the target function is executed, where the target function is "funcA". Through the hook method, when "funcA" is called during the running of the test code, the custom function can be run, where "funcA" can be pre-processed and post-processed according to the user configuration in the custom function to achieve the control of the behavior of "funcA". Pre-processing, for example, controls the parameter value passed into the target function, and after the output value of "funcA" is obtained after executing "funcA", post-processing, for example, returns the function execution result based on the expected output value and the output value of "funcA".

[0127] In different test environments, the way to implement the hook may be different. In the embodiments of the present application, the cross-platform issue is taken into consideration, and the way to implement the hook may be adjusted based on different test environments.

[0128] For example, in a Linux system, the hook of the target function can be implemented by preloading. In step S303, the following can be done:

[0129] During the process of running the test code, in response to the test code calling the target function and the existence of a custom function with a function signature consistent with the target function in the custom function library, the custom function with the function signature consistent with the target function is run according to the user configuration, and the function execution result is returned.

[0130] Among them, in the preload mode, the specified dynamic library (i.e., the custom function library) can be loaded into the test case process, and the dynamic library can store the custom function that has the same function signature as the target function. The function signature consistency can be that the parameter type, number of parameters, and parameter order in the function declaration of the custom function and the target function are the same.

[0131] Thus, when the test code calls the target function, the custom function can be run according to the user configuration.

[0132] For another example, in a Windows system, the target function can be inline hooked through the detours library. In step S303, the following can be done:

[0133] In the process of running the test code, in response to the test code calling the target function and the target function including a preset jump instruction, a custom function is determined based on the preset jump instruction; the custom function corresponding to the preset jump instruction is run according to the user configuration, and the function execution result is returned.

[0134] In the Windows system, a jump instruction can be preset at the beginning of the target function, so that when the test code calls the target function, it can jump to the custom function corresponding to the preset jump instruction, so that when the target function is called, the behavior of the original function can be modified to execute additional custom logic (such as the above-mentioned pre-processing and post-processing).

[0135] Figure 7 A schematic diagram of a unit testing method according to an embodiment of the present application is shown. Figure 2 Under the structure of the unit test system shown in Figure 7 As shown, the main control module of the embodiment of the present application can receive the configuration (test configuration) input by the user and process the user configuration. When processing the user configuration, the test case and test environment executed by the user can be selected to initialize the environment (including determining the test code corresponding to the test environment and the test case). This part of the process can correspond to the above step S301;

[0136] After that, the main process may start the test case process and start waiting for the test case process to be executed. This part of the process may correspond to the above step S501;

[0137] In the test case process, the hook library may be loaded and initialized, and this part of the process may correspond to the above step S502;

[0138] After the hook library is loaded and initialized, the function behavior control module can be initialized. In this process, the function behavior control module can load the function behavior data (i.e., user configuration), and the function behavior data is loaded into the form of a configuration file by the data configuration module and returned to the function behavior configuration module, which is saved by the function behavior configuration module. This part of the process can correspond to the above step S503;

[0139] After the user configuration is loaded, the function behavior control module can notify the test case process through the hook library, so that the test code associated with the test case can be run in the test environment;

[0140] Referring to the loop process in the figure, since the test code may include multiple target functions, when the test code is run in the test case process, when the hooked function (i.e., the target function) is run, the custom function in the hook library may be called, so that the custom function may be run and the custom function processing logic may be entered. In the process of running the custom function, the hook library may obtain the behavior configuration information (i.e., user configuration) of the currently executed function (i.e., the target function) and query the configuration information, and return the function execution result according to the configuration information. This part of the process may correspond to the above step S303;

[0141] When the test code is executed, the main control module can be notified to end the test case process.

[0142] Figure 8 FIG. 2 shows a structural diagram of a unit testing device according to an embodiment of the present application. Figure 8 As shown, the device comprises:

[0143] Determination module 801, used to determine user configuration, the user configuration is used to determine the test case and test environment;

[0144] An operation module 802 is used to execute the test code associated with the test case in the test environment;

[0145] The control module 803 is used to control the behavior of the target function according to the user configuration and return the function execution result in response to the test code calling the target function during the running of the test code.

[0146] In a possible implementation, the control module 803 is configured to:

[0147] In the process of running the test code, in response to the test code calling the target function, the custom function corresponding to the target function is run according to the user configuration, and the function execution result is returned.

[0148] In a possible implementation, the user configuration for the test case includes the configuration for the behavior of the objective function and the expected output value of the objective function.

[0149] In the process of running the test code, in response to the test code calling the target function, the custom function corresponding to the target function is run according to the user configuration, and the function execution result is returned, including:

[0150] In the process of running the test code, in response to the test code calling the target function, the input parameters of the custom function are determined according to the configuration for the behavior of the target function;

[0151] Based on the input parameters of the custom function, the custom function corresponding to the target function is run to determine the output value of the target function;

[0152] Returns the function execution result based on the output value of the objective function and the expected output value.

[0153] In a possible implementation, during the process of running the test code, in response to the test code calling the target function, running the custom function corresponding to the target function according to the user configuration, and returning the function execution result, including:

[0154] During the process of running the test code, in response to the test code calling the target function and the existence of a custom function with a function signature consistent with the target function in the custom function library, the custom function with the function signature consistent with the target function is run according to the user configuration, and the function execution result is returned.

[0155] In a possible implementation, during the process of running the test code, in response to the test code calling the target function, running the custom function corresponding to the target function according to the user configuration, and returning the function execution result, including:

[0156] In the process of running the test code, in response to the test code calling the target function and the target function including a preset jump instruction, determining the custom function based on the preset jump instruction;

[0157] Run the custom function corresponding to the preset jump instruction according to the user configuration and return the function execution result.

[0158] In a possible implementation, the control module 803 is further configured to:

[0159] In the process of running the test code, in response to the test code calling the target function, the input parameters of the target function are modified, the output value of the target function is modified, and any one or more operations in the target function are skipped according to the user configuration, and the function execution result is returned.

[0160] In a possible implementation, the device further includes:

[0161] Startup module, used to start the test case process;

[0162] A first loading module, configured to load a custom function library in response to the start of a test case process, wherein the custom function library includes one or more custom functions;

[0163] A second loading module, configured to load the received user configuration in response to completion of loading of the custom function library;

[0164] The operation module 802 is used to:

[0165] In response to the user configuration loading being completed, the test code associated with the test case is run in the test environment.

[0166] In a possible implementation, the user configuration includes any one or more forms of a user configuration file, configuration information stored in a database, and a visual interface configuration.

[0167] In a possible implementation, the target function includes an interface function of the system and an interface function of the test code.

[0168] According to the embodiment of the present application, by determining the user configuration, the user configuration is used to determine the test case and the test environment, and various required test environments can be simulated to decouple the test case from the test environment, so that when running under the gtest framework, it can be run in the virtual test environment configured by the user. Through the user configuration, the test code associated with the test case can also be separated from the environment configuration for executing the test, which is convenient for the writing and maintenance of the test case. By running the test code associated with the test case in the test environment, and in the process of running the test code, responding to the test code to call the target function, controlling the behavior of the target function according to the user configuration, and returning the function execution result, different processing logics can be implemented through different configurations, so that a set of test cases can be run in different test environments by loading different configurations, and various test environments can be covered by a set of test cases, so as to achieve the effect of test case reusability.

[0169] In some embodiments, the functions or modules included in the device provided by the embodiments of the present disclosure can be used to execute the method described in the above method embodiments. The specific implementation can refer to the description of the above method embodiments, and for the sake of brevity, it will not be repeated here.

[0170] The embodiment of the present disclosure also provides a computer-readable storage medium on which computer program instructions are stored, and the computer program instructions implement the above method when executed by a processor. The computer-readable storage medium can be a volatile or non-volatile computer-readable storage medium.

[0171] The embodiment of the present disclosure also proposes a unit testing device, comprising: a processor; a memory for storing processor executable instructions; wherein the processor is configured to implement the above method when executing the instructions stored in the memory.

[0172] The embodiments of the present disclosure also provide a computer program product, including a computer-readable code, or a non-volatile computer-readable storage medium carrying the computer-readable code. When the computer-readable code runs in a processor of an electronic device, the processor in the electronic device executes the above method.

[0173] Fig. 91 is a block diagram of an apparatus 1900 for unit testing according to an exemplary embodiment. For example, the apparatus 1900 may be provided as a server or a terminal device. Fig. 9 , the apparatus 1900 includes a processing component 1922, which further includes one or more processors, and a memory resource represented by a memory 1932 for storing instructions, such as an application, that can be executed by the processing component 1922. The application stored in the memory 1932 may include one or more modules, each corresponding to a set of instructions. In addition, the processing component 1922 is configured to execute instructions to perform the above method.

[0174] The device 1900 may also include a power supply component 1926 configured to perform power management of the device 1900, a wired or wireless network interface 1950 configured to connect the device 1900 to a network, and an input / output interface 1958 (I / O interface). The device 1900 may operate based on an operating system stored in the memory 1932, such as Windows Server 2000. TM , MacOS X TM , Unix TM ,Linux TM , FreeBSD TM or similar.

[0175] In an exemplary embodiment, a non-volatile computer-readable storage medium is also provided, such as a memory 1932 including computer program instructions, which can be executed by the processing component 1922 of the device 1900 to perform the above method.

[0176] The present disclosure may be a system, a method and / or a computer program product. The computer program product may include a computer-readable storage medium carrying computer-readable program instructions for causing a processor to implement various aspects of the present disclosure.

[0177] A computer-readable storage medium may be a tangible device that can hold and store instructions used by an instruction execution device. A computer-readable storage medium may be, for example, but not limited to, an electrical storage device, a magnetic storage device, an optical storage device, an electromagnetic storage device, a semiconductor storage device, or any suitable combination of the foregoing. More specific examples of computer-readable storage media (a non-exhaustive list) include: a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), a static random access memory (SRAM), a portable compact disk read-only memory (CD-ROM), a digital versatile disk (DVD), a memory stick, a floppy disk, a mechanical encoding device, such as a punch card or a raised structure in a groove on which instructions are stored, and any suitable combination of the foregoing. As used herein, a computer-readable storage medium is not to be interpreted as a transient signal per se, such as a radio wave or other freely propagating electromagnetic wave, an electromagnetic wave propagating through a waveguide or other transmission medium (e.g., a light pulse through a fiber optic cable), or an electrical signal transmitted through a wire.

[0178] The computer-readable program instructions described herein can be downloaded from a computer-readable storage medium to each computing / processing device, or downloaded to an external computer or external storage device via a network, such as the Internet, a local area network, a wide area network, and / or a wireless network. The network can include copper transmission cables, optical fiber transmissions, wireless transmissions, routers, firewalls, switches, gateway computers, and / or edge servers. The network adapter card or network interface in each computing / processing device receives the computer-readable program instructions from the network and forwards the computer-readable program instructions for storage in the computer-readable storage medium in each computing / processing device.

[0179] The computer program instructions for performing the operation of the present disclosure may be assembly instructions, instruction set architecture (ISA) instructions, machine instructions, machine-related instructions, microcode, firmware instructions, state setting data, or source code or object code written in any combination of one or more programming languages, including object-oriented programming languages, such as Smalltalk, C++, etc., and conventional procedural programming languages, such as "C" language or similar programming languages. Computer-readable program instructions may be executed completely on a user's computer, partially on a user's computer, as an independent software package, partially on a user's computer, partially on a remote computer, or completely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer via any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., using an Internet service provider to connect via the Internet). In some embodiments, an electronic circuit, such as a programmable logic circuit, a field programmable gate array (FPGA), or a programmable logic array (PLA), may be personalized by utilizing the state information of the computer-readable program instructions, and the electronic circuit may execute the computer-readable program instructions, thereby realizing various aspects of the present disclosure.

[0180] Various aspects of the present disclosure are described herein with reference to the flowcharts and / or block diagrams of the methods, devices (systems) and computer program products according to the embodiments of the present disclosure. It should be understood that each box in the flowchart and / or block diagram and the combination of each box in the flowchart and / or block diagram can be implemented by computer-readable program instructions.

[0181] These computer-readable program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, thereby producing a machine, so that when these instructions are executed by the processor of the computer or other programmable data processing device, a device that implements the functions / actions specified in one or more boxes in the flowchart and / or block diagram is generated. These computer-readable program instructions can also be stored in a computer-readable storage medium, and these instructions cause the computer, programmable data processing device, and / or other equipment to work in a specific manner, so that the computer-readable medium storing the instructions includes a manufactured product, which includes instructions for implementing various aspects of the functions / actions specified in one or more boxes in the flowchart and / or block diagram.

[0182] Computer-readable program instructions may also be loaded onto a computer, other programmable data processing apparatus, or other device so that a series of operating steps are performed on the computer, other programmable data processing apparatus, or other device to produce a computer-implemented process, thereby causing the instructions executed on the computer, other programmable data processing apparatus, or other device to implement the functions / actions specified in one or more boxes in the flowchart and / or block diagram.

[0183] The flow chart and block diagram in the accompanying drawings show the possible architecture, function and operation of the system, method and computer program product according to multiple embodiments of the present disclosure. In this regard, each square box in the flow chart or block diagram can represent a part of a module, program segment or instruction, and a part of the module, program segment or instruction includes one or more executable instructions for realizing the specified logical function. In some alternative implementations, the function marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two continuous square boxes can actually be executed substantially in parallel, and they can sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs the specified function or action, or can be implemented with a combination of special hardware and computer instructions.

[0184] The embodiments of the present disclosure have been described above, and the above description is exemplary, not exhaustive, and is not limited to the disclosed embodiments. Many modifications and changes will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the described embodiments. The selection of terms used herein is intended to best explain the principles of the embodiments, practical applications, or technical improvements in the market, or to enable other persons of ordinary skill in the art to understand the embodiments disclosed herein.

Claims

1. A unit testing method, characterized in that: The method comprises: Determine a user configuration, wherein the user configuration is used to determine a test case and a test environment; Running the test code associated with the test case in the test environment; In the process of running the test code, in response to the test code calling the target function, the behavior of the target function is controlled according to the user configuration, and the function execution result is returned.

2. The method according to claim 1, characterized in that In the process of running the test code, in response to the test code calling the target function, controlling the behavior of the target function according to the user configuration, and returning the function execution result, comprises: In the process of running the test code, in response to the test code calling the target function, a custom function corresponding to the target function is run according to the user configuration, and a function execution result is returned.

3. The method according to claim 2, characterized in that The user configuration for the test case includes the configuration for the behavior of the objective function and the expected output value of the objective function. In the process of running the test code, in response to the test code calling the target function, running the custom function corresponding to the target function according to the user configuration, and returning the function execution result, includes: In the process of running the test code, in response to the test code calling the target function, determining the input parameters of the custom function according to the configuration for the target function behavior; Based on the input parameters of the user-defined function, running the user-defined function corresponding to the objective function to determine the output value of the objective function; A function execution result is returned based on the output value of the objective function and the expected output value.

4. The method according to claim 2, characterized in that: In the process of running the test code, in response to the test code calling the target function, running the custom function corresponding to the target function according to the user configuration, and returning the function execution result, includes: During the process of running the test code, in response to the test code calling the target function and the existence of a custom function with a function signature consistent with the target function in the custom function library, the custom function with the function signature consistent with the target function is run according to the user configuration, and the function execution result is returned.

5. The method according to claim 2, characterized in that: In the process of running the test code, in response to the test code calling the target function, running the custom function corresponding to the target function according to the user configuration, and returning the function execution result, includes: In the process of running the test code, in response to the test code calling a target function and the target function including a preset jump instruction, determining a custom function based on the preset jump instruction; Run the custom function corresponding to the preset jump instruction according to the user configuration, and return the function execution result.

6. The method according to claim 1, characterized in that In the process of running the test code, in response to the test code calling the target function, controlling the behavior of the target function according to the user configuration, and returning the function execution result, further comprising: In the process of running the test code, in response to the test code calling the target function, the input parameters of the target function are modified, the output value of the target function is modified, and any one or more operations in the target function are skipped according to the user configuration, and the function execution result is returned.

7. The method according to claim 1, characterized in that The method further comprises: Start the test case process; In response to the test case process being started, loading a custom function library, wherein the custom function library includes one or more custom functions; In response to completion of loading the custom function library, loading the received user configuration; The running of the test code associated with the test case in the test environment includes: In response to the completion of loading the user configuration, the test code associated with the test case is run in the test environment.

8. The method according to claim 1, characterized in that The user configuration includes any one or more forms of a user configuration file, configuration information stored in a database, and a visual interface configuration.

9. The method according to claim 1, characterized in that: The target function includes the interface function of the system and the interface function of the test code.

10. A unit testing device, characterized in that: The device comprises: A determination module, used to determine a user configuration, wherein the user configuration is used to determine a test case and a test environment; A running module, used for running the test code associated with the test case in the test environment; The control module is used to call the target function in response to the test code during the running of the test code, control the behavior of the target function according to the user configuration, and return the function execution result.

11. A unit testing device, characterized in that: include: processor; a memory for storing processor-executable instructions; The processor is configured to implement the method described in any one of claims 1 to 9 when executing the instructions stored in the memory.

12. A non-volatile computer-readable storage medium having computer program instructions stored thereon, characterized in that: When the computer program instructions are executed by a processor, the method according to any one of claims 1 to 9 is implemented.