Method, apparatus, and computing device for generating a simulation object

CN122653616APending Publication Date: 2026-08-28HUAWEI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202610163828.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Priority Date
2025-02-26
Filing Date
2026-02-04
Publication Date
2026-08-28

AI Technical Summary

Technical Problem

因此,对生产代码中对象的类别进行变更会降低软件产品的质量和性能,甚至会导致软件产品出现安全问题

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122653616A_ABST
    Figure CN122653616A_ABST
Patent Text Reader

Abstract

The application provides a simulation object generation method and device and a computing device. The method comprises the following steps: a compiler obtains user code, the user code comprising a first declaration statement and a first generation statement, the first declaration statement defining the type of a target object, and the first generation statement being used to generate a corresponding simulation object according to the type of the target object; the compiler generates a compiled second declaration statement according to the first declaration statement and the first generation statement, the compiled second declaration statement defining the type of the simulation object generated by the compiler and the calling relationship between the simulation object and the target object; and the compiler generates an executable file of the computing device according to the first declaration statement, the compiled second declaration statement and the first generation statement, the executable file comprising the compiled first declaration statement, the compiled second declaration statement and the compiled first generation statement. The scheme can expand the use range of the mock mechanism and improve the development efficiency of test code.
Need to check novelty before this filing date? Find Prior Art

Description

[0001] This application claims priority to Russian Federation patent application No. 2025104420, filed on February 26, 2025, entitled "Method, Apparatus and Computing Device for Generating Simulated Objects", the entire contents of which are incorporated herein by reference. Technical Field

[0002] This application relates to the field of software testing, and more specifically, to a method, apparatus, and computing device for generating simulated objects. Background Technology

[0003] In software testing, test cases are used to test the functionality of a specific object under test. During the testing process, the object under test may depend on other objects. These dependent objects may need to call external systems or modules during operation, increasing the complexity or accuracy of the test. To reduce testing complexity and improve accuracy, a mocking mechanism can be used. This involves using mock objects to replace the actual dependent objects, thus simulating their behavior.

[0004] In one related technical solution, developers write the type declaration of a mock object and the calling relationship between the mock object and the real object into the test code. For example, in Java, developers might write an inheritance relationship between the real object and the mock object into the test code. During the execution of the test code, by instrumenting the mock object before the real object, the object under test calls the mock object to replace the behavior of the real object during the test. In this solution, both the real object and the mock object need to be of inheritable types to allow the mock object to replace the real object. For types that cannot be inherited, the mock object cannot be used to replace the real object, thus limiting the scope of the mock mechanism.

[0005] In another related technical solution, to expand the scope of the mock mechanism, developers modify the real objects in the production code from non-inheritable categories to inheritable categories. In this solution, since the production code is the software code that has undergone multiple stages such as development, testing, and version control before being deployed and run, its quality directly affects the quality and performance of the software product. Therefore, changing the categories of objects in the production code can reduce the quality and performance of the software product, and may even lead to security issues.

[0006] Therefore, how to expand the scope of mock mechanism and improve the development efficiency of test code without affecting the quality of production code has become an urgent technical problem to be solved. Summary of the Invention

[0007] This application provides a method for generating mock objects. This method can not only expand the scope of use of the mock mechanism without affecting the quality of production code, but also improve the development efficiency of test code.

[0008] Firstly, a method for generating a simulated object is provided. The method includes: a compiler obtaining user code, which includes a first declaration statement and a first generation statement, wherein the first declaration statement defines the type of a target object, which is an object on which the object under test in the code depends; the first generation statement generates a simulated object corresponding to the target object based on the type of the target object; the compiler generates a compiled second declaration statement based on the first declaration statement and the first generation statement, wherein the compiled second declaration statement defines the type of the simulated object generated by the compiler and the calling relationship between the simulated object and the target object, the calling relationship indicating that the simulated object is used instead of the target object during the testing of the object under test; and the compiler generates an executable file of a computing device based on the first declaration statement, the compiled second declaration statement, and the first generation statement, wherein the executable file includes the compiled first declaration statement, the compiled second declaration statement, and the compiled first generation statement.

[0009] In the above technical solution, the compiler generates mock objects based on the first declaration and first generation statements in the user code, and generates declaration statements based on the type of the generated mock objects and the calling relationship between the mock objects and the target objects. This eliminates the need for developers to directly write the generated mock objects, the declaration statements for declaring the types of the mock objects, and the calling relationship between the mock objects and the target objects in the user code (e.g., test code). On the one hand, this simplifies test code and improves its development efficiency. On the other hand, without affecting the quality of the production code, for categories of objects that cannot be inherited in user code written in high-level programming languages, the compiler-generated mock objects can replace real objects, thereby expanding the scope of the mock mechanism and improving the testing efficiency and flexibility of the test code.

[0010] In conjunction with the first aspect, in some implementations of the first aspect, the method further includes: the compiler generating the mock object based on the first declaration statement and the first generation statement.

[0011] In the above technical solution, developers do not need to generate mock objects in user code using a high-level programming language. Instead, the compiler generates the mock object based on the first declaration statement and the first generation statement. In this way, for cases where mock objects cannot be generated in user code because the corresponding real categories cannot be inherited, the compiler can generate mock objects corresponding to these categories, thereby expanding the scope of the mock mechanism and improving the testing efficiency and flexibility of the test code.

