CN117609064A - Unit test method and device, electronic equipment and storage medium - Google Patents

Unit test method and device, electronic equipment and storage medium Download PDF

Info

Publication number
CN117609064A
CN117609064A CN202311642872.3A CN202311642872A CN117609064A CN 117609064 A CN117609064 A CN 117609064A CN 202311642872 A CN202311642872 A CN 202311642872A CN 117609064 A CN117609064 A CN 117609064A
Authority
CN
China
Prior art keywords
test
class
target
unit
target test
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
CN202311642872.3A
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.)
Agricultural Bank of China
Original Assignee
Agricultural Bank of China
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 Agricultural Bank of China filed Critical Agricultural Bank of China
Priority to CN202311642872.3A priority Critical patent/CN117609064A/en
Publication of CN117609064A publication Critical patent/CN117609064A/en
Pending legal-status Critical Current

Links

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/3688Test management for test execution, e.g. scheduling of test suites
    • 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/3692Test management for test results analysis

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 embodiment of the invention discloses a unit testing method, a unit testing device, electronic equipment and a storage medium. The method comprises the following steps: determining a class object corresponding to a unit test instruction in response to the unit test instruction, wherein the class object is an object created by a target test class; determining at least one candidate test method defined in the target test class based on the class object, and selecting a target test method corresponding to the test instruction from the at least one candidate test method; generating a test case corresponding to the unit test instruction based on the target test method; after the test input data input for the test case is obtained, testing the function of the target test class based on the test case and the test input data. According to the technical scheme provided by the embodiment of the invention, the automatic unit test is carried out on the class to be tested, so that the test cost is reduced, the test efficiency is improved, and the software development efficiency is improved.

Description

