CN113778849A - Method, apparatus, device and storage medium for testing code - Google Patents

Method, apparatus, device and storage medium for testing code Download PDF

Info

Publication number
CN113778849A
CN113778849A CN202110278053.XA CN202110278053A CN113778849A CN 113778849 A CN113778849 A CN 113778849A CN 202110278053 A CN202110278053 A CN 202110278053A CN 113778849 A CN113778849 A CN 113778849A
Authority
CN
China
Prior art keywords
code
test case
target
test
function
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202110278053.XA
Other languages
Chinese (zh)
Inventor
吕望
郑炜铭
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Jingdong Century Trading Co Ltd
Beijing Wodong Tianjun Information Technology Co Ltd
Original Assignee
Beijing Jingdong Century Trading Co Ltd
Beijing Wodong Tianjun Information Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Jingdong Century Trading Co Ltd, Beijing Wodong Tianjun Information Technology Co Ltd filed Critical Beijing Jingdong Century Trading Co Ltd
Priority to CN202110278053.XA priority Critical patent/CN113778849A/en
Publication of CN113778849A publication Critical patent/CN113778849A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3676Test management for coverage analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3688Test management for test execution, e.g. scheduling of test suites

Abstract

The application discloses a method, a device, equipment and a storage medium for testing codes, and relates to the field of code testing. The specific implementation scheme is as follows: acquiring an object code, wherein the object code comprises at least one method function; running the target code; acquiring input parameters and return values generated by at least one method function in the operation process; acquiring a target test case corresponding to the target code; and determining the test result of the target code according to the input parameters, the return values and the target test case. The implementation mode can realize the test of the code in the running process, so that technical personnel are not required to compile complex test cases, and the test efficiency is improved.

Description