[0012] In conjunction with the first aspect, in some implementations of the first aspect, the first declaration statement and the first generation statement are written in a high-level language, and the compiled second declaration statement is written in machine language.

[0013] In the above technical solution, after receiving the first declaration statement and the first generation statement written in a high-level language, the compiler can generate, based on these statements, machine language declaration statements defining the type of the mock object generated by the compiler and the calling relationship between the mock object and the target object. This eliminates the need for developers to write these declaration statements in high-level languages ​​in user code. Furthermore, for object categories that cannot be inherited in user code written in high-level programming languages, mock objects can be used instead of real objects, thereby expanding the scope of the mock mechanism and improving the testing efficiency and flexibility of the test code.

[0014] In conjunction with the first aspect, in some implementations of the first aspect, the method further includes: the compiler transferring the executable file to the computing device, which uses the simulated object to replace the target object during the testing of the object under test according to the calling relationship.

[0015] In conjunction with the first aspect, in some implementations of the first aspect, the compiler obtains an object file that includes the user code.

[0016] In conjunction with the first aspect, in some implementations of the first aspect, the user code includes test code and production code, wherein the test code includes the first generation statement and the production code includes the first declaration statement.

[0017] In conjunction with the first aspect, in some implementations of the first aspect, the calling relationship between the simulated object and the target object includes an inheritance relationship.

[0018] In the above-mentioned technical solutions, the calling relationship between the simulated object and the target object can be represented by inheritance in Java. This allows the computing device to use the simulated object to replace the target object during the testing process based on the inheritance relationship between the simulated object and the target object, thereby simplifying the software testing logic.

[0019] Secondly, a device for generating simulated objects is provided, applied to a compiler. The device includes an acquisition module and a generation module. The acquisition module acquires user code, which includes a first declaration statement and a first generation statement. The first declaration statement defines the type of a target object, which is an object on which the object under test in the code depends. The first generation statement generates a simulated object corresponding to the target object based on its type. The generation module generates a compiled second declaration statement based on the first declaration statement and the first generation statement. The compiled second declaration statement defines the type of the simulated object generated by the compiler and the calling relationship between the simulated object and the target object. This calling relationship instructs that the simulated object be used instead of the target object during testing of the object under test. The generation module also generates an executable file for a computing device based on the first declaration statement, the compiled second declaration statement, and the first generation statement. The executable file includes the compiled first declaration statement, the compiled second declaration statement, and the compiled first generation statement.

[0020] In conjunction with the second aspect, in some implementations of the second aspect, the generation module is also used to generate the simulated object based on the first declaration statement and the first generation statement.

[0021] In conjunction with the second aspect, in some implementations of the second aspect, the first declaration statement and the first generation statement are written in a high-level language, and the compiled second declaration statement is written in machine language.

[0022] In conjunction with the second aspect, in some implementations of the second aspect, the apparatus further includes: a transmission module for transmitting the executable file to the computing device, the computing device for using the simulated object to replace the target object during the testing of the object under test according to the calling relationship.

[0023] In conjunction with the second aspect, in some implementations of the second aspect, the acquisition module is specifically used to: acquire a target file, which includes the user code.

[0024] In conjunction with the second aspect, in some implementations of the second aspect, the user code includes test code and production code, wherein the test code includes the first generation statement and the production code includes the first declaration statement.

[0025] In conjunction with the second aspect, in some implementations of the second aspect, the calling relationship between the simulated object and the target object includes an inheritance relationship.

[0026] It should be understood that for the beneficial effects of the second aspect and its various implementations, please refer to the first aspect and its various implementations; they will not be repeated here.

[0027] Thirdly, a computing device is provided, including a processor and a memory, and optionally, an input / output interface. The processor controls the input / output interface to send and receive information, the memory stores a computer program, and the processor retrieves and runs the computer program from the memory, causing the computing device to execute the method of the first aspect or any possible implementation thereof.

[0028] Optionally, the processor can be a general-purpose processor, which can be implemented in hardware or software. When implemented in hardware, the processor can be a logic circuit, integrated circuit, etc.; when implemented in software, the processor can be a general-purpose processor that reads software code stored in memory. This memory can be integrated into the processor or located outside the processor and exist independently.

[0029] Fourthly, a chip is provided that acquires and executes instructions to implement the methods described in the first aspect and any implementation thereof.

[0030] Optionally, as one implementation, the chip includes a processor and a data interface, through which the processor reads instructions stored in the memory and executes the methods in the first aspect and any implementation thereof.

[0031] Optionally, as one implementation, the chip may further include a memory storing instructions, and the processor is used to execute the instructions stored in the memory. When the instructions are executed, the processor is used to perform the method in the first aspect and any implementation thereof.

[0032] Fifthly, a computer program product containing instructions is provided, which, when executed by a computing device, causes the computing device to perform the methods described in the first aspect and any implementation thereof.

[0033] In a sixth aspect, a computer-readable storage medium is provided, including computer program instructions that, when executed by a computing device, perform the method as described in the first aspect and any implementation thereof.

[0034] As examples, these computer-readable storage devices include, but are not limited to, one or more of the following: read-only memory (ROM), programmable ROM (PROM), erasable PROM (EPROM), flash memory, electrically EPROM (EEPROM), and hard drive.

[0035] Alternatively, as one implementation method, the aforementioned storage medium can specifically be a non-volatile storage medium. Attached Figure Description

