Interface testing method and device
1. An interface testing method, the method comprising:
acquiring a test case file; the test case file comprises a test instruction and an expected test result corresponding to the test instruction;
calling a target interface to execute the test instruction to obtain a test result;
and determining a test conclusion of the target interface according to the test result and the expected test result.
2. The method of claim 1, wherein the test case file further comprises a preamble; before the step of calling the target interface to execute the test instruction to obtain the test result, the method further includes:
calling the target interface to execute the preposed instruction so as to enable the target interface to be connected with a tested database;
correspondingly, the calling target interface executes the test instruction to obtain a test result, and the test result comprises:
and calling the target interface to execute the test instruction on the tested database to obtain the test result.
3. The method of claim 2, wherein the test case file further comprises: post-setting an instruction; after the step of calling the target interface to execute the test instruction on the tested database to obtain the test result, the method further includes:
and calling the target interface to execute the post instruction so as to close the connection between the target interface and the database to be tested.
4. The method according to claim 3, wherein the test case file is an SQL file, and the test instruction, the pre-instruction and the post-instruction are SQL statements.
5. The method of claim 3, wherein before the step of executing the test instruction by the call target interface to obtain the test result, the method further comprises:
and extracting the test instruction, the prepositive instruction and the postpositive instruction in the test case file.
6. The method of any one of claims 1-5, wherein the target interface is a JDBC interface.
7. The method of any of claims 1-5, wherein the pre-order comprises a connection URL, a username, a password; the connection URL comprises an IP address, a port parameter and a database name of the tested database.
8. An interface testing apparatus, the apparatus comprising:
the file acquisition unit is used for acquiring a test case file; the test case file comprises a test instruction and an expected test result corresponding to the test instruction;
the result acquisition unit is used for calling a target interface to execute the test instruction to obtain a test result;
and the conclusion obtaining unit is used for determining the test conclusion of the target interface according to the test result and the expected test result.
9. An electronic device, comprising: a processor and a memory for storing a program; the processor is configured to run the program to implement the interface testing method according to any one of claims 1 to 7.
10. A computer-readable storage medium having stored therein instructions which, when run on a computer, cause the computer to perform the interface testing method of any one of claims 1-7.
Background
Software testing is generated in connection with the generation of software. The software scale in the early software development process is very small and low in complexity, the software development process is disordered and quite random, the test meaning is narrow, a developer is equal to 'debugging' in testing, the purpose is to correct known faults in the software, and the developer usually completes the part of work. The testing is done with little investment and late testing intervention, often until the code is formed and the product is substantially complete. By the beginning of the 80 s of the last century, software and IT industries are greatly developed, software tends to be large-sized and high in complexity, and the quality of the software is more and more important. At this time, some basic theories and practical technologies of software testing are formed, and people begin to design various processes and management methods for software development, and the software development mode gradually transits from a chaotic and disordered development process to a structured development process, and is characterized by structured analysis and design, structured review, structured programming and structured testing.
Since the big data era, software applications have not kept databases apart, and whether in the Web, desktop applications, client servers, enterprises, and personal businesses, the databases are required to be operated at the back end. Databases are also indispensable in the financial, rental, retail, mailing, and medical fields as well. As the complexity of applications increases, more powerful and secure databases are needed to meet the demand. To satisfy the high frequency of application transactions (e.g., banking or financial applications), the reliability, and security of databases becomes paramount.
The JDBC interface is a java api that can access any type of tabular data, particularly data stored in a relational database. JDBC stands for Java database connectivity. API tasks contained in JDBC libraries are typically used with databases: such as connecting to a database, creating SQL or MySQL statements, executing SQL or MySQL queries in the database, viewing and modifying records.
In the JAVA field, the JUnit is the most popular test framework, which can help us to write unit tests, integrate tests, etc., and has the functions of test classification, test operator, etc. It includes the following characteristics: assertions (assertions) for testing desired results, test tools for sharing common test data, test suites for convenient organization and running tests, graphical and textual test runners.
Nowadays, the iterative cycle of a project is shorter and shorter, the frequency is higher and higher, each change needs to do the regression test, and the regression test moves dozens or even hundreds, under this rhythm, there is no way to rely on traditional manual test to go to support again, it can be understood that, artifical manual detection's inefficiency, and because the quantity that needs to detect is more leads to the problem that detection error appears in the middle of the manual detection's of manual work process easily, so current artifical manual detection's mode can lead to the interface to return the inefficiency and the accuracy of test reduces, so, need a new interface regression test scheme urgently.
Disclosure of Invention
The application provides an interface testing method, which aims to improve the efficiency of interface testing and the accuracy of testing conclusion.
In a first aspect, the present application provides an interface testing method, including:
acquiring a test case file; the test case file comprises a test instruction and an expected test result corresponding to the test instruction;
calling a target interface to execute the test instruction to obtain a test result;
and determining a test conclusion of the target interface according to the test result and the expected test result.
In a second aspect, the present application provides an interface testing apparatus, the apparatus comprising:
the file acquisition unit is used for acquiring a test case file; the test case file comprises a test instruction and an expected test result corresponding to the test instruction;
the result acquisition unit is used for calling a target interface to execute the test instruction to obtain a test result;
and the conclusion obtaining unit is used for determining the test conclusion of the target interface according to the test result and the expected test result.
In a third aspect, the present application provides a readable medium comprising executable instructions, which when executed by a processor of an electronic device, perform the method according to any of the first aspect.
In a fourth aspect, the present application provides an electronic device comprising a processor and a memory storing execution instructions, wherein when the processor executes the execution instructions stored in the memory, the processor performs the method according to any one of the first aspect.
According to the technical scheme, the interface testing method can be used for obtaining the test case file firstly, wherein the test case file comprises the test instruction and the expected test result corresponding to the test instruction; then, a target interface can be called to execute the test instruction to obtain a test result; then, a test conclusion of the target interface may be determined according to the test result and the expected test result. Therefore, the test instruction is executed by calling the target interface to complete the test of the target interface and obtain the test conclusion of the target interface, so that the automatic test of the target interface can be realized, the interface is not required to be manually tested as in the prior art, the problem of detection errors in the manual detection process is avoided, and the efficiency of interface test and the accuracy of the test conclusion are improved.
Further effects of the above-mentioned unconventional preferred modes will be described below in conjunction with specific embodiments.
Drawings
In order to more clearly illustrate the embodiments or prior art solutions of the present application, the drawings needed for describing the embodiments or prior art will be briefly described below, and it is obvious that the drawings in the following description are only some embodiments described in the present application, and that other drawings can be obtained by those skilled in the art without inventive exercise.
Fig. 1 is a schematic flowchart illustrating an interface testing method according to an embodiment of the present application;
fig. 2 is a schematic flowchart of an interface testing method according to an embodiment of the present application;
fig. 3 is a schematic flowchart of an interface testing method according to an embodiment of the present application;
fig. 4 is a schematic structural diagram of an interface testing apparatus according to an embodiment of the present application;
fig. 5 is a schematic structural diagram of an electronic device according to an embodiment of the present application.
Detailed Description
In order to make the objects, technical solutions and advantages of the present application more apparent, the technical solutions of the present application will be described in detail and completely with reference to the following embodiments and accompanying drawings. It should be apparent that the described embodiments are only some of the embodiments of the present application, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
Various non-limiting embodiments of the present application are described in detail below with reference to the accompanying drawings.
Referring to fig. 1, an interface testing method in the embodiment of the present application is shown, where the present application may be applied to an electronic device, such as a client, a terminal device, or a server, and it should be noted that, in an implementation manner, the present application may be implemented based on a javajunt testing framework. In this embodiment, the method may include, for example, the steps of:
s101: and acquiring a test case file.
The test case file may include a test instruction and an expected test result corresponding to the test instruction. The test instruction can be understood as an instruction for calling the target interface to execute the test, and the expected test result corresponding to the test instruction can be understood as an ideal result obtained by calling the target interface to execute the test instruction.
In an implementation manner of this embodiment, the test case file may be an SQL file, where the test case file may include an SQL statement corresponding to the test instruction, such as an SQL statement identified by SQL statements [ SQL-test ], as a main test step, so as to invoke the target interface to execute the test instruction.
In one implementation, in the JUNIT test framework, all file names under the SQL file directory of the test case files are read and transmitted into the test program entry as parameters, so as to realize batch execution of the test cases. The test case is remotely connected with the tested database through the JDBC interface, executes SQL statements, receives returned data, writes the returned data into the out file and stores the data into the out file. Defining an expected result exp file (namely an expected test result corresponding to the test instruction), and writing and saving an expected returned result. And carrying out diff comparison on the defined exp expected result file and an actually saved return out result file (namely the test result corresponding to the test instruction), and checking the consistency of the exp expected result file and the actually saved return out result file to realize the automatic test.
S102: and calling a target interface to execute the test instruction to obtain a test result.
In this embodiment, after the test instruction is obtained, the target interface may be called to execute the test instruction, so as to obtain a test result. In one implementation, the target interface may be a JDBC interface. By way of example in fig. 2, it is assumed that in the test function of the test instruction SQL statement [ SQL-test ], it can be used to execute the SQL statement in the read SQL file and execute [ SQL-test ], and make an assertion on the diff result of the contents of the out file and the exp file.
In one implementation, the test case file further includes a prefix instruction, that is, the test case file may include an SQL statement corresponding to the prefix instruction, such as an SQL statement [ SQL-setUp ], and in the setUp function, a database connection may be created, and the SQL file is read and the SQL statement in [ SQL-setUp ] is executed. Specifically, before the step of executing the test instruction by the calling target interface to obtain a test result, the method further includes: and calling the target interface to execute the preposed instruction so as to enable the target interface to be connected with the database to be tested. That is to say, in the present application, the target interface may be called to execute the pre-command, so that the target interface is connected to the database under test. Testing the preposition function setUp (@ Beform), Test (@ Test), teaDown (@ After), data (parameterized function), and constructing function. As shown in fig. 3, in one implementation, the pre-instruction may include a connection URL, a user name, and a password, where the connection URL includes an IP address, a port parameter, and a database name of the measured database, so that the measured database may be determined and connected to the measured database by using the connection URL, and the user name and the password may be used to obtain an access right to the measured database for a subsequent operation; it can be understood that the setup function of the SQL statement [ SQL-setup ] corresponding to the command can be used to call the target interface to connect to the tested database, and execute the statement with the tag [ SQL-setup ] in the SQL file. It should be noted that, the database information such as IP address, port number, database name, user name, password, etc. is all stored in the properties file, and these information and the common operation of the database are encapsulated into a JDBC class for the upper layer to call. Correspondingly, as shown in fig. 2, the invoking target interface executes the test instruction to obtain a test result, which may specifically be: and calling the target interface to execute the test instruction on the tested database to obtain the test result, namely calling the target interface JDBC interface module to execute the SQL statement corresponding to the test instruction on the tested database to obtain the test result (namely an initial result).
In one implementation, the test case file further includes: post-setting an instruction; that is, the test case file may include an SQL statement corresponding to the post instruction, such as the SQL statement [ SQL-teardown ], and the SQL statement identified by the SQL statement [ SQL-teardown ] as a step of recovering the test environment, and the returned data is not verified, so as to ensure that the configuration of the test case does not affect the next test case. In the teaDown function of the SQL statement [ SQL-tearDown ], reading the SQL file and executing the SQL statement in [ SQL-tearDown ], and interrupting the database connection, in the data function, receiving test parameter data, namely the SQL file name, wherein the function can match all SQL files ending with a certain field, namely in the tearDown function, realizing the following operation, executing the statement with a label [ SQL-tearDown ] in the SQL file, and closing the database connection. In the data function, all the sql file names to be tested are imported in batches as parameters. Specifically, after the step of calling the target interface to execute the test instruction on the tested database to obtain the test result, the method further includes: and calling the target interface to execute the post instruction so as to close the connection between the target interface and the database to be tested.
It should be noted that, in an implementation manner, the test case file is an SQL file, and the test instruction, the pre-instruction, and the post-instruction are SQL statements. For example, one sql file corresponds to one test case, each test case (i.e., test case file) contains sql statements to be executed, and the tags [ sql-setup ], [ sql-test ], [ sql-teardown ] are used to distinguish different execution stages, that is, before testing, during testing, after testing, when designing a test case, the content of the test case is ensured to be correct and complete. In this embodiment, a test script may be written by using a JUNIT test framework as a test entry, and a setup, test, teardown, and data function may be rewritten, thereby obtaining a test case file. That is, the SQL statement in the test case file is divided into three parts, and the following tags are added as identifiers: [ sql-setup ], [ sql-test ], [ sql-teardown ]. The SQL statement identified by [ SQL-setup ] is used as a pre-test environment preparation operation, and the returned data is not verified. The SQL statement identified by [ SQL-test ] is used as a main testing step, the returned data is written into an out file, and diff comparison is carried out between the returned data and an expected result exp file. The SQL statement marked by [ SQL-Teardown ] is used as a test environment recovery step, and returned data is not verified, so that the configuration of the test case does not influence the next test case.
Java, in this embodiment, implements file operations, and this framework involves three main files sql, exp, out. The file sql operation comprises the following: reading the sentences in the sql file, excluding the lines containing the annotation symbols, taking the incoming labels as extraction marks, extracting the sentences of the corresponding parts, and returning. The file exp operation contains the following: and defining a statement in a label [ sql-test ] of the sql file and a returned result after execution. The file out operation includes the following: and outputting and saving the result of the statement execution in the sql file label [ sql-test ]. Java, implementing the encapsulation of JDBC interfaces. And realizing the database connection, executing the sql statement, returning a result, and closing the database connection operation.
Since in this application, the test case file includes the test instruction, the pre-instruction, and the post-instruction, as shown in fig. 2, before the step of executing the test instruction by the call target interface to obtain the test result, the method further includes: and extracting the test instruction, the prepositive instruction and the postpositive instruction in the test case file. In this embodiment, after the test case file is obtained, each SQL statement in the test case file may be obtained first, and then the test instruction, the pre-instruction, and the post-instruction are determined according to the label carried by each SOL statement; for example, if the label of the SQL statement is [ SQL-setup ], the SQL statement is determined as the SQL statement corresponding to the leading instruction, if the label of the SQL statement is [ SQL-test ], the SQL statement is determined as the SQL statement corresponding to the test instruction, and if the label of the SQL statement is [ SQL-teardown ], the SQL statement is determined as the SQL statement corresponding to the trailing instruction.
S103: and determining a test conclusion of the target interface according to the test result and the expected test result.
After the test result is obtained, the test result may be compared with an expected test result, if the test result is the same as the expected test result, the test conclusion of the target interface is that the test is passed, and if the test result is different from the expected test result, the test conclusion of the target interface is that the test is failed.
In this embodiment, the detailed process of predefining the expected test result (i.e. exp file verification result) corresponding to the test instruction is as follows:
the test cases comprise two types, one is a positive test case, and the other is a negative test case. The forward test case is to verify whether the returned result (i.e., test result) like a select sql statement or a database meta-command is consistent with the content definition within the exp file (i.e., expected test result). And the negative test case is used for verifying whether the error information returned after executing the illegal sql statement is consistent with the definition of the exp file. And if the content stored in the output out file is consistent with the content predefined by the exp file, the output out file is judged to pass, and otherwise, the output out file is judged to fail.
According to the technical scheme, the interface testing method can be used for obtaining the test case file firstly, wherein the test case file comprises the test instruction and the expected test result corresponding to the test instruction; then, a target interface can be called to execute the test instruction to obtain a test result; then, a test conclusion of the target interface may be determined according to the test result and the expected test result. Therefore, the test instruction is executed by calling the target interface to complete the test of the target interface and obtain the test conclusion of the target interface, so that the automatic test of the target interface can be realized, the interface is not required to be manually tested as in the prior art, the problem of detection errors in the manual detection process is avoided, and the efficiency of interface test and the accuracy of the test conclusion are improved.
It can be understood that the present application has the following advantages over the prior art:
1. the time for executing the regression test is greatly shortened. Before the test frame is introduced, a whole set of test cases (the number is 1000+) of the database are manually executed, 10 people are needed, and after the test frame is introduced, only 0.5 people are needed, so that the automatic regression test of the JDBC interface can be completed more quickly by using less manpower.
2. The tester reduces the workload. Other more complex work can be performed with the saved time, and the quality of the product can be ensured more greatly.
3. The reliability of the test is increased. Because the automatic test case defines the test process, the expected result is tested, and the execution integrity and reliability of the test case are ensured.
4. This framework also provides ease of subsequent expansion. The tester only needs to write the test case into the sql file and store the sql file in the corresponding directory, and defines the exp file with the expected result, that is, the tester successfully adds one test case without deeply knowing the code part.
Fig. 4 shows an embodiment of an interface testing apparatus according to the present application. The apparatus of this embodiment is a physical apparatus for executing the method of the above embodiment. The technical solution is essentially the same as that in the above embodiment, and the corresponding description in the above embodiment is also applicable to this embodiment. The device in this embodiment includes:
a file obtaining unit 401, configured to obtain a test case file; the test case file comprises a test instruction and an expected test result corresponding to the test instruction;
a result obtaining unit 402, configured to invoke a target interface to execute the test instruction, so as to obtain a test result;
a conclusion obtaining unit 403, configured to determine a test conclusion of the target interface according to the test result and the expected test result.
Optionally, the test case file further includes a prefix instruction; the apparatus further comprises a calling unit for:
calling the target interface to execute the preposed instruction so as to enable the target interface to be connected with a tested database;
accordingly, the result obtaining unit 402 is configured to:
and calling the target interface to execute the test instruction on the tested database to obtain the test result.
Optionally, the test case file further includes: post-setting an instruction; the call is further to:
and calling the target interface to execute the post instruction so as to close the connection between the target interface and the database to be tested.
Optionally, the test case file is an SQL file, and the test instruction, the pre-instruction, and the post-instruction are SQL statements.
Optionally, the apparatus further includes an extracting unit, configured to:
and extracting the test instruction, the prepositive instruction and the postpositive instruction in the test case file.
Optionally, the target interface is a JDBC interface.
Optionally, the prefix instruction includes a connection URL, a user name, and a password; the connection URL comprises an IP address, a port parameter and a database name of the tested database.
Fig. 5 is a schematic structural diagram of an electronic device according to an embodiment of the present application. On the hardware level, the electronic device comprises a processor and optionally an internal bus, a network interface and a memory. The Memory may include a Memory, such as a Random-Access Memory (RAM), and may further include a non-volatile Memory, such as at least 1 disk Memory. Of course, the electronic device may also include hardware required for other services.
The processor, the network interface, and the memory may be connected to each other via an internal bus, which may be an ISA (Industry standard architecture) bus, a PCI (Peripheral Component Interconnect) bus, an EISA (Extended Industry standard architecture) bus, or the like. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, only one double-headed arrow is shown in FIG. 5, but this does not indicate only one bus or one type of bus.
And the memory is used for storing the execution instruction. In particular, a computer program that can be executed by executing instructions. The memory may include both memory and non-volatile storage and provides execution instructions and data to the processor.
In a possible implementation manner, the processor reads the corresponding execution instruction from the nonvolatile memory into the memory and then runs the execution instruction, and the corresponding execution instruction can also be obtained from other equipment so as to form the interface testing device on a logic level. The processor executes the execution instructions stored in the memory, so that the interface testing method provided by any embodiment of the application is realized through the executed execution instructions.
The method executed by the interface testing device according to the embodiment shown in fig. 1 of the present application may be applied to a processor, or may be implemented by a processor. The processor 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 in a processor or instructions in the form of software. The Processor may be a general-purpose Processor, including a Central Processing Unit (CPU), a Network Processor (NP), and the like; but also Digital Signal Processors (DSPs), Application Specific Integrated Circuits (ASICs), Field Programmable Gate Arrays (FPGAs) or other Programmable logic devices, discrete gate or transistor logic devices, 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 software module may be located in ram, flash memory, rom, prom, or eprom, registers, etc. storage media as is well known in the art. The storage medium is located in a memory, and a processor reads information in the memory and completes the steps of the method in combination with hardware of the processor.
An embodiment of the present application further provides a readable storage medium, where the readable storage medium stores an execution instruction, and when the stored execution instruction is executed by a processor of an electronic device, the stored execution instruction can enable the electronic device to execute the interface testing method provided in any embodiment of the present application, and is specifically configured to execute the method described in the interface testing.
The electronic device described in the foregoing embodiments may be a computer.
It will be apparent to those skilled in the art that embodiments of the present application may be provided as a method or computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects.
The embodiments in the present application are described in a progressive manner, and the same and similar parts among the embodiments can be referred to each other, and each embodiment focuses on the differences from the other embodiments. In particular, as for the apparatus embodiment, since it is substantially similar to the method embodiment, the description is relatively simple, and for the relevant points, reference may be made to the partial description of the method embodiment.
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 an … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
The above description is only an example of the present application and is not intended to limit the present application. Various modifications and changes may occur to those skilled in the art. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the scope of the claims of the present application.