Method, apparatus, device and storage medium for testing code
Technical Field
The present application relates to the field of computer technologies, and in particular, to the field of code testing, and in particular, to a method, an apparatus, a device, and a storage medium for testing a code.
Background
In order to control the quality of software codes and better avoid the occurrence of program errors, generally, a corresponding unit test case needs to be written into an updated code before or after a software function code is updated, the unit test case is executed after the code is updated each time, and whether the software runs correctly is verified, so that the occurrence of the program errors is avoided.
The existing method generally comprises the steps of manually searching a corresponding unit test case and manually starting to execute unit test after monitoring code updating; and after the unit test is finished, manually analyzing whether the test structure completely passes through.
Disclosure of Invention
A method, apparatus, device, and storage medium for testing code are provided.
According to a first aspect, there is provided a method for testing code, comprising: acquiring an object code, wherein the object code comprises at least one method function; running the target code; acquiring input parameters and return values generated by at least one method function in the operation process; acquiring a target test case corresponding to the target code; and determining the test result of the target code according to the input parameters, the return values and the target test case.
According to a second aspect, there is provided an apparatus for testing code, comprising: an object code obtaining unit configured to obtain an object code, the object code including at least one method function; an object code execution unit configured to execute object code; a parameter acquisition unit configured to acquire an input parameter and a return value generated by at least one method function during operation; a test case acquisition unit configured to acquire a target test case corresponding to the target code; and the test result determining unit is configured to determine the test result of the target code according to the input parameters, the return values and the target test case.
According to a third aspect, there is provided an electronic device for performing a method for testing code, comprising: at least one processor; and a memory communicatively coupled to the at least one processor; wherein the memory stores instructions executable by the at least one processor to enable the at least one processor to perform the method as described in the first aspect.
According to a fourth aspect, there is provided a non-transitory computer readable storage medium having stored thereon computer instructions for causing a computer to perform the method as described in the first aspect.
According to a fifth aspect, a computer program product comprising a computer program which, when executed by a processor, implements the method as described in the first aspect.
According to the code testing method provided by the technology of the application, the code can be tested in the running process, so that technicians are not required to compile complex test cases, and the testing efficiency is improved.
It should be understood that the statements in this section do not necessarily identify key or critical features of the embodiments of the present disclosure, nor do they limit the scope of the present disclosure. Other features of the present disclosure will become apparent from the following description.
Drawings
The drawings are included to provide a better understanding of the present solution and are not intended to limit the present application. Wherein:
FIG. 1 is an exemplary system architecture diagram in which one embodiment of the present application may be applied;
FIG. 2 is a flow diagram of one embodiment of a method for testing code according to the present application;
FIG. 3 is a schematic diagram of one application scenario of a method for testing code according to the present application;
FIG. 4 is a flow diagram of another embodiment of a method for testing code according to the present application;
FIG. 5 is a schematic block diagram of one embodiment of an apparatus for testing code according to the present application;
FIG. 6 is a block diagram of an electronic device used to implement the method for testing code of an embodiment of the present application.
Detailed Description
The following description of the exemplary embodiments of the present application, taken in conjunction with the accompanying drawings, includes various details of the embodiments of the application for the understanding of the same, which are to be considered exemplary only. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the present application. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.
It should be noted that the embodiments and features of the embodiments in the present application may be combined with each other without conflict. The present application will be described in detail below with reference to the embodiments with reference to the attached drawings.
Fig. 1 shows an exemplary system architecture 100 to which embodiments of the method for testing code or the apparatus for testing code of the present application may be applied.
As shown in fig. 1, the system architecture 100 may include terminal devices 101, 102, 103, a network 104, and a server 105. The network 104 serves as a medium for providing communication links between the terminal devices 101, 102, 103 and the server 105. Network 104 may include various connection types, such as wired, wireless communication links, or fiber optic cables, to name a few.
The user may use the terminal devices 101, 102, 103 to interact with the server 105 via the network 104 to receive or send messages or the like. Various communication client applications, such as a code integration application, may be installed on the terminal devices 101, 102, 103. The user may write code using the code integration class application installed on the terminal device 101, 102, 103 and send the code to the server 105.
The terminal apparatuses 101, 102, and 103 may be hardware or software. When the terminal devices 101, 102, 103 are hardware, they may be various electronic devices including, but not limited to, laptop portable computers, desktop computers, and the like. When the terminal apparatuses 101, 102, 103 are software, they can be installed in the electronic apparatuses listed above. It may be implemented as multiple pieces of software or software modules (e.g., to provide distributed services) or as a single piece of software or software module. And is not particularly limited herein.
The server 105 may be a server providing various services, such as a background server that tests code submitted on the terminal devices 101, 102, 103. The background server may test the code using the test case and feed back the test result to the terminal devices 101, 102, and 103.
The server 105 may be hardware or software. When the server 105 is hardware, it may be implemented as a distributed server cluster composed of a plurality of servers, or may be implemented as a single server. When the server 105 is software, it may be implemented as multiple pieces of software or software modules (e.g., to provide distributed services), or as a single piece of software or software module. And is not particularly limited herein.
It should be noted that the method for testing code provided by the embodiment of the present application is generally executed by the server 105. Accordingly, the means for testing the code is typically located in the server 105.
It should be understood that the number of terminal devices, networks, and servers in fig. 1 is merely illustrative. There may be any number of terminal devices, networks, and servers, as desired for implementation.
With continued reference to FIG. 2, a flow 200 of one embodiment of a method for testing code in accordance with the present application is shown. The method for testing the code of the embodiment comprises the following steps:
step 201, acquiring an object code.
In this embodiment, an execution subject (e.g., the server 105 shown in fig. 1) of the method for testing a code may acquire an object code in various ways. Here, the object code may be code edited by a developer through a code integration class application, which may include at least one method function. The method functions may also be embodied in the form of interfaces.
At step 202, the object code is run.
The execution subject may run the object code in various ways after obtaining the object code. For example, the execution subject may trigger execution of the object code by receiving an operation of a user. Such operation may be either online or offline. The operation of online may be understood as processing a request of a user, or providing an online service, etc. The offline operation may be the operation using the code integration application before the software is online.
Step 203, obtaining input parameters and return values generated by at least one method function in the running process.
The execution subject can acquire the input parameters and the return values generated by each method function in the running process of the target code in various ways. Specifically, the execution agent may acquire the generated input parameter and return value by inserting a pre-function and/or a post-function in the object code in advance. Alternatively, the execution agent may intercept input parameters and return values generated by each method function through other executable files during execution of the object code.
And step 204, acquiring a target test case corresponding to the target code.
In this embodiment, the execution subject may further obtain a target test case corresponding to the target code. The test cases can be pre-written by a user according to the target code, and can also be used by other codes comprising various method functions. Or, the execution subject may also obtain the test cases from a preset test case library. Each test case in the test case library may have a corresponding relationship with at least one method function, and the execution subject may use the test case corresponding to each method function in the target code as a target test case.
And step 205, determining a test result of the target code according to each input parameter, each return value and the target test case.
In this embodiment, the execution subject may determine the test result of the target code by using each input parameter, each return value, and the target test case. Specifically, for a single method function, the execution subject may input the input parameter of the method function into the test case corresponding to the method function, compare the result obtained by the test case with each return value, and take the comparison result as the test result. Alternatively, the execution subject may substitute the input parameters and the return values into the target test case to obtain an analysis result of the target test case for the input parameters and the return values, and use the analysis result as the test result.
In the conventional white-box test, in order to call a code to be tested, a code tester needs to construct input parameters and return values corresponding to each method function in the code to be tested. For some method functions with simple parameters, a code tester can be easily competent. However, for some complex data types of input parameters or return values, the construction of parameters is particularly cumbersome. For example, if a parameter is a complex object and many objects are nested inside the object, the complex architecture of the object needs to be constructed completely, and then the corresponding test case can be called.
In addition, interfaces, method functions, parameters, etc. in the code under test may not be independent and may rely on other information, such as cache, online services, etc. If it is desired to construct the execution of such test cases, then there must be many additional packages. Such as cache, common data information, from which the interface needs to retrieve some data. When constructing the test case of this interface, an offline cache must be created, and then corresponding data is supplemented therein, so as to initiate a call to the interface to be tested, which is particularly troublesome. When some remote interfaces are depended on in the interfaces, the calling logic of the remote interfaces must be processed, so that the execution can be initiated on the code to be tested. When the interface relies on some database query logic, the unit test process must implement the database query logic and must have the database accessible locally and the corresponding data exists.
In the embodiment, the input parameters and the return values of the method functions in the target code are obtained, so that a code tester does not need to manually construct the parameters, the working complexity of the code tester is reduced, the test errors caused by manual errors are avoided, and the accuracy of code testing is improved.
With continued reference to FIG. 3, a schematic diagram of one application scenario of a method for testing code according to the present application is shown. In the application scenario of fig. 3, a developer edits an event code through a terminal 301, and transmits an object code to a server 302. The server 302 runs the object code after receiving the object code. And the input parameters and the return values generated by the method functions in the running process of the object code are obtained. The target test case corresponding to the target code is also obtained from the database 303. And combining the acquired input parameters and return values with the target test case to obtain a test result. And returns the test results to the terminal 301.
The method for testing the code provided by the embodiment of the application can realize the test of the code in the running process, so that a technician does not need to compile a complex test case online, and the test efficiency is improved.
With continued reference to FIG. 4, a flow 400 of another embodiment of a method for testing code in accordance with the present application is shown. As shown in fig. 4, the method of the present embodiment may include the following steps:
step 401, an object code is obtained.
Step 402, in response to determining that the target code meets the preset condition, acquiring an upstream code for calling the target code; the upstream code is run.
In this embodiment, the execution subject may further determine whether the target code satisfies a preset condition. The preset condition here may be various conditions set according to actual application scenarios. For example, the number of method functions in the object code may be greater than a preset value, the total number of input parameters may be greater than a preset value, the number of nested layers may be greater than a preset value, and so on. And if the target code meets the preset condition, the target code is relatively complicated. When the target code is operated, the input parameters of the target code are needed to be triggered. When the target code is relatively complex, the input parameters of the target code are also relatively complex, and the triggering is difficult. The upstream code that called the target code may be obtained at this point. Here, there is a call relationship between codes, and a code calling a target code may be referred to as an upstream code of the target code. The code called by the target code may be referred to as downstream code of the target code. When determining the upstream code, the codes which do not meet the preset condition, namely the codes with fewer input parameters, can be obtained, and the codes which are easy to trigger to run can be obtained. Since the upstream code can call the target code in the running process, the target code can be run by running the target code.
Or, the preset condition further includes receiving a user instruction. If the execution subject receives a user instruction, the target code may be regarded as having a high complexity. If no user instruction is received, the target code may be deemed not to be of high complexity.
Step 403, determining dynamic bytecode generated by each method function in the operation process; and (4) weaving a pre-function and a post-function in the dynamic byte codes to obtain input parameters and return values of the method functions.
In general, code cannot be inserted at will during execution. Generally, before running, a code can be inserted into an object code, and in the running process, the object code is run so as to realize the running of the inserted code and realize the acquisition of an input parameter and a return value.
In this embodiment, if the object code is Java code, it will generate dynamic bytecode during the running process. The dynamic bytecode is loaded into a Java Virtual Machine (JVM) and can be interpreted and executed. In this embodiment, by modifying the dynamic bytecode, it is possible to weave a pre-function and a post-function into the target code in the running process. Here, the pre-function (before) may intercept input parameters of each method function in the object code, and the post-function (after) may intercept return values of each method function in the object code. It is understood that the pre-function and the post-function require the skilled person to write according to the actual application.
In some specific practices, the ASM framework may be utilized to modify a dynamic bytecode file (class file) generated during the running of the object code. ASM is a Java bytecode manipulation framework. It can be used to dynamically generate classes or to enhance the functionality of existing classes. The ASM can either directly generate the binary class file or dynamically change the class behavior before the class is loaded into the Java virtual machine. class files are stored in a strictly format defined class file that has enough metadata to parse all the elements in the class: class name, method, attribute, and Java bytecode (instruction). After the ASM reads information from the class file, the ASM can change class behaviors, analyze class information and even generate a new class according to user requirements.
In some specific applications, detection points may be used to represent each method function in the object code, or to represent an interface in the object code. When the ASM framework is used for modifying the dynamic byte codes, the following steps can be utilized for realizing the following steps:
1. creating a Java agent program and loading the test metadata.
Here, a Java agent needs to include a Premain method. The Premain method is a proxy method appointed by the Java virtual machine, and is executed before the main method of the system is executed, so that some processing can be performed before the main method is executed. The Premain method is implemented by first loading all the test metadata. The test metadata may include information of a detection point and information of an entry point. The information of the detection point mainly describes data information related to detection, such as which implementation class is used to detect which interface, and the like. The information of the entry point can be understood as the position of the detection point of the code to be inserted.
2. And dynamically weaving the call of the master control module by using the ASM according to the test metadata.
The test metadata comprises information of the detection point and information of the entry point. Each entry point describes the location information that needs to be woven in, i.e. which method of which class etc. These classes and methods are the objects that we need to modify, i.e. to weave certain processing logic in these locations.
Because the ASM weaving process mainly adopts the disassembling operation of the dynamic byte codes, the logic is very complex, a specific cut-in point is not woven, but a master control module is woven, and then each cut-in point is called by the master control module to complete the specific logic, so that only how to weave the master control module into a target position needs to be considered. This ensures that the logic of different entry point weaves is the same.
The master control module mainly comprises a pre-function and a post-function. The pre-function is mainly used to cut into the entry of the method function, i.e. the first line of the method function. And the post function essentially weaves in the method function back to the previous line. The pre-function mainly transmits information such as identification of an entry point, description information of a method function, reference of an interface object, execution parameters of the method function and the like. A post function is a return value that accesses a method function before the method function performs a return.
3. And configuring the total control module to a system where the target code is located.
The codes of the master control module can be packaged, the target codes are operated by using the parameters of the virtual machine, the Java agent program can be added into the operated target codes, and the dynamic modification of the target codes is realized.
Step 404, obtaining a general test case and a special test case corresponding to at least one method function from a preset test case library.
In this embodiment, a test case library may be preset, and the test case library may include a general test case and a special test case. The general test case can be a test case unrelated to the business logic of the target code, such as test performance, interface calling times and the like. For these tests, only one write is needed. The specialized test cases are test cases that are related to the business logic of the target code. Which has a correspondence with the method function.
In some specific applications, the test cases in the test case library can be used commonly with the Junit test cases, so that the Junit can be seamlessly compatible. JUnit is a Java language unit testing framework.
The execution subject may obtain a generic test case and a specific test case corresponding to at least one method function from the test case library.
And 405, executing the target test case according to the input parameters and the return values, and determining the test result of the target code according to the execution result of the target test case.
After the target test case is obtained, the execution main body can execute the target test case according to each input parameter and each return value, and determine the test result of the target code according to the execution result of the target test case. When the test case is executed, the target test case can be run by taking the input parameters of the method function as input, so as to obtain an execution result. The execution subject may use the execution result as a test result of the target code, or may compare the execution result with a return value of the method function, and use a difference between the two as a test result.
In some optional implementations of this embodiment, if the object code includes an interface and the object code is an update of the interface, the execution subject may implement testing of the updated interface by: substituting each input parameter into a target test case to determine an output result of the target test case; and comparing the output result with each return value to determine the test result of the target code.
In this implementation, the execution main body may substitute each input parameter of the interface into the target test case to obtain an output result of the target test case. Then, the output result is compared with each return value, and the test result of the target code can be obtained.
It can be understood that, in this case, the target test case may be a test case used by the interface before updating, or may be an updated test case. If the test case used before is used, the test case can be reused, and the test efficiency is improved.
And step 406, in response to determining that the preset test completion condition is met, stopping executing the target test case.
In this embodiment, the execution subject may determine whether the preset test completion condition is satisfied, and if so, it indicates that the target code does not need to be tested any more, so that the execution of the target test case may be stopped. It is to be understood that, if the target test case includes a plurality of test cases, the preset test completion condition may include a plurality of sub-conditions matching with the plurality of test cases. The execution subject may determine whether each sub-condition is satisfied, respectively. And if a certain sub-condition is met, stopping executing the test case corresponding to the sub-condition. The test completion condition may include a preset time period for the test.
In some specific applications, the execution subject may indicate whether a certain test case is executed by setting a flag bit for the test case. For example, the flag bit is true, indicating that the test case application is executed. The flag bit is false, indicating that the test case is not executed.
Further, if it is determined that the object code no longer requires testing, the testing of the object code may be stopped by disconnecting the Java agent. Thus, the pre-function and the post-function are not woven in the running process of the target code. Which corresponds to deleting the pre-function and the post-function from the object code. The method of the embodiment can realize the control of the target test case by setting the flag bit of the test case, and realize the personalized test of the target code. By setting the Java agent program, the test can be started and stopped at any time, namely the test can be started and stopped at any time.
Step 407, saving the input parameters and return values of each method function; and generating a test case of each method function according to the stored input parameters and the return values.
In this embodiment, the execution main body may also implement a "recording" function. The obtained input parameters and return values of each method function are stored. And generating a test case of each method function according to the stored data. Specifically, for a certain method function, the execution subject may obtain a test case of a method function similar to the method function. And replacing the input parameters and the return values to generate a test case for the method function. Alternatively, the execution principal may output the saved input parameters and return values to the user. Therefore, the user can conveniently and manually construct the input parameters and the return values when writing the test case.
In some optional implementation manners of this embodiment, when the execution subject generates the test case, the coverage of the method function may be determined first, that is, the coverage test is performed on the method function. If the coverage meets the preset coverage condition, a test case can be generated. Here, the preset coverage condition may correspond to a coverage test of different levels. The coverage test may include multiple levels of function coverage, statement coverage, judgment coverage, condition coverage, and the like. Each level has different requirements on the granularity of execution. For example, function overrides require execution to every statement in code, and predicate overrides require execution to branch of every predicate. And if the coverage meets the requirements corresponding to different levels, generating a corresponding test case.
In some optional implementation manners of this embodiment, if the coverage is smaller than the preset threshold, which indicates that some branches in the method function are not covered by the test case, the stored input parameter and the return value may be output to a user, and the user may write a test code of the uncovered branch, and generate the test case with a larger coverage.
Step 408, adding the generated test case into the test case library.
The execution subject can also add the generated test cases to the test case library. Therefore, the test case library can be enriched, so that the recorded test cases can be used again, namely, the test cases are played back. Moreover, the test case and the Junit test case are seamlessly fused, so that a Junit test case library can be enriched.
The method for testing the code provided by the above embodiment of the application can realize dynamic weaving of the pre-function and the post-function in the running process of the code, and realize dynamic testing.
With further reference to fig. 5, as an implementation of the method shown in the above figures, the present application provides an embodiment of an apparatus for testing a code, which corresponds to the embodiment of the method shown in fig. 2, and which is particularly applicable to various electronic devices.
As shown in fig. 5, the apparatus 500 for testing a code of the present embodiment includes: an object code obtaining unit 501, an object code running unit 502, a parameter obtaining unit 503, a test case obtaining unit 504, and a test result determining unit 505.
An object code obtaining unit 501 configured to obtain an object code. The object code includes at least one method function.
An object code execution unit 502 configured to execute object code.
A parameter obtaining unit 503 configured to obtain the input parameters and the return values generated by at least one method function during operation.
A test case obtaining unit 504 configured to obtain a target test case corresponding to the target code.
And a test result determining unit 505 configured to determine a test result of the target code according to the input parameters, the return values, and the target test case.
In some optional implementations of this embodiment, the object code execution unit 502 may be further configured to: in response to determining that the target code meets a preset condition, acquiring an upstream code calling the target code; the upstream code is run.
In some optional implementations of this embodiment, the parameter obtaining unit 503 may be further configured to: determining dynamic byte codes generated by various method functions in the operation process; and (4) weaving a pre-function and a post-function in the dynamic byte codes to obtain input parameters and return values of the method functions.
In some optional implementations of this embodiment, the test case obtaining unit 504 may be further configured to: and acquiring the general test case and the special test case corresponding to at least one method function from a preset test case library.
In some optional implementations of the present embodiment, the test result determining unit 505 may be further configured to: and executing the target test case according to the input parameters and the return values, and determining the test result of the target code according to the execution result of the target test case.
In some optional implementations of the present embodiment, the test result determining unit 505 may be further configured to: substituting each input parameter into a target test case to determine an output result of the target test case; and comparing the output result with each return value to determine the test result of the target code.
In some optional implementations of this embodiment, the apparatus 500 may further include a test stopping unit, not shown in fig. 5, configured to: and stopping executing the target test case in response to determining that the preset test completion condition is met.
In some optional implementations of this embodiment, the apparatus 500 may further include a test case generation unit not shown in fig. 5, configured to: saving input parameters and return values of each method function; and generating a test case of each method function according to the stored input parameters and the return values.
In some optional implementations of this embodiment, the apparatus 500 may further include a test case saving unit, not shown in fig. 5, configured to: and adding the generated test case into a test case library.
In some optional implementations of this embodiment, the test case generation unit is further configured to: determining the coverage of the method function; and responding to the fact that the coverage is larger than or equal to the preset threshold, and generating the test cases of the method functions according to the saved input parameters and the return values.
In some optional implementations of this embodiment, the apparatus 500 may further include a parameter output unit, not shown in fig. 5, configured to: in response to determining that the coverage is less than the preset threshold, outputting the saved input parameters and return values.
It should be understood that units 501 to 505, which are described in the apparatus 500 for testing a code, correspond to the respective steps in the method described with reference to fig. 2, respectively. Thus, the operations and features described above for the method for testing code are equally applicable to the apparatus 500 and the units included therein and will not be described again here.
The application also provides an electronic device, a readable storage medium and a computer program product according to the embodiment of the application.
FIG. 6 shows a block diagram of an electronic device 600 that performs a method for testing code according to an embodiment of the application. Electronic devices are intended to represent various forms of digital computers, such as laptops, desktops, workstations, personal digital assistants, servers, blade servers, mainframes, and other appropriate computers. The electronic device may also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions, are meant to be examples only, and are not meant to limit implementations of the present application that are described and/or claimed herein.
As shown in fig. 6, the device 600 includes a processor 601 that may perform various appropriate actions and processes in accordance with a computer program stored in a Read Only Memory (ROM)602 or a computer program loaded from a memory 608 into a Random Access Memory (RAM) 603. In the RAM603, various programs and data required for the operation of the device 600 can also be stored. The processor 601, the ROM602, and the RAM603 are connected to each other via a bus 604. An I/O interface (input/output interface) 605 is also connected to the bus 604.
A number of components in the device 600 are connected to the I/O interface 605, including: an input unit 606 such as a keyboard, a mouse, or the like; an output unit 607 such as various types of displays, speakers, and the like; a memory 608, such as a magnetic disk, optical disk, or the like; and a communication unit 609 such as a network card, modem, wireless communication transceiver, etc. The communication unit 609 allows the device 600 to exchange information/data with other devices via a computer network such as the internet and/or various telecommunication networks.
Processor 601 may be a variety of general and/or special purpose processing components with processing and computing capabilities. Some examples of processor 601 include, but are not limited to, a Central Processing Unit (CPU), a Graphics Processing Unit (GPU), various specialized Artificial Intelligence (AI) computing chips, various processors running machine learning model algorithms, a Digital Signal Processor (DSP), and any suitable processor, controller, microcontroller, or the like. The processor 601 performs the various methods and processes described above, such as methods for testing code. For example, in some embodiments, the method for testing code may be implemented as a computer software program tangibly embodied in a machine-readable storage medium, such as memory 608. In some embodiments, part or all of the computer program may be loaded and/or installed onto the electronic device 600 via the ROM602 and/or the communication unit 609. When the computer program is loaded into the RAM603 and executed by the processor 601, one or more steps of the method for testing code described above may be performed. Alternatively, in other embodiments, the processor 601 may be configured by any other suitable means (e.g., by means of firmware) to perform the method for testing code.
Various implementations of the systems and techniques described here above may be implemented in digital electronic circuitry, integrated circuitry, Field Programmable Gate Arrays (FPGAs), Application Specific Integrated Circuits (ASICs), Application Specific Standard Products (ASSPs), system on a chip (SOCs), load programmable logic devices (CPLDs), computer hardware, firmware, software, and/or combinations thereof. These various embodiments may include: implemented in one or more computer programs that are executable and/or interpretable on a programmable system including at least one programmable processor, which may be special or general purpose, receiving data and instructions from, and transmitting data and instructions to, a storage system, at least one input device, and at least one output device.
Program code for implementing the methods of the present application may be written in any combination of one or more programming languages. The program code described above may be packaged as a computer program product. These program code or computer program products may be provided to a processor or controller of a general purpose computer, special purpose computer, or other programmable data processing apparatus, such that the program code, when executed by the processor 601, causes the functions/acts specified in the flowchart and/or block diagram block or blocks to be performed. The program code may execute entirely on the machine, partly on the machine, as a stand-alone software package partly on the machine and partly on a remote machine or entirely on the remote machine or server.
In the context of this application, a machine-readable storage medium may be a tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. The machine-readable storage medium may be a machine-readable signal storage medium or a machine-readable storage medium. A machine-readable storage medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the foregoing. More specific examples of a machine-readable storage medium would include an electrical connection based on one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
To provide for interaction with a user, the systems and techniques described here can be implemented on a computer having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to a user; and a keyboard and a pointing device (e.g., a mouse or a trackball) by which a user can provide input to the computer. Other kinds of devices may also be used to provide for interaction with a user; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user may be received in any form, including acoustic, speech, or tactile input.
The systems and techniques described here can be implemented in a computing system that includes a back-end component (e.g., as a data server), or that includes a middleware component (e.g., an application server), or that includes a front-end component (e.g., a user computer having a graphical user interface or a web browser through which a user can interact with an implementation of the systems and techniques described here), or any combination of such back-end, middleware, or front-end components. The components of the system can be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: local Area Networks (LANs), Wide Area Networks (WANs), and the Internet.
The computer system may include clients and servers. A client and server are generally remote from each other and typically interact through a communication network. The relationship of client and server arises by virtue of computer programs running on the respective computers and having a client-server relationship to each other. The Server can be a cloud Server, also called a cloud computing Server or a cloud host, and is a host product in a cloud computing service system, so as to solve the defects of high management difficulty and weak service expansibility in the traditional physical host and VPS service ("Virtual Private Server", or simply "VPS"). The server may also be a server of a distributed system, or a server incorporating a blockchain.
It should be understood that various forms of the flows shown above may be used, with steps reordered, added, or deleted. For example, the steps described in the present application may be executed in parallel, sequentially, or in different orders, as long as the desired results of the technical solution of the present application can be achieved, and the present invention is not limited thereto.
The above-described embodiments should not be construed as limiting the scope of the present application. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may be made in accordance with design requirements and other factors. Any modification, equivalent replacement, and improvement made within the spirit and principle of the present application shall be included in the protection scope of the present application.