Unit test method and device, electronic equipment and storage medium
Technical Field
The embodiment of the invention relates to the technical field of computers, in particular to a unit testing method and device, electronic equipment and a storage medium.
Background
Unit testing (unit testing) refers to checking and verifying the smallest testable unit in software. For projects developed in the Java language, unit testing is typically deployed for classes that need to be tested. In the related art, unit testing is generally performed on a class to be tested using test cases written by a developer. However, this test method has the technical problems of higher test cost and lower test efficiency, thereby affecting the software development efficiency.
Disclosure of Invention
The invention provides a unit testing method, a unit testing device, electronic equipment and a storage medium, which are used for realizing automatic unit testing of a class to be tested, reducing testing cost, improving testing efficiency and further improving software development efficiency.
According to an aspect of the present invention, there is provided a unit testing method, the method comprising:
determining a class object corresponding to a unit test instruction in response to the unit test instruction, wherein the class object is an object created by a target test class;
Determining at least one candidate test method defined in the target test class based on the class object, and selecting a target test method corresponding to the test instruction from the at least one candidate test method;
generating a test case corresponding to the unit test instruction based on the target test method;
after the test input data input for the test case is obtained, testing the function of the target test class based on the test case and the test input data.
Optionally, the determining, based on the class object, at least one candidate test method defined in the target test class includes: based on the Class object, loading the Class of the target test Class by adopting a reflection mechanism; and analyzing the Class to obtain at least one candidate test method defined in the target test Class.
Optionally, the generating, based on the target test method, a test case corresponding to the unit test instruction includes: determining a method calling relation chain among the target test methods under the condition that the number of the target test methods is a plurality of; and generating a test case corresponding to the unit test instruction based on the method calling relation chain.
Optionally, the method further comprises: and aiming at each target test method, storing the target test method and a method calling relation chain corresponding to the target test method into a preset XML file.
Optionally, the method further comprises: determining a preset virtual object corresponding to the association method under the condition that the association method defined in other classes except the target test class exists in the method body of the target test method, wherein the preset virtual object has the function of the association method; the testing the function of the target test class based on the test case and the test input data includes: and testing the function of the target test class based on the test case, the test input data and the preset virtual object.
Optionally, after testing the function of the target test class, the method further comprises: obtaining a test output result corresponding to the test input data; acquiring an expected output result corresponding to the test input data, which is set for the test case; and verifying the function of the target test class based on the test output result and the expected output result, and taking the verification result as a unit test result aiming at the target test class.
Optionally, the method further comprises: determining at least one member variable defined in the target test class based on the class object; and saving the at least one member variable and the at least one candidate test method as the structural information of the target test class into a predefined class structure body for saving the target test class.
According to another aspect of the present invention, a unit testing apparatus is provided. The device comprises:
the class object determining module is used for responding to the unit test instruction and determining a class object corresponding to the unit test instruction, wherein the class object is an object created by a target test class;
a test method determining module, configured to determine at least one candidate test method defined in the target test class based on the class object, and select a target test method corresponding to the test instruction from the at least one candidate test method;
the test case generation module is used for generating a test case corresponding to the unit test instruction based on the target test method;
and the unit test module is used for testing the function of the target test class based on the test case and the test input data after the test input data input for the test case is acquired.
According to another aspect of the present invention, there is provided an electronic apparatus including:
at least one processor; and
a memory communicatively coupled to the at least one processor; wherein,
the memory stores a computer program executable by the at least one processor to enable the at least one processor to perform the unit testing method according to any one of the embodiments of the present invention.
According to another aspect of the present invention, there is provided a computer readable storage medium storing computer instructions for causing a processor to execute a unit testing method according to any one of the embodiments of the present invention.
According to the technical scheme, after a unit test instruction is received, a class object corresponding to the unit test instruction is determined, wherein the class object is an object created by a target test class; determining at least one candidate test method defined in the target test class based on the class object, and selecting a target test method corresponding to the test instruction from the at least one candidate test method; generating a test case corresponding to the unit test instruction based on the target test method; after the test input data input for the test case is obtained, testing the function of the target test class based on the test case and the test input data. The technical scheme of the embodiment of the invention solves the technical problems of higher test cost and lower test efficiency in the related technology unit test, realizes the automatic unit test of the class to be tested, reduces the test cost, improves the test efficiency, and further improves the software development efficiency.
It should be understood that the description in this section is not intended to identify key or critical features of the embodiments of the invention or to delineate the scope of the invention. Other features of the present invention will become apparent from the description that follows.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for the description of the embodiments will be briefly described below, and it is apparent that the drawings in the following description are only some embodiments of the present invention, and other drawings may be obtained according to these drawings without inventive effort for a person skilled in the art.
FIG. 1 is a flow chart of a unit testing method according to a first embodiment of the invention;
FIG. 2 is a flow chart of a unit testing method according to a second embodiment of the invention;
FIG. 3 is a flow chart of a unit testing method according to a third embodiment of the present invention;
fig. 4 is a schematic structural diagram of a unit testing device according to a fourth embodiment of the present invention;
fig. 5 is a schematic structural diagram of an electronic device according to a fifth embodiment of the present invention.
Detailed Description
In order that those skilled in the art will better understand the present invention, a technical solution in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in which it is apparent that the described embodiments are only some embodiments of the present invention, not all embodiments. All other embodiments, which can be made by those skilled in the art based on the embodiments of the present invention without making any inventive effort, shall fall within the scope of the present invention.
It is noted that the terms "comprises" and "comprising," and any variations thereof, in the description and claims of the present invention and in the foregoing figures, are intended to cover a non-exclusive inclusion, such that a process, method, system, article, or apparatus that comprises a list of steps or elements is not necessarily limited to those steps or elements expressly listed or inherent to such process, method, article, or apparatus.
It will be appreciated that the data (including but not limited to the data itself, the acquisition or use of the data) involved in the present technical solution should comply with the corresponding legal regulations and the requirements of the relevant regulations.
Example 1
Fig. 1 is a schematic flow chart of a unit testing method according to a first embodiment of the present invention, where the method may be applied to a case of performing a unit test on a certain class in software development, and the method may be performed by a unit testing device, where the unit testing device may be implemented in a form of hardware and/or software, and the unit testing device may be configured in an electronic device such as a computer or a server.
As shown in fig. 1, the method of the present embodiment includes:
S110, responding to a unit test instruction, and determining a class object corresponding to the unit test instruction, wherein the class object is an object created by a target test class.
The unit test instruction may be understood as an instruction for performing a unit test in software development. The unit test instruction may include a class name of the target test class or an object created by the target test class. The target test class may be understood as a test class that requires unit testing. Class objects may be understood as objects created by a target test class. In the implementation of the present invention, the mode of generating the unit test instruction may specifically be that the target test class is determined; and generating unit test instructions for testing the target test class based on the target test class.
Specifically, after receiving the unit test instruction, the unit test instruction may be parsed, so that a class identifier carried in the unit test instruction may be obtained. After determining the class identifier, a class corresponding to the class identifier, i.e., a target test class, may be determined. After determining the target test class, it may be determined whether a class object of the target test class exists. If the unit test instruction exists, the class object of the target test class can be used as the class object corresponding to the unit test instruction; if the unit test instruction does not exist, a class object of the target test class can be created based on the target test class, so that a created class object is obtained, and the created class object is used as the class object corresponding to the unit test instruction.
In the embodiment of the present invention, there are various ways to determine the target test class, which are not specifically limited herein. As an alternative implementation manner in the embodiment of the present invention, determining the target test class may include: after receiving the class identifier of the target test class based on the unit test input box, the class corresponding to the input class identifier can be used as the target test class.
As another alternative implementation manner in the embodiment of the present invention, determining the target test class may include: and displaying a test class selection interface, wherein class identification of at least one candidate test class can be displayed in the test class selection interface. Wherein the class identification may include class names of candidate test classes. In response to a selection operation for the class identification, a selected class identification may be determined. And then the candidate test class corresponding to the selected class identifier can be used as the target test class.
On the basis of the above embodiment, the unit test instruction for testing the target test class is generated based on the target test class, specifically, after the target test class is determined, after receiving a control trigger operation acting on a unit test control, the unit test instruction for the target test class may be generated. Where the unit test control may understand the control used to generate the unit test instruction.
S120, determining at least one candidate test method defined in the target test class based on the class object, and selecting a target test method corresponding to the test instruction from the at least one candidate test method.
Candidate test methods are understood to be methods defined in the target test class that require testing, among other things. The target test method may be understood as a method currently required to perform a test among candidate test methods. The number of target test methods may be one, two or more. It should be noted that, in the embodiment of the present invention, the target test method corresponding to the test instruction is selected from at least one candidate test method, which accords with a complete service processing logic.
Specifically, after determining the class object, the target test class corresponding to the class object may be analyzed. So that the method defined in the target test class, i.e. the candidate test method, can be determined. In the embodiment of the present invention, the number of candidate test methods defined in the target test class may be one, two or more. In the case where the number of candidate test methods defined in the target test class is one, the candidate test method may be regarded as the target test method corresponding to the test instruction. In the case where the number of candidate test methods defined in the target test class is plural, at least one candidate test method may be selected from among the candidate test methods, and the selected candidate test method may be used as the target test method corresponding to the test instruction.
In the embodiment of the present invention, at least one candidate test method is selected from the candidate test methods, specifically, according to a method calling relationship between the candidate test methods,
and determining a method calling relation among the candidate test methods. In the implementation of the present invention, the number of method call relationships may be one or more. In one embodiment, in a case where the number of method call relations is one, a candidate test method associated with the method call relation may be used as the target test method corresponding to the unit test instruction. In another embodiment, in the case where the number of the method call relationships is plural, one method call relationship may be selected from the plural method call relationships. And then the candidate test method associated with the selected method call relationship can be used as a target test method corresponding to the unit test instruction.
S130, generating a test case corresponding to the unit test instruction based on the target test method.
The test case is understood to be a test case for testing a target test method. In the embodiment of the present invention, the generating, based on the target test method, a test case corresponding to the unit test instruction may include: in the case that the number of the target test methods is a plurality of, a method calling relation chain between the target test methods can be determined; and further, based on the method calling relation chain, a test case corresponding to the unit test instruction can be generated. The method calling relation chain can be used for representing the calling sequence of each target test method. In an embodiment of the invention, the method call relation chain may include a method name, a method return value type, a method parameter, and a parameter type of each target test method.
Optionally, based on the method calling relation chain, generating the test case corresponding to the unit test instruction may include: analyzing each node in the method call relation chain to obtain the method call relation information; and further, according to the method call relation information, a test case corresponding to the unit test instruction can be generated.
In the implementation of the present invention, the method may further include: for each target test method, the target test method and a method calling relation chain corresponding to the target test method can be stored in a preset XML file. The advantage of this arrangement is that the target test method and the chain of method call relations corresponding to the target test method can be represented in an XML file. In addition, in the embodiment of the invention, the XML file can also comprise the content of the test case corresponding to the method calling relation chain. The content of the test case corresponding to the method calling relation chain may include a series of information such as a class to be introduced, a test class name, an object to be simulated, a behavior of the object to be modeled, a determination method for determining whether the test case passes or not, and an input of each test case.
And S140, after the test input data input for the test case is obtained, testing the function of the target test class based on the test case and the test input data.
The test input data may be understood as actual parameters input for the test case to be tested. Specifically, after the test input data input for the test case is obtained, the test input data may be used to test the function of the target test class according to the test case.
On the basis of the above embodiment, the method may further include: in the case that the association method defined in the class other than the target test class exists in the method body of the target test method, a preset virtual object corresponding to the association method may be determined.
Wherein an association method may be understood as a method defined in other classes than the target test class present in the method body of the target test method. The preset virtual object may be understood as an object having a function of the association method. Alternatively, the preset virtual object may be a virtual Mock object. The Mock object is an object created under the Mockito unit test framework.
In the embodiment of the invention, for the method defined in other classes except the target test class, the method defined in the other classes can be simulated by presetting the virtual object, so that decoupling between classes in the unit test process is realized, the other classes are prevented from being called in the unit test process aiming at the target test class, and the test complexity is further reduced.
Based on the above embodiment, the testing the function of the target test class based on the test case and the test input data may include: and testing the function of the target test class based on the test case, the test input data and the preset virtual object. Specifically, the functions of the target test class are tested according to the test case by using the test input data and the preset virtual object.
In one embodiment, using the test input data and the preset virtual object, testing the function of the target test class according to the test case may include: in the case that the test input data includes a first parameter set for the preset virtual object and a second parameter other than the first parameter, the function of the target test class may be tested according to the test case using the first parameter and the second parameter.
In another embodiment, using the test input data and the preset virtual object, testing the function of the target test class according to the test case may include: in the case that the test input data includes object parameters set for the preset virtual object, the object parameters set for the preset virtual object may be determined; thus, the functions of the target test class can be tested according to the test case by using the test input data and the object parameters.
On the basis of the above embodiment, the method may further include: determining at least one member variable defined in the target test class based on the class object; and saving the at least one member variable and the at least one candidate test method as the structural information of the target test class into a predefined class structure body for saving the target test class.
The class structure may be understood as a structure defined in advance for storing structural information of the target test class.
Specifically, a structure for storing the structure information of the target test class is defined in advance. After determining a class object, a target test class corresponding to the class object may be analyzed, such that member variables defined in the target test class may be determined. And the member variable candidate test method defined by the target test class can be used as the structural information of the target test class. After obtaining the structural information of the target test class, the structural information of the target test class can be stored into a predefined class structure body for storing the target test class. This has the advantage of facilitating subsequent viewing of the class information.
According to the technical scheme, after a unit test instruction is received, a class object corresponding to the unit test instruction is determined, wherein the class object is an object created by a target test class; determining at least one candidate test method defined in the target test class based on the class object, and selecting a target test method corresponding to the test instruction from the at least one candidate test method; generating a test case corresponding to the unit test instruction based on the target test method; after the test input data input for the test case is obtained, testing the function of the target test class based on the test case and the test input data. The technical scheme of the embodiment of the invention solves the technical problems of higher test cost and lower test efficiency in the related technology unit test, realizes the automatic unit test of the class to be tested, reduces the test cost, improves the test efficiency, and further improves the software development efficiency.
Example two
Fig. 2 is a flow chart of a unit testing method according to a second embodiment of the present invention, and on the basis of the foregoing embodiment, optionally, the determining, based on the class object, at least one candidate testing method defined in the target testing class includes: based on the Class object, loading the Class of the target test Class by adopting a reflection mechanism; and analyzing the Class to obtain at least one candidate test method defined in the target test Class. The technical features that are the same as or similar to those of the above embodiments are not described herein.
As shown in fig. 2, the method in this embodiment specifically includes:
s210, responding to a unit test instruction, and determining a class object corresponding to the unit test instruction, wherein the class object is an object created by a target test class.
S220, loading a Class of the target test Class by adopting a reflection mechanism based on the Class object; and analyzing the Class to obtain at least one candidate test method defined in the target test Class.
The Class may be java Class. In the implementation of the invention, the java Class comprises candidate test methods defined in the target test Class. In an embodiment of the present invention, the method types of the candidate test method may include a public type and a private type. Alternatively, the reflection mechanism may be a Java reflection mechanism.
Specifically, after determining the Class object corresponding to the unit test instruction, a reflection mechanism may be used to load a Class and analyze the Class. Thus, the method defined in the Class with respect to the target test Class, i.e. the candidate test method, can be obtained.
In the embodiment of the invention, the method defined in the target test class is obtained by adopting the reflection mechanism, which has the advantages of not only increasing the flexibility of the program and avoiding the inherent logic program from being written in the code, but also reducing the code quantity of the program, improving the readability of the program and improving the multiplexing rate of the code.
S230, selecting a target test method corresponding to the test instruction from the at least one candidate test method.
S240, generating a test case corresponding to the unit test instruction based on the target test method.
S250, after the test input data input for the test case is obtained, testing the function of the target test class based on the test case and the test input data.
According to the technical scheme, the Class object is based on the Class object, and the Class of the target test Class is loaded by adopting a reflection mechanism; and analyzing the Class to obtain at least one candidate test method defined in the target test Class, so that the function of conveniently and rapidly obtaining the method defined in the target test Class is realized.
Example III
Fig. 3 is a schematic flow chart of a unit testing method according to a third embodiment of the present invention, and on the basis of the foregoing embodiment, optionally, the unit testing method according to the embodiment of the present invention further includes: after testing the function of the target test class, obtaining a test output result corresponding to the test input data; acquiring an expected output result corresponding to the test input data, which is set for the test case; and verifying the function of the target test class based on the test output result and the expected output result, and taking the verification result as a unit test result aiming at the target test class. The technical features that are the same as or similar to those of the above embodiments are not described herein.
As shown in fig. 3, the method in this embodiment specifically includes:
s310, responding to a unit test instruction, and determining a class object corresponding to the unit test instruction, wherein the class object is an object created by a target test class.
S320, determining at least one candidate test method defined in the target test class based on the class object, and selecting a target test method corresponding to the test instruction from the at least one candidate test method.
S330, generating a test case corresponding to the unit test instruction based on the target test method.
S340, after the test input data input for the test case is obtained, testing the function of the target test class based on the test case and the test input data, and obtaining a test output result corresponding to the test input data.
The test output result may be understood as a test result obtained by testing the target test class according to the test case using the test input parameters.
Specifically, after the test input data input for the test case is obtained, the function of the target test class may be tested based on the test case and the test input data, so as to obtain a test output result corresponding to the test input data.
S350, acquiring an expected output result corresponding to the test input data, which is set for the test case, verifying the function of the target test class based on the test output result and the expected output result, and taking the verification result as a unit test result for the target test class.
The expected output result may be understood as a test result expected to be output after the test is performed according to the test case using the test input data.
Specifically, a desired output result corresponding to the test input data set for the test case is obtained. And further, based on the test output result and the expected output result, the function of the target test class can be verified, so that a verification result is obtained. After the verification result is obtained, the verification result may be used as a unit test result for the target test class.
In the embodiment of the invention, under the condition that the test output result is consistent with the expected output result, the verification result of the target test class can be determined to be correct in service function. And under the condition that the test output result and the expected output result are inconsistent, determining that the verification result of the target test class is that the service function is not correct.
On the basis of the embodiment, in the case that the service function is determined to be incorrect, prompt information may be generated to prompt a developer to modify a target test method defined in the target test class and corresponding to the test case.
According to the technical scheme, after the function of the target test class is tested, a test output result corresponding to the test input data is obtained; acquiring an expected output result corresponding to the test input data, which is set for the test case; based on the test output result and the expected output result, verifying the function of the target test class, and taking the verification result as a unit test result aiming at the target test class, thereby realizing more comprehensive unit test on the target test class.
Example IV
Fig. 4 is a schematic structural diagram of a unit testing device according to a fourth embodiment of the present invention. As shown in fig. 4, the apparatus includes: a class object determination module 410, a test method determination module 420, a test case generation module 430, and a unit test module 440.
The class object determining module 410 is configured to determine, in response to a unit test instruction, a class object corresponding to the unit test instruction, where the class object is an object created by a target test class; a test method determining module 420, configured to determine at least one candidate test method defined in the target test class based on the class object, and select a target test method corresponding to the test instruction from the at least one candidate test method; the test case generation module 430 is configured to generate a test case corresponding to the unit test instruction based on the target test method; and the unit test module 440 is configured to test the function of the target test class based on the test case and the test input data after obtaining the test input data input for the test case. The technical scheme of the embodiment of the invention solves the technical problems of higher test cost and lower test efficiency in the related technology unit test, realizes the automatic unit test of the class to be tested, reduces the test cost, improves the test efficiency, and further improves the software development efficiency.
Optionally, the test method determining module 420 is configured to:
based on the Class object, loading the Class of the target test Class by adopting a reflection mechanism;
and analyzing the Class to obtain at least one candidate test method defined in the target test Class.
Optionally, the test case generation module 430 is configured to:
determining a method calling relation chain among the target test methods under the condition that the number of the target test methods is a plurality of;
and generating a test case corresponding to the unit test instruction based on the method calling relation chain.
Optionally, the apparatus further comprises an information storage module; wherein, information storage module is used for:
and aiming at each target test method, storing the target test method and a method calling relation chain corresponding to the target test method into a preset XML file.
Optionally, the apparatus further comprises a virtual object determination module; wherein, virtual object determination module is used for:
determining a preset virtual object corresponding to the association method under the condition that the association method defined in other classes except the target test class exists in the method body of the target test method, wherein the preset virtual object has the function of the association method;
Correspondingly, the unit test module 440 is configured to test the function of the target test class based on the test case, the test input data and the preset virtual object.
Optionally, the apparatus further comprises a test verification module; the test verification module is used for:
after testing the function of the target test class, obtaining a test output result corresponding to the test input data;
acquiring an expected output result corresponding to the test input data, which is set for the test case;
and verifying the function of the target test class based on the test output result and the expected output result, and taking the verification result as a unit test result aiming at the target test class.
Optionally, the device further includes a class structure information storage module, where the class structure information storage module is configured to:
determining at least one member variable defined in the target test class based on the class object;
and saving the at least one member variable and the at least one candidate test method as the structural information of the target test class into a predefined class structure body for saving the target test class.
The unit testing device provided by the embodiment of the invention can execute the unit testing method provided by any embodiment of the invention, and has the corresponding functional modules and beneficial effects of the execution method.
It should be noted that each unit and module included in the unit testing device are only divided according to the functional logic, but not limited to the above division, so long as the corresponding functions can be implemented; in addition, the specific names of the functional units are also only for distinguishing from each other, and are not used to limit the protection scope of the embodiments of the present invention.
Example five
Fig. 5 shows a schematic diagram of the structure of an electronic device 10 that may be used to implement an embodiment of the invention. Electronic devices are intended to represent various forms of digital computers, such as laptops, desktops, workstations, personal digital assistants, servers, blade servers, mainframes, and other appropriate computers. Electronic equipment may also represent various forms of mobile devices, such as personal digital processing, cellular telephones, smartphones, wearable devices (e.g., helmets, glasses, watches, etc.), and other similar computing devices. The components shown herein, their connections and relationships, and their functions, are meant to be exemplary only, and are not meant to limit implementations of the inventions described and/or claimed herein.
As shown in fig. 5, the electronic device 10 includes at least one processor 11, and a memory, such as a Read Only Memory (ROM) 12, a Random Access Memory (RAM) 13, etc., communicatively connected to the at least one processor 11, in which the memory stores a computer program executable by the at least one processor, and the processor 11 may perform various appropriate actions and processes according to the computer program stored in the Read Only Memory (ROM) 12 or the computer program loaded from the storage unit 18 into the Random Access Memory (RAM) 13. In the RAM 13, various programs and data required for the operation of the electronic device 10 may also be stored. The processor 11, the ROM 12 and the RAM 13 are connected to each other via a bus 14. An input/output (I/O) interface 15 is also connected to bus 14.
Various components in the electronic device 10 are connected to the I/O interface 15, including: an input unit 16 such as a keyboard, a mouse, etc.; an output unit 17 such as various types of displays, speakers, and the like; a storage unit 18 such as a magnetic disk, an optical disk, or the like; and a communication unit 19 such as a network card, modem, wireless communication transceiver, etc. The communication unit 19 allows the electronic device 10 to exchange information/data with other devices via a computer network, such as the internet, and/or various telecommunication networks.
The processor 11 may be a variety of general and/or special purpose processing components having processing and computing capabilities. Some examples of processor 11 include, but are not limited to, a Central Processing Unit (CPU), a Graphics Processing Unit (GPU), various specialized Artificial Intelligence (AI) computing chips, various processors running machine learning model algorithms, digital Signal Processors (DSPs), and any suitable processor, controller, microcontroller, etc. The processor 11 performs the various methods and processes described above, such as the unit test method.
In some embodiments, the unit testing method may be implemented as a computer program tangibly embodied on a computer-readable storage medium, such as storage unit 18. In some embodiments, part or all of the computer program may be loaded and/or installed onto the electronic device 10 via the ROM 12 and/or the communication unit 19. When the computer program is loaded into RAM 13 and executed by processor 11, one or more steps of the unit testing method described above may be performed. Alternatively, in other embodiments, processor 11 may be configured to perform the unit testing method in any other suitable manner (e.g., by means of firmware).
Various implementations of the systems and techniques described here above may be implemented in digital electronic circuitry, integrated circuit systems, field Programmable Gate Arrays (FPGAs), application Specific Integrated Circuits (ASICs), application Specific Standard Products (ASSPs), systems On Chip (SOCs), load programmable logic devices (CPLDs), computer hardware, firmware, software, and/or combinations thereof. These various embodiments may include: implemented in one or more computer programs, the one or more computer programs may be executed and/or interpreted on a programmable system including at least one programmable processor, which may be a special purpose or general-purpose programmable processor, that may receive data and instructions from, and transmit data and instructions to, a storage system, at least one input device, and at least one output device.
A computer program for carrying out methods of the present invention may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general purpose computer, special purpose computer, or other programmable data processing apparatus, such that the computer programs, when executed by the processor, cause the functions/acts specified in the flowchart and/or block diagram block or blocks to be implemented. The computer program may execute entirely on the machine, partly on the machine, as a stand-alone software package, partly on the machine and partly on a remote machine or entirely on the remote machine or server.
In the context of the present invention, a computer-readable storage medium may be a tangible medium that can contain, or store a computer program for use by or in connection with an instruction execution system, apparatus, or device. The computer readable storage medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the foregoing. Alternatively, the computer readable storage medium may be a machine readable signal medium. More specific examples of a machine-readable storage medium would include an electrical connection based on one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
To provide for interaction with a user, the systems and techniques described here can be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to a user; and a keyboard and a pointing device (e.g., a mouse or a trackball) through which a user can provide input to the electronic device. Other kinds of devices may also be used to provide for interaction with a user; for example, feedback provided to the user may be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user may be received in any form, including acoustic input, speech input, or tactile input.
The systems and techniques described here can be implemented in a computing system that includes a background component (e.g., as a data server), or that includes a middleware component (e.g., an application server), or that includes a front-end component (e.g., a user computer having a graphical user interface or a web browser through which a user can interact with an implementation of the systems and techniques described here), or any combination of such background, middleware, or front-end components. The components of the system can be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: local Area Networks (LANs), wide Area Networks (WANs), blockchain networks, and the internet.
The computing system may include clients and servers. The client and server are typically remote from each other and typically interact through a communication network. The relationship of client and server arises by virtue of computer programs running on the respective computers and having a client-server relationship to each other. The server can be a cloud server, also called a cloud computing server or a cloud host, and is a host product in a cloud computing service system, so that the defects of high management difficulty and weak service expansibility in the traditional physical hosts and VPS service are overcome.
It should be appreciated that various forms of the flows shown above may be used to reorder, add, or delete steps. For example, the steps described in the present invention may be performed in parallel, sequentially, or in a different order, so long as the desired results of the technical solution of the present invention are achieved, and the present invention is not limited herein.
The above embodiments do not limit the scope of the present invention. It will be apparent to those skilled in the art that various modifications, combinations, sub-combinations and alternatives are possible, depending on design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the scope of the present invention.

