Unit test simulation method, device, equipment and computer readable storage medium
By building a dependency topology diagram and a preset simulation pool, the logic of the unit test framework is simplified, the problem of repeated creation and injection operations is solved, and the performance of the unit test framework is improved.
Patent Information
- Application Number
- CN202010597542.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-06-28
- Publication Date
- 2024-10-01
- Estimated Expiration
- 2040-06-28
AI Technical Summary
During the execution of the existing unit testing framework, different test cases for the same dependent object need to repeatedly create mock objects and inject statements, resulting in code redundancy and low performance.
By constructing the dependency topology diagram and preset simulation pool of each member object in the test case, the target simulation object of the object to be simulated is determined, avoiding repeated creation and injection operations, and simplifying the test logic.
It improves the performance of the single testing framework, reduces the code workload of developers and the performance burden of the framework, and reduces repetitive work.
Smart Images

Figure CN111752841B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of financial technology (Fi ntech) technology, and in particular to a single test simulation method, device, equipment and computer-readable storage medium. Background Art
[0002] In recent years, with the continuous development of Fintech, especially Internet finance, testing technology has been introduced into the daily development of banks and other financial institutions. In the daily development process of banks and other financial institutions, developers often need to develop different functional modules for different business functions. In order to ensure that the developed functional modules are available, developers need to test the programs they write. Therefore, how to conduct program usability testing has always been a technical problem that banks and other financial institutions need to solve.
[0003] In the prior art, a unit testing framework (single test framework), such as Mockito and PowerMock, is generally used to perform usability testing on developed functional modules. During the testing process, a test case set written by a developer is received and the test behavior of the test case set is executed to obtain an expected value. In the process of executing the test behavior of the test case set, it is necessary to read the simulation annotation part of each test case in the test case set to create a corresponding simulation object, and according to the injection declaration, the simulation object is injected into the object under test to replace the dependent object of the object under test. Finally, the dependent object is simulated to return the expected value, so that the process of the object under test calling the dependent object is replaced by calling the simulation object, and the simulation object unconditionally returns the expected value without executing the real code inside the dependent object, thereby completing the verification of each branch of the object under test.
[0004] In this process, for the same dependent object, different test cases in the test case set need to repeatedly create the simulation object of the dependent object in the simulation stage before execution, and each simulation object depends on the injection declaration part before the simulation object can be injected into the object under test. A large number of repeated creations and declarations will cause code redundancy. It can be seen that the execution logic of the existing unit test framework is relatively complex and repetitive, resulting in low performance. Summary of the invention
[0005] The main purpose of the present invention is to propose a single test simulation method, device, equipment and computer-readable storage medium, aiming to simplify the test logic to improve the performance of the test framework.
[0006] To achieve the above object, the present invention provides a single test simulation method, which comprises the following steps:
[0007] If a startup instruction is detected, a test case set corresponding to the startup instruction is loaded, and a dependency topology diagram of each member object in the test case set is established;
[0008] If a single test instruction is detected, determining a target test case corresponding to the single test instruction, and determining an object to be simulated in the target test case;
[0009] Determine a target simulation object corresponding to the object to be simulated based on the dependency topology diagram and a preset simulation pool, wherein the preset simulation pool is used to store simulation objects;
[0010] The object to be simulated is replaced with the target simulation object, and the test behavior of the test case is executed based on the target simulation object.
[0011] Preferably, the step of determining the target simulation object corresponding to the object to be simulated based on the dependency topology diagram and the preset simulation pool includes:
[0012] Determine whether there is a simulation object corresponding to the object to be simulated in a preset simulation pool;
[0013] If so, determining the injection object of the simulation object based on the dependency topology diagram;
[0014] The injection object is injected into the simulation object to obtain a target simulation object corresponding to the object to be simulated.
[0015] Preferably, after the step of determining whether there is a simulation object corresponding to the object to be simulated in the preset simulation pool, the single test simulation method further includes:
[0016] If it does not exist, the simulation object is created in the preset simulation pool, and the step of determining the injection object of the simulation object based on the dependency topology diagram is performed.
[0017] Preferably, the step of determining the injection object of the simulation object based on the dependency topology graph includes:
[0018] Acquire the simulation object from the preset simulation pool, and reset the simulation object in the preset simulation pool;
[0019] Determine a target member attribute of the object to be simulated, and based on the target member attribute, determine a target class to which the object to be simulated belongs;
[0020] Determine the target reverse zipper corresponding to the target class in the dependency topology relationship graph, wherein the dependency topology relationship graph is composed of classes and reverse zippers corresponding to the classes;
[0021] The object in the target reverse zipper is read, and the object in the target reverse zipper is determined to be an injection object of the simulation object.
[0022] Preferably, before the step of determining whether there is a simulation object corresponding to the object to be simulated in the preset simulation pool, the single test simulation method further includes:
[0023] Determine whether there is a simulation scene corresponding to the object to be simulated in the preset simulation pool;
[0024] If not, the step of determining whether there is a simulation object corresponding to the object to be simulated in the preset simulation pool is executed.
[0025] Preferably, after the step of determining whether there is a simulation scene corresponding to the object to be simulated in the preset simulation pool, the single test simulation method further includes:
[0026] If so, obtaining the simulation combination object corresponding to the simulation scene from the preset simulation pool, and resetting the simulation combination object in the preset simulation pool;
[0027] The simulation combination object is determined to be a target simulation object, and the steps of replacing the object to be simulated with the target simulation object and executing the test behavior of the test case based on the target simulation object are performed.
[0028] Preferably, if a startup instruction is detected, the step of loading a test case set corresponding to the startup instruction and establishing a dependency topology diagram of each member object in the test case set comprises:
[0029] If a startup instruction is detected, a test case set corresponding to the startup instruction is loaded, and member attributes of each member object in the test case set and dependency relationships of each member object are determined by scanning;
[0030] Based on the member attributes and the dependency relationships, a dependency topology diagram of each member object in the test case is established.
[0031] In addition, to achieve the above-mentioned purpose, the present invention further provides a single-test simulation device, the single-test simulation device comprising:
[0032] A construction module, for, if a startup instruction is detected, loading a test case set corresponding to the startup instruction, and establishing a dependency topology diagram of each member object in the test case set;
[0033] A determination module, configured to determine a target test case corresponding to the single test instruction and determine an object to be simulated in the target test case if a single test instruction is detected;
[0034] The determination module is further used to determine the target simulation object corresponding to the object to be simulated based on the dependency topology relationship diagram and a preset simulation pool, wherein the preset simulation pool is used to store simulation objects;
[0035] An execution module is used to replace the object to be simulated with the target simulation object, and execute the test behavior of the test case based on the target simulation object.
[0036] Preferably, the determining module is further used for:
[0037] Determine whether there is a simulation object corresponding to the object to be simulated in a preset simulation pool;
[0038] If so, determining the injection object of the simulation object based on the dependency topology diagram;
[0039] The injection object is injected into the simulation object to obtain a target simulation object corresponding to the object to be simulated.
[0040] Preferably, the determining module is further used for:
[0041] If it does not exist, the simulation object is created in the preset simulation pool, and the step of determining the injection object of the simulation object based on the dependency topology diagram is performed.
[0042] Preferably, the determining module is further used for:
[0043] Acquire the simulation object from the preset simulation pool, and reset the simulation object in the preset simulation pool;
[0044] Determine a target member attribute of the object to be simulated, and based on the target member attribute, determine a target class to which the object to be simulated belongs;
[0045] Determine the target reverse zipper corresponding to the target class in the dependency topology relationship graph, wherein the dependency topology relationship graph is composed of classes and reverse zippers corresponding to the classes;
[0046] The object in the target reverse zipper is read, and the object in the target reverse zipper is determined to be an injection object of the simulation object.
[0047] Preferably, the determining module is further used for:
[0048] Determine whether there is a simulation scene corresponding to the object to be simulated in the preset simulation pool;
[0049] If not, the step of determining whether there is a simulation object corresponding to the object to be simulated in the preset simulation pool is executed.
[0050] Preferably, the determining module is further used for:
[0051] If so, obtaining the simulation combination object corresponding to the simulation scene from the preset simulation pool, and resetting the simulation combination object in the preset simulation pool;
[0052] The execution module is also used for:
[0053] The simulation combination object is determined to be a target simulation object, and the steps of replacing the object to be simulated with the target simulation object and executing the test behavior of the test case based on the target simulation object are performed.
[0054] Preferably, the building block is also used for:
[0055] If a startup instruction is detected, a test case set corresponding to the startup instruction is loaded, and member attributes of each member object in the test case set and dependency relationships of each member object are determined by scanning;
[0056] Based on the member attributes and the dependency relationships, a dependency topology diagram of each member object in the test case is established.
[0057] In addition, to achieve the above-mentioned purpose, the present invention also provides a single test simulation device, which includes: a memory, a processor, and a single test simulation program stored in the memory and executable on the processor, and the single test simulation program implements the steps of the single test simulation method described above when executed by the processor.
[0058] In addition, to achieve the above-mentioned purpose, the present invention further provides a computer-readable storage medium, on which a unit test simulation program is stored, and when the unit test simulation program is executed by a processor, the steps of the unit test simulation method described above are implemented.
[0059] The single test simulation method proposed in the present invention, if a start instruction is detected, then the test case set corresponding to the start instruction is loaded, and a dependency topology relationship diagram of each member object in the test case set is established; if a single test instruction is detected, then the target test case corresponding to the single test instruction is determined, and the object to be simulated in the target test case is determined; based on the dependency topology relationship diagram and a preset simulation pool, the target simulation object corresponding to the object to be simulated is determined, wherein the preset simulation pool is used to store simulation objects; the object to be simulated is replaced with the target simulation object, and based on the target simulation object, the test behavior of the test case is executed. When the single test instruction is detected, the present invention determines the target simulation object corresponding to the object to be simulated in the member object by constructing a dependency topology relationship diagram of each member object in the test case and a preset simulation pool, and no longer requires the simulation annotation part and the injection declaration part in the test case, thereby simplifying the code logic and improving the test performance of the single test framework. BRIEF DESCRIPTION OF THE DRAWINGS
[0060] Figure 1It is a schematic diagram of the device structure of the hardware operating environment involved in the embodiment of the present invention;
[0061] Figure 2 It is a schematic diagram of the flow chart of the first embodiment of the single test simulation method of the present invention;
[0062] Figure 3 A schematic diagram of a test case in the first embodiment of the single test simulation method of the present invention;
[0063] Figure 4 This is a schematic diagram of code text corresponding to the test case of the existing single test framework in the first embodiment of the single test simulation method of the present invention;
[0064] Figure 5 This is a schematic diagram of code text corresponding to the test case of the single test framework of the present invention in the first embodiment of the single test simulation method of the present invention;
[0065] Figure 6 It is a schematic diagram of a dependency topology relationship diagram in the first embodiment of the single test simulation method of the present invention.
[0066] The realization of the purpose, functional features and advantages of the present invention will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0067] It should be understood that the specific embodiments described herein are only used to explain the present invention, and are not used to limit the present invention.
[0068] like Figure 1 As shown, Figure 1 It is a schematic diagram of the device structure of the hardware operating environment involved in the embodiment of the present invention.
[0069] The device in the embodiment of the present invention may be a mobile terminal or a server device.
[0070] like Figure 1 As shown, the device may include: a processor 1001, such as a CPU, a network interface 1004, a user interface 1003, a memory 1005, and a communication bus 1002. Among them, the communication bus 1002 is used to realize the connection and communication between these components. The user interface 1003 may include a display screen (Dis l ay), an input unit such as a keyboard (Keyboard), and the optional user interface 1003 may also include a standard wired interface and a wireless interface. The network interface 1004 may optionally include a standard wired interface and a wireless interface (such as a WI-FI interface). The memory 1005 may be a high-speed RAM memory, or a stable memory (non-vol at ile memory), such as a disk memory. The memory 1005 may also be a storage device independent of the aforementioned processor 1001.
[0071] Those skilled in the art will understand that Figure 1 The device structure shown in the figure does not constitute a limitation of the device, and may include more or less components than shown in the figure, or combine certain components, or arrange the components differently.
[0072] like Figure 1 As shown, the memory 1005 as a computer storage medium may include an operating system, a network communication module, a user interface module, and a unit test simulation program.
[0073] Among them, the operating system is a program for managing and controlling single-test simulation devices and software resources, supporting the operation of network communication modules, user interface modules, single-test simulation programs and other programs or software; the network communication module is used to manage and control the network interface 1002; the user interface module is used to manage and control the user interface 1003.
[0074] exist Figure 1 In the single test simulation device shown, the single test simulation device calls the single test simulation program stored in the memory 1005 through the processor 1001, and executes the operations in each embodiment of the following single test simulation method.
[0075] Based on the above hardware structure, an embodiment of a single test simulation method of the present invention is proposed.
[0076] Reference Figure 2 , Figure 2 The flowchart of the first embodiment of the single test simulation method of the present invention is as follows:
[0077] Step S10, if a startup instruction is detected, then the test case set corresponding to the startup instruction is loaded, and a dependency topology diagram of each member object in the test case set is established;
[0078] Step S20, if a single test instruction is detected, determining a target test case corresponding to the single test instruction, and determining an object to be simulated in the target test case;
[0079] Step S30, determining a target simulation object corresponding to the object to be simulated based on the dependency topology diagram and a preset simulation pool, wherein the preset simulation pool is used to store simulation objects;
[0080] Step S40: replacing the object to be simulated with the target simulation object, and executing the test behavior of the test case based on the target simulation object.
[0081] The single test simulation method of this embodiment is applied to the single test simulation device of financial institutions such as financial institutions or banking systems. The single test simulation device can be a terminal, a robot or a PC device. In this embodiment, the single test simulation device is configured with a single test framework. For the convenience of description, the single test simulation device is referred to as the single test framework. When the simulation device detects a single test instruction, the test case corresponding to the single test instruction is loaded through the JAVA framework container in the single test framework, and then the test case is executed by the single test framework, and the availability of the product function is judged by the obtained result value, wherein the test case is the test code written by the developer to test the product function. It can be understood that in the process of program development, in order to realize the product requirements, the corresponding program code needs to be written. The code has passed the compilation, which only means that its syntax is correct, but whether the function can be realized cannot be guaranteed. Therefore, after some functional codes of the product are completed, in order to verify whether it meets the requirements of program operation, it is necessary to write test code to simulate the process of program operation, so as to verify whether the functional code meets expectations.
[0082] In the existing single test framework, such as Mockito, when testing the object under test, it is necessary to perform simulation test operations in sequence according to the code in the test case, such as Figure 3 In the test case shown, the object under test depends on object A, object B and object C, and object A depends on object X, and object B depends on object Y. Object X and object Y respectively make remote calls or database queries, that is, when object X and object Y are called, the returned values are uncontrollable. Therefore, in the process of testing the object under test, object X and object Y need to be simulated.
[0083] The code text corresponding to the test case is as follows Figure 4 As shown in the figure, published ServiceTest represents a single test class, that is, a test case. When the existing single test framework is executed, it first reads the simulation annotation part, and then simulates the dependent objects of the object under test according to the simulation annotation part, such as simulating object X as x and simulating object Y as y. Among them, the @Mock annotation represents that the mocked object method is an empty method and does not retain the original code behavior, while the @Spy annotation represents that the mocked object retains the behavior of the original object by default; then, the injection declaration part is read, where @I njectMocks represents that the declared object is an object that needs to be injected. If this annotation is not present, the mock simulation object cannot be injected; then, the annotation is read, where the @Autowire annotation indicates that the toTestObject object under test is toTestObj; the method annotated with @Before will be executed before each test case is executed. Figure 4In the example, it means that before executing the test case, you need to execute the MockitoAnnotations.initMocks(this) method so that the mock objects x and y can be injected into objects A and B, and finally read the test part. The @Test annotation indicates that during the execution of the object under test, when x.remoteRpc() or y.queryDB() needs to be called, 10 and 20 are returned unconditionally respectively, without executing the real code inside the two methods.
[0084] For developers, when using the existing single test framework, they need to be familiar with various annotations when writing test cases, and need to use multiple annotations to declare the test behavior of the test cases, that is, developers need to enter more code; for the single test framework, there are too many annotations, especially when encountering a test case set, that is, multiple test cases, various annotations can be seen everywhere, which increases the performance burden of the single test framework, and the execution logic is not smart enough. Each test case requires a mock object, and there are a lot of repetitive behaviors, which reduces the performance of the single test framework.
[0085] The single test simulation framework of this embodiment, in the process of executing the test case (the code text corresponding to the test case is as follows Figure 5 As shown in the figure, you only need to read the @SimpleMock declaration part to get the TrimFacilitiesMock simulation object of the TrimFacilities dependent object in the simulation pool, and the TrimFacilities object that callService (custom service) depends on is automatically replaced by TrimFacilitiesMock. In this process, you don't need to care about @Spy, @Mock, @InjectMocks and other annotations or declarations, which means that developers no longer need to enter the code of the simulation annotation part and the injection declaration part in the process of writing test cases, which greatly reduces the code workload of developers; and after the single test framework loads the test case, it does not need to perform the simulation annotation or injection declaration operation in the process of executing the test behavior of the test case, which simplifies the execution logic and reduces the performance burden of the single test framework. In addition, due to the existence of the simulation pool, each test case does not need to repeatedly create a mock object or repeatedly perform the injection operation, which reduces a lot of repetitive work and improves the performance of the single test simulation framework.
[0086] The unit testing framework of this embodiment, after loading the test case set, establishes a dependency topology diagram for each member object in the test case set. Thus, when unit testing the target test case, the target simulation object corresponding to the object to be simulated of the target test case is determined from the preset simulation pool, and then the object to be simulated is replaced with the target simulation object, and the test value corresponding to the target simulation object is obtained. There is no need to perform simulation operations or injection operations, which simplifies the execution logic and improves the performance of the unit testing framework.
[0087] The following is a detailed description of each step:
[0088] Step S10, if a startup instruction is detected, then the test case set corresponding to the startup instruction is loaded, and a dependency topology diagram of each member object in the test case set is established;
[0089] In this embodiment, if a startup instruction is detected, the test case set corresponding to the startup instruction is first loaded through the JAVA framework container in the single test framework, and then the member objects involved in the test case set are scanned and determined, and a dependency topology relationship graph of each member object is established, wherein the dependency topology relationship graph refers to the chain dependency relationship between member objects in the test, which is different from the existing simple dependency relationship between two objects. The dependency topology relationship graph of this embodiment is composed of multiple dependency chains.
[0090] Specifically, in one embodiment, step S10 includes:
[0091] Step a1: if a startup instruction is detected, then load the test case set corresponding to the startup instruction, and scan and determine the member attributes of each member object in the test case set, as well as the dependency relationship of each member object;
[0092] In one embodiment, if a startup instruction is detected, the test case set corresponding to the startup instruction is first loaded through the JAVA framework container in the single test framework, and the member attributes of each member object in the test case set and the dependency relationship of each member object are scanned to determine, wherein the member attributes are information that describes the characteristics of the member object.
[0093] Step a2: establishing a dependency topology diagram of each member object in the test case based on the member attributes and the dependency relationship.
[0094] Then, according to the member attributes and dependency relationships of each member object in the test case, traverse each member object in turn and recursively. Specifically, adopt the matrix diagram + linked list method, use each member attribute in the test case as the key of the matrix Hash, determine the dependency relationship between each member object and the current member object in turn, and add the member objects with dependent relationships into the linked list corresponding to the class to which the current member object belongs in turn, so as to obtain the reverse zipper of the current member object, and finally set each reverse zipper to the value-list (dependency topology diagram, where value is the class corresponding to the key, list is a linked list for storing reverse zippers) corresponding to the key, so as to obtain the dependency topology diagram, as shown in the figure. Figure 6 As shown in the figure, K is the member attribute of the member object in the test case, and the linked list corresponding to K is the reverse zipper. In the reverse zipper, C is the member object that has a dependency relationship with the current member object. In the subsequent simulation process, if the K1 object needs to be simulated, the position of the class to which K1 belongs is found in the dependency topology diagram according to the member attribute of the K1 object, and then the object in the reverse zipper corresponding to K1 is injected into the simulation object of K1.
[0095] Step S20: If a single test instruction is detected, a target test case corresponding to the single test instruction is determined, and an object to be simulated in the target test case is determined.
[0096] In this embodiment, if a unit test instruction is detected, it is first determined which target test case in the case test set is to be unit tested, and then the object to be simulated in the target test case is determined, that is, it is determined which objects need to be mocked. In one embodiment, the member objects that need to be mocked are highlighted in the target test case in the form of an identifier, that is, the unit test framework only needs to identify the identifiers of each member object in the test case to determine which objects need to be mocked.
[0097] Further, in one embodiment, the step of determining the object to be simulated in the target test case includes:
[0098] Determine the object to be simulated in the member object based on the call information of the object under test in the target test case;
[0099] In one embodiment, the object to be simulated in the member object is determined through the calling information of the object under test in the target test case, wherein the calling information includes interface information, that is, the calling object, that is, whether the dependent object needs to be mocked is determined through the interface information called by the object under test. It can be understood that when the dependent object is an external system or database, the result value returned is uncontrollable, so a mock is required. Therefore, it can be determined based on the interface information whether the calling object is an external system or database, so as to determine the object to be simulated in the member object. It should be noted that the object to be simulated can be one or more.
[0100] Step S30, determining a target simulation object corresponding to the object to be simulated based on the dependency topology diagram and a preset simulation pool, wherein the preset simulation pool is used to store simulation objects.
[0101] In this embodiment, the object to be simulated is determined based on the dependency topology diagram and the preset simulation pool, wherein the preset simulation pool is an area pre-divided by the single test framework for storing simulation objects, and all simulation objects in the simulation pool are created and unique.
[0102] Further, in one embodiment, step S30 includes:
[0103] Step b1, determining whether there is a simulation object corresponding to the object to be simulated in a preset simulation pool;
[0104] In one embodiment, it is determined whether there is a simulation object corresponding to the determined object to be simulated in the preset simulation pool. Specifically, it can be searched in the preset simulation pool whether there is a simulation object whose member attributes are the same as those of the object to be simulated.
[0105] Step b2: if it exists, determining the injection object of the simulation object based on the dependency topology diagram;
[0106] In one embodiment, if it is determined that there is a simulation object corresponding to the object to be simulated in the simulation pool, the injection object of the simulation object is further determined based on the dependency topology diagram, that is, which member objects need to be injected into the simulation object, so that the simulation object has the calling logic of the object to be simulated after replacing the object to be simulated, so as to accurately simulate the object to be simulated and return the expected value when processing the calling request of the object under test.
[0107] In another embodiment, if it does not exist, the simulation object is created in the preset simulation pool, and the step of determining the injection object of the simulation object based on the dependency topology graph is performed.
[0108] That is, if it is determined that there is no simulation object corresponding to the object to be simulated in the simulation pool, a new simulation object is created in the preset simulation pool, and the specific creation process is similar to the creation process of the existing simulation object, which will not be described in detail here. Then, the step of determining the injection object of the simulation object based on the dependency topology diagram is performed.
[0109] Further, in one embodiment, the step of determining the injection object of the simulation object based on the dependency topology graph includes:
[0110] Step b21, obtaining the simulation object from the preset simulation pool, and resetting the simulation object in the preset simulation pool;
[0111] In one embodiment, the unit test framework first obtains the simulation object from the preset simulation pool, and then resets the simulation object in the preset simulation pool. This is to facilitate other test cases that also need the current simulation object to execute their tests without interfering with each other. For example, the current test case contains two cases. Both the first case and the second case need to simulate the A object to obtain the simulation object A', but the simulation object of the first case needs to be injected with the B object, while the second case needs to inject the B object and the C object. Therefore, after the first case obtains the simulation object A', the simulation object A' is reset in the preset simulation pool so that the second case obtains the simulation object A', and then the B object and the C object are injected into the simulation object A'.
[0112] Step b22, determining the target member attribute of the object to be simulated, and based on the target member attribute, determining the target class to which the object to be simulated belongs;
[0113] In one embodiment, after obtaining the simulation object, the target member attribute of the object to be simulated is determined, and it is determined to which class the target member attribute belongs. It can be understood that a class contains multiple member objects and therefore also contains multiple member attributes, that is, a class corresponds to multiple member attributes, and one member attribute corresponds to one class.
[0114] Step b23, determining the target reverse zipper corresponding to the target class in the dependency topology relationship graph, wherein the dependency topology relationship graph is composed of classes and reverse zippers corresponding to the classes;
[0115] Then, in the dependency topology diagram, find the location of the target class to which the target member attribute belongs. The reverse zipper corresponding to the location of the target class is the target reverse zipper. It can be understood that the member object in the target reverse zipper is the injection object to be injected into the simulation object.
[0116] Step b24, reading the object in the target reverse zipper, and determining that the object in the target reverse zipper is the injection object of the simulation object.
[0117] Finally, the member objects in the target reverse zipper are read, and the objects in the target reverse zipper are determined to be the injected objects of the simulated objects.
[0118] Step b3, injecting the injection object into the simulation object to obtain a target simulation object corresponding to the object to be simulated.
[0119] In one embodiment, after determining the simulation object and the injection object of the simulation object, the injection object is injected into the simulation object to obtain a target simulation object corresponding to the object to be simulated. When the object to be simulated subsequently calls the object to be simulated, it is converted into calling the target simulation object. For example, the object A under test calls the object B, and the object B calls the object C. When simulating the object B to obtain the simulation object B', it is converted into the object under test calling the B' object. This is because the C object has been injected into the B' object, and the B' object is equivalent to the B object + the C object, which simplifies the calling logic so that the unit test framework does not need to perform other calling operations during the test process.
[0120] Step S40: replacing the object to be simulated with the target simulation object, and executing the test behavior of the test case based on the target simulation object.
[0121] In this embodiment, after the target simulation object is determined, the object to be simulated in the test case is replaced with the target simulation object, and then the test behavior of the test case is executed to obtain the test result of the test case, wherein, since the uncontrollable dependent object has been replaced with the target simulation object, the value returned by the target simulation object is an expected value, thereby eliminating the interference of the uncontrollable dependent object on this test. Therefore, whether the functional code meets expectations can be verified according to the test results. The specific test behavior is similar to the execution logic of the existing single test framework and will not be described in detail here.
[0122] It should be noted that after the test case uses the target simulation object, it is deleted. It is understandable that if the target simulation object is not reset or recycled, it may pollute other test cases. For example, in the aforementioned object injection link, the current test case will not be able to inject the injection object due to the injection operation of the previous test case, or the correct target simulation object cannot be obtained after injection. Therefore, each time the single test framework obtains the simulation object from the simulation pool, it automatically resets the object, so that a clean simulation object is retained in the simulation pool and deleted after use. There is no need for manual intervention in object recycling, and other test cases are not polluted.
[0123] If the present embodiment detects a startup instruction, the test case set corresponding to the startup instruction is loaded, and a dependency topology diagram of each member object in the test case set is established; if a single test instruction is detected, the target test case corresponding to the single test instruction is determined, and the object to be simulated in the target test case is determined; based on the dependency topology diagram and the preset simulation pool, the target simulation object corresponding to the object to be simulated is determined, wherein the preset simulation pool is used to store simulation objects; the object to be simulated is replaced with the target simulation object, and based on the target simulation object, the test behavior of the test case is executed. When the present invention detects a single test instruction, the target simulation object corresponding to the object to be simulated in the member object is determined by constructing a dependency topology diagram of each member object in the test case and a preset simulation pool, and the simulation annotation part and the injection declaration part in the test case are no longer required, thereby simplifying the code logic and improving the test performance of the single test framework.
[0124] Furthermore, based on the first embodiment of the single test simulation method of the present invention, a second embodiment of the single test simulation method of the present invention is proposed.
[0125] The second embodiment of the single test simulation method is different from the first embodiment of the single test simulation method in that, before the step of determining whether there is a simulation object corresponding to the object to be simulated in the preset simulation pool, the single test simulation method further includes:
[0126] Step c, determining whether there is a simulation scene corresponding to the object to be simulated in the preset simulation pool;
[0127] Step d: If not, execute the step of determining whether there is a simulation object corresponding to the object to be simulated in the preset simulation pool.
[0128] In this embodiment, the object under test calls many dependent objects. For example, when the object under test needs to access multiple downstream external systems or the called dependent objects return various result values, multiple objects or multiple result values need to be simulated. In this embodiment, multiple simulation behaviors are packaged into a mock scene (simulation scene) so that different test cases can use the same simulation scene to achieve test reusability.
[0129] The following is a detailed description of each step:
[0130] Step c: determining whether there is a simulation scene corresponding to the object to be simulated in the preset simulation pool.
[0131] In this embodiment, in the process of determining the target simulation object, it is first determined whether there is a simulation scene corresponding to the object to be simulated in the preset simulation pool, wherein the simulation scene can also be called a simulation component, which is essentially a special class. In the unit test framework of this embodiment, only @MockScene (simulation scene) or @MockService (simulation service) needs to be declared, and then the unit test framework can automatically identify and load it, which is usually used for combined simulation of multiple simulation objects.
[0132] Therefore, in one embodiment, the single test framework first determines the number of objects to be simulated. If the number of objects to be simulated exceeds a preset number, such as more than 1, it is necessary to determine whether there is a corresponding simulation scenario for the object to be simulated in the preset simulation pool. For example, in the current test case, the object under test accessed three uncontrollable external systems, namely object A, object B and object C. The access behaviors of objects A, object B and object C can be packaged into a simulation scenario. If other test cases also meet the simulation scenario, there is no need to replace the objects to be simulated one by one, but only need to declare the use of the simulation scenario for automatic replacement.
[0133] It should be noted that the simulation scenario can also target multiple results of the same calling object. For example, when the object under test accesses the downstream A module, the A module may return 10 possible results. At this time, these results can also be encapsulated into a mock component (simulation component).
[0134] Therefore, after determining the object to be simulated, first determine whether there is a corresponding simulation scene in the preset simulation pool.
[0135] Step d: If not, execute the step of determining whether there is a simulation object corresponding to the object to be simulated in the preset simulation pool.
[0136] In this embodiment, if the simulation scene corresponding to the object to be simulated does not exist in the preset simulation pool, a step of determining whether a simulation object corresponding to the object to be simulated exists in the preset simulation pool is performed.
[0137] Further, if it exists, obtaining the simulation combination object corresponding to the simulation scene from the preset simulation pool, and resetting the simulation combination object in the preset simulation pool;
[0138] In one embodiment, if a simulation scene corresponding to the object to be simulated exists in the preset simulation pool, the simulation combination object corresponding to the simulation scene is obtained from the preset simulation pool. It should be noted that since the simulation scene is fixed, the corresponding simulation combination object is also fixed, that is, after the simulation combination object is determined, there is no need to inject the injection object into the simulation combination object.
[0139] The simulation combination object is determined to be a target simulation object, and the steps of replacing the object to be simulated with the target simulation object and executing the test behavior of the test case based on the target simulation object are performed.
[0140] Afterwards, the simulation combination object is determined as the target simulation object, and the steps of replacing the object to be simulated with the target simulation object and executing the test behavior of the test case based on the target simulation object are performed, which will not be described in detail here.
[0141] The simulation scenario or simulation component of this embodiment essentially aggregates a group of common simulation objects to form a reusable atomic code block, so that the same simulation scenario can be reused by multiple test cases, and once the simulation scenario is modified, all test cases will take effect accordingly, making maintenance easier and more efficient.
[0142] The present invention also provides a single test simulation device. The single test simulation device of the present invention comprises:
[0143] A construction module, for, if a startup instruction is detected, loading a test case set corresponding to the startup instruction, and establishing a dependency topology diagram of each member object in the test case set;
[0144] A determination module, configured to determine a target test case corresponding to the single test instruction and determine an object to be simulated in the target test case if a single test instruction is detected;
[0145] The determination module is further used to determine the target simulation object corresponding to the object to be simulated based on the dependency topology relationship diagram and a preset simulation pool, wherein the preset simulation pool is used to store simulation objects;
[0146] An execution module is used to replace the object to be simulated with the target simulation object, and execute the test behavior of the test case based on the target simulation object.
[0147] Preferably, the determining module is further used for:
[0148] Determine whether there is a simulation object corresponding to the object to be simulated in a preset simulation pool;
[0149] If so, determining the injection object of the simulation object based on the dependency topology diagram;
[0150] The injection object is injected into the simulation object to obtain a target simulation object corresponding to the object to be simulated.
[0151] Preferably, the determining module is further used for:
[0152] If it does not exist, the simulation object is created in the preset simulation pool, and the step of determining the injection object of the simulation object based on the dependency topology diagram is performed.
[0153] Preferably, the determining module is further used for:
[0154] Acquire the simulation object from the preset simulation pool, and reset the simulation object in the preset simulation pool;
[0155] Determine a target member attribute of the object to be simulated, and based on the target member attribute, determine a target class to which the object to be simulated belongs;
[0156] Determine the target reverse zipper corresponding to the target class in the dependency topology relationship graph, wherein the dependency topology relationship graph is composed of classes and reverse zippers corresponding to the classes;
[0157] The object in the target reverse zipper is read, and the object in the target reverse zipper is determined to be an injection object of the simulation object.
[0158] Preferably, the determining module is further used for:
[0159] Determine whether there is a simulation scene corresponding to the object to be simulated in the preset simulation pool;
[0160] If not, the step of determining whether there is a simulation object corresponding to the object to be simulated in the preset simulation pool is executed.
[0161] Preferably, the determining module is further used for:
[0162] If so, obtaining the simulation combination object corresponding to the simulation scene from the preset simulation pool, and resetting the simulation combination object in the preset simulation pool;
[0163] The execution module is also used for:
[0164] The simulation combination object is determined to be a target simulation object, and the steps of replacing the object to be simulated with the target simulation object and executing the test behavior of the test case based on the target simulation object are performed.
[0165] Preferably, the building block is also used for:
[0166] If a startup instruction is detected, a test case set corresponding to the startup instruction is loaded, and member attributes of each member object in the test case set and dependency relationships of each member object are determined by scanning;
[0167] Based on the member attributes and the dependency relationships, a dependency topology diagram of each member object in the test case is established.
[0168] The present invention also provides a computer-readable storage medium.
[0169] The computer-readable storage medium of the present invention stores a unit test simulation program, and when the unit test simulation program is executed by a processor, the steps of the unit test simulation method described above are implemented.
[0170] Among them, the method implemented when the unit test simulation program running on the processor is executed can refer to the various embodiments of the unit test simulation method of the present invention, and will not be repeated here.
[0171] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or system including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or system. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or system including the element.
[0172] The serial numbers of the above embodiments of the present invention are only for description and do not represent the advantages or disadvantages of the embodiments.
[0173] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) as described above, and includes a number of instructions for a terminal device (which can be a mobile phone, computer, server, air conditioner, or network device, etc.) to execute the methods described in each embodiment of the present invention.
[0174] The above are only preferred embodiments of the present invention, and are not intended to limit the patent scope of the present invention. Any equivalent structure or equivalent process transformation made using the contents of the present invention specification and drawings, or directly or indirectly applied in other related technical fields, are also included in the patent protection scope of the present invention.
Claims
1. A single test simulation method, characterized in that: The single test simulation method comprises the following steps: If a startup instruction is detected, a test case set corresponding to the startup instruction is loaded, and a dependency topology diagram of each member object in the test case set is established; the test case is a test code written by a developer to test product functions; If a single test instruction is detected, determining a target test case corresponding to the single test instruction, and determining an object to be simulated in the target test case; Determine a target simulation object corresponding to the object to be simulated based on the dependency topology diagram and a preset simulation pool, wherein the preset simulation pool is used to store simulation objects; Replacing the object to be simulated with the target simulation object, and executing the test behavior of the test case based on the target simulation object; The step of determining the target simulation object corresponding to the object to be simulated based on the dependency topology diagram and the preset simulation pool comprises: Determine whether there is a simulation object corresponding to the object to be simulated in a preset simulation pool; If so, determining the injection object of the simulation object based on the dependency topology diagram; Injecting the injection object into the simulation object to obtain a target simulation object corresponding to the object to be simulated; The step of determining the injection object of the simulation object based on the dependency topology diagram comprises: Acquire the simulation object from the preset simulation pool, and reset the simulation object in the preset simulation pool; Determine a target member attribute of the object to be simulated, and based on the target member attribute, determine a target class to which the object to be simulated belongs; Determine the target reverse zipper corresponding to the target class in the dependency topology relationship graph, wherein the dependency topology relationship graph is composed of classes and reverse zippers corresponding to the classes; The object in the target reverse zipper is read, and the object in the target reverse zipper is determined to be an injection object of the simulation object.
2. The single test simulation method according to claim 1, characterized in that: After the step of determining whether there is a simulation object corresponding to the object to be simulated in the preset simulation pool, the single test simulation method further includes: If it does not exist, the simulation object is created in the preset simulation pool, and the step of determining the injection object of the simulation object based on the dependency topology diagram is performed.
3. The single test simulation method according to claim 1, characterized in that: Before the step of determining whether there is a simulation object corresponding to the object to be simulated in the preset simulation pool, the single test simulation method further includes: Determine whether there is a simulation scene corresponding to the object to be simulated in the preset simulation pool; If not, the step of determining whether there is a simulation object corresponding to the object to be simulated in the preset simulation pool is executed.
4. The single test simulation method according to claim 3, characterized in that: After the step of determining whether there is a simulation scene corresponding to the object to be simulated in the preset simulation pool, the single test simulation method further includes: If so, obtaining the simulation combination object corresponding to the simulation scene from the preset simulation pool, and resetting the simulation combination object in the preset simulation pool; The simulation combination object is determined to be a target simulation object, and the steps of replacing the object to be simulated with the target simulation object and executing the test behavior of the test case based on the target simulation object are performed.
5. The single test simulation method according to any one of claims 1 to 4, characterized in that: If a startup instruction is detected, the step of loading a test case set corresponding to the startup instruction and establishing a dependency topology diagram of each member object in the test case set includes: If a startup instruction is detected, a test case set corresponding to the startup instruction is loaded, and member attributes of each member object in the test case set and dependency relationships of each member object are determined by scanning; Based on the member attributes and the dependency relationships, a dependency topology diagram of each member object in the test case is established.
6. A single-test simulation device, characterized in that: The single test simulation device comprises: A construction module is used to load the test case set corresponding to the startup instruction if a startup instruction is detected, and to establish a dependency topology diagram of each member object in the test case set; the test case is a test code written by a developer to test product functions; A determination module, configured to determine a target test case corresponding to the single test instruction and determine an object to be simulated in the target test case if a single test instruction is detected; The determination module is further used to determine the target simulation object corresponding to the object to be simulated based on the dependency topology relationship diagram and a preset simulation pool, wherein the preset simulation pool is used to store simulation objects; An execution module, used to replace the object to be simulated with the target simulation object, and execute the test behavior of the test case based on the target simulation object; The determining module is also used for: Determine whether there is a simulation object corresponding to the object to be simulated in a preset simulation pool; If so, determining the injection object of the simulation object based on the dependency topology diagram; Injecting the injection object into the simulation object to obtain a target simulation object corresponding to the object to be simulated; The determining module is also used for: Acquire the simulation object from the preset simulation pool, and reset the simulation object in the preset simulation pool; Determine a target member attribute of the object to be simulated, and based on the target member attribute, determine a target class to which the object to be simulated belongs; Determine the target reverse zipper corresponding to the target class in the dependency topology relationship graph, wherein the dependency topology relationship graph is composed of classes and reverse zippers corresponding to the classes; The object in the target reverse zipper is read, and the object in the target reverse zipper is determined to be an injection object of the simulation object.
7. A single-test simulation device, characterized in that: The unit test simulation device comprises: a memory, a processor, and a unit test simulation program stored in the memory and executable on the processor, wherein the unit test simulation program implements the steps of the unit test simulation method according to any one of claims 1 to 5 when executed by the processor.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a unit test simulation program, and when the unit test simulation program is executed by a processor, the steps of the unit test simulation method according to any one of claims 1 to 5 are implemented.