[0036] Figure 1 This is a schematic block diagram of a cloud scenario applicable to embodiments of this application.

[0037] Figure 2 This is a schematic flowchart of a method for generating a simulated object provided in an embodiment of this application.

[0038] Figure 3 This is a schematic block diagram of a simulated object generation device 300 provided in an embodiment of this application.

[0039] Figure 4 This is a schematic diagram of the architecture of a computing device 1500 provided in an embodiment of this application. Detailed Implementation

[0040] The technical solutions in this application will now be described with reference to the accompanying drawings.

[0041] This application will present various aspects, embodiments, or features relating to systems comprising multiple devices, components, modules, etc. It should be understood and appreciated that individual systems may include additional devices, components, modules, etc., and / or may not include all devices, components, modules, etc. discussed in conjunction with the accompanying drawings. Furthermore, combinations of these approaches are also possible.

[0042] Furthermore, in the embodiments of this application, the words "exemplary," "for example," etc., are used to indicate that they are examples, illustrations, or descriptions. Any embodiment or design scheme described as "exemplary" in this application should not be construed as being more preferred or advantageous than other embodiments or design schemes. Specifically, the use of the term "exemplary" is intended to present the concept in a concrete manner.

[0043] In the embodiments of this application, "corresponding" and "corresponding" can sometimes be used interchangeably. It should be noted that when the distinction is not emphasized, their intended meanings are consistent.

[0044] The business scenarios described in the embodiments of this application are for the purpose of more clearly illustrating the technical solutions of the embodiments of this application, and do not constitute a limitation on the technical solutions provided in the embodiments of this application. As those skilled in the art will know, with the evolution of network architecture and the emergence of new business scenarios, the technical solutions provided in the embodiments of this application are also applicable to similar technical problems.

[0045] References to "one embodiment" or "some embodiments" as described in this specification mean that one or more embodiments of this application include a specific feature, structure, or characteristic described in connection with that embodiment. Therefore, the phrases "in one embodiment," "in some embodiments," "in other embodiments," "in still other embodiments," etc., appearing in different parts of this specification do not necessarily refer to the same embodiment, but rather mean "one or more, but not all, embodiments," unless otherwise specifically emphasized. The terms "comprising," "including," "having," and variations thereof mean "including but not limited to," unless otherwise specifically emphasized.

[0046] In this application, "at least one" means one or more, and "more than one" means two or more. "And / or" describes the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can mean: A alone, A and B simultaneously, and B alone, where A and B can be singular or plural. The character " / " generally indicates that the preceding and following related objects are in an "or" relationship. "At least one of the following" or similar expressions refer to any combination of these items, including any combination of single or plural items. For example, at least one of a, b, or c can mean: a, b, c, ab, ac, bc, or abc, where a, b, and c can be single or multiple.

[0047] In software testing, test cases are used to test the functionality of a specific object under test. During the testing process, the object under test may depend on other objects. For example, these dependent objects might need to call external systems or modules during operation, increasing the complexity of the test. Furthermore, if the state or behavior of these dependent objects is unstable, the testing process may become unpredictable, affecting the accuracy of the test results.

[0048] For example, a mock mechanism can be used to simulate the behavior of actual dependent objects by using mock objects instead of actual dependent objects. It should be understood that a mock object is a virtual object that replaces the actual dependent object in testing. By using mock objects to replace the actual dependent object, testing can be performed without actually calling the dependent object. This avoids calls to external systems or modules by the actual dependent object, thus achieving flexibility and efficiency in software testing.

[0049] For example, suppose there is an online shopping system with a processing module responsible for handling user order payments. This module relies on a payment gateway service to process payment requests. When testing the functionality of this processing module, a simulated payment gateway can be created. This simulated payment gateway is used in the test to replace the actual payment gateway service. The simulated payment gateway can simulate various behaviors of the actual payment gateway service, such as successful payment and payment failure.

[0050] Therefore, in software testing, how to generate mock objects and how to instrument the behavior of mock objects have become issues of concern for unit testing tools of different programming languages.