Claims (10)

1. A method of testing a unit, comprising:
determining a class object corresponding to a unit test instruction in response to the unit test instruction, wherein the class object is an object created by a target test class;
determining at least one candidate test method defined in the target test class based on the class object, and selecting a target test method corresponding to the test instruction from the at least one candidate test method;
Generating a test case corresponding to the unit test instruction based on the target test method;
after the test input data input for the test case is obtained, testing the function of the target test class based on the test case and the test input data.
2. The method of claim 1, wherein the determining at least one candidate test method defined in the target test class based on the class object comprises:
based on the Class object, loading the Class of the target test Class by adopting a reflection mechanism;
and analyzing the Class to obtain at least one candidate test method defined in the target test Class.
3. The method of claim 1, wherein generating test cases corresponding to the unit test instructions based on the target test method comprises:
determining a method calling relation chain among the target test methods under the condition that the number of the target test methods is a plurality of;
and generating a test case corresponding to the unit test instruction based on the method calling relation chain.
4. The method according to claim 1, wherein the method further comprises:
And aiming at each target test method, storing the target test method and a method calling relation chain corresponding to the target test method into a preset XML file.
5. The method according to claim 1, wherein the method further comprises:
determining a preset virtual object corresponding to the association method under the condition that the association method defined in other classes except the target test class exists in the method body of the target test method, wherein the preset virtual object has the function of the association method;
the testing the function of the target test class based on the test case and the test input data includes:
and testing the function of the target test class based on the test case, the test input data and the preset virtual object.
6. The method of claim 1, wherein after testing the functionality of the target test class, the method further comprises:
obtaining a test output result corresponding to the test input data;
acquiring an expected output result corresponding to the test input data, which is set for the test case;
And verifying the function of the target test class based on the test output result and the expected output result, and taking the verification result as a unit test result aiming at the target test class.
7. The method according to claim 1, wherein the method further comprises:
determining at least one member variable defined in the target test class based on the class object;
and saving the at least one member variable and the at least one candidate test method as the structural information of the target test class into a predefined class structure body for saving the target test class.
8. A unit testing apparatus, comprising:
the class object determining module is used for responding to the unit test instruction and determining a class object corresponding to the unit test instruction, wherein the class object is an object created by a target test class;
a test method determining module, configured to determine at least one candidate test method defined in the target test class based on the class object, and select a target test method corresponding to the test instruction from the at least one candidate test method;
the test case generation module is used for generating a test case corresponding to the unit test instruction based on the target test method;
And the unit test module is used for testing the function of the target test class based on the test case and the test input data after the test input data input for the test case is acquired.
9. An electronic device, the electronic device comprising:
at least one processor; and
a memory communicatively coupled to the at least one processor; wherein,
the memory stores a computer program executable by the at least one processor to enable the at least one processor to perform the unit testing method of any one of claims 1-7.
10. A computer readable storage medium storing computer instructions for causing a processor to perform the unit testing method of any one of claims 1-7.
CN202311642872.3A 2023-12-04 2023-12-04 Unit test method and device, electronic equipment and storage medium Pending CN117609064A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202311642872.3A CN117609064A (en) 2023-12-04 2023-12-04 Unit test method and device, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202311642872.3A CN117609064A (en) 2023-12-04 2023-12-04 Unit test method and device, electronic equipment and storage medium

