Disclosure of Invention
The present invention provides a unit testing method, a unit testing device, an electronic apparatus, and a storage medium for overcoming the above-mentioned deficiencies in the prior art, and the object is achieved by the following technical solutions.
A first aspect of the present invention provides a unit testing method, the method comprising:
acquiring parameter description information of a pre-registered method to be tested;
dynamically generating a parameter operation style in a designated page according to the parameter description information;
receiving parameter values submitted by a user on the specified page;
and acquiring response information of the method to be tested based on the parameter values for correctness verification.
In some embodiments of the present application, the method further comprises a registration process of the method under test: receiving registration information of a method to be tested, which is submitted by a user on a preset registration page, wherein the registration information comprises basic information, access path information and parameter description information; detecting whether the registration information is correct; and if the registration information is correct, storing the registration information into a database.
In some embodiments of the present application, the obtaining of the parameter description information of the pre-registered method to be tested includes: reading basic information of the method to be tested from the database; displaying the relevant description of the method to be tested in a specified page according to the basic information; and when a trigger command of the method to be tested is received in the specified page, reading the parameter description information of the method to be tested from the database.
In some embodiments of the present application, the dynamically generating a parameter operation style in a specified page according to the parameter description information includes: and for each parameter in the parameter description information, dynamically generating a parameter operation style of the parameter on the specified page according to the name, the type and the to-be-selected list of the parameter.
In some embodiments of the present application, the dynamically generating a parameter operation style of the parameter on the specified page according to the name, the type, and the to-be-selected list of the parameter includes: if the list to be selected of the parameter is empty, the parameter operation style of the parameter is an input box; and if the to-be-selected list of the parameter is not empty, the parameter operation style of the parameter is a drop-down box.
In some embodiments of the present application, the receiving the parameter value submitted by the user on the specified page includes: detecting whether the submitted parameter value is correct or not according to the parameter description information; if the answer is correct, the step of calling the method to be tested based on the parameter value is executed to obtain response information; and if not, returning a prompt of parameter abnormity to the specified page.
In some embodiments of the present application, the obtaining response information of the method to be tested based on the parameter value includes: reading access path information of the method to be tested from the database; generating a calling request according to the parameter value, the access path information and the parameter description information; and sending the calling request to acquire response information of the method to be tested.
In some embodiments of the present application, after obtaining the response information of the method under test based on the parameter value, the method further includes: and displaying the response information so as to verify whether the response information is correct or not by a user.
A second aspect of the present invention provides a unit testing apparatus, the apparatus comprising:
the first acquisition module is used for acquiring parameter description information of a pre-registered method to be detected;
the dynamic generation module is used for dynamically generating a parameter operation style in a specified page according to the parameter description information;
the parameter submitting module is used for receiving parameter values submitted by a user on the specified page;
and the second acquisition module is used for acquiring the response information of the method to be tested based on the parameter value so as to check the correctness.
A third aspect of the present invention proposes an electronic device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, the processor implementing the steps of the method according to the first aspect when executing the program.
A fourth aspect of the present invention proposes a computer-readable storage medium having stored thereon a computer program which, when executed by a processor, carries out the steps of the method according to the first aspect as described above.
Based on the unit testing method and device of the first aspect and the second aspect, the invention has at least the following advantages or advantages:
the method comprises the steps that a user registers necessary information of a method to be tested in advance, so that when the test is needed, parameter description information of the method to be tested can be obtained from the registered necessary information, a parameter operation pattern of the method to be tested is generated on a visual interface, the method to be tested is mapped to a front-end visual interface, the user can conveniently initiate unit test after inputting parameter values on the parameter operation pattern of the visual interface, the test process is simple and convenient for the user, any code does not need to be written, and the user can freely replace the parameter values in the visual interface to carry out multiple tests without modifying the codes. Therefore, the scheme adopts the code-free unit test and can improve the test efficiency.
Detailed Description
Reference will now be made in detail to the exemplary embodiments, examples of which are illustrated in the accompanying drawings. When the following description refers to the accompanying drawings, like numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary examples do not represent all implementations consistent with the present invention. Rather, they are merely examples of apparatus and methods consistent with certain aspects of the invention, as detailed in the appended claims.
The terminology used herein is for the purpose of describing particular embodiments only and is not intended to be limiting of the invention. As used in this specification and the appended claims, the singular forms "a", "an", and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise. It should also be understood that the term "and/or" as used herein refers to and encompasses any and all possible combinations of one or more of the associated listed items.
It is to be understood that although the terms first, second, third, etc. may be used herein to describe various information, these information should not be limited to these terms. These terms are only used to distinguish one type of information from another. For example, first information may also be referred to as second information, and similarly, second information may also be referred to as first information, without departing from the scope of the present invention. The word "if," as used herein, may be interpreted as "at … …" or "at … …" or "in response to a determination," depending on the context.
In the prior art, when performing unit testing, a user is required to write a section of code for testing, for example, whether a test function performs secondary verification on a login state or submits an error data type to the function, whether the compatibility of the test function is robust, and the like.
In order to solve the technical problem, the invention provides an improved unit test method, namely when a test requirement exists, parameter description information of a pre-registered method to be tested is obtained, a parameter operation pattern is dynamically generated in a specified page according to the parameter description information, then a parameter value submitted by a user on the specified page is received, and further response information of the method to be tested is obtained based on the parameter value so as to be used for correctness checking and completing the test.
The technical effects that can be achieved based on the above description are:
the method comprises the steps that a user registers necessary information of a method to be tested in advance, so that when the test is needed, parameter description information of the method to be tested can be obtained from the registered necessary information, a parameter operation pattern of the method to be tested is generated on a visual interface, the method to be tested is mapped to a front-end visual interface, the user can conveniently initiate unit test after inputting parameter values on the parameter operation pattern of the visual interface, the test process is simple and convenient for the user, any code does not need to be written, and the user can freely replace the parameter values in the visual interface to carry out multiple tests without modifying the codes. Therefore, the scheme adopts the code-free unit test and can improve the test efficiency.
In order to make the technical solutions better understood by those skilled in the art, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application.
The first embodiment is as follows:
fig. 1 is a flowchart illustrating an embodiment of a unit testing method according to an exemplary embodiment of the present invention, where the unit testing method may be applied to an electronic device, and the electronic device may include any network-enabled device such as a terminal device, a mobile terminal, a PC, a server, and the like. As shown in fig. 1, the unit testing method includes the steps of:
step 101: and acquiring parameter description information of the pre-registered method to be tested.
Before step 101 is executed, the necessary information of the method to be tested needs to be registered in advance, so as to facilitate subsequent calling. In an optional embodiment, the registration process for the method to be tested includes the following steps:
step 201: and receiving registration information of the method to be tested, which is submitted by a user on a preset registration page.
Optionally, the submitted registration information includes three items of information: basic information, access path information, and parameter description information.
As shown in the schematic diagram of the registration page in fig. 2B, the registration information that the user needs to input includes basic information 10, access path information 20, and parameter description information 30. The basic information 10 includes information such as function description of the method to be tested, the position of the menu, the registrant, remark reminding and the like; the access path information 20 includes information such as a service name, a module name, a class name of a belonging class, a method name, and the like; the parameter description information 30 includes parameter names, parameter types, candidate lists, and the like.
When the access road strength information includes a service name and a module name, it is described that the currently registered method to be tested belongs to a method provided by an application on a Remote server, that is, an RPC (Remote Procedure Call) method.
Therefore, the user can complete the registration of the relevant information of the method to be tested only by filling the relevant information of the method to be tested according to the information input box provided on the registration page, and the operation is simple and convenient.
It should be noted that the basic information is used for displaying the functions of the method to be tested on the designated page, which is convenient for the user to understand; the parameter description information is used for describing relevant information of input parameters of the method to be tested; the access path information belongs to information needed to be used when the method to be tested is called.
Step 202: and detecting whether the registration information is correct.
Optionally, based on the description in step 201, it is detected whether the basic information and the access path information are empty, and it is detected whether the information formats are correct for the parameter description information, so as to avoid that the user fills in wrong information and cannot call the method to be tested.
Specifically, the correctness of the registration information can be detected by adopting a jsonschema rule.
Continuing with the above fig. 2B, the parameter operation style is generated in real time according to the parameter description information of the method to be tested, which is filled in by the user, by displaying at the lowest part of the registration page, so as to remind the user that the user can correct the input error in time.
Step 203: if the registration information is correct, the registration information is stored in a database.
Optionally, in order to facilitate subsequent calls, a unique primary key value may be generated for the method to be tested, and the generated primary key value and the registration information are stored in the database correspondingly.
Based on the above description, for the process of obtaining the parameter description information of the method to be tested in step 101, the basic information of the method to be tested may be read from the database, and the related function description of the method to be tested may be displayed in the designated page according to the basic information, so that when the trigger command of the method to be tested is received in the designated page, the parameter description information of the method to be tested may be read from the database.
And the related function description information of all the methods to be tested in the database is displayed in the designated page.
For example, as shown in fig. 3, the menu 101 includes: the method comprises five menu positions of ' sharing ', ' innovation ', ' doctor ', ' mechanism ' and ' user ', wherein relevant information of six methods to be tested is stored in a database, each row 102 represents one method to be tested, tool names, registrant information and remark reminding information of the methods to be tested are displayed in each row, and a ' button 103 on the left side of each row generates a trigger command for instructing parameter description information of the methods to be tested to be read from the database for display if the trigger command is triggered by the user.
Step 102: and dynamically generating a parameter operation style in a specified page according to the parameter description information.
In an optional specific embodiment, for each parameter in the parameter description information, a parameter operation style of the parameter may be dynamically generated on a specified page according to the name, the type, and the to-be-selected list of the parameter.
Illustratively, the type of parameter may include a number type, a character string type, and the like. The list to be selected is used for indicating the value range of the parameter to be limited, if the list to be selected in the parameter description information is empty, the parameter is not limited to the value range, and if the list to be selected is not empty, the parameter is limited to the value in the list to be selected.
Optionally, if the to-be-selected list of the parameter is empty, the parameter operation style of the parameter may be an input box; if the candidate list of the parameter is not empty, the parameter operation style of the parameter may be a drop-down box.
Continuing with FIG. 3, when the user selects the method to be tested in the first row, the left ">" button 103 is triggered, and the parameter description information obtained from the database includes two parameters, "doctor id" and "institution id". Because the to-be-selected list of the parameter "doctor id" is not empty, the generated parameter operation style is a drop-down box, and a user can select one of the numerical values (833241, 3023, 833244) listed in the drop-down box as a parameter value of the parameter; because the list to be selected of the parameter "mechanism id" is empty, the generated parameter operation style is an input box, and a user can input the parameter value of the parameter in the input box according to actual requirements.
Therefore, the parameter operation style of each parameter is dynamically generated on the designated page, so that the parameter value taking mode is mapped to the visual interface, and a user can conveniently input the corresponding parameter value for each parameter without writing any code.
It should be noted that the parameter operation style may include a search box, a drop-down box, a selection button, a multi-level linkage, etc. in addition to the drop-down box and the input box given above, the specific parameter operation style is not specifically limited in the present invention, as long as it is ensured that the user can conveniently fill parameter values for corresponding parameters in the operation style.
Step 103: parameter values submitted by a user on a specified page are received.
In an optional specific implementation manner, when the submitted parameter value is received, whether the submitted parameter value is correct or not may be further detected according to the parameter description information, and if the submitted parameter value is correct, step 104 is executed; if not, returning a prompt of parameter abnormity to the specified page to remind the user to re-input the parameter value of the parameter. Therefore, the influence on the judgment of the unit test result due to the input of wrong parameter values by the user can be avoided.
The specific conditions for detecting the correctness of the parameter values comprise whether the number of the parameters is correct or not and whether the types of the parameter values meet requirements or not.
Step 104: and acquiring response information of the method to be tested based on the parameter value for correctness verification.
In an optional specific embodiment, the access path information of the method to be tested is read from the database, a call request is generated according to the parameter value, the access path information and the parameter description information, and the call request is sent to obtain the response information of the method to be tested.
Specifically, the access path information and the parameter description information may be extracted from the database according to the primary key value of the method to be tested, and based on the above step 201, the calling request is generated by specifically combining the service name, the module name, the class name, the method name in the access path information, the parameter name in the parameter description information, and the parameter value submitted by the user.
It should be noted that after the step 104 is executed, as shown in fig. 4, the obtained response information may be displayed in a page manner, so that the user can check whether the response information is correct, that is, whether the response information is in accordance with the expectation.
To this end, the test flow shown in fig. 1 is completed, and a user registers necessary information of a method to be tested in advance, so that when a test is required, parameter description information of the method to be tested can be obtained from the registered necessary information, and a parameter operation pattern of the method to be tested is generated on a visual interface, so that the method to be tested is mapped to a front-end visual interface, a unit test is initiated after the user inputs a parameter value on the parameter operation pattern of the visual interface, the test process is simple and convenient for the user, no code needs to be written, and the user can replace the parameter value in the visual interface at will for multiple tests without modifying the code. Therefore, the scheme adopts the code-free unit test and can improve the test efficiency.
Example two:
fig. 5 is a schematic diagram illustrating a specific flow of a unit testing method according to the embodiment shown in fig. 1, and based on the embodiments shown in fig. 1 to fig. 4, the specific flow of the unit testing method includes the following steps:
step 501: and receiving registration information of the method to be tested, which is submitted by a user on a preset registration page, wherein the registration information comprises basic information, access path information and parameter description information.
Step 502: and detecting whether the registration information is correct.
Step 503: if the registration information is correct, the registration information is stored in a database.
Step 504: and reading basic information of the method to be tested from the database.
Step 505: and displaying the related description of the method to be tested in a specified page according to the basic information.
Step 506: and when a trigger command of the method to be tested is received in a designated page, reading the parameter description information of the method to be tested from a database.
Step 507: and dynamically generating a parameter operation style of each parameter on a specified page according to the name, the type and the to-be-selected list of each parameter in the parameter description information.
Step 508: and detecting whether the submitted parameter value is correct or not according to the parameter description information.
Step 509: and if the access path information is correct, reading the access path information of the method to be tested from the database.
Step 510: and generating a calling request according to the parameter value, the access path information and the parameter description information.
Step 511: and sending a calling request to acquire response information of the method to be tested.
Step 512: and displaying the response information so as to verify whether the response information is correct or not by the user.
For the processes of step 501 to step 512, reference may be made to the relevant descriptions in the above embodiments, and the present invention is not described in detail herein.
To this end, the specific test flow shown in fig. 5 is completed, and the user registers necessary information of the method to be tested in advance, so that when the test is required, the parameter description information of the method to be tested can be obtained from the registered necessary information, and the parameter operation pattern of the method to be tested is generated on the visual interface, so that the method to be tested is mapped to the front-end visual interface, the user can conveniently initiate a unit test after inputting a parameter value on the parameter operation pattern of the visual interface, the test process is simple and convenient for the user, no code needs to be written, and the user can freely change the parameter value in the visual interface to perform multiple tests without modifying the code. Therefore, the scheme adopts the code-free unit test and can improve the test efficiency.
Corresponding to the embodiment of the unit testing method, the invention also provides an embodiment of the unit testing device.
Fig. 6 is a flowchart illustrating an embodiment of a unit testing apparatus according to an exemplary embodiment of the present invention, the apparatus being configured to perform the unit testing method provided in any of the above embodiments, as shown in fig. 6, the unit testing apparatus includes:
a first obtaining module 610, configured to obtain parameter description information of a pre-registered method to be tested;
a dynamic generation module 620, configured to dynamically generate a parameter operation style in a specified page according to the parameter description information;
a parameter submitting module 630, configured to receive a parameter value submitted by a user on the specified page;
a second obtaining module 640, configured to obtain response information of the method to be tested based on the parameter value, so as to be used for correctness checking.
The implementation process of the functions and actions of each unit in the above device is specifically described in the implementation process of the corresponding step in the above method, and is not described herein again.
For the device embodiments, since they substantially correspond to the method embodiments, reference may be made to the partial description of the method embodiments for relevant points. The above-described embodiments of the apparatus are merely illustrative, and the units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the modules can be selected according to actual needs to achieve the purpose of the scheme of the invention. One of ordinary skill in the art can understand and implement it without inventive effort.
The embodiment of the invention also provides electronic equipment corresponding to the unit testing method provided by the embodiment, so as to execute the unit testing method.
Fig. 7 is a hardware block diagram of an electronic device according to an exemplary embodiment of the present invention, the electronic device including: a communication interface 601, a processor 602, a memory 603, and a bus 604; the communication interface 601, the processor 602 and the memory 603 communicate with each other via a bus 604. The processor 602 may execute the unit testing method described above by reading and executing the machine-executable instructions corresponding to the control logic of the unit testing method in the memory 603, and the details of the method are described in the above embodiments, which will not be repeated herein.
The memory 603 referred to in this disclosure may be any electronic, magnetic, optical, or other physical storage device that can contain stored information, such as executable instructions, data, and so forth. Specifically, the Memory 603 may be a RAM (Random Access Memory), a flash Memory, a storage drive (e.g., a hard disk drive), any type of storage disk (e.g., an optical disk, a DVD, etc.), or similar storage medium, or a combination thereof. The communication connection between the network element of the system and at least one other network element is realized through at least one communication interface 601 (which may be wired or wireless), and the internet, a wide area network, a local network, a metropolitan area network, and the like can be used.
Bus 604 can be an ISA bus, PCI bus, EISA bus, or the like. The bus may be divided into an address bus, a data bus, a control bus, etc. The memory 603 is used for storing a program, and the processor 602 executes the program after receiving the execution instruction.
The processor 602 may be an integrated circuit chip having signal processing capabilities. In implementation, the steps of the above method may be performed by integrated logic circuits of hardware or instructions in the form of software in the processor 602. The Processor 602 may be a general-purpose Processor, and includes a Central Processing Unit (CPU), a Network Processor (NP), and the like; but may also be a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), an off-the-shelf programmable gate array (FPGA) or other programmable logic device, discrete gate or transistor logic, discrete hardware components. The various methods, steps, and logic blocks disclosed in the embodiments of the present application may be implemented or performed. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like. The steps of the method disclosed in connection with the embodiments of the present application may be directly implemented by a hardware decoding processor, or implemented by a combination of hardware and software modules in the decoding processor.
The electronic device provided by the embodiment of the application and the unit testing method provided by the embodiment of the application have the same inventive concept and have the same beneficial effects as the method adopted, operated or realized by the electronic device.
Referring to fig. 8, the computer readable storage medium is an optical disc 30, and a computer program (i.e., a program product) is stored thereon, and when the computer program is executed by a processor, the computer program may perform the unit testing method according to any of the foregoing embodiments.
It should be noted that examples of the computer-readable storage medium may also include, but are not limited to, a phase change memory (PRAM), a Static Random Access Memory (SRAM), a Dynamic Random Access Memory (DRAM), other types of Random Access Memories (RAM), a Read Only Memory (ROM), an Electrically Erasable Programmable Read Only Memory (EEPROM), a flash memory, or other optical and magnetic storage media, which are not described in detail herein.
The computer-readable storage medium provided by the above-mentioned embodiments of the present application and the unit testing method provided by the embodiments of the present application have the same beneficial effects as the method adopted, executed or implemented by the application program stored in the computer-readable storage medium.
Other embodiments of the invention will be apparent to those skilled in the art from consideration of the specification and practice of the invention disclosed herein. This invention is intended to cover any variations, uses, or adaptations of the invention following, in general, the principles of the invention and including such departures from the present disclosure as come within known or customary practice within the art to which the invention pertains. It is intended that the specification and examples be considered as exemplary only, with a true scope and spirit of the invention being indicated by the following claims.
It should also be noted that the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising a … …" does not exclude the presence of another identical element in a process, method, article, or apparatus that comprises the element.
The above description is only for the purpose of illustrating the preferred embodiments of the present invention and is not to be construed as limiting the invention, and any modifications, equivalents, improvements and the like made within the spirit and principle of the present invention should be included in the scope of the present invention.