CN114996152A - Unit testing method, device, equipment and medium - Google Patents

Unit testing method, device, equipment and medium Download PDF

Info

Publication number
CN114996152A
CN114996152A CN202210692374.9A CN202210692374A CN114996152A CN 114996152 A CN114996152 A CN 114996152A CN 202210692374 A CN202210692374 A CN 202210692374A CN 114996152 A CN114996152 A CN 114996152A
Authority
CN
China
Prior art keywords
unit test
unit
software
test case
tested
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
CN202210692374.9A
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.)
Douyin Vision Beijing Co Ltd
Original Assignee
Douyin Vision Beijing 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 Douyin Vision Beijing Co Ltd filed Critical Douyin Vision Beijing Co Ltd
Priority to CN202210692374.9A priority Critical patent/CN114996152A/en
Publication of CN114996152A publication Critical patent/CN114996152A/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/3684Test management for test design, e.g. generating new test cases
    • 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

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Computer Hardware Design (AREA)
  • Quality & Reliability (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Debugging And Monitoring (AREA)

Abstract

The disclosed embodiments relate to a unit testing method, apparatus, device and medium, wherein the method comprises: acquiring basic data of a unit test case; expressing the basic data by adopting a preset format to obtain target data; in the running process of the software to be tested, a unit test case is generated after context environment information is supplemented based on the target data, and the unit test is executed on the software to be tested based on the unit test case. By adopting the technical scheme, the basic data required by the unit test case is obtained and is represented by adopting the preset format, the complete unit test case can be generated and the unit test can be executed in the software running process based on the basic data of the preset format, the iteration and maintenance are only needed to be carried out on the basic data of the unit test case during the iteration and maintenance, the code does not need to be modified, the iteration and maintenance cost of the unit test case is greatly reduced, the coverage rate and quality of the unit test are improved, and the iteration speed of the software is improved.

Description

Unit testing method, device, equipment and medium
Technical Field
The present disclosure relates to the field of software testing technologies, and in particular, to a unit testing method, apparatus, device, and medium.
Background
The unit test can find the software defects in the pre-stage of the development stage, can find problems in the early stage of the software development process, ensures the development quality of the software, avoids causing on-line accidents, and reduces the cost for solving the problems, so the unit test is an important step in the test process.
The unit test generally needs to manually write codes and design unit test cases, and the codes of the unit test cases need to be modified when the software codes change, so that the writing and maintenance costs are high. In the related art, the unit test case can be automatically generated at a code level, but the iteration and maintenance of the unit test case still need to be realized by modifying the code, a large amount of manpower is consumed, and the defect of high cost still exists.
Disclosure of Invention
In order to solve the technical problem, the present disclosure provides a unit testing method, apparatus, device and medium.
The embodiment of the disclosure provides a unit testing method, which comprises the following steps:
acquiring basic data of a unit test case;
expressing the basic data by adopting a preset format to obtain target data;
and in the running process of the software to be tested, generating the unit test case after supplementing context environment information based on the target data, and executing unit test on the software to be tested based on the unit test case.
The disclosed embodiment also provides a unit testing device, which comprises:
the acquisition module is used for acquiring basic data of the unit test case;
the format module is used for representing the basic data by adopting a preset format to obtain target data;
and the test module is used for generating the unit test case after supplementing the context environment information based on the target data in the running process of the software to be tested, and testing the unit execution unit of the software to be tested based on the unit test case.
An embodiment of the present disclosure further provides an electronic device, which includes: a processor; a memory for storing the processor-executable instructions; the processor is used for reading the executable instructions from the memory and executing the instructions to realize the unit testing method provided by the embodiment of the disclosure.
The embodiment of the present disclosure also provides a computer-readable storage medium, which stores a computer program for executing the unit testing method provided by the embodiment of the present disclosure.
Compared with the prior art, the technical scheme provided by the embodiment of the disclosure has the following advantages: the unit test scheme provided by the embodiment of the disclosure acquires basic data of a unit test case; expressing the basic data by adopting a preset format to obtain target data; in the running process of the software to be tested, a unit test case is generated after context environment information is supplemented based on the target data, and the unit test is executed on the software to be tested based on the unit test case. By adopting the technical scheme, the basic data required by the unit test case is obtained and is represented by adopting the preset format, the complete unit test case can be generated and the unit test can be executed after the context environment information is supplemented based on the basic data of the preset format in the software running process, the iteration and the maintenance are only needed to be carried out on the basic data of the unit test case without modifying codes, the iteration and the maintenance cost of the unit test case are greatly reduced, the coverage rate and the quality of the unit test are further improved, and the iteration speed of the software is improved.
Drawings
The above and other features, advantages and aspects of various embodiments of the present disclosure will become more apparent by referring to the following detailed description when taken in conjunction with the accompanying drawings. Throughout the drawings, the same or similar reference numbers refer to the same or similar elements. It should be understood that the drawings are schematic and that elements and features are not necessarily drawn to scale.
Fig. 1 is a schematic flow chart of a unit testing method according to an embodiment of the present disclosure;
FIG. 2 is a schematic flow chart diagram illustrating another method for unit testing provided by the embodiments of the present disclosure;
FIG. 3 is a schematic diagram of a unit test provided by an embodiment of the present disclosure;
FIG. 4 is a schematic structural diagram of a unit testing apparatus according to an embodiment of the present disclosure;
fig. 5 is a schematic structural diagram of an electronic device according to an embodiment of the present disclosure.
Detailed Description
Embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. While certain embodiments of the present disclosure are shown in the drawings, it is to be understood that the present disclosure may be embodied in various forms and should not be construed as limited to the embodiments set forth herein, but rather are provided for a more thorough and complete understanding of the present disclosure. It should be understood that the drawings and embodiments of the disclosure are for illustration purposes only and are not intended to limit the scope of the disclosure.
It should be understood that the various steps recited in the method embodiments of the present disclosure may be performed in a different order, and/or performed in parallel. Moreover, method embodiments may include additional steps and/or omit performing the illustrated steps. The scope of the present disclosure is not limited in this respect.
The term "include" and variations thereof as used herein are open-ended, i.e., "including but not limited to". The term "based on" is "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". Relevant definitions for other terms will be given in the following description.
It should be noted that the terms "first", "second", and the like in the present disclosure are only used for distinguishing different devices, modules or units, and are not used for limiting the order or interdependence relationship of the functions performed by the devices, modules or units.
It is noted that references to "a", "an", and "the" modifications in this disclosure are intended to be illustrative rather than limiting, and that those skilled in the art will recognize that "one or more" may be used unless the context clearly dictates otherwise.
The names of messages or information exchanged between devices in the embodiments of the present disclosure are for illustrative purposes only, and are not intended to limit the scope of the messages or information.
The unit test generally needs to manually compile codes and design unit test cases, when the code design is complex, the codes need to be reconstructed before the unit test codes are compiled, and in addition, the unit test codes need to be modified when the original codes are changed, so that the maintenance cost is high, and the quick requirement iteration conflicts. For example, taking a native test suite XCTest provided by an IOS system as an example, in the process of writing and maintaining a unit test case, a developer needs to provide a context environment, declare and construct input and output variables, call a test method, compare output results, and the like for the unit test case, and for the developer, the developer usually only concerns about the input and output of a method to be tested, but does not concern about how the input variables are constructed and how the method is called, so that the current scheme inevitably needs to write many redundant codes, and the write and maintenance costs of the unit test case are inevitably increased by the redundant codes. In addition, all the current unit test cases need to be specially written and maintained by developers, in the process of quick iteration of software requirements, high-quality unit test is required to be guaranteed, and iteration and maintenance of the unit test cases consume a large amount of manpower, so that the unit test cases become the tragus stones blocking quick iteration. In the related art, the unit test case can be automatically generated at a code level, but the iteration and maintenance of the unit test case still need to be realized by modifying the code, a large amount of manpower is consumed, and the defect of high cost still exists.
In order to solve the above problem, embodiments of the present disclosure provide a unit testing method, which is described below with reference to specific embodiments.
Fig. 1 is a schematic flow chart of a unit testing method provided by an embodiment of the present disclosure, which may be executed by a unit testing apparatus, where the apparatus may be implemented by software and/or hardware, and may be generally integrated in an electronic device. As shown in fig. 1, the method includes:
step 101, acquiring basic data of a unit test case.
The unit test case may be understood as a code or a specific set written for effectively verifying the characteristics of the software unit and discovering the defects of the software unit code, and the specific set may include a case name, a context environment, a test object, a test purpose, input data, an expected result, and the like. The software units can be functions, methods, modules and the like which form software, one piece of software is generally formed by a plurality of software units, each software unit respectively realizes a determined function, and the software units are mutually called with other software units through input and output interfaces provided by the software units to jointly realize the whole function of the software. The unit testing method of the embodiments of the present disclosure may be performed by a unit testing suite, which may be understood as a comprehensive functional module capable of performing unit testing on one or more software.
The basic data can be necessary data required by the unit test case during testing and also data concerned by developers aiming at the unit test case, and the basic data in the embodiment of the disclosure can comprise the name, class information, a test object, input parameters and an output expected result of the unit test method; the class information can be a class to which the unit test method belongs, the class information is provided when the code language of the software to be tested is an Objective-C code language, and the class information can be adaptively modified into other functions or upper modules when the code language of the software to be tested is other languages; the test object can be an object for calling a unit test method, the input parameters correspond to the input variables of the specific software unit, and the output expected result corresponds to the output variables of the software unit.
In some embodiments, obtaining the basic data of the unit test case may include at least one of: acquiring software running data of the software to be tested through an acquisition module, and analyzing the software running data to obtain basic data of the unit test case; generating basic data of the unit test case by adopting a heuristic algorithm based on the basic data of the existing unit test case of the software to be tested; and responding to the input operation of the user, and acquiring basic data of the unit test case.
The acquisition module can be a functional module used for acquiring software running data, the acquisition module is arranged in the unit test suite, the software running data can be data generated by different software units during daily software running, and the specific data type is not limited. The heuristic algorithm may be an algorithm constructed based on intuition or experience, and gives a feasible solution to each instance of the combinatorial optimization problem to be solved in an acceptable calculation time and space, in the embodiment of the present disclosure, the base data of the new unit test case may be generated by the heuristic algorithm, and the heuristic algorithm may include a plurality of algorithms, for example, the heuristic algorithm may include a genetic algorithm, a simulated annealing algorithm, an ant colony algorithm, and the like.
When the unit testing device acquires the basic data of the unit testing case, one mode can acquire the software running data of the software to be tested through the acquisition module and acquire the basic data of the unit testing case through analyzing 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 obj C _ msgSend function, acquire the register and stack context data of the OC method, and analyze the context data to acquire the actual parameter list and the result of the OC method, namely the basic data of the unit testing case; for static languages such as C/C + + and the like, similar effects can be achieved through a pile inserting mode; in another mode, based on the basic data of the existing unit test cases of the software to be tested, a heuristic algorithm is adopted to generate the basic data of the new unit test cases, for example, the heuristic algorithm is taken as a genetic algorithm, the basic data of each existing unit test case is taken as a gene segment, the coverage rate is taken as the fitness, and the basic data of the new unit test cases are continuously generated through the behaviors of random generation, hybridization, mutation and the like of genetic factors; in another mode, the basic data of the unit test case input by the user can be obtained in response to the input operation of the user, the user is a developer, that is, the developer can manually input the basic data of the unit test case, and the specific input mode is not limited, for example, the basic data can be directly written by the developer, and can also be input through a preset interactive system.
The three modes can achieve the acquisition of the basic data of the unit test case, and the efficiency and diversity of the basic data acquisition are improved.
And 102, representing the basic data by adopting a preset format to obtain target data.
The preset format may be a universal data specification or a data format, and in the embodiment of the present disclosure, the preset format may be a data format with readability, that is, readability is strong, which is beneficial for maintenance and iteration, and a specific format of the preset format may be set according to an actual situation, and is not limited specifically, for example, the preset format may be set to be a JSON format or a table format. The target data can be converted into basic data in a preset format, and the target data supports at least one of the following operations: addition, deletion and modification.
In the embodiment of the present disclosure, after the unit testing device obtains the basic data of the unit testing case, the unit testing device may use a preset format to represent the basic data to obtain corresponding target data.
For example, when the preset format is JSON format, the target data may be expressed as follows:
{
Figure BDA0003700628390000071
Figure BDA0003700628390000081
in the scheme, basic data of the unit test case is expressed by adopting a preset format, on one hand, a universal data storage format is convenient to store to the database, for example, when the preset format is a JSON format, a plurality of non-relational databases based on document storage support the JSON format as the data storage format, such as MongoDB, CouchDB, RavenDB and the like; on the other hand, target data based on a preset format, such as target data based on a JSON format, can quickly retrieve all test cases belonging to classes and methods, and conveniently and selectively perform local unit test on certain classes and methods.
And 103, in the running process of the software to be tested, generating a unit test case after context environment information is supplemented based on the target data, and testing the execution unit of the software to be tested based on the unit test case.
The software to be tested may be any software that needs to perform unit testing, and the type and the number of the software to be tested are not limited in the embodiment of the present disclosure.
In the embodiment of the disclosure, the unit testing device may run the software to be tested after representing the basic data by using a preset format, obtaining the target data and storing the target data, and may automatically obtain the corresponding target data according to the language characteristics of the software to be tested in the running process of the software to be tested, generate a complete unit testing case after analyzing the target data to supplement the context environment information required by the unit testing, execute the unit testing method of each software unit to obtain the output testing result, and then compare the output expected result with the output testing result according to the output expected result in the target data to determine whether the output expected result meets the expectation, so as to obtain the unit testing result.
The unit test scheme can be a code-free automatic execution unit test scheme, basic data in a preset format can be automatically acquired in the software running process, unit test cases are generated, the unit test cases are dynamically injected, the compiling and maintenance cost of the unit test cases is reduced, developers are helped to save labor, and the unit test coverage rate and quality are improved.
The unit test scheme provided by the embodiment of the disclosure acquires basic data of a unit test case; expressing the basic data by adopting a preset format to obtain target data; in the running process of the software to be tested, a unit test case is generated after context environment information is supplemented based on the target data, and the unit test is executed on the software to be tested based on the unit test case. By adopting the technical scheme, the basic data required by the unit test case is obtained and is represented by adopting the preset format, the complete unit test case can be generated and the unit test can be executed after the context environment information is supplemented based on the basic data of the preset format in the software running process, the iteration and the maintenance are only needed to be carried out on the basic data of the unit test case without modifying codes, the iteration and the maintenance cost of the unit test case are greatly reduced, the coverage rate and the quality of the unit test are further improved, and the iteration speed of the software is improved.
For example, fig. 2 is a flowchart illustrating another unit testing method provided in the embodiment of the present disclosure, as shown in fig. 2, in a possible implementation manner, when the code language of the software to be tested is a dynamic call type, the context environment information includes a temporary variable, and the step 103 may include the following steps:
step 201, obtaining and analyzing the target data, and obtaining the parameter type of the test object, the parameter type of the input parameter, and the parameter value.
The code language of the software to be tested is a dynamic call type, which indicates that functions, methods and the like in the code of the software to be tested can be mutually dynamically called, for example, Objective-C language and Python language and the like all belong to dynamic call types. The test object can be an object for calling a unit test method and also belongs to a parameter; the input parameters correspond to input variables of a particular software cell.
When the unit testing device generates a unit testing case after context environment information is supplemented based on target data in the running process of software to be tested, whether a code language of the software to be tested is a dynamic calling type or not can be judged firstly, if yes, the required target data can be obtained from a database based on language characteristics, namely, the target data is obtained and analyzed based on functions and the like under the code language, and the parameter type of a testing object, the parameter type of an input parameter and a parameter value are 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 Objective-C language.
Step 202, 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 the parameter value of the input parameter.
The temporary variable may be a variable required for executing the unit test, a block of memory needs to be allocated in the memory for storing parameters during the test when the software executes, the temporary variable may be a variable corresponding to the block of memory, and the meaning of constructing the context environment is to construct the temporary variable. The temporary variables may include a first temporary variable and a second temporary variable. The first temporary variable may refer to a temporary variable of the test object, and the second temporary variable may be a temporary variable of the input parameter, distinguished by the first and second.
Specifically, the unit testing device may construct a corresponding first temporary variable according to the parameter type of the test object, and may construct a corresponding second temporary variable according to the parameter type and the parameter value of the input parameter, and the construction manner may adopt a related technique, for example, when the parameter type of the test object is an int type, an int variable may be constructed.
And step 203, combining and determining the first temporary variable, the second temporary variable, the name and the class information of the unit test method extracted from the target data and the output expected result as a unit test case.
And step 204, taking the first temporary variable and the second temporary variable as actual parameters of the unit test method, and executing the unit test method based on the name and the class information of the unit test method to obtain an output test result.
The argument may be a parameter transmitted when a function or a method is called, and may be a constant, a variable, an expression, or the like. And outputting a test result which can be a result obtained by the current test.
Specifically, after the unit testing device generates the unit testing case, when the unit testing device tests the software execution unit to be tested based on the unit testing case, the first temporary variable and the second temporary variable may be used as actual parameters of the unit testing method, and the unit testing method is obtained based on the name and class information of the unit testing method, automatically transferred to the unit testing method, and executed, so as to obtain an output testing result.
And step 205, comparing the output test result with the output pre-starting result to obtain a unit test result.
Wherein the unit test result may be a result characterizing whether the unit test meets expectations.
In the embodiment of the disclosure, after the unit testing device obtains the output test result, it may determine whether the output expected result in the target data is in line with the output test result according to comparison between the output expected result and the output test result, and then obtain the unit test result.
In the scheme, for the code language of the dynamic calling type, context environment information required by unit testing can be automatically supplemented based on language characteristics, the unit testing collection result is executed, the unit testing efficiency is improved, and the compiling and maintaining cost of the unit testing case is reduced.
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 context environment code, in the running process of the software to be tested, generating a unit test case after supplementing the context environment information based on the target data, and performing unit test on the software to be tested based on the unit test case may include: converting the target data into code format data corresponding to a code language of the software to be tested through a preset conversion module, adding context environment codes into the code format data to generate unit test cases in a code format, and compiling the unit test cases in the code format to be embedded into codes of the software to be tested; and in the running process of the software to be tested, executing unit test by using the unit test case based on the code format.
The code language of the software to be tested is a non-dynamic calling type, which means that functions, methods and the like in the code of the software to be tested do not support dynamic calling, for example, C language, C + + language and the like all belong to the non-dynamic calling type. The preset conversion module can be a functional module used for performing language conversion on target data and supplementing a context environment code to generate a unit test case, is set for software to be tested with a code language of a non-dynamic calling type, and can be a functional module of a black box for developers of the unit test case, so that the maintenance of the developers on the unit test case is not influenced.
Specifically, when the code language of the software to be tested is a non-dynamic call type, the unit testing device may convert the target data into code format data that is the same as or an intermediate code as the code language of the software to be tested through a preset conversion module, add a context environment code to the code format data to obtain a unit test case in a code format, compile an algorithm of a code of the software to be tested, and temporarily embed the unit test case in the code format into the software, so as to automatically execute the unit test based on the unit test case in the code format during the running process of the software to be tested.
In the above scheme, for the code language of the non-dynamic calling type, the unit test case in the code form can be automatically generated by the conversion module and is internally packaged in the code of the software to be tested,
aiming at a non-dynamic language, a set of scheme for automatically generating test codes based on input and output of test cases can be designed, and test case writers can not care about the test codes and only care about the input and output of the test cases. The automatically generated test codes are internally packaged, only the basic data of the unit test case needs to be iterated and maintained during iteration and maintenance, the codes do not need to be modified, and the iteration and maintenance cost of the unit test case is reduced.
The unit test method of the embodiments of the present disclosure is further illustrated by a specific example. Fig. 3 is a schematic diagram of a unit test provided by an embodiment of the present disclosure, and as shown in fig. 3, a complete unit test process is shown in the diagram, a part without filling in the diagram represents a flow that needs manual intervention of a developer, and a part with gray filling represents a flow that can implement automation, which may specifically include: the basic data of the unit test case can be obtained in three ways in the figure, including: a developer inputs and collects software operation data, analyzes the software operation data and automatically generates a heuristic algorithm, and then target data obtained by expressing basic data in a standard-meeting, namely a preset format can be stored in a unit test case database; aiming at software to be tested needing unit testing, target data can be automatically obtained in the running process of the software to be tested, and unit test cases generated after context environment information is supplemented based on the target data are automatically tested aiming at the software to be tested periodically; according to the collected unit test results of the unit test cases, calculating the passing rate of the unit test cases, and collecting the code coverage rate of unit test execution; after the unit test is executed, a software developer can check the unit test case which fails to execute, evaluate whether the software meets the requirements of a new version of the software, and finally evaluate the software quality according to the passing rate of the unit test case.
In the scheme, by providing a codeless automatic unit test suite, basic data of a unit test case is generated by automatically acquiring on-line software running data, random basic data and manual part input are generated by combining a heuristic algorithm, the basic data required by the unit test case can be comprehensively covered, then, the context environment information is automatically supplemented to generate the unit test case and execute the unit test, for developers, the unit test can be performed only by inputting based on an appointed format, the writing of unit test codes and the dependence on a native code test suite are not needed, the writing cost of the unit test case is reduced, the capability of automatically generating the unit test case is provided, only the basic data of the unit test case needs to be iterated and maintained during iteration and maintenance, codes do not need to be modified, and the manpower and material resources for unit test case maintenance and iterator can be effectively saved, the iteration and maintenance cost of the unit test case is greatly reduced, the coverage rate and quality of software unit test can be effectively improved, and the software quality is effectively guaranteed.
Fig. 4 is a schematic structural diagram of a unit testing apparatus provided in an embodiment of the present disclosure, where the apparatus may be implemented by software and/or hardware, and may be generally integrated in an electronic device. As shown in fig. 4, the apparatus includes:
an obtaining module 401, configured to obtain basic data of a unit test case;
a format module 402, configured to represent the basic data in a preset format to obtain target data;
the testing module 403 is configured to generate the unit test case after supplementing the context information based on the target data in the running process of the software to be tested, and perform unit testing on the software to be tested based on the unit test case.
Optionally, the obtaining module 401 includes at least one of:
the first unit is used for acquiring software running data of the software to be tested through an acquisition module and analyzing the software running data to obtain basic data of the unit test case;
the second unit is used for generating the basic data of the unit test case by adopting a heuristic algorithm based on the basic data of the existing unit test case of the software to be tested;
and the third unit is used for responding to the input operation of the user and acquiring the basic data of the unit test case.
Optionally, the basic data includes names of unit test methods, class information, test objects, input parameters, and output expected results.
Optionally, when the code language of the software to be tested is a dynamic call type, the context environment information includes a temporary variable, and the test module 403 includes a generation unit configured to:
acquiring and analyzing the target data to acquire the parameter type of the test object, the parameter type of the input parameter and the parameter value;
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 the parameter value of the input parameter, wherein the temporary variable comprises the first temporary variable and the second temporary variable;
and combining the first temporary variable, the second temporary variable, the name of the unit test method extracted from the target data, the class information and the output expected result to determine the unit test case.
Optionally, the test module 403 includes an execution unit, configured to:
taking the first temporary variable and the second temporary variable as actual parameters of the unit testing method, and executing the unit testing method based on the name of the unit testing method and the class information to obtain an output testing result;
and comparing the output test result with the output pre-starting result to obtain a unit test result.
Optionally, when the code language of the software to be tested is a non-dynamic call type, the context environment information is a context environment code, and the test module 403 is configured to:
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 context environment codes into the code format data to generate unit test cases in a code format, and compiling the unit test cases in the code format to be embedded into the codes of the software to be tested;
and executing unit test based on the unit test case of the code format in the running process of the software to be tested.
Optionally, the preset format is a data format with readability, and the target data supports at least one of the following operations: addition, deletion and modification.
The unit testing device provided by the embodiment of the disclosure can execute the unit testing method provided by any embodiment of the disclosure, and has corresponding functional modules and beneficial effects of the execution method.
Embodiments of the present disclosure also provide a computer program product comprising a computer program/instructions, which when executed by a processor, provides a unit testing method according to any of the embodiments of the present disclosure.
Fig. 5 is a schematic structural diagram of an electronic device according to an embodiment of the present disclosure. Referring now specifically to fig. 5, a schematic block diagram of an electronic device 500 suitable for use in implementing embodiments of the present disclosure is shown. The electronic device 500 in the disclosed embodiment may include, but is not limited to, a mobile terminal such as a mobile phone, a notebook computer, a digital broadcast receiver, a PDA (personal digital assistant), a PAD (tablet computer), a PMP (portable multimedia player), a vehicle terminal (e.g., a car navigation terminal), and the like, and a stationary terminal such as a digital TV, a desktop computer, and the like. The electronic device shown in fig. 5 is only an example, and should not bring any limitation to the functions and the scope of use of the embodiments of the present disclosure.
As shown in fig. 5, electronic device 500 may include a processing means (e.g., central processing unit, graphics processor, etc.) 501 that may perform various appropriate actions and processes in accordance with a program stored in a Read Only Memory (ROM)502 or a program loaded from a storage means 508 into a Random Access Memory (RAM) 503. In the RAM 503, various programs and data necessary for the operation of the electronic apparatus 500 are also stored. The processing device 501, the ROM 502, and the RAM 503 are connected to each other through a bus 504. An input/output (I/O) interface 505 is also connected to bus 504.
Generally, the following devices may be connected to the I/O interface 505: input devices 506 including, for example, a touch screen, touch pad, keyboard, mouse, camera, microphone, accelerometer, gyroscope, etc.; output devices 507 including, for example, a Liquid Crystal Display (LCD), speakers, vibrators, and the like; storage devices 508 including, for example, magnetic tape, hard disk, etc.; and a communication device 509. The communication means 509 may allow the electronic device 500 to communicate with other devices wirelessly or by wire to exchange data. While fig. 5 illustrates an electronic device 500 having various means, it is to be understood that not all illustrated means are required to be implemented or provided. More or fewer devices may alternatively be implemented or provided.
In particular, according to an embodiment of the present disclosure, the processes described above with reference to the flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program product comprising a computer program carried on a non-transitory computer readable medium, the computer program containing program code for performing the method illustrated by the flow chart. In such an embodiment, the computer program may be downloaded and installed from a network via the communication means 509, or installed from the storage means 508, or installed from the ROM 502. The computer program performs the above-described functions defined in the unit test method of the embodiments of the present disclosure when executed by the processing device 501.
It should be noted that the computer readable medium in the present disclosure can be a computer readable signal medium or a computer readable storage medium or any combination of the two. A computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples of the computer readable storage medium may include, but are not limited to: an electrical connection having 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. In the present disclosure, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In contrast, in the present disclosure, a computer readable signal medium may comprise a propagated data signal with computer readable program code embodied therein, either in baseband or as part of a carrier wave. Such a propagated data signal may take any of a variety of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: electrical wires, optical cables, RF (radio frequency), etc., or any suitable combination of the foregoing.
In some embodiments, the clients, servers may communicate using any currently known or future developed network Protocol, such as HTTP (HyperText Transfer Protocol), and may interconnect with any form or medium of digital data communication (e.g., a communications network). Examples of communication networks include a local area network ("LAN"), a wide area network ("WAN"), the Internet (e.g., the Internet), and peer-to-peer networks (e.g., ad hoc peer-to-peer networks), as well as any currently known or future developed network.
The computer readable medium may be embodied in the electronic device; or may be separate and not incorporated into the electronic device.
The computer readable medium carries one or more programs which, when executed by the electronic device, cause the electronic device to: acquiring basic data of a unit test case; expressing the basic data by adopting a preset format to obtain target data; and in the running process of the software to be tested, generating the unit test case after supplementing context environment information based on the target data, and executing unit test on the software to be tested based on the unit test case.
Computer program code for carrying out operations for the present disclosure may be written in any combination of one or more programming languages, including but not limited to an object oriented programming language such as Java, Smalltalk, C + +, and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the case of 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 the connection may be made to an external computer (for example, through the Internet using an Internet service provider).
The flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present disclosure. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and/or flowchart illustration, and combinations of blocks in the block diagrams and/or flowchart illustration, can be implemented by special purpose hardware-based systems that perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The units described in the embodiments of the present disclosure may be implemented by software or hardware. Where the name of an element does not in some cases constitute a limitation on the element itself.
The functions described herein above 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 a chip (SOCs), Complex Programmable Logic Devices (CPLDs), and the like.
In the context of this disclosure, a machine-readable 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 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, 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.
The foregoing description is only exemplary of the preferred embodiments of the disclosure and is illustrative of the principles of the technology employed. It will be appreciated by those skilled in the art that the scope of the disclosure herein is not limited to the particular combination of features described above, but also encompasses other embodiments in which any combination of the features described above or their equivalents does not depart from the spirit of the disclosure. For example, the above features and the technical features disclosed in the present disclosure (but not limited to) having similar functions are replaced with each other to form the technical solution.
Further, while operations are depicted in a particular order, this should not be understood as requiring that such operations be performed in the particular order shown or in sequential order. Under certain circumstances, multitasking and parallel processing may be advantageous. Likewise, while several specific implementation details are included in the above discussion, these should not be construed as limitations on the scope of the disclosure. Certain features that are described in the context of separate embodiments can also be implemented in combination in a single embodiment. Conversely, various features that are described in the context of a single embodiment can also be implemented in multiple embodiments separately or in any suitable subcombination.
Although the subject matter has been described in language specific to structural features and/or methodological acts, it is to be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or acts described above. Rather, the specific features and acts described above are disclosed as example forms of implementing the claims.

Claims (10)

1. A method of unit testing, comprising:
acquiring basic data of a unit test case;
expressing the basic data by adopting a preset format to obtain target data;
and in the running process of the software to be tested, generating the unit test case after supplementing context environment information based on the target data, and executing unit test on the software to be tested based on the unit test case.
2. The method of claim 1, wherein obtaining the base data of the unit test case comprises at least one of:
acquiring software running data of the software to be tested through an acquisition module, and analyzing the software running data to obtain basic data of the unit test case;
generating basic data of the unit test cases by adopting a heuristic algorithm based on the basic data of the existing unit test cases of the software to be tested;
and responding to the input operation of the user, and acquiring basic data of the unit test case.
3. The method of claim 1, wherein the base data includes a name of a unit test method, class information, a test object, input parameters, and an output expected result.
4. The method of claim 3, wherein when the code language of the software under test is a dynamic call type, the context information includes a temporary variable, and generating a unit test case after supplementing the context information based on the target data comprises:
acquiring and analyzing the target data to acquire the parameter type of the test object, the parameter type of the input parameter and the parameter value;
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 the parameter value of the input parameter, wherein the temporary variable comprises the first temporary variable and the second temporary variable;
and combining the first temporary variable, the second temporary variable, the name of the unit test method extracted from the target data, the class information and the output expected result to determine the unit test case.
5. The method of claim 4, wherein performing unit testing on the software under test based on the unit test cases comprises:
taking the first temporary variable and the second temporary variable as actual parameters of the unit testing method, and executing the unit testing method based on the name of the unit testing method and the class information to obtain an output testing result;
and comparing the output test result with the output pre-starting result to obtain a unit test result.
6. The method of claim 1, wherein when the code language of the software to be tested is a non-dynamic call type, and the context environment information is context environment code, in a process of running the software to be tested, generating the unit test case after supplementing the context environment information based on the target data, and performing unit test on the software to be tested based on the unit test case includes:
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 context environment codes into the code format data to generate unit test cases in a code format, and compiling the unit test cases in the code format to be embedded into the codes of the software to be tested;
and executing unit test based on the unit test case of the code format in the running process of the software to be tested.
7. The method according to any one of claims 1-6, wherein the predetermined format is a data format with readability, and the target data supports at least one of: addition, deletion and modification.
8. A unit testing apparatus, comprising:
the acquisition module is used for acquiring basic data of the unit test case;
the format module is used for representing the basic data by adopting a preset format to obtain target data;
and the test module is used for generating the unit test case after supplementing the context environment information based on the target data in the running process of the software to be tested, and testing the unit execution unit of the software to be tested based on the unit test case.
9. An electronic device, characterized in that the electronic device comprises:
a processor;
a memory for storing the processor-executable instructions;
the processor is configured to read the executable instructions from the memory and execute the instructions to implement the unit testing method of any of claims 1-7.
10. A computer-readable storage medium, characterized in that the storage medium stores a computer program for performing the unit testing method of any of the preceding claims 1-7.
CN202210692374.9A 2022-06-17 2022-06-17 Unit testing method, device, equipment and medium Pending CN114996152A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210692374.9A CN114996152A (en) 2022-06-17 2022-06-17 Unit testing method, device, equipment and medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210692374.9A CN114996152A (en) 2022-06-17 2022-06-17 Unit testing method, device, equipment and medium

Publications (1)

Publication Number Publication Date
CN114996152A true CN114996152A (en) 2022-09-02

Family

ID=83034778

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210692374.9A Pending CN114996152A (en) 2022-06-17 2022-06-17 Unit testing method, device, equipment and medium

Country Status (1)

Country Link
CN (1) CN114996152A (en)

Citations (11)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101165658A (en) * 2006-10-18 2008-04-23 国际商业机器公司 Method and system for automatically generating unit test case of reproduced operation problem
US20080120602A1 (en) * 2006-11-21 2008-05-22 Microsoft Corporation Test Automation for Business Applications
CN102819488A (en) * 2012-06-29 2012-12-12 用友软件股份有限公司 Test processing device and test processing method
US20150007138A1 (en) * 2013-06-26 2015-01-01 Sap Ag Method and system for incrementally updating a test suite utilizing run-time application executions
CN107368406A (en) * 2016-05-11 2017-11-21 阿里巴巴集团控股有限公司 The method, apparatus and equipment of test application program
US20180210823A1 (en) * 2017-01-25 2018-07-26 Wipro Limited System and method for performing script-less unit testing
CN109726128A (en) * 2018-12-29 2019-05-07 亚信科技(中国)有限公司 A kind of method for generating test case, device and server
CN109992511A (en) * 2019-03-25 2019-07-09 口碑(上海)信息技术有限公司 Obtain the device and method of code tester coverage rate
US20200285563A1 (en) * 2019-03-08 2020-09-10 Fujitsu Limited Generating inputs for computer-program testing
CN113778849A (en) * 2021-03-15 2021-12-10 北京沃东天骏信息技术有限公司 Method, apparatus, device and storage medium for testing code
CN114168454A (en) * 2021-11-23 2022-03-11 叶嵩 Asynchronous testing method based on dynamic pile inserting-pile pinning technology

Patent Citations (11)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101165658A (en) * 2006-10-18 2008-04-23 国际商业机器公司 Method and system for automatically generating unit test case of reproduced operation problem
US20080120602A1 (en) * 2006-11-21 2008-05-22 Microsoft Corporation Test Automation for Business Applications
CN102819488A (en) * 2012-06-29 2012-12-12 用友软件股份有限公司 Test processing device and test processing method
US20150007138A1 (en) * 2013-06-26 2015-01-01 Sap Ag Method and system for incrementally updating a test suite utilizing run-time application executions
CN107368406A (en) * 2016-05-11 2017-11-21 阿里巴巴集团控股有限公司 The method, apparatus and equipment of test application program
US20180210823A1 (en) * 2017-01-25 2018-07-26 Wipro Limited System and method for performing script-less unit testing
CN109726128A (en) * 2018-12-29 2019-05-07 亚信科技(中国)有限公司 A kind of method for generating test case, device and server
US20200285563A1 (en) * 2019-03-08 2020-09-10 Fujitsu Limited Generating inputs for computer-program testing
CN109992511A (en) * 2019-03-25 2019-07-09 口碑(上海)信息技术有限公司 Obtain the device and method of code tester coverage rate
CN113778849A (en) * 2021-03-15 2021-12-10 北京沃东天骏信息技术有限公司 Method, apparatus, device and storage medium for testing code
CN114168454A (en) * 2021-11-23 2022-03-11 叶嵩 Asynchronous testing method based on dynamic pile inserting-pile pinning technology

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
FWEIGL: "Getting context in AndroidTestCase or InstrumentationTestCase in Android Studio\'s Unit Test feature", Retrieved from the Internet <URL:https://stackoverflow.com/questions/28960898/getting-context-in-androidtestcase-or-instrumentationtestcase-in-android-studio> *

Similar Documents

Publication Publication Date Title
CN111475298B (en) Task processing method, device, equipment and storage medium
CN111625473B (en) Interface test case generation method and device, storage medium and electronic equipment
CN111897740B (en) User interface testing method and device, electronic equipment and computer readable medium
CN110879776A (en) Test case generation method and device
CN105630683A (en) Cloud testing architecture
CN111309304B (en) Method, device, medium and electronic equipment for generating IDL file
CN113392018B (en) Traffic distribution method and device, storage medium and electronic equipment
CN115203004A (en) Code coverage rate testing method and device, storage medium and electronic equipment
CN112596738B (en) Method and device for determining front-end page to be tested, storage medium and electronic equipment
CN116225886A (en) Test case generation method, device, equipment, storage medium and program product
CN111625291B (en) Automatic iteration method and device for data processing model and electronic equipment
CN112416303A (en) Software development kit thermal restoration method and device and electronic equipment
CN110689285A (en) Test method, test device, electronic equipment and computer readable storage medium
CN111367791B (en) Method, device, medium and electronic equipment for generating test case
CN114996152A (en) Unit testing method, device, equipment and medium
US8615744B2 (en) Methods and system for managing assets in programming code translation
CN111538657B (en) Page overdrawing detection method and device, electronic equipment and readable medium
CN116149978A (en) Service interface testing method and device, electronic equipment and storage medium
CN112286808A (en) Application program testing method and device, electronic equipment and medium
CN111309323A (en) Parameter initialization method and device and electronic equipment
CN114036053A (en) Test method, test device, readable medium and electronic equipment
CN116467178B (en) Database detection method, apparatus, electronic device and computer readable medium
CN111581331B (en) Method, device, electronic equipment and computer readable medium for processing text
CN111367555B (en) Assertion method, assertion device, electronic equipment and computer readable medium
CN111562913B (en) Method, device and equipment for pre-creating view component and computer readable 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