Publications (1)

Publication Number Publication Date
CN117609064A true CN117609064A (en) 2024-02-27

Family

ID=89944093

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202311642872.3A Pending CN117609064A (en) 2023-12-04 2023-12-04 Unit test method and device, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN117609064A (en)

Similar Documents

Publication Publication Date Title
CN115576828A (en) Test case generation method, device, equipment and storage medium
CN116303013A (en) Source code analysis method, device, electronic equipment and storage medium
CN116774673A (en) Data calibration method and device, electronic equipment and storage medium
CN116126719A (en) Interface testing method and device, electronic equipment and storage medium
CN115481594B (en) Scoreboard implementation method, scoreboard, electronic equipment and storage medium
CN115269431A (en) Interface testing method and device, electronic equipment and storage medium
CN117609064A (en) Unit test method and device, electronic equipment and storage medium
CN116401113B (en) Environment verification method, device and medium for heterogeneous many-core architecture acceleration card
CN117709255B (en) Test method, device, equipment and medium for indirect access register
CN116955504B (en) Data processing method and device, electronic equipment and storage medium
CN116303098A (en) Application testing method and device, electronic equipment and storage medium
CN117670236A (en) Mobile-terminal-based to-be-handled flow approval method, device, equipment and medium
CN117453747A (en) Data quality detection method and device, electronic equipment and storage medium
CN116991737A (en) Software testing method, system, electronic equipment and storage medium
CN116303071A (en) Interface testing method and device, electronic equipment and storage medium
CN117931155A (en) Code generation method, device, equipment and medium
CN117061564A (en) Verification method, device, equipment and medium for vehicle signal transmission state
CN117493214A (en) Interface message generation method and device, electronic equipment and storage medium
CN117075861A (en) Method, device, equipment and medium for constructing backlight module
CN117056222A (en) Interface test file generation method and device, electronic equipment and storage medium
CN116302944A (en) Service system testing method and device, electronic equipment and storage medium
CN116932033A (en) Component packaging method and device, electronic equipment and storage medium
CN116719719A (en) Test method, test device, electronic equipment and storage medium
CN116991825A (en) Database flashback method, device, equipment and storage medium
CN118277275A (en) Interface testing method and device, electronic equipment and storage medium

Legal Events

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