Claims (15)

1. A method for testing code, comprising:
acquiring an object code, wherein the object code comprises at least one method function;
running the object code;
acquiring input parameters and return values generated by the at least one method function in the operation process;
acquiring a target test case corresponding to the target code;
and determining the test result of the target code according to each input parameter, each return value and the target test case.
2. The method of claim 1, wherein the running the object code comprises:
in response to determining that the target code meets a preset condition, acquiring an upstream code calling the target code;
the upstream code is executed.
3. The method of claim 1, wherein said obtaining input parameters and return values generated by said at least one method function during execution comprises:
determining dynamic byte codes generated by various method functions in the operation process;
and weaving a pre-function and a post-function in the dynamic byte codes to obtain input parameters and return values of the method functions.
4. The method of claim 1, wherein the obtaining of the target test case corresponding to the target code comprises:
and acquiring a general test case and a special test case corresponding to the at least one method function from a preset test case library.
5. The method of claim 3, wherein determining the test result of the target code according to the input parameters, the return values and the target test case comprises:
and executing the target test case according to the input parameters and the return values, and determining the test result of the target code according to the execution result of the target test case.
6. The method of claim 5, wherein the executing the target test case according to the input parameters and the return values and determining the test result of the target code according to the execution result of the target test case comprise:
substituting each input parameter into the target test case to determine an output result of the target test case;
and comparing the output result with each return value to determine the test result of the target code.
7. The method of claim 1, wherein the method further comprises:
and stopping executing the target test case in response to determining that a preset test completion condition is met.
8. The method of claim 4, wherein the method further comprises:
saving input parameters and return values of each method function;
and generating a test case of each method function according to the stored input parameters and the return values.
9. The method of claim 8, wherein the method further comprises:
and adding the generated test case into the test case library.
10. The method of claim 8, wherein generating the test case for each method function according to the saved input parameters and return values comprises:
determining a coverage of the method function;
and in response to the fact that the coverage degree meets the preset coverage condition, generating the test cases of the method functions according to the stored input parameters and the return values.
11. The method of claim 10, wherein the method further comprises:
in response to determining that the coverage is less than the preset threshold, outputting the saved input parameters and return values.
12. An apparatus for testing code, comprising:
an object code obtaining unit configured to obtain an object code including at least one method function;
an object code execution unit configured to execute the object code;
a parameter acquiring unit configured to acquire an input parameter and a return value generated by the at least one method function during operation;
a test case acquisition unit configured to acquire a target test case corresponding to the target code;
and the test result determining unit is configured to determine the test result of the target code according to each input parameter, each return value and the target test case.
13. An electronic device that performs a method for testing code, comprising:
at least one processor; and
a memory communicatively coupled to the at least one processor; wherein the content of the first and second substances,
the memory stores instructions executable by the at least one processor to enable the at least one processor to perform the method of any one of claims 1-11.
14. A non-transitory computer readable storage medium having stored thereon computer instructions for causing the computer to perform the method of any one of claims 1-11.
15. A computer program product comprising a computer program which, when executed by a processor, implements the method according to any one of claims 1-11.
CN202110278053.XA 2021-03-15 2021-03-15 Method, apparatus, device and storage medium for testing code Pending CN113778849A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202110278053.XA CN113778849A (en) 2021-03-15 2021-03-15 Method, apparatus, device and storage medium for testing code

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202110278053.XA CN113778849A (en) 2021-03-15 2021-03-15 Method, apparatus, device and storage medium for testing code

