A unit testing method, device, equipment and medium
By obtaining and formatting the basic data of unit test cases and generating test cases during software operation, the problem of high unit test costs is solved, automated testing is realized, and software quality and iteration speed is improved.
Patent Information
- Application Number
- CN202210692374.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-17
- Publication Date
- 2024-12-20
- Estimated Expiration
- 2042-06-17
AI Technical Summary
Unit testing requires manual code writing and design use cases during software development, resulting in high writing and maintenance costs, and iteration and maintenance require modification of code, which consumes a lot of manpower and affects the rapid iteration and quality of the software.
By obtaining the basic data of unit test cases, using a preset format representation and supplementing contextual environment information during the software operation, unit test cases are generated to realize code-free automation of unit tests, only iterating and maintaining the basic data, no need to modify the code.
It reduces the iteration and maintenance costs of unit test cases, improves test coverage and quality, and improves the iteration speed of software.
Smart Images

Figure CN114996152B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of software testing technology, and in particular to a unit testing method, device, equipment and medium. Background Art
[0002] Unit testing can discover software defects in the early stages of the development phase and detect problems in the early stages of the software development process, thereby ensuring the quality of software development, avoiding online accidents, and reducing the cost of solving problems. Therefore, unit testing is a very important step in the testing process.
[0003] Unit testing generally requires manual coding and design of unit test cases, and the code of the unit test cases needs to be modified when the software code changes, which results in high coding and maintenance costs. In related technologies, unit test cases can be automatically generated at the code level, but the iteration and maintenance of unit test cases still need to be achieved by modifying the code, which requires a lot of manpower and still has the defect of high cost. Summary of the invention
[0004] In order to solve the above technical problems, the present disclosure provides a unit testing method, device, equipment and medium.
[0005] The present disclosure provides a unit testing method, the method comprising:
[0006] Get the basic data of unit test cases;
[0007] The basic data is expressed in a preset format to obtain target data;
[0008] During the running of the software to be tested, the unit test case is generated after the context environment information is supplemented based on the target data, and the unit test is performed on the software to be tested based on the unit test case.
[0009] The present disclosure also provides a unit testing device, the device comprising:
[0010] Acquisition module, used to obtain basic data of unit test cases;
[0011] A format module, used to express the basic data in a preset format to obtain target data;
[0012] The test module is used to generate the unit test case after supplementing the context environment information based on the target data during the running of the software to be tested, and perform unit testing on the software to be tested based on the unit test case.
[0013] An embodiment of the present disclosure also provides an electronic device, which includes: a processor; a memory for storing executable instructions of the processor; the processor is used to read the executable instructions from the memory and execute the instructions to implement the unit testing method provided by the embodiment of the present disclosure.
[0014] The embodiment of the present disclosure further provides a computer-readable storage medium, wherein the storage medium stores a computer program, and the computer program is used to execute the unit testing method provided by the embodiment of the present disclosure.
[0015] The technical solution provided by the embodiment of the present disclosure has the following advantages over the prior art: the unit test solution provided by the embodiment of the present disclosure obtains the basic data of the unit test case; the basic data is represented in a preset format to obtain the target data; during the operation of the software to be tested, the unit test case is generated after the context environment information is supplemented based on the target data, and the unit test is performed on the software to be tested based on the unit test case. By adopting the above technical solution, by obtaining the basic data required for the unit test case and representing the basic data in a preset format, a complete unit test case can be generated and the unit test can be performed after the context environment information is supplemented based on the basic data in the preset format during the operation of the software. During iteration and maintenance, only the basic data of the unit test case needs to be iterated and maintained, and there is no need to modify the code, which greatly reduces the iteration and maintenance costs of the unit test case, thereby improving the coverage and quality of the unit test, and improving the iteration speed of the software. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] The above and other features, advantages and aspects of the embodiments of the present disclosure will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. Throughout the accompanying drawings, the same or similar reference numerals represent the same or similar elements. It should be understood that the drawings are schematic and the originals and elements are not necessarily drawn to scale.
[0017] Figure 1 A flowchart of a unit testing method provided by an embodiment of the present disclosure;
[0018] Figure 2 A flowchart of another unit testing method provided by an embodiment of the present disclosure;
[0019] Figure 3 A schematic diagram of a unit test provided in an embodiment of the present disclosure;
[0020] Figure 4 A schematic diagram of the structure of a unit testing device provided in an embodiment of the present disclosure;
[0021] Figure 5 A schematic diagram of the structure of an electronic device provided in an embodiment of the present disclosure. DETAILED DESCRIPTION
[0022] Embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although certain embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be construed as being limited to the embodiments described herein, which are instead provided for a more thorough and complete understanding of the present disclosure. It should be understood that the drawings and embodiments of the present disclosure are only for exemplary purposes and are not intended to limit the scope of protection of the present disclosure.
[0023] It should be understood that the various steps described in the method embodiments of the present disclosure may be performed in different orders and / or in parallel. In addition, the method embodiments may include additional steps and / or omit the steps shown. The scope of the present disclosure is not limited in this respect.
[0024] The term "including" and its variations used herein are open inclusions, i.e., "including but not limited to". The term "based on" means "based at least in part on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one additional embodiment"; the term "some embodiments" means "at least some embodiments". The relevant definitions of other terms will be given in the following description.
[0025] It should be noted that the concepts such as "first" and "second" mentioned in the present disclosure are only used to distinguish different devices, modules or units, and are not used to limit the order or interdependence of the functions performed by these devices, modules or units.
[0026] It should be noted that the modifications of "one" and "plurality" mentioned in the present disclosure are illustrative rather than restrictive, and those skilled in the art should understand that unless otherwise clearly indicated in the context, it should be understood as "one or more".
[0027] The names of the messages or information exchanged between multiple devices in the embodiments of the present disclosure are only used for illustrative purposes and are not used to limit the scope of these messages or information.
[0028] Unit testing generally requires manual coding and design of unit test cases. When the code design is more complex, the unit test code needs to be reconstructed before writing. In addition, the unit test code needs to be modified when the original code changes. The maintenance cost is very high, which conflicts with the rapid iteration of requirements. For example, taking the native test suite XCTest provided by the iOS system as an example, in the process of writing and maintaining unit test cases, developers need to provide a context environment for the unit test cases, declare and construct input and output variables, call test methods, and compare output results. Developers often only care about the input and output of the tested method, but not how these input variables are constructed and how the method is called. Therefore, the current solution inevitably requires writing a lot of redundant code, which inevitably increases the cost of writing and maintaining unit test cases. In addition, the current unit test cases all require developers to write and maintain them specifically. In the process of rapid iteration of software requirements, if you want to ensure high-quality unit tests, the iteration and maintenance of unit test cases requires a lot of manpower, which becomes a stumbling block to blocking rapid iteration. In the related art, unit test cases can be automatically generated at the code level, but the iteration and maintenance of unit test cases still need to be realized by modifying the code, which requires a lot of manpower and still has the defect of high cost.
[0029] In order to solve the above problems, the embodiments of the present disclosure provide a unit testing method, which is introduced below in conjunction with specific embodiments.
[0030] Figure 1 The present invention provides a flow chart of a unit testing method provided in an embodiment of the present invention. The method can be executed by a unit testing device, wherein the device can be implemented by software and / or hardware and can generally be integrated in an electronic device. Figure 1 As shown, the method includes:
[0031] Step 101: Obtain basic data of unit test cases.
[0032] Among them, the unit test case can be understood as a code or a specific set written for the specific function of the software unit in order to effectively verify the characteristics of the software unit and discover the defects of the software unit code. The specific set may include the name of the case, the context environment, the test object, the test purpose, the input data, the expected results, etc. The software unit can be the function, method, module, etc. that constitute the software. A software is usually composed of multiple software units mentioned above. Each software unit implements its own determined function respectively, and calls other software units through the input and output interfaces provided by it to jointly realize the overall function of the software. The unit testing method of the embodiment of the present disclosure can be executed by a unit test suite, which can be understood as a comprehensive functional module that can implement unit testing for one or more software.
[0033] Basic data can be the necessary data required by the unit test case during testing, and it is also the data that developers are concerned about for the unit test case. In the embodiment of the present disclosure, the basic data may include the name of the unit test method, class information, test object, input parameters and expected output results; the class information may be the class to which the unit test method belongs. The class information is available when the code language of the software to be tested is the Objective-C code language, and can be modified to other functions or upper-level modules when the code language of the software to be tested is other languages; the test object may be the object that calls the unit test method, the input parameters correspond to the input variables of the specific software unit, and the output expected results correspond to the output variables of the software unit.
[0034] In some embodiments, obtaining basic data of a unit test case may include at least one of the following: collecting software running data of the software to be tested through a collection module, and parsing the software running data to obtain the basic data of the unit test case; generating the basic data of the unit test case by using a heuristic algorithm based on the basic data of existing unit test cases of the software to be tested; and obtaining the basic data of the unit test case in response to user input operations.
[0035] Among them, the acquisition module can be a functional module for collecting software operation data, which is set in the unit test suite. The software operation data can be the data generated by different software units during the daily operation of the software, and the specific data type is not limited. The heuristic algorithm can be an algorithm based on intuition or experience, which gives a feasible solution for each instance of the combinatorial optimization problem to be solved within an acceptable computing time and space. In the embodiment of the present disclosure, the basic data of new unit test cases can be generated by the heuristic algorithm. The heuristic algorithm can include multiple ones, which are not limited in the embodiment of the present disclosure. For example, the heuristic algorithm can include a genetic algorithm, a simulated annealing algorithm, an ant colony algorithm, and the like.
[0036] When the unit test device obtains the basic data of the unit test case, one way is to collect the software running data of the software to be tested through the acquisition module, and obtain the basic data of the unit test case by parsing the software running data. For example, taking the software to be tested in Objective-C language as an example, the acquisition module can be a hook The objc_msgSend function collects the register and stack context data of the OC method, and parses the context data to obtain the actual parameter list and result of the OC method, that is, the basic data of the unit test case; and for static languages such as C / C++, a similar effect can be achieved by plugging in; another way is to use a heuristic algorithm to generate the basic data of new unit test cases based on the basic data of the existing unit test cases of the software to be tested. For example, taking the heuristic algorithm as a genetic algorithm as an example, the basic data of each existing unit test case is regarded as a gene fragment, and the coverage rate is used as the fitness. Through the random generation, hybridization, mutation and other behaviors of genetic factors, the basic data of new unit test cases is continuously generated; another way is to respond to the user's input operation and obtain the basic data of the unit test case input by the user. The user is the developer, that is, the developer can manually enter the basic data of the unit test case. The specific input method is not limited. For example, it can be directly written by the developer or input through a preset interactive system.
[0037] The above three methods can all realize the acquisition of basic data of unit test cases, improving the efficiency and diversity of basic data acquisition.
[0038] Step 102: The basic data is expressed in a preset format to obtain target data.
[0039] The preset format may be a general data specification or data format. In the embodiment of the present disclosure, the preset format may be a readable data format, that is, it is highly readable and is helpful for maintenance and iteration. The specific format of the preset format may be set according to the actual situation, and is not limited to it. For example, the preset format may be set to JSON format or table format. The target data may be converted into basic data in a preset format, and the target data supports at least one of the following operations: adding, deleting, and modifying.
[0040] In the disclosed embodiment, after acquiring basic data of a unit test case, the unit testing apparatus may use a preset format to represent the basic data to obtain corresponding target data.
[0041] For example, when the preset format is JSON format, the target data can be expressed as follows:
[0042]
[0043] }
[0044] In this solution, the basic data of the unit test case is represented in a preset format. On the one hand, the common data storage format is convenient for storage in the database. For example, when the preset format is JSON format, many document-based non-relational databases support JSON format as their data storage format, such as MongoDB, CouchDB, RavenDB, etc. On the other hand, based on the target data in the preset format, such as the target data in JSON format, all test cases belonging to the class and method can be quickly retrieved, which is convenient for selective local unit testing of certain classes and methods.
[0045] Step 103: During the running of the software to be tested, a unit test case is generated after the context environment information is supplemented based on the target data, and a unit test is performed on the software to be tested based on the unit test case.
[0046] The software to be tested may be any software that needs to be unit tested, and the embodiments of the present disclosure do not limit the type and quantity of the software to be tested.
[0047] In the disclosed embodiment, the unit testing device can run the software to be tested after representing the basic data in a preset format, obtaining the target data and storing it. During the running of the software to be tested, the corresponding target data can be automatically obtained according to the language characteristics of the software to be tested, and a complete unit test case can be generated after parsing the target data to supplement the context environment information required for the unit test, and the unit testing method of each software unit is executed to obtain the output test result. After that, the output expected result in the target data is compared with the output test result to determine whether it meets the expectations and obtain the unit test result.
[0048] The unit testing solution disclosed in the present invention can be a code-free automated unit testing solution, which can automatically obtain basic data in a preset format and generate unit test cases during the software operation process. By dynamically injecting unit test cases, the cost of writing and maintaining unit test cases can be reduced, helping developers save manpower and improving unit test coverage and quality.
[0049] The unit test solution provided by the embodiment of the present disclosure obtains the basic data of the unit test case; represents the basic data in a preset format to obtain the target data; during the operation of the software to be tested, the unit test case is generated after the context environment information is supplemented based on the target data, and the unit test is performed on the software to be tested based on the unit test case. By adopting the above technical solution, by obtaining the basic data required for the unit test case and representing the basic data in a preset format, a complete unit test case can be generated and the unit test can be performed after the context environment information is supplemented based on the basic data in the preset format during the operation of the software. During iteration and maintenance, only the basic data of the unit test case needs to be iterated and maintained, and there is no need to modify the code, which greatly reduces the iteration and maintenance costs of the unit test case, thereby improving the coverage and quality of the unit test and improving the iteration speed of the software.
[0050] For example, Figure 2 A flow chart of another unit testing method provided by the embodiment of the present disclosure is as follows: Figure 2 As shown, in a feasible implementation manner, when the code language of the software to be tested is a dynamic call type, and the context environment information includes temporary variables, the above step 103 may include the following steps:
[0051] Step 201: Acquire and parse target data to obtain parameter types of the test object, parameter types of input parameters, and parameter values.
[0052] The code language of the software under test is a dynamic call type, which means that the functions and methods in the code of the software under test can be dynamically called by each other. For example, Objective-C and Python are both dynamic call types. The test object can be the object that calls the unit test method, and is also a parameter; the input parameter corresponds to the input variable of the specific software unit.
[0053] When the unit test device generates a unit test case after supplementing the context information based on the target data during the running of the software to be tested, it can first determine whether the code language of the software to be tested is a dynamic call type. If so, the required target data can be obtained from the database based on the language characteristics, that is, the target data can be obtained and parsed based on the functions under the code language, and the parameter type of the test object, the parameter type of the input parameter, and the parameter value can be obtained for later use. For example, when the code language is Objective-C language, the target data can be obtained and parsed based on the NSInvocation class of the Objective-C language.
[0054] Step 202: construct a corresponding first temporary variable based on the parameter type of the test object, and construct a second temporary variable of the input parameter based on the parameter type and parameter value of the input parameter.
[0055] Among them, temporary variables can be variables required for unit test execution. When the software is executed, a piece of memory needs to be allocated in the memory to store the parameters during the test. The temporary variable can be a variable corresponding to this piece of memory, and the meaning of constructing the context environment is to construct temporary variables. Temporary variables can include a first temporary variable and a second temporary variable. The first temporary variable can refer to a temporary variable of the test object, and the second temporary variable can be a temporary variable of the input parameter, which is distinguished by the first and the second.
[0056] Specifically, the unit testing device can construct a corresponding first temporary variable according to the parameter type of the test object, and can construct a corresponding second temporary variable according to the parameter type and parameter value of the input parameter. The construction method can adopt relevant technologies. For example, when the parameter type of the test object is int type, an int variable can be constructed.
[0057] Step 203: Combine the first temporary variable, the second temporary variable, and the name, class information, and expected output result of the unit test method extracted from the target data to determine a unit test case.
[0058] Step 204: Use the first temporary variable and the second temporary variable as actual parameters of the unit test method, execute the unit test method based on the name and class information of the unit test method, and obtain an output test result.
[0059] The actual parameter can be understood as the parameter transmitted when calling a function or method, and the actual parameter can be a constant, a variable, an expression, etc. The output test result can be a result obtained after the current test.
[0060] Specifically, after generating a unit test case, when the unit testing device performs unit testing on the software to be tested based on the unit test case, the first temporary variable and the second temporary variable can be used as actual parameters of the unit testing method, and the unit testing method can be obtained based on the name and class information of the unit testing method, and the parameters are automatically passed to the unit testing method and executed to obtain the output test results.
[0061] Step 205: Compare the output test result with the output expected effect to obtain the unit test result.
[0062] The unit test result may be a result indicating whether the unit test meets expectations.
[0063] In the disclosed embodiment, after obtaining the output test result, the unit test device can compare the output expected result in the target data with the output test result to determine whether it meets expectations, thereby obtaining the unit test result.
[0064] In the above scheme, for the code language of dynamic call type, the context environment information required for unit testing can be automatically supplemented based on the language characteristics, the unit test is executed to collect results, the unit test efficiency is improved, and the cost of writing and maintaining unit test cases is reduced.
[0065] In some embodiments, when the code language of the software to be tested is a non-dynamic call type and the context environment information is a context environment code, during the running of the software to be tested, a unit test case is generated after the context environment information is supplemented based on the target data, and a unit test is performed on the software to be tested based on the unit test case. It may include: converting the target data into code format data corresponding to the code language of the software to be tested through a preset conversion module, adding the context environment code to the code format data to generate a unit test case in code format, and assembling the unit test case in code format and embedding it into the code of the software to be tested; during the running of the software to be tested, a unit test is performed based on the unit test case in code format.
[0066] Among them, the code language of the software under test is a non-dynamic call type, which means that the functions, methods, etc. in the code of the software under test do not support dynamic calls. For example, C language, C++ language, etc. are all non-dynamic call types. The preset conversion module can be a functional module for converting the language of the target data and supplementing the context environment code to generate unit test cases. It is set for the software under test whose code language is a non-dynamic call type. This module can be a black box functional module for the developers of unit test cases, and does not affect the developers' maintenance of unit test cases.
[0067] Specifically, when the code language of the software to be tested is of a non-dynamic call type, the unit testing device can convert the target data into code format data that is the same as the code language of the software to be tested or an intermediate code through a pre-set preset conversion module, and add a context environment code to the code format data to obtain a unit test case in code format, and assemble the algorithm of the code of the software to be tested, and temporarily embed the unit test case in code format into the software, so as to automatically execute unit testing based on the unit test case in code format during the running process of the software to be tested.
[0068] In the above scheme, for non-dynamic call type code languages, the conversion module can automatically generate unit test cases in code form and encapsulate them internally in the code of the software to be tested.
[0069] For non-dynamic languages, a solution can be designed to automatically generate test code based on the input and output of the test case. The test case writer does not need to care about the test code but only the input and output of the test case. The automatically generated test code is encapsulated internally, and during iteration and maintenance, only the basic data of the unit test case needs to be iterated and maintained, without modifying the code, thus reducing the iteration and maintenance cost of the unit test case.
[0070] Next, the unit testing method of the embodiment of the present disclosure is further described by a specific example. Figure 3 A schematic diagram of a unit test provided by an embodiment of the present disclosure, such as Figure 3 As shown, the figure shows a complete unit testing process. The unfilled part in the figure represents the process that requires manual intervention by the developer, and the gray-filled part represents the process that can be automated, which may specifically include: the acquisition of basic data of the unit test case, which can be obtained in three ways as shown in the figure, including: input by the developer, collection and analysis of software operation data, and automatic generation by the heuristic algorithm, after which the basic data can be stored in the unit test case database in a format that conforms to the specification, that is, the target data obtained by the preset format; for the software to be tested that needs to be unit tested, the target data can be automatically obtained during the operation of the software to be tested, and the unit test cases generated after the context environment information is supplemented based on the target data can be automatically executed for the software to be tested regularly; based on the unit test results of each unit test case collected, the pass rate of the unit test case is calculated, and the code coverage of the unit test execution is collected; after executing the unit test, the software developer can check the unit test cases that failed to execute to evaluate whether they meet the requirements of the new version of the software, and finally the software quality can be evaluated based on the pass rate of the unit test cases.
[0071] In this solution, a codeless automated unit test suite is provided, which automatically collects online software running data to generate basic data for unit test cases. Combined with the random basic data generated based on heuristic algorithms and manual partial entry, it can fully cover the basic data required for unit test cases, and then automatically supplement the context environment information to generate unit test cases and execute unit tests. For developers, they only need to enter data based on the agreed format to perform unit testing, without having to write unit test code and rely on native code test suites, thereby reducing the cost of writing unit test cases. It also has the ability to automatically generate unit test cases. During iteration and maintenance, only the basic data of the unit test cases needs to be iterated and maintained, and there is no need to modify the code. This can effectively save the manpower and material resources of unit test case maintenance and iteration personnel, greatly reducing the iteration and maintenance costs of unit test cases, and thus effectively improving the coverage and quality of software unit testing, and effectively ensuring software quality.
[0072] Figure 4 This is a schematic diagram of the structure of a unit test device provided by an embodiment of the present disclosure. The device can be implemented by software and / or hardware and can generally be integrated into an electronic device. Figure 4 As shown, the device comprises:
[0073] Acquisition module 401, used to acquire basic data of unit test cases;
[0074] A format module 402 is used to express the basic data in a preset format to obtain target data;
[0075] The testing module 403 is used to generate the unit test case after supplementing the context environment information based on the target data during the running of the software to be tested, and perform unit testing on the software to be tested based on the unit test case.
[0076] Optionally, the acquisition module 401 includes at least one of the following:
[0077] The first unit is used to collect the software running data of the software to be tested through the collection module, and parse the software running data to obtain the basic data of the unit test case;
[0078] The second unit is used to generate basic data of the unit test case by using a heuristic algorithm based on the basic data of the existing unit test case of the software to be tested;
[0079] The third unit is used to obtain basic data of the unit test case in response to the user's input operation.
[0080] Optionally, the basic data includes the name of the unit test method, class information, test object, input parameters and expected output results.
[0081] Optionally, when the code language of the software to be tested is of a dynamic call type, the context information includes temporary variables, and the test module 403 includes a generation unit for:
[0082] Acquire and parse the target data to obtain the parameter type of the test object, the parameter type and parameter value of the input parameter;
[0083] Constructing a corresponding first temporary variable based on the parameter type of the test object, and constructing a second temporary variable of the input parameter based on the parameter type and parameter value of the input parameter, wherein the temporary variables include the first temporary variable and the second temporary variable;
[0084] The first temporary variable, the second temporary variable, and the name of the unit test method extracted from the target data, the class information, and the expected output result are combined to determine the unit test case.
[0085] Optionally, the testing module 403 includes an execution unit, configured to:
[0086] Using the first temporary variable and the second temporary variable as actual parameters of the unit test method, executing the unit test method based on the name of the unit test method and the class information, and obtaining an output test result;
[0087] The output test result is compared with the output expected effect to obtain a unit test result.
[0088] Optionally, when the code language of the software to be tested is a non-dynamic call type, and the context information is a context code, the testing module 403 is used to:
[0089] The target data is converted into code format data corresponding to the code language of the software to be tested by a preset conversion module, and context environment code is added to the code format data to generate unit test cases in code format, and the unit test cases in code format are assembled and embedded into the code of the software to be tested;
[0090] During the running of the software to be tested, unit testing is performed based on unit test cases in the code format.
[0091] Optionally, the preset format is a readable data format, and the target data supports at least one of the following operations: adding, deleting, and modifying.
[0092] The unit testing device provided in the embodiments of the present disclosure can execute the unit testing method provided in any embodiment of the present disclosure, and has the corresponding functional modules and beneficial effects of the execution method.
[0093] The embodiments of the present disclosure also provide a computer program product, including a computer program / instruction, which is a unit testing method provided by any embodiment of the present disclosure when the computer program / instruction is executed by a processor.
[0094] Figure 5 This is a schematic diagram of the structure of an electronic device provided by an embodiment of the present disclosure. Figure 5, which shows a schematic diagram of the structure of an electronic device 500 suitable for implementing the embodiment of the present disclosure. The electronic device 500 in the embodiment of the present disclosure may include but is not limited to mobile terminals such as mobile phones, notebook computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle terminals (such as vehicle navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 5 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0095] like Figure 5 As shown, the electronic device 500 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 501, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 502 or a program loaded from a storage device 508 into a random access memory (RAM) 503. In the RAM 503, various programs and data required for the operation of the electronic device 500 are also stored. The processing device 501, the ROM 502, and the RAM 503 are connected to each other via a bus 504. An input / output (I / O) interface 505 is also connected to the bus 504.
[0096] Typically, the following devices may be connected to the I / O interface 505: an input device 506 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 507 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 508 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 509. The communication device 509 may allow the electronic device 500 to communicate with other devices wirelessly or by wire to exchange data. Although Figure 5 The electronic device 500 is shown with various devices, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.
[0097] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a non-transitory computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device 509, or installed from a storage device 508, or installed from a ROM 502. When the computer program is executed by the processing device 501, the above-mentioned functions defined in the unit test method of the embodiment of the present disclosure are executed.
[0098] It should be noted that the computer-readable medium disclosed above may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program that may be used by or in combination with an instruction execution system, device or device. In the present disclosure, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, in which a computer-readable program code is carried. This propagated data signal may take a variety of forms, including but not limited to an electromagnetic signal, an optical signal, or any suitable combination of the above. The computer readable signal medium may also be any computer readable medium other than a computer readable storage medium, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer readable medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, RF (radio frequency), etc., or any suitable combination of the above.
[0099] In some embodiments, the client and the server may communicate using any currently known or future developed network protocol such as HTTP (HyperText Transfer Protocol), and may be interconnected with any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include a local area network ("LAN"), a wide area network ("WAN"), an internet (e.g., the Internet), and a peer-to-peer network (e.g., an ad hoc peer-to-peer network), as well as any currently known or future developed network.
[0100] The computer-readable medium may be included in the electronic device, or may exist independently without being incorporated into the electronic device.
[0101] The above-mentioned computer-readable medium carries one or more programs. When the above-mentioned one or more programs are executed by the electronic device, the electronic device: obtains basic data of the unit test case; represents the basic data in a preset format to obtain target data; during the running of the software to be tested, generates the unit test case after supplementing the context environment information based on the target data, and performs unit testing on the software to be tested based on the unit test case.
[0102] Computer program code for performing the operations of the present disclosure may be written in one or more programming languages or a combination thereof, including, but not limited to, object-oriented programming languages, such as Java, Smalltalk, C++, and conventional procedural programming languages, such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., through the Internet using an Internet service provider).
[0103] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present disclosure. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some implementations as replacements, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0104] The units involved in the embodiments described in the present disclosure may be implemented by software or hardware, wherein the name of a unit does not, in some cases, limit the unit itself.
[0105] The functions described above herein may be performed at least in part by one or more hardware logic components. For example, without limitation, exemplary types of hardware logic components that may be used include: field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), and the like.
[0106] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0107] The above description is only a preferred embodiment of the present disclosure and an explanation of the technical principles used. Those skilled in the art should understand that the scope of disclosure involved in the present disclosure is not limited to the technical solutions formed by a specific combination of the above technical features, but should also cover other technical solutions formed by any combination of the above technical features or their equivalent features without departing from the above disclosed concept. For example, the above features are replaced with the technical features with similar functions disclosed in the present disclosure (but not limited to) by each other to form a technical solution.
[0108] In addition, although each operation is described in a specific order, this should not be understood as requiring these operations to be performed in the specific order shown or in a sequential order. Under certain circumstances, multitasking and parallel processing may be advantageous. Similarly, although some specific implementation details are included in the above discussion, these should not be interpreted as limiting the scope of the present disclosure. Some features described in the context of a separate embodiment can also be implemented in a single embodiment in combination. On the contrary, the various features described in the context of a single embodiment can also be implemented in multiple embodiments individually or in any suitable sub-combination mode.
[0109] Although the subject matter has been described in language specific to structural features and / or methodological logical actions, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. On the contrary, the specific features and actions described above are merely example forms of implementing the claims.
Claims
1. A unit testing method, It is characterized in that include: Get the basic data of unit test cases; The basic data is expressed in a preset format to obtain target data; During the running of the software to be tested, the unit test case is generated after the corresponding target data is acquired according to the language characteristics of the software to be tested and the context environment information is supplemented, and the unit test is performed on the software to be tested based on the unit test case; Wherein, when the code language of the software to be tested is of a dynamic call type, the context environment includes temporary variables, and the unit test case is generated based on the temporary variables constructed by parsing the target data and the target data; When the software code language to be tested is of a non-dynamic call type, the context environment includes a context environment code, and the unit test case is generated based on converting the target data into corresponding code format data and adding the context environment code.
2. The method according to claim 1, It is characterized in that Obtain basic data for unit test cases, including at least one of the following: Collecting the software running data of the software to be tested through the acquisition module, and parsing the software running data to obtain the basic data of the unit test case; Based on the basic data of the existing unit test cases of the software to be tested, a heuristic algorithm is used to generate the basic data of the unit test cases; In response to an input operation by a user, basic data of the unit test case is obtained.
3. The method according to claim 1, It is characterized in that The basic data includes the name of the unit test method, class information, test object, input parameters and expected output results.
4. The method according to claim 3, It is characterized in that When the code language of the software to be tested is a dynamic call type, generating a unit test case after supplementing the context information based on the target data includes: Acquire and parse the target data to obtain the parameter type of the test object, the parameter type and parameter value of the input parameter; Constructing a corresponding first temporary variable based on the parameter type of the test object, and constructing a second temporary variable of the input parameter based on the parameter type and parameter value of the input parameter, wherein the temporary variables include the first temporary variable and the second temporary variable; The first temporary variable, the second temporary variable, and the name of the unit test method extracted from the target data, the class information, and the expected output result are combined to determine the unit test case.
5. The method according to claim 4, It is characterized in that Performing unit testing on the software to be tested based on the unit test case includes: Using the first temporary variable and the second temporary variable as actual parameters of the unit test method, executing the unit test method based on the name of the unit test method and the class information, and obtaining an output test result; The output test result is compared with the output expected effect to obtain a unit test result.
6. The method according to claim 1, It is characterized in that When the code language of the software to be tested is a non-dynamic call type, during the running of the software to be tested, the unit test case is generated after the context environment information is supplemented based on the target data, and the unit test is performed on the software to be tested based on the unit test case, including: The target data is converted into code format data corresponding to the code language of the software to be tested by a preset conversion module, and context environment code is added to the code format data to generate unit test cases in code format, and the unit test cases in code format are assembled and embedded into the code of the software to be tested; During the running of the software to be tested, unit testing is performed based on unit test cases in the code format.
7. The method according to any one of claims 1 to 6, It is characterized in that The preset format is a readable data format, and the target data supports at least one of the following operations: adding, deleting, and modifying.
8. A unit testing device, It is characterized in that include: Acquisition module, used to obtain basic data of unit test cases; A format module, used to express the basic data in a preset format to obtain target data; A test module, used for generating the unit test case after supplementing the context environment information with the corresponding target data according to the language characteristics of the software to be tested during the running of the software to be tested, and performing unit testing on the software to be tested based on the unit test case; Wherein, when the code language of the software to be tested is of a dynamic call type, the context environment includes temporary variables, and the unit test case is generated based on the temporary variables constructed by parsing the target data and the target data; When the software code language to be tested is of a non-dynamic call type, the context environment includes a context environment code, and the unit test case is generated based on converting the target data into corresponding code format data and adding the context environment code.
9. An electronic device, It is characterized in that The electronic device comprises: processor; a memory for storing instructions executable by the processor; The processor is used to read the executable instructions from the memory and execute the instructions to implement the unit testing method described in any one of claims 1-7.
10. A computer-readable storage medium, It is characterized in that The storage medium stores a computer program, and the computer program is used to execute the unit testing method described in any one of claims 1 to 7.
Citation Information
Patent Citations
Method and system for automatically generating unit test case of reproduced operation problem
CN101165658A
Test processing device and test processing method
CN102819488A
Application test method, apparatus and device
CN107368406A
Test case generation method and device and server
CN109726128A
Device and method for obtaining code test coverage rate
CN109992511A