[0051] In one related technical solution, developers write the type declaration of a mock object and the calling relationship between the mock object and the real object into the test code. For example, in Java, developers can use the real object as the parent type and the mock object as the child type in the test code, establishing an inheritance relationship between them. During the execution of the test code, by instrumenting the mock object before the real object (generally through an interface provided by the Java Virtual Machine (JVM), the object under test calls the mock object to replace the behavior of the real object during the test.

[0052] In the aforementioned technical solutions, both the real object and the mock object need to be inheritable categories to allow the mock object to replace the real object. For categories that cannot be inherited, mock objects cannot be used to replace real objects, thus limiting the scope of the mock mechanism.

[0053] In another related technical solution, in order to expand the scope of use of the mock mechanism, developers modify the real objects in the production code from non-inheritable categories to inheritable categories.

[0054] In the aforementioned technical solutions, production code, having undergone multiple stages including development, testing, and version control, is the software code ultimately used for actual deployment and operation. The quality of production code directly impacts the quality and performance of the software product. Therefore, changing the categories of objects in production code can reduce the quality and performance of the software product, and may even lead to security issues.

[0055] In view of this, embodiments of this application provide a method for generating mock objects. This method can not only expand the scope of application of the mock mechanism without affecting the quality of production code, but also improve the development efficiency of test code.

[0056] In one possible implementation, the method provided in this application embodiment can be applied to a cloud service scenario, where the method is executed by a cloud management platform within the cloud service scenario. For ease of description, the following will first refer to... Figure 1 It provides a detailed description of cloud service scenarios.

[0057] Figure 1 This is a schematic block diagram illustrating a cloud scenario applicable to embodiments of this application. For example... Figure 1 As shown, the cloud scenario may include: cloud management platform 110, Internet 120, and client 130.

[0058] like Figure 1 As shown, the cloud management platform 110 is used to manage the infrastructure that provides multiple cloud services. The infrastructure includes multiple cloud data centers, each cloud data center includes multiple servers, and each server includes cloud service resources to provide corresponding cloud services to tenants.

[0059] The cloud management platform 110 can be located in a cloud data center and provides access interfaces (such as user interfaces or application program interfaces, APIs). Tenants can use client 130 to remotely access the access interface to register a cloud account and password on the cloud management platform 110 and log in. After successful authentication of the cloud account and password on the cloud management platform 110, the tenant can further select and purchase virtual machines of specific specifications (processor, memory, disk) on the cloud management platform 110. After successful purchase, the cloud management platform 110 provides the remote login account and password for the purchased virtual machine, and client 130 can remotely log in to the virtual machine to install and run the tenant's applications. Therefore, tenants can create, manage, log in to, and operate virtual machines in the cloud data center through the cloud management platform 110. Virtual machines can also be referred to as Elastic Compute Service (ECS) or Elastic Instances (different cloud service providers may use different names).

[0060] It should be understood that cloud service tenants can be individuals, businesses, schools, hospitals, government agencies, etc.

[0061] The cloud management platform 110 includes, but is not limited to, a user console, compute management services, network management services, storage management services, authentication services, and image management services. The user console provides an interface or API for interaction with tenants. The compute management services manage servers running virtual machines and containers, as well as bare metal servers. The network management services manage network services (such as gateways and firewalls). The storage management services manage storage services (such as data bucket services). The authentication services manage tenant account passwords. The image management services manage virtual machine images. Tenants use client 130 and the internet 120 to log in to the cloud management platform 110 to manage their rented cloud services.

[0062] Let's combine the following... Figure 2 This application provides a detailed description of a method for generating simulated objects according to embodiments. It should be understood that... Figure 2 The examples are merely to help those skilled in the art understand the embodiments of this application, and are not intended to limit the embodiments of the application to... Figure 2 The specific numerical values ​​or specific scenarios illustrated. Those skilled in the art will understand based on... Figure 2 The examples given below can obviously be modified or varied in various ways, and such modifications and variations also fall within the scope of the embodiments of this application.

[0063] Figure 2 This is a schematic flowchart illustrating a method for generating a simulated object according to an embodiment of this application. Figure 2 As shown, the method may include steps 210-230, which will be described in detail below.

[0064] Step 210: The compiler obtains the user code.

[0065] In this embodiment of the application, the compiler can obtain user code. For example, the compiler can obtain a file containing user code written by the user.

[0066] The user can write user code to a file through an interactive interface or through an application programming interface (API). This application embodiment does not specifically limit this.

[0067] For example, the user code mentioned above may include test code and production code.

[0068] As an example, the test code above is used to test the functionality of an object under test, whose actual dependent object is a target object of type A.

[0069] For example, the test code above includes a first generation statement. This first generation statement is used to generate (or create) an entity of a simulated object of type A' based on a target object of type A.

[0070] For example, one possible generation statement is as follows: `let mymock = createMock` ()` It should be understood that in the above generation statement, A is the type name of the target object, the variable "mymock" is the entity of the mock object corresponding to the generated target object, and the "createMock" function call expression is the above generation statement.

[0071] Another example is the production code mentioned above, which is software code intended for actual deployment and operation. This production code includes a first declaration statement for type A of the target object.

[0072] For example, a possible declaration statement is shown below: class A{ func bar():int64{ ··· } } It should be understood that in the above declaration statement, A is the type name of the target object in the production code, and "class A" represents the declaration of type A of the target object.

[0073] For example, the first generation statement and the first declaration statement described above are scripts written in a high-level language. This application does not specifically limit the high-level language used; such high-level languages ​​may include, but are not limited to, Java, C, C#, Python, etc.

[0074] Step 220: The compiler generates the compiled second declaration statement based on the first declaration statement and the first generation statement.

[0075] In this embodiment, after obtaining the user code, the compiler can generate a compiled second declaration statement based on the first declaration statement and the first generation statement in the user code. The second declaration statement defines the type of the mock object generated by the compiler and the calling relationship between the mock object and the target object.

[0076] For example, the compiler can generate a mock object of the corresponding type based on the first declaration statement and the first generation statement, for example, generate an entity of a mock object of type A'.

[0077] In the above technical solution, developers do not need to generate mock objects in user code using a high-level programming language. Instead, the compiler generates the mock object based on the first declaration statement and the first generation statement. In this way, for cases where mock objects cannot be generated in user code because the corresponding real categories cannot be inherited, the compiler can generate mock objects corresponding to these categories, thereby expanding the scope of the mock mechanism and improving the testing efficiency and flexibility of the test code.

[0078] The mock objects and compiled second declaration statements generated by the compiler are written in machine language. That is, the mock objects and compiled second declaration statements generated by the compiler are data structures that can be executed by computing devices (e.g., binary formats specifically for computers).

[0079] For example, the compiler includes a generator for generating an entity of a mock object of type A' and a second declaration statement for declaring the type A' of the mock object, based on a first generation statement in the test code and a first declaration statement in the production code.

[0080] For example, the second declaration statement above, in addition to defining the type A' of the simulated object, also includes the calling relationship between the type A' of the simulated object and the type A of the target object.

[0081] The embodiments of this application do not specifically limit the implementation of the calling relationship between the above-mentioned simulated object and the target object, as long as the calling relationship can indicate that the simulated object is used instead of the target object during the testing of the object under test.

[0082] In one possible implementation, in Java, the aforementioned calling relationship could be, for example, an inheritance relationship between a parent class and a child class. For instance, the type A' of the mock object could be a subclass of the type A of the target object. This way, if a target object of type A needs to be called during testing of the object under test, the mock object of type A' can be called directly based on this inheritance relationship, instead of actually calling the target object of type A.

[0083] In another possible implementation, the type A' of the mock object can be used as an extension of the type A of the target object. This way, if the target object of type A needs to be called during the testing of the object under test, the mock object of type A' can be called directly based on this extension relationship, instead of actually calling the target object of type A.

[0084] Step 230: The compiler generates the executable file for the computing device based on the first declaration statement, the compiled second declaration statement, and the first generation statement.

[0085] In this embodiment of the application, the compiler can generate an executable file of the computing device based on the first declaration statement, the compiled second declaration statement, and the first generation statement.

[0086] It should be understood that the compiler can be deployed on a computing device that executes the above-mentioned executable file, or it can be deployed on other computing devices. This application embodiment does not specifically limit this.

[0087] For example, the executable file mentioned above includes: a compiled first declaration statement, a compiled second declaration statement, and a compiled first generation statement.

[0088] For example, a compiler can compile the first declaration statement in the above production code into machine instructions (e.g., binary instructions) that can be directly executed on a computing device, thus obtaining the compiled first declaration statement.

[0089] For example, the compiler can compile the first generated statement in the test code above into machine instructions (e.g., binary instructions) that can be directly executed on a computing device, thus obtaining the compiled first generated statement.

[0090] In this embodiment of the application, after obtaining the executable file, the compiler can transfer the executable file to the computing device for execution.

[0091] For example, during the execution of a file transmitted by the compiler, when the test object needs to call the target object, the computing device can directly call the entity of the created simulated object according to the calling relationship between the simulated object and the target object in the second declaration statement, without actually calling the target object.

[0092] For example, when a computing device executes a file transmitted by the compiler, and the computing device needs to call a target object of type A when testing the object under test, it directly calls the entity of a simulated object of type A' created by the compiler according to the calling relationship, instead of actually calling the target object of type A.

[0093] In the above technical solution, the compiler generates mock objects based on the first declaration and first generation statements in the user code, and generates declaration statements based on the type of the generated mock objects and the calling relationship between the mock objects and the target objects. This eliminates the need for developers to directly write the generated mock objects, the declaration statements for declaring the types of the mock objects, and the calling relationship between the mock objects and the target objects in the user code (e.g., test code). On the one hand, this simplifies test code and improves its development efficiency. On the other hand, without affecting the quality of the production code, for categories of objects that cannot be inherited in user code written in high-level programming languages, the compiler-generated mock objects can replace real objects, thereby expanding the scope of the mock mechanism and improving the testing efficiency and flexibility of the test code.

[0094] For example, let's describe the pseudocode after the compiler processes the user code shown below.

[0095] For example, the first declaration statement in the production code within the user code would look like this: class Foo{ func bar():int64{ ··· } } For example, the first generate statement in the test code within the user code is shown below: let mymock = createMock <foo>(handler) For example, the pseudocode for the first declaration statement after compiler processing is shown below: class Foo{ func bar():int64{ ··· } } For example, the pseudocode for the first generated statement after compiler processing is shown below: let mymock = Foo_Mock(handler) For example, the pseudocode for the second declaration statement generated by the compiler is shown below: Class Foo_Mock<:Foo{ private let handler:CallHandler private let objectToSpy:Foo init(handle:CallHandler){ this.handler_handler this.objectToSpy = zeroValue <foo>() } func bar():Int64{ let result=handler.onCall(Call(…)) / / … } } In this context, "class Foo" is the first declaration statement of the type to be mocked in the production code; the "createMock" function call expression is the first generation statement; "myMock" is the mock object; and "Foo_Mock" is the type of the mock object. "myMock" and "Foo_Mock" can be generated by calling the constructor of "Foo_Mock".

[0096] The above text combined< / foo> < / foo> Figures 1 to 2 The method provided in the embodiments of this application is described in detail below. Figures 3-4 The embodiments of the apparatus of this application are described in detail below. It should be understood that the descriptions of the method embodiments correspond to the descriptions of the apparatus embodiments; therefore, any parts not described in detail can be referred to the foregoing method embodiments.

[0097] Figure 3 This is a schematic block diagram of a simulated object generation device 300 provided in an embodiment of this application. The device 300 can be implemented by software, hardware, or a combination of both. The device 300 provided in this embodiment can implement the embodiments of this application. Figure 2 The method flow shown describes an application of the device 300 to a compiler. The device 300 includes an acquisition module 310 and a generation module 320. The acquisition module 310 acquires user code, which includes a first declaration statement and a first generation statement. The first declaration statement defines the type of a target object, which is the object on which the object under test in the code depends. The first generation statement generates a simulated object corresponding to the target object based on its type. The generation module 320 generates a compiled second declaration statement based on the first declaration statement and the first generation statement. The compiled second declaration statement defines the type of the simulated object generated by the compiler and the calling relationship between the simulated object and the target object. This calling relationship instructs the simulated object to be used instead of the target object during testing of the object under test. The generation module 320 also generates an executable file for a computing device based on the first declaration statement, the compiled second declaration statement, and the first generation statement. The executable file includes the compiled first declaration statement, the compiled second declaration statement, and the compiled first generation statement.

[0098] Optionally, the generation module 320 is also configured to generate the simulated object based on the first declaration statement and the first generation statement.

[0099] Optionally, the first declaration statement and the first generation statement are written in a high-level language, and the compiled second declaration statement is written in machine language.

[0100] Optionally, the device 300 further includes a transmission module for transmitting the executable file to the computing device, which uses the simulated object to replace the target object during the testing of the object under test according to the calling relationship.

[0101] Optionally, the acquisition module 310 is specifically used to: acquire a target file, which includes the user code.

[0102] Optionally, the user code includes test code and production code, wherein the test code includes the first generation statement and the production code includes the first declaration statement.

[0103] Optionally, the calling relationship between the simulated object and the target object includes an inheritance relationship.

[0104] The device 300 here can be embodied in the form of a functional module. The term "module" here can be implemented in software and / or hardware, without specific limitations.

[0105] For example, a "module" can be a software program, a hardware circuit, or a combination of both that implements the above functions. For instance, the implementation of module 310 will be described below using module 310 as an example. Similarly, the implementation of other modules, such as generation module 320 and transmission module, can refer to the implementation of module 310.

[0106] As an example of a software functional unit, the acquisition module 310 may include code running on a computing instance. The computing instance may include at least one of a physical host (computing device), a virtual machine, or a container. Further, the aforementioned computing instance may be one or more. For example, the acquisition module 310 may include code running on multiple hosts / virtual machines / containers. It should be noted that the multiple hosts / virtual machines / containers used to run the code may be distributed in the same region or in different regions. Further, the multiple hosts / virtual machines / containers used to run the code may be distributed in the same availability zone (AZ) or in different AZs, each AZ including one or more geographically proximate data centers. Typically, a region may include multiple AZs.

[0107] Similarly, multiple hosts / virtual machines / containers used to run this code can be distributed within the same Virtual Private Cloud (VPC) or across multiple VPCs. Typically, a VPC is set up within a region. Communication between two VPCs within the same region, as well as between VPCs in different regions, requires a communication gateway to be set up within each VPC to enable interconnection between VPCs.

[0108] As an example of a hardware functional unit, the acquisition module 310 may include at least one computing device, such as a server. Alternatively, the acquisition module 310 may also be a device implemented using an application-specific integrated circuit (ASIC) or a programmable logic device (PLD). The PLD may be implemented using a complex programmable logical device (CPLD), a field-programmable gate array (FPGA), generic array logic (GAL), or any combination thereof.

[0109] The multiple computing devices included in the acquisition module 310 can be distributed in the same region or in different regions. Similarly, the multiple computing devices included in the acquisition module 310 can be distributed in the same Availability Zone (AZ) or in different AZs. Likewise, the multiple computing devices included in the acquisition module 310 can be distributed in the same Virtual Private Cloud (VPC) or in multiple VPCs. These multiple computing devices can be any combination of computing devices such as servers, ASICs, PLDs, CPLDs, FPGAs, and GALs.

[0110] Therefore, the modules of the various examples described in the embodiments of this application can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0111] It should be noted that the above embodiments of the device, when executing the above methods, are only illustrative examples of the division of the above functional modules. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. For example, the acquisition module 310 can be used to execute any step in the above methods, the generation module 320 can be used to execute any step in the above methods, and the transmission module can be used to execute any step in the above methods. The steps implemented by the acquisition module 310, the generation module 320, and the transmission module can be specified as needed. By implementing different steps in the above methods through the acquisition module 310, the generation module 320, and the transmission module, all the functions of the above device can be realized.

[0112] Furthermore, the apparatus and method embodiments provided in the above embodiments belong to the same concept, and their specific implementation process can be found in the method embodiments above, which will not be repeated here.

[0113] The method provided in this application can be executed by a computing device, which can also be referred to as a computer system. It includes a hardware layer, an operating system layer running on top of the hardware layer, and an application layer running on the operating system layer. The hardware layer includes hardware such as processing units, memory, and memory control units; the functions and structure of this hardware will be described in detail later. The operating system is any one or more computer operating systems that implement business processing through processes, such as Linux, Unix, Android, iOS, or Windows. The application layer includes applications such as browsers, address books, word processing software, and instant messaging software. Optionally, the computer system can be a handheld device such as a smartphone, or a terminal device such as a personal computer; this application does not particularly limit this, as long as the method provided in this application can be used. The executing entity of the method provided in this application can be a computing device, or a functional module within the computing device capable of calling and executing programs.

[0114] The following is combined with Figure 4 This application provides a detailed description of a computing device provided in an embodiment.

[0115] Figure 4 This is a schematic diagram of the architecture of a computing device 1500 provided in an embodiment of this application. The computing device 1500 can be a server, a computer, or other device with computing capabilities. Figure 4 The computing device 1500 shown includes at least one processor 1510 and a memory 1520.

[0116] It should be understood that this application does not limit the number of processors and memories in the computing device 1500.

[0117] The processor 1510 executes instructions in the memory 1520, causing the computing device 1500 to implement the method provided in this application. Alternatively, the processor 1510 executes instructions in the memory 1520, causing the computing device 1500 to implement the various functional modules provided in this application, thereby implementing the method provided in this application.

[0118] Optionally, the computing device 1500 also includes a communication interface 1530. The communication interface 1530 uses a transceiver module, such as, but not limited to, a network interface card or a transceiver, to enable communication between the computing device 1500 and other devices or communication networks.

[0119] Optionally, the computing device 1500 further includes a system bus 1540, wherein the processor 1510, memory 1520, and communication interface 1530 are respectively connected to the system bus 1540. The processor 1510 can access the memory 1520 through the system bus 1540; for example, the processor 1510 can perform data read / write or code execution in the memory 1520 through the system bus 1540. The system bus 1540 is a peripheral component interconnect express (PCI) bus or an extended industry standard architecture (EISA) bus, etc. The system bus 1540 is divided into an address bus, a data bus, a control bus, etc. For ease of illustration, Figure 4 The bus is represented by a single thick line, but this does not mean that there is only one bus or one type of bus.

[0120] In one possible implementation, the processor 1510 primarily functions to interpret the instructions (or code) of a computer program and process data within the computer software. The instructions of the computer program and the data within the computer software can be stored in memory 1520 or cache 1516.

[0121] Optionally, processor 1510 may be an integrated circuit chip with signal processing capabilities. By way of example and not limitation, processor 1510 can be a general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. A general-purpose processor is a microprocessor, etc. For example, processor 1510 is a central processing unit (CPU).

[0122] Optionally, each processor 1510 includes at least one processing unit 1512 and a memory control unit 1514.

[0123] Optionally, the processing unit 1512, also known as the core, is the most important component of the processor. The processing unit 1512 is manufactured from single-crystal silicon using a specific production process. All calculations, command reception, command storage, and data processing are performed by the core. Each processing unit independently executes program instructions, utilizing parallel computing capabilities to accelerate program execution. Each processing unit has a fixed logical structure; for example, a processing unit includes logical units such as a Level 1 cache, a Level 2 cache, an execution unit, an instruction-level unit, and a bus interface.

[0124] In one implementation example, the memory control unit 1514 controls the data interaction between the memory 1520 and the processing unit 1512. Specifically, the memory control unit 1514 receives memory access requests from the processing unit 1512 and controls access to memory based on the memory access requests. By way of example and not limitation, the memory control unit is a device such as a memory management unit (MMU).

[0125] In one implementation example, each memory control unit 1514 addresses the memory 1520 via the system bus. An arbitrator is configured in the system bus. Figure 4 (Not shown in the image), this arbitrator is responsible for handling and coordinating competing accesses by multiple processing units 1512.

[0126] In one implementation example, the processing unit 1512 and the memory control unit 1514 are connected via internal chip connection lines, such as address lines, thereby enabling communication between the processing unit 1512 and the memory control unit 1514.

[0127] Optionally, each processor 1510 also includes a cache 1516, which is a buffer for data exchange (called a cache). When the processing unit 1512 needs to read data, it first looks for the required data in the cache. If the data is found, it is executed directly; otherwise, it looks for the data in memory. Since the cache operates much faster than memory, its role is to help the processing unit 1512 run faster.

[0128] The memory 1520 provides runtime space for processes in the computing device 1500. For example, the memory 1520 stores the computer program (specifically, the program code) used to generate the process. After the computer program is executed by the processor to generate a process, the processor allocates corresponding storage space for the process in the memory 1520. Furthermore, the aforementioned storage space further includes text segments, initialized data segments, bit initialized data segments, stack segments, heap segments, etc. The memory 1520 stores data generated during the process's execution, such as intermediate data or process data, in the aforementioned process-specific storage space.

[0129] Optionally, the memory, also known as RAM, is used to temporarily store the data processed by the processor 1510, as well as data exchanged with external storage devices such as hard disks. As long as the computer is running, the processor 1510 will load the data that needs to be processed into RAM for processing, and after the processing is completed, the processing unit 1512 will send the result out.

[0130] By way of example and not limitation, memory 1520 is volatile memory or non-volatile memory, or may include both volatile and non-volatile memory. Non-volatile memory is read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory. Volatile memory is random access memory (RAM) used as an external cache. By way of example, but not limitation, many forms of RAM are available, such as static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous linked dynamic random access memory (SLDRAM), and direct rambus RAM (DRRAM). It should be noted that the memory 1520 of the systems and methods described herein is intended to include, but is not limited to, these and any other suitable types of memory.

[0131] The above-described structure of the computing device 1500 is merely illustrative and is not intended to limit the application. The computing device 1500 in this application includes various hardware components found in existing computer systems. For example, the computing device 1500 may also include other memories besides the memory 1520, such as disk storage. Those skilled in the art should understand that the computing device 1500 may also include other devices necessary for normal operation. Furthermore, depending on specific needs, those skilled in the art should understand that the computing device 1500 may also include hardware devices for implementing other additional functions. Moreover, those skilled in the art should understand that the computing device 1500 may only include the devices necessary for implementing the embodiments of this application, and may not necessarily include... Figure 4 All the devices shown.

[0132] In this embodiment, a computer program product containing instructions is also provided. The computer program product may be a software or program product containing instructions capable of running on a computing device or stored on any usable medium. When run on a computing device, it causes the computing device to perform the methods provided above, or causes the computing device to perform the functions of the apparatus provided above.

[0133] In this embodiment, a computer-readable storage medium is also provided. This computer-readable storage medium can be any available medium that a computing device can store, or a data storage device such as a data center containing one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid-state drive). The computer-readable storage medium includes instructions that, when executed on a computing device, cause the computing device to perform the method described above.

[0134] It should be understood that in the various embodiments of this application, the order of the above-mentioned processes does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application.

[0135] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0136] Those skilled in the art will understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.

[0137] In the several embodiments provided in this application, it should be understood that the disclosed systems, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.

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

[0139] In addition, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.

[0140] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0141] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

Claims

1. A method for generating simulated objects, characterized in that, The method includes: The compiler obtains user code, which includes a first declaration statement and a first generation statement. The first declaration statement is used to define the type of the target object, which is the object that the object under test in the code depends on. The first generation statement is used to generate a simulated object corresponding to the target object based on the type of the target object. The compiler generates a compiled second declaration statement based on the first declaration statement and the first generation statement. The compiled second declaration statement is used to define the type of the simulated object generated by the compiler and the calling relationship between the simulated object and the target object. The calling relationship is used to indicate that the simulated object is used instead of the target object during the testing of the object under test. The compiler generates an executable file for the computing device based on the first declaration statement, the compiled second declaration statement, and the first generation statement, wherein the executable file includes the compiled first declaration statement, the compiled second declaration statement, and the compiled first generation statement.

2. The method according to claim 1, characterized in that, The method further includes: The compiler generates the simulated object based on the first declaration statement and the first generation statement.

3. The method according to claim 1 or 2, characterized in that, The first declaration statement and the first generation statement are written in a high-level language, while the compiled second declaration statement is written in machine language.

4. The method according to any one of claims 1 to 3, characterized in that, The method further includes: The compiler transmits the executable file to the computing device, which is used to replace the target object with the simulated object during the testing of the object under test according to the calling relationship.

5. The method according to any one of claims 1 to 4, characterized in that, The compiler obtains user code, including: The compiler obtains an object file, which includes the user code.

6. The method according to any one of claims 1 to 5, characterized in that, The user code includes test code and production code, wherein the test code includes the first generation statement and the production code includes the first declaration statement.

7. The method according to any one of claims 1 to 6, characterized in that, The calling relationship between the simulated object and the target object includes an inheritance relationship.

8. A device for generating simulated objects, characterized in that, The apparatus is used in a compiler, and the apparatus includes: The acquisition module is used to acquire user code, which includes a first declaration statement and a first generation statement. The first declaration statement is used to define the type of the target object, which is the object that the object under test in the code depends on. The first generation statement is used to generate a simulated object corresponding to the target object according to the type of the target object. A generation module is used to generate a compiled second declaration statement based on the first declaration statement and the first generation statement. The compiled second declaration statement is used to define the type of the simulated object generated by the compiler and the calling relationship between the simulated object and the target object. The calling relationship is used to indicate that the simulated object is used instead of the target object during the testing of the object under test. The generation module is further configured to generate an executable file of the computing device based on the first declaration statement, the compiled second declaration statement, and the first generation statement, wherein the executable file includes the compiled first declaration statement, the compiled second declaration statement, and the compiled first generation statement.

9. The apparatus according to claim 8, characterized in that, The generation module is further configured to generate the simulated object based on the first declaration statement and the first generation statement.

10. The apparatus according to claim 8 or 9, characterized in that, The first declaration statement and the first generation statement are written in a high-level language, while the compiled second declaration statement is written in machine language.

11. The apparatus according to any one of claims 8 to 10, characterized in that, The device further includes: A transmission module is used to transmit the executable file to the computing device, wherein the computing device is used to use the simulated object to replace the target object during the testing of the object under test according to the calling relationship.

12. The apparatus according to any one of claims 8 to 11, characterized in that, The acquisition module is specifically used for: Obtain the target file, which includes the user code.

13. The apparatus according to any one of claims 8 to 12, characterized in that, The user code includes test code and production code, wherein the test code includes the first generation statement and the production code includes the first declaration statement.

14. The apparatus according to any one of claims 8 to 13, characterized in that, The calling relationship between the simulated object and the target object includes an inheritance relationship.

15. A computing device cluster, characterized in that, It includes at least one computing device, each computing device including a processor and memory; The processor of the at least one computing device is configured to execute instructions stored in the memory of the at least one computing device to cause the cluster of computing devices to perform the method as described in any one of claims 1 to 7.

16. A computer program product containing instructions, characterized in that, When the instruction is executed by the computing device cluster, the computing device cluster performs the method as described in any one of claims 1 to 7.

17. A computer-readable storage medium, characterized in that, It includes computer program instructions, which, when executed by a cluster of computing devices, perform the method as described in any one of claims 1 to 7.