Publications (1)

Publication Number Publication Date
CN113778849A true CN113778849A (en) 2021-12-10

Family

ID=78835483

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202110278053.XA Pending CN113778849A (en) 2021-03-15 2021-03-15 Method, apparatus, device and storage medium for testing code

Country Status (1)

Country Link
CN (1) CN113778849A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2023217118A1 (en) * 2022-05-10 2023-11-16 支付宝(杭州)信息技术有限公司 Code test method and apparatus, and test case generation method and apparatus

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2023217118A1 (en) * 2022-05-10 2023-11-16 支付宝(杭州)信息技术有限公司 Code test method and apparatus, and test case generation method and apparatus

Similar Documents

Publication Publication Date Title
CN112486820B (en) Method, apparatus, device and storage medium for testing code
CN113760769B (en) Test case processing method and device, electronic equipment and storage medium
CN109977012B (en) Joint debugging test method, device, equipment and computer readable storage medium of system
US20220174122A1 (en) Fusing and degradation method and apparatus for micro-service, device, and medium
CN115509522A (en) Interface arranging method and system for low-code scene and electronic equipment
CN111209181A (en) Regression testing method, system, device and computer storage medium
CN113778849A (en) Method, apparatus, device and storage medium for testing code
CN112925522A (en) Dependency graph generation method, dependency graph generation device, dependency graph generation apparatus, storage medium, and program product
CN115576831A (en) Test case recommendation method, device, equipment and storage medium
CN115269431A (en) Interface testing method and device, electronic equipment and storage medium
CN115017047A (en) Test method, system, equipment and medium based on B/S architecture
CN114416029A (en) Data processing method, device, equipment, storage medium and computer program product
CN114546799A (en) Point burying log checking method and device, electronic equipment, storage medium and product
CN113448852A (en) Test case obtaining method and device, electronic equipment and storage medium
CN113127357A (en) Unit testing method, device, equipment, storage medium and program product
CN113360365A (en) Flow testing method and flow testing system
CN113760765B (en) Code testing method and device, electronic equipment and storage medium
CN117130945B (en) Test method and device
CN115374010A (en) Function testing method, device, equipment and storage medium
CN115567624A (en) Message processing method and device, electronic equipment and medium
CN117435503A (en) Test tool generation method, test device, test equipment and test medium
CN115167847A (en) Application log acquisition method, device, equipment and storage medium
CN114327577A (en) Code change determining method and device, electronic equipment and storage medium
CN117827624A (en) Function test method, device and storage medium
CN116974940A (en) Interface testing method, device, equipment and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination