A unit testing method and device

By parsing and converting the historical interaction log of the unit test module, generating database insert statements and executing them in the test database, the problem of artificially constructing simulated data is solved and the accuracy of unit tests is improved.

CN111625472BActive Publication Date: 2025-02-18WEBANK (CHINA)
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202010617487.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-06-30
Publication Date
2025-02-18
Estimated Expiration
2040-06-30

AI Technical Summary

Technical Problem

In the prior art, the simulation data of artificially constructing a large number of external services is prone to errors, resulting in inaccurate unit testing.

Method used

By obtaining the historical interaction log of the unit module to be tested, parsing the non-database interaction log and the database interaction log, converting it into database insert statements, and executing these statements in the test database to simulate the data interaction process.

Benefits of technology

There is no need to artificially construct large amounts of simulation data, which reduces the possibility of errors and improves the accuracy and reliability of unit tests.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN111625472B_ABST
    Figure CN111625472B_ABST
Patent Text Reader

Abstract

The present invention discloses a unit testing method and device, wherein the method comprises: obtaining a historical interaction log of a unit module to be tested; if the historical interaction log includes a non-database interaction log, parsing the non-database interaction log into a first database insert statement; and\or if the historical interaction log includes a database interaction log, parsing the database interaction log into a database query statement; converting the database query statement into a second database insert statement; executing the first database insert statement and\or the second database insert statement on a test database; and performing unit testing on the unit module to be tested according to the test database. When the above method is applied to Fintech, the test database is executed to import corresponding data into the test database, thereby eliminating the need to manually construct a large amount of simulated data for external services.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of software testing in the field of financial technology (Fintech), and in particular to a unit testing method and device. Background Art

[0002] With the development of computer technology, more and more technologies are applied in the financial field. The traditional financial industry is gradually transforming to Fintech. However, due to the security and real-time requirements of the financial industry, higher requirements are also placed on technology. The functional stability of the application software of financial institutions is crucial to the normal operation of financial institutions. At present, the functional testing of the application software of financial institutions is often carried out through unit testing. Among them, the unit is the smallest functional module to be tested artificially. Unit testing is the lowest level of testing activities to be carried out in the software development process. The independent unit of the software will be tested in isolation from the other parts of the program.

[0003] An independent unit of application software often involves data interaction with one or more external services. The commonly used method is to manually construct the response data and input data of the external service of the independent unit, so as to simulate the data interaction process with the external service. However, a unit often corresponds to a complete set of business processes, and at each business node of the business process, there may be multiple different branches according to different response data. Therefore, it is easy to make mistakes when constructing a large number of simulated data of external services manually, and unusable simulated data is constructed. Therefore, it is easy to make mistakes when constructing a large number of simulated data of external services manually during unit testing, which is a problem that needs to be solved urgently. Summary of the invention

[0004] The present invention provides a unit testing method and device, which solves the problem in the prior art that artificially constructing simulation data of a large number of external services is prone to errors.

[0005] In a first aspect, the present invention provides a unit testing method, comprising: obtaining a historical interaction log of a unit module to be tested; if the historical interaction log includes a non-database interaction log, parsing the non-database interaction log into a first database insert statement; and\or if the historical interaction log includes a database interaction log, parsing the database interaction log into a database query statement; converting the database query statement into a second database insert statement; executing the first database insert statement and\or the second database insert statement on a test database; and performing unit testing on the unit module to be tested according to the test database.

[0006] In the above method, the historical interaction log of the unit module under test obtained is a log in which the unit module under test has run and interacted with data. Therefore, it can be determined that it is a valid log that enables the unit module under test to interact normally. Therefore, the historical interaction log can be parsed to obtain the first database insert statement and / or the second database insert statement, and the corresponding data can be imported into the test database. In this way, there is no need to artificially construct a large amount of simulated data of external services, and valid data available to the unit test module can be obtained, and unit testing can be performed on the unit module under test.

[0007] Optionally, the converting of the database query statement into a second database insert statement includes: if the database query statement is a multi-table connection statement, using each layer of connection conditions in the multi-table connection statement as the query conditions of the connection data table of the layer of connection conditions, to obtain a single-table compound query statement after the multi-table connection statement is converted; and obtaining the second database insert statement according to the single-table compound query statement.

[0008] In the above method, direct decomposition of multi-table connection statements may result in infinite circular dependencies. Therefore, after using each layer of connection conditions in the multi-table connection statement as the query conditions of the connection data table of the connection conditions of that layer, a single-table compound query statement can be obtained. In this way, the database insert statement obtained by decomposing each layer of query statements in the single-table compound query statement will not have infinite circular dependencies, thereby providing a method for converting a multi-table connection statement into a second database insert statement.

[0009] Optionally, converting the database query statement into a second database insert statement includes: if the database query statement is a multi-table associated query statement, and the associated query conditions in the multi-table associated query statement do not contain an "or" logical operator, or the sub-query statements separated by the "or" logical operator in the multi-table associated query statement are sub-query statements of the same data table, then splitting the multi-table associated query statement into multiple first single-table query statements; and obtaining the second database insert statement according to the multiple first single-table query statements.

[0010] In the above method, if the database query statement is a multi-table associated query statement, and the multi-table associated query statement does not contain an "or" logical operator, or the sub-query statements separated by the "or" logical operator in the database query statement are the same data table, it can be split to obtain multiple first single-table insert statements corresponding to the data required for querying the database query statement, thereby providing a method for obtaining a second database insert statement corresponding to the data required for insertion from a multi-table associated query statement.

[0011] Optionally, the converting of the database query statement into a second database insert statement includes: if the database query statement is a multi-table associated query statement, and the associated query conditions in the multi-table associated query statement contain an "or" logical operator, and the sub-query statements separated by the "or" logical operator in the multi-table associated query statement are sub-query statements of different data tables, then updating the "or" logical operator to an "and" logical operator to obtain an operator update query statement after the database query statement is converted; then splitting the operator update query statement into multiple second single-table query statements; and obtaining the second database insert statement based on the multiple second single-table query statements.

[0012] In the above method, if the database query statement is a multi-table associated query statement, and the associated query conditions in the multi-table associated query statement contain an "or" logical operator, and the sub-query statements separated by the "or" logical operator in the multi-table associated query statement are sub-query statements of different data tables, by updating the "or" logical operator to an "and" logical operator, multiple second single-table insert statements corresponding to the data required for querying the database query statement can be split, thereby providing a method for obtaining a second database insert statement corresponding to the data required for insertion from a multi-table associated query statement.

[0013] Optionally, the converting the database query statement into a second database insert statement includes: if the database query statement includes a sub-query statement, and the data table of the sub-query statement is different from the data table outside the sub-query statement, then eliminating the sub-query statement to obtain a third single-table query statement after the database query statement is converted; and obtaining the second database insert statement according to the third single-table query statement.

[0014] In the above manner, if the database query statement includes a sub-query statement, and the sub-query statement is only used to determine the key value of the search field, and the data table of the sub-query statement is different from the data table outside the sub-query statement, no matter what the sub-query statement is, the data in the data table outside the sub-query statement is the required data, thereby providing a method for the database query statement including the sub-query statement to obtain the data required for insertion corresponding to the second database insert statement.

[0015] Optionally, the non-database interaction log includes a request message log and a response message log; the parsing of the non-database interaction log into a first database insert statement includes: parsing a first serial number, a first request identification code and the request message log into a first sub-insert statement; parsing the first serial number, the first request identification code and the response message log into a second sub-insert statement; using the first sub-insert statement and the second sub-insert statement as the first database insert statement; the parsing of the database interaction log into a database query statement includes: parsing a second serial number, a second request identification code, a database statement in the database interaction log and a database parameter in the database interaction log into the database query statement.

[0016] Under the above method, the request message and response message involved are encapsulated together by serial number and request identification code, and parsed into the first database insert statement, and the database statements and database parameters in the database interaction log are parsed into the database query statement, so that the required data for each non-database interaction test is marked by serial number and request identification code, which facilitates the test database to find the required corresponding data.

[0017] Optionally, the unit test is performed on the unit module under test according to the test database; including: when the unit module under test interacts with a non-database external service test, intercepting an interactive service request, and reading a data record corresponding to the interactive service request from the test database according to the interactive service request; converting the data record corresponding to the interactive service request into interactive data in a data format corresponding to the interactive service request; performing unit test on the unit module under test according to the interactive data; and\or when the unit module under test interacts with the test database test, according to the code function of the unit module under test, reading the data record corresponding to the code function from the test database; performing unit test on the unit module under test according to the data record corresponding to the code function.

[0018] Under the above method, when the unit module under test interacts with the external service test of non-database, the data record corresponding to the interactive service request is read from the test database; and when the unit module under test interacts with the test database test, the data record corresponding to the code function is read from the test database, thereby restoring the original interaction environment and interaction data during the unit test, thereby increasing the reliability of the unit test.

[0019] In a second aspect, the present invention provides a unit testing device, comprising: an acquisition module, used to acquire a historical interaction log of a unit module to be tested; a processing module, used to parse the non-database interaction log into a first database insert statement if the historical interaction log includes a non-database interaction log; and\or if the historical interaction log includes a database interaction log, parse the database interaction log into a database query statement; convert the database query statement into a second database insert statement; execute the first database insert statement and\or the second database insert statement on a test database; and a testing module, used to perform unit testing on the unit module to be tested according to the test database.

[0020] Optionally, the processing module is specifically used for: if the database query statement is a multi-table connection statement, using each layer of connection conditions in the multi-table connection statement as the query condition of the connection data table of the connection condition of that layer, to obtain a single-table compound query statement after the multi-table connection statement is converted; according to the single-table compound query statement, obtaining the second database insert statement.

[0021] Optionally, the processing module is specifically used to: if the database query statement is a multi-table associated query statement, and the associated query conditions in the multi-table associated query statement do not contain an "or" logical operator, or the sub-query statements separated by the "or" logical operator in the multi-table associated query statement are sub-query statements of the same data table, then split the multi-table associated query statement into multiple first single-table query statements; and obtain the second database insert statement based on the multiple first single-table query statements.

[0022] Optionally, the processing module is specifically used for: if the database query statement is a multi-table associated query statement, and the associated query conditions in the multi-table associated query statement contain an "or" logical operator, and the sub-query statements separated by the "or" logical operator in the multi-table associated query statement are sub-query statements of different data tables, then the "or" logical operator is updated to an "and" logical operator to obtain an operator update query statement after the database query statement is converted; then the operator update query statement is split into multiple second single-table query statements; and according to the multiple second single-table query statements, the second database insert statement is obtained.

[0023] Optionally, the processing module is specifically used to: if the database query statement includes a sub-query statement, and the data table of the sub-query statement is different from the data table outside the sub-query statement, then eliminate the sub-query statement to obtain a third single-table query statement after the database query statement is converted; and obtain the second database insert statement according to the third single-table query statement.

[0024] Optionally, the non-database interaction log includes a request message log and a response message log; the processing module is specifically used to: parse the first serial number, the first request identification code and the request message log into a first sub-insert statement; parse the first serial number, the first request identification code and the response message log into a second sub-insert statement; use the first sub-insert statement and the second sub-insert statement as the first database insert statement; parse the second serial number, the second request identification code, the database statement in the database interaction log and the database parameters in the database interaction log into the database query statement.

[0025] Optionally, the test module is specifically used to: when the unit module under test interacts with an external service test other than a database, intercept the interactive service request, and read the data record corresponding to the interactive service request from the test database; convert the data record corresponding to the interactive service request into interactive data in a data format corresponding to the interactive service request; perform unit testing on the unit module under test according to the interactive data; and\or when the unit module under test interacts with the test database test, according to the code function of the unit module under test, read the data record corresponding to the code function from the test database; and perform unit testing on the unit module under test according to the data record corresponding to the code function.

[0026] The beneficial effects of the above-mentioned second aspect and each optional device of the second aspect can refer to the beneficial effects of the above-mentioned first aspect and each optional method of the first aspect, and will not be repeated here.

[0027] In a third aspect, the present invention provides a computer device, including a program or an instruction, which, when executed, is used to execute the above-mentioned first aspect and each optional method of the first aspect.

[0028] In a fourth aspect, the present invention provides a storage medium, comprising a program or an instruction, which, when executed, is used to execute the above-mentioned first aspect and each optional method of the first aspect. BRIEF DESCRIPTION OF THE DRAWINGS

[0029] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for describing the embodiments are briefly introduced below.

[0030] Figure 1 A schematic diagram of the steps of a unit testing method provided in an embodiment of the present application;

[0031] Figure 2 A schematic diagram of an applicable architecture of a unit testing method provided in an embodiment of the present application;

[0032] Figure 3This is a schematic diagram of the storage of interaction logs between the unit module under test and the external service in an embodiment of the present application;

[0033] Figure 4 A schematic diagram of the test process steps in a unit testing method provided in an embodiment of the present application;

[0034] Figure 5 A schematic diagram of data search during a test in a unit testing method provided in an embodiment of the present application;

[0035] Figure 6 A schematic diagram of the structure of a unit testing device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0036] In order to better understand the above-mentioned technical scheme, the above-mentioned technical scheme will be described in detail below in combination with the accompanying drawings and specific implementation methods of the specification. It should be understood that the embodiments of the present application and the specific features in the embodiments are detailed descriptions of the technical scheme of the present application, rather than limitations on the technical scheme of the present application. In the absence of conflict, the embodiments of the present application and the technical features in the embodiments may be combined with each other.

[0037] The definitions of terms appearing in this application are first listed below.

[0038] Unit testing: refers to checking and verifying the smallest testable unit in the software. A unit is the smallest artificially defined functional module to be tested. Unit testing is the lowest level of testing activity to be performed during the software development process. Independent units of the software will be tested in isolation from other parts of the program.

[0039] Database: A database is a "warehouse that organizes, stores, and manages data according to data structures". It is a collection of large amounts of data that is stored in a computer for a long time, organized, shareable, and uniformly managed. The storage space is large and can store millions, tens of millions, or even hundreds of millions of data.

[0040] When a financial institution (banking institution, insurance institution or securities institution) is operating its business (such as a bank's loan business, deposit business, etc.), the functional stability of the financial institution's application software is crucial to the normal operation of the financial institution. Currently, the functional testing of the financial institution's application software is often carried out through unit testing. The commonly used method is to manually construct the response data and input data of the external service of an independent unit. However, this may cause errors in the simulation data of a large number of external services and construct unusable simulation data. This situation does not meet the needs of banks and other financial institutions and cannot guarantee the efficient operation of various businesses of financial institutions. For this reason, Figure 1 As shown, the present invention provides a unit testing method.

[0041] Step 101: Obtain the historical interaction log of the unit module under test.

[0042] Step 102: If the historical interaction log includes a non-database interaction log, the non-database interaction log is parsed into a first database insert statement.

[0043] Step 103: If the historical interaction log includes a database interaction log, the database interaction log is parsed into a database query statement; and the database query statement is converted into a second database insert statement.

[0044] Step 104: Execute the first database insert statement and / or the second database insert statement on the test database.

[0045] Step 105: Perform unit testing on the unit module to be tested according to the test database.

[0046] The execution of step 102 and step 103 is in an AND / OR relationship, and both can be executed, or only one of them can be executed.

[0047] It should be noted that, before step 101, the historical interaction process between the program logic of the unit module to be tested and the external service can be as follows: Figure 2 Among them, external services may include: database, file I / O (input / output), remote call, resource acquisition and public services, etc. Figure 2 The external service shown is used as an example. Among them, request A, request B, request C, request D and request E are request data of the program logic of the unit module under test interacting with the external service history, response A, response B, response C, response D and response E are response data of the program logic of the unit module under test interacting with the external service history, request A and response A can be stored in the database interaction log; request B, request C, request D and request E, and response B, response C, response D and response E can be stored in the non-database interaction log.

[0048] Specifically, you can Figure 3 As shown, the interceptor intercepts program logic components and dependent components (dependencies include databases, file resources, network resources, remote calls, etc.) in a specified format (serial number, request identification code, request parameters, request results) for storage.

[0049] In step 101, it should be noted that the historical interaction log can record the data of a test in one log, and can store the data of each step in a test in multiple data. The log can be obtained by the log unique identifier, or by keywords in the test data. In the method of obtaining by the log unique identifier, the specific process can be as follows:

[0050] Get the serial number to be analyzed. The serial number format is: 2005010Q702190SSG5VC7IDCW9MNWRDO (yyMMDD+3-digit dcn+4-digit subsystem+18-digit random value). Truncate the first 6 digits of the date data, add 20 in front of 200501, change it to 20200501, and compare it with the current time and date. If it is the same day, directly get mefs-core.log, mybatis.log or get the specified date mefs-core.log.2020-05-01*,mybatis.log.2020-05-01*.

[0051] In an optional implementation, in steps 102 to 103, if the non-database interaction log includes a request message log and a response message log, then step 102 may specifically be:

[0052] Step (2-1): Parse the first serial number, the first request identification code and the request message log into a first sub-insert statement.

[0053] Step (2-2): Parse the first serial number, the first request identification code and the response message log into a second sub-insert statement.

[0054] Step (2-3): Use the first sub-insert statement and the second sub-insert statement as the first database insert statement.

[0055] The log format in step (2-1) may be: time|first request identification code|first serial number|request message (weup_rmb_deal_received_reqq). The first request identification code|first serial number|request message may be parsed into an insert statement, for example, the first request identification code|first serial number|request message may be converted into json format as the value of the first sub-insert statement.

[0056] The log format in step (2-2) may be: time|second request identification code|second serial number|request message (weup_rmb_deal_received_reqq). The second request identification code|second serial number|request message may be parsed into an insert statement. For example, with time as the primary key, the second request identification code|second serial number|request message may be converted into json format as the value of the second sub-insert statement.

[0057] In step (2-3), the data storage format can be set according to the actual scenario. For example, the data storage format can be: INSERT INTO test_mock_config(busi_no,test_serviceid,mock_request,mock_response,mock_type,remark,create_time)VALUES, where the corresponding meanings are (serial number, request identifier, request parameter, response parameter, parameter type, remark, operation time).

[0058] Then the process of parsing into database query log in step 103 may specifically be:

[0059] Parse the second serial number, the second request identification code, the database statement in the database interaction log, and the database parameters in the database interaction log into the database query statement

[0060] The log format in step 103 may be: time|second request identification code|second serial number|database statement (Preparing), time|second request identification code|second serial number|database statement parameters (Parameters).

[0061] It should be noted that the interaction between unit testing and the database is based on real data records in the database. The interaction logic between the unit module under test and the database is not shielded during unit testing, so that the interaction problems such as statements or data that interact with the database when the program logic is executed can be checked. For single-table database query statements, you only need to replace the select keyword with the insert keyword. For multi-table database query statements, you cannot directly convert them into insert statements by modifying keywords. You need to convert the multi-table database query statements into single-table database query statements based on the characteristics of the specific database query statements. Specifically, it includes the following situations:

[0062] It should be noted that the following situations are all based on one principle: the query of the database query statement must be based on certain data records to achieve correct query. Simply put, there must be a certain data record to correctly find this data record. Then the database query statement can determine the data records required for the correct execution of the database query statement, and how to insert these data records into the database also corresponds to the second database insert statement. In this way, these data records can be inserted into a new database, such as the test database, by executing the second database insert statement, so that these data records can be used when testing the test database. In the database operation statement, although data records of multiple tables are allowed to be queried at the same time, data records of multiple tables are not allowed to be inserted at the same time. Only a single table can be inserted one by one. Therefore, when inserting data records of multiple tables, they need to be converted into one or more single-table insert statements.

[0063] (1) If the database query statement is a multi-table join statement, use each layer of join conditions in the multi-table join statement as the query condition of the join data table of the layer of join conditions to obtain a single-table composite query statement after the multi-table join statement is converted; and obtain the second database insert statement based on the single-table composite query statement.

[0064] For example, the multi-table join statement is select * from A left join B on Aa=Bb left join C Cc=Aa. It should be noted that the left outer join of the data table is taken as an example here, and the same is true for the right outer join.

[0065] Among them, the connection conditions of the multi-table connection statement include Aa=Bb, Cc=Aa, which means that when Table A, Table B and Table C all meet the condition that "the value of column a in Table A is equal to the value of column b in Table B, and the value of column a in Table A is equal to the value of column c in Table C", Table A, Table B and Table C are left outer connected in the order of Table A, Table B and Table C, and * indicates that the data table obtained after the connection includes all the columns in Table A, Table B and Table C. Obviously, according to the order of left outer connection, the connection data table of this layer with the connection condition Aa=Bb is Table A or Table B, and the connection data table of this layer with Cc=Aa is Table C.

[0066] If you want to convert a multi-table join statement into a single-table join statement, even if you consider tables A and B first, if both tables A and B record the join condition (Aa = Bb), a circular dependency will occur:

[0067] Select*from A where a IN(select b from B), Select*from B where b IN(select a from A where a IN(select b from B). Because Select*from A where aIN(select b from B) means that the query of table A depends on the query result of table B, and the query result of table B depends on the query of table A, the result will become an infinite loop, and adding table C into the consideration will lead to an infinite loop. So consider multi-table join, and one-way dependency is enough.

[0068] You can first use the join condition as the query condition of the outermost connection data table, that is, migrate the join condition to table A. B does not hold the join condition. If only tables A and B are considered, taking the connection condition Aa=Bb as an example, the connection data table of this layer is table A, then the query of select*from A left join B on Aa=Bb can be converted to:

[0069] Select*from A where a IN(select b from B);

[0070] Among them, where a IN (select b from B) is the query condition of the A data table.

[0071] Furthermore, we continue to consider the left outer join with table C, that is, the query select * from A left join B on Aa = Bb left join C Cc = Aa can be converted to:

[0072] Select*from C where c IN(select a from A where a IN(select b fromB));

[0073] Among them, where c IN(select a from A where a IN(select b from B)) is the query condition of the data table.

[0074] The second database insert statement may be obtained according to the single-table compound query statement.

[0075] For example, the single-table compound query statement is Select*from C where c IN(select afrom A where a IN(select b from B)).

[0076] One implementation method is to convert the single-table compound query statement according to a preset conversion rule. It is also possible to obtain a corresponding single-table insert statement according to the query result obtained by executing the single-table compound query statement and the data records in the query result:

[0077] The second database insert statement, that is, the single table insert statement is:

[0078] insert ignore into A(a)values;

[0079] insert ignore into B(b)values;

[0080] insert ignore into C(c)values;

[0081] Among them, values ​​represents the data records of table A that satisfy the insertion conditions of each column, but no matter which table or column of a, b or c is inserted, these specific values ​​satisfy A(a)=B(b)=C(c).

[0082] (2) If the database query statement is a multi-table associated query statement, and the associated query conditions in the multi-table associated query statement do not contain an "or" logical operator, or the sub-query statements separated by the "or" logical operator in the multi-table associated query statement are sub-query statements of the same data table, then the multi-table associated query statement is split into multiple first single-table query statements; and the second database insert statement is obtained according to the multiple first single-table query statements.

[0083] For example, the multi-table association query statement is: select * from A, B, C where Aa = 'a0' and Bb = 'b' and Aa = Bb and Cc = Aa. The multi-table association query statement does not contain the "or" logical operator.

[0084] The associated query condition is where Aa='a0'and Bb='b'and Aa=Bb and Cc=Aa, and does not contain an "or" logical operator.

[0085] Then we can directly split the query statements that associate the three tables A, B, and C into query statements that query only tables A, B, and C. Specifically:

[0086] The method of splitting the multi-table association query statement into multiple first single-table query statements may be:

[0087] The splitting is performed according to the pre-set splitting rules. This is because the values ​​of the columns in each table involved in the query results obtained by the above multi-table association query statement may be used when querying, so it is necessary to ensure that the data of the corresponding column values ​​of each table are queried. For example, for a first single-table query statement, the conditions containing the table in the multi-table association query statement are extracted separately as the query conditions of this first single-table query statement. The multiple first single-table query statements are specifically:

[0088] Select*from A where Aa='a0'and a IN(select b from B where Bb='a0');

[0089] Select*from B where Bb='b';

[0090] Select*from C where Cc IN(Select a from A where Aa='a0'and a IN(select b from B where Bb='b').

[0091] Then, according to the multiple first single-table query statements, the process of obtaining the second database insert statement is specifically as follows:

[0092] One implementation is to convert the multi-table association query statement according to a preset conversion rule. It is also possible to obtain the corresponding first single-table insert statement according to the query result obtained by executing the multi-table association query statement and the data records in the query result:

[0093] insert ignore into A(a)values;

[0094] insert ignore into B(b)values;

[0095] insert ignore into C(c)values;

[0096] Among them, values ​​represents the data records of table A that satisfy the insertion conditions of each column, but no matter which table or column of a, b or c is inserted, these specific values ​​satisfy Aa='a0'and Bb='b'and Aa=Bb and Cc=Aa.

[0097] Alternatively, when a multi-table association query statement contains an "or" logical operator, the sub-query statements separated by the "or" logical operator in the multi-table association query statement are sub-query statements of the same data table, for example, select*fromA,B where A.a1='1'and A.a1=B.b1 and A.a2='2'or A.a3='3'.

[0098] There are two subquery statements, namely A.a1='1'and A.a1=B.b1 and A.a2='2' and A.a3='3', all of which only involve the query of table A, and B.b1 is only a value of a query column of A.a1.

[0099] Therefore, it can also be directly split into the multiple first single-table query statements. Since only the query of table A is involved, the sub-query statements at both ends of or only need to satisfy one query result to be correct, and do not need to satisfy the data records of both ends at the same time. Then, each sub-query statement can be split separately, each corresponding to a first single-table query statement.

[0100] How each sub-query statement is converted to obtain the first single-table query statement can be implemented according to a pre-set conversion rule, as long as the converted first single-table query statement satisfies the query result equivalent to the sub-query statement. For example, for a first single-table query statement, the conditions containing the table in the multi-table associated query statement are extracted separately as the query conditions of the first single-table query statement.

[0101] Specifically, the multiple first single-table query statements are:

[0102] The first single table query statement corresponding to the subquery statement select*from A,B where A.a1='1'and A.a1=B.b1 and A.a2='2' is:

[0103] Select*from A where A.a1='1'and a IN(select b1 from B where B.b1='1')and A.a2='2'.

[0104] The first single table query statement corresponding to the subquery statement Select*from A where A.a3='3' is:

[0105] Select*from A where A.a3='3'.

[0106] Furthermore, according to the multiple first single-table query statements, the process of obtaining the second database insert statement may be:

[0107] Execute the above multiple first single-table query statements to obtain query results, which include multiple data records. According to the data records in the query results, corresponding first single-table insert statements are obtained:

[0108] Then, the process of converting the multiple first single-table query statements into multiple first single-table insert statements as the first database insert statements is specifically as follows:

[0109] insert ignore into A(a1)values('1');

[0110] insert ignore into A(a2)values('2');

[0111] insert ignore into B(b2)values('2');

[0112] insert ignore into A(a3)values('3').

[0113] (3) The step of converting the database query statement into a second database insert statement includes: if the database query statement is a multi-table associated query statement, and the associated query condition in the multi-table associated query statement contains an "or" logical operator, and the sub-query statements separated by the "or" logical operator in the multi-table associated query statement are sub-query statements of different data tables, then updating the "or" logical operator to an "and" logical operator to obtain an operator update query statement after the database query statement is converted; then splitting the operator update query statement into multiple second single-table query statements; and obtaining the second database insert statement based on the multiple second single-table query statements.

[0114] For example, the multi-table association query statement is: select * from A,B where A.a1='1'and A.a1=B.b1 and B.b2='2'or A.a2='3'

[0115] The associated query condition is where A.a1='1'and A.a1=B.b1 and B.b2='2'or A.a2='3', which contains the "or" logical operator.

[0116] One of the subqueries is select*from A,B where A.a1='1'and A.a1=B.b1and B.b2='2', which contains queries for both tables A and B; the other is select*from A,B where A.a2='3', which only contains queries for table A. Obviously, the two subqueries are subqueries for different data tables. At this time, although the two subqueries are still connected by or, the query result of table A is constrained by table B. In this case, a data record may be the result of the conflict between the two subqueries, and it must be discarded. Therefore, here we need to convert or into and to obtain the operator update query statement.

[0117] Furthermore, the operator update query statement is split into multiple second single-table query statements, and the query of each data table can be converted to obtain a second single-table query statement. This can be implemented specifically according to a pre-set conversion rule, as long as the converted multiple second single-table query statements meet the equivalent query result of the operator update query statement. For example, for a second single-table query statement, the condition containing the table in the multi-table association query statement is extracted separately as the query condition of the second single-table query statement.

[0118] The multiple second single-table query statements are specifically:

[0119] Select*from A where A.a1='1'and a IN(select b1 from B where B.b2='2')and A.a2='3';

[0120] Select*from B where Bb='b'.

[0121] Then, according to the multiple second single-table query statements, the process of obtaining the second database insert statement is specifically as follows:

[0122] One implementation is to convert the multiple second single-table query statements according to a preset conversion rule. It is also possible to obtain the corresponding second single-table insert statement according to the query result obtained by executing the multi-table associated query statement and the data records in the query result:

[0123] insert ignore into A(a1)values('1');

[0124] insert ignore into B(b2)values('2');

[0125] insert ignore into A(a2)values('3').

[0126] (4) Converting the database query statement into a second database insert statement includes: if the database query statement includes a subquery statement, and the data table of the subquery statement is different from the data table outside the subquery statement, eliminating the subquery statement to obtain a third single-table query statement after the database query statement is converted; and obtaining the second database insert statement according to the third single-table query statement.

[0127] If the database query statement includes a sub-query statement, and the data table of the sub-query statement is the same as the data table outside the sub-query statement, it is necessary to recursively enter the sub-query.

[0128] For example, the database query statement is Select*from(select a from A where a='1'), wherein the subquery statement is select a from A where a='1', and the data table of the subquery statement and the data table outside the subquery statement are the same data table A, then it is necessary to recursively enter the subquery.

[0129] The first recursion obtains the third single-table query statement select*from A where a='1', and then the second database insert statement can be further obtained: insert ignore into A(a)values('1').

[0130] One implementation is to execute the third single-table query statement, and obtain the database insert statement corresponding to the data record in the query result according to the query result of the third single-table query statement. It can also be based on a preset conversion rule. For example, select*from A where a='1' is a single-value query statement, and the conversion rule can be directly converted into a keyword corresponding to the insert statement.

[0131] If the database query statement includes a sub-query statement, and the data table of the sub-query statement and the data table outside the sub-query statement are different data tables, for example, the database query statement is select a, (select b from B where b=a) from A.

[0132] Among them, the subquery statement is (select b from B where b=a), and the data table B of the subquery statement is different from the data table A outside the subquery statement. Then the logic of table B can be directly eliminated, and the third single-table query statement is obtained as Select*from A.

[0133] This is because a and b are two columns, and the subquery statement only affects the result of b. However, no matter what the query result of B in the subquery statement is, it will not affect the result of the query of column a in table A. Therefore, it can be eliminated directly. We only need to care about the data records in table A that satisfy column a.

[0134] Step 105 may be performed in the following manner:

[0135] When the unit module under test interacts with a non-database external service test, an interactive service request is intercepted, and a data record corresponding to the interactive service request is read from the test database according to the interactive service request; the data record corresponding to the interactive service request is converted into interactive data in a data format corresponding to the interactive service request; a unit test is performed on the unit module under test according to the interactive data; and\or when the unit module under test interacts with the test database test, according to the code function of the unit module under test, data records corresponding to the code function are read from the test database; and a unit test is performed on the unit module under test according to the data record corresponding to the code function.

[0136] Specifically, you can Figure 4 As shown in the figure, by setting up the pre-processing, remote call and post-processing modules, when the interactive service request comes, the interactive service request can be intercepted by the interception server, and the specified record can be obtained from the database, such as obtaining the return result according to the request identification code, or directly intercepting the interactive data of the database. You can create a mockit object and put the return result into the handler method to return the program logic to interact with the database, and directly read the real data from the database for execution. The interceptor can also intercept other external services, such as uploading and downloading ftp files.

[0137] The specific process of finding the record can be as follows Figure 5 As shown, during unit testing, a unit test processor component uses a request tag code to map to the test database to different specified storage records. It should be noted that the storage content can be read to simulate access to real resources, encapsulate requests, respond to results, and restore to real processing actions, thereby realizing the separation of unit testing and dependent data. Unit testing only needs to implement a unified process to read the storage records, and the storage data can be directly modified to return different results for the same request. Implement local unit testing closed-loop simulation production, and test environment problems.

[0138] Steps 101 to 105 obtain database insert statements through database query statements from the interaction data logs when the production and test environment programs are executed, and save the interaction data in the form of snapshot data. Then, during unit testing, execute the database insert statement to obtain the snapshot data, and encapsulate the snapshot data together (such as in a mockit object). This solves the problem of restoring the end-to-end simulation of program execution in the production and test environments. If you need to refine the return results, you can directly modify the snapshot data without developing code. When the dependent service interface fields change, you only need to obtain the latest interaction data to overwrite the previous ones, without separate processing. Steps 101 to 105 have no code intrusion on the program logic, and there is no need to process the methods that need to be intercepted.

[0139] After step 105, if the unit test is completed, the transaction rollback can be used to restore the test database after adding, deleting, and modifying the data, which can ensure repeated execution without repeated maintenance.

[0140] like Figure 6 As shown, the present invention provides a unit testing device, including: an acquisition module 601, used to acquire a historical interaction log of a unit module to be tested; a processing module 602, used to parse the non-database interaction log into a first database insert statement if the historical interaction log includes a non-database interaction log; and\or if the historical interaction log includes a database interaction log, parse the database interaction log into a database query statement; convert the database query statement into a second database insert statement; execute the first database insert statement and\or the second database insert statement on a test database; and a testing module 603, used to perform unit testing on the unit module to be tested according to the test database.

[0141] Optionally, the processing module 602 is specifically used for: if the database query statement is a multi-table connection statement, using each layer of connection conditions in the multi-table connection statement as the query condition of the connection data table of the connection condition of that layer, to obtain a single-table compound query statement after the multi-table connection statement is converted; according to the single-table compound query statement, obtaining the second database insert statement.

[0142] Optionally, the processing module 602 is specifically used for: if the database query statement is a multi-table associated query statement, and the associated query conditions in the multi-table associated query statement do not contain an "or" logical operator, or the sub-query statements separated by the "or" logical operator in the multi-table associated query statement are sub-query statements of the same data table, then the multi-table associated query statement is split into multiple first single-table query statements; and the second database insert statement is obtained according to the multiple first single-table query statements.

[0143] Optionally, the processing module 602 is specifically used for: if the database query statement is a multi-table associated query statement, and the associated query conditions in the multi-table associated query statement contain an "or" logical operator, and the sub-query statements separated by the "or" logical operator in the multi-table associated query statement are sub-query statements of different data tables, then the "or" logical operator is updated to an "and" logical operator to obtain an operator update query statement after the database query statement is converted; then the operator update query statement is split into multiple second single-table query statements; and according to the multiple second single-table query statements, the second database insert statement is obtained.

[0144] Optionally, the processing module 602 is specifically used for: if the database query statement includes a sub-query statement, and the data table of the sub-query statement is different from the data table outside the sub-query statement, then eliminating the sub-query statement to obtain a third single-table query statement after the database query statement is converted; and obtaining the second database insert statement according to the third single-table query statement.

[0145] Optionally, the non-database interaction log includes a request message log and a response message log; the processing module 602 is specifically used to: parse the first serial number, the first request identification code and the request message log into a first sub-insert statement; parse the first serial number, the first request identification code and the response message log into a second sub-insert statement; use the first sub-insert statement and the second sub-insert statement as the first database insert statement; parse the second serial number, the second request identification code, the database statement in the database interaction log and the database parameters in the database interaction log into the database query statement.

[0146] Optionally, the test module 603 is specifically used to: when the unit module under test interacts with an external service test other than a database, intercept the interactive service request, and read the data record corresponding to the interactive service request from the test database; convert the data record corresponding to the interactive service request into interactive data in a data format corresponding to the interactive service request; perform unit testing on the unit module under test according to the interactive data; and\or when the unit module under test interacts with the test database test, according to the code function of the unit module under test, read the data record corresponding to the code function from the test database; and perform unit testing on the unit module under test according to the data record corresponding to the code function.

[0147] An embodiment of the present application provides a computer device, including a program or an instruction, which, when executed, is used to execute a unit testing method and any optional method provided in the embodiment of the present application.

[0148] An embodiment of the present application provides a storage medium, including a program or an instruction, which, when executed, is used to execute a unit testing method and any optional method provided in an embodiment of the present application.

[0149] Finally, it should be noted that: those skilled in the art should understand that the embodiments of the present application can be provided as methods, systems, or computer program products. Therefore, the present application can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present application can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, optical storage, etc.) containing computer-usable program code.

[0150] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0151] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0152] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.

Claims

1. A unit testing method, characterized in that: include: Get the historical interaction log of the unit module under test; If the historical interaction log includes a non-database interaction log, parsing the non-database interaction log into a first database insert statement; and\or If the historical interaction log includes a database interaction log, parsing the database interaction log into a database query statement; converting the database query statement into a second database insert statement; Execute the first database insert statement and / or the second database insert statement on the test database; Performing unit testing on the unit module to be tested according to the test database; The step of converting the database query statement into a second database insert statement includes: If the database query statement is a multi-table connection statement, each layer of connection conditions in the multi-table connection statement is used as a query condition of the connection data table of the connection condition of the layer, to obtain a single-table composite query statement after the multi-table connection statement is converted; According to the single-table compound query statement, the second database insert statement is obtained.

2. The method according to claim 1, characterized in that The converting the database query statement into a second database insert statement also includes: If the database query statement is a multi-table associated query statement, and the associated query condition in the multi-table associated query statement does not contain an "or" logical operator, or the sub-query statements separated by the "or" logical operator in the multi-table associated query statement are sub-query statements of the same data table, then the multi-table associated query statement is split into multiple first single-table query statements; The second database insert statement is obtained according to the multiple first single-table query statements.

3. The method according to claim 1, characterized in that The converting the database query statement into a second database insert statement also includes: If the database query statement is a multi-table associated query statement, and the associated query condition in the multi-table associated query statement contains an "or" logical operator, and the sub-query statements separated by the "or" logical operator in the multi-table associated query statement are sub-query statements of different data tables, then the "or" logical operator is updated to an "and" logical operator to obtain the operator-updated query statement after the database query statement is converted; then splitting the operator update query statement into multiple second single-table query statements; The second database insert statement is obtained according to the multiple second single-table query statements.

4. The method according to claim 1, characterized in that The converting the database query statement into a second database insert statement also includes: If the database query statement includes a subquery statement, and the data table of the subquery statement is different from the data table outside the subquery statement, then the subquery statement is eliminated to obtain a third single-table query statement after the database query statement is converted; The second database insert statement is obtained according to the third single-table query statement.

5. The method according to any one of claims 1 to 4, characterized in that: The non-database interaction log includes a request message log and a response message log; the parsing of the non-database interaction log into a first database insert statement includes: Parsing the first serial number, the first request identification code and the request message log into a first sub-insert statement; Parsing the first serial number, the first request identification code and the response message log into a second sub-insert statement; Using the first sub-insert statement and the second sub-insert statement as the first database insert statement; The step of parsing the database interaction log into a database query statement comprises: The second serial number, the second request identification code, the database statement in the database interaction log, and the database parameters in the database interaction log are parsed into the database query statement.

6. The method according to any one of claims 1 to 4, characterized in that: The performing unit test on the unit module to be tested according to the test database comprises: When the unit module under test interacts with an external service test other than a database, intercept an interactive service request and read a data record corresponding to the interactive service request from the test database; convert the data record corresponding to the interactive service request into interactive data in a data format corresponding to the interactive service request; perform unit testing on the unit module under test according to the interactive data; and\or When the unit module under test interacts with the test database for testing, data records corresponding to the code functions of the unit module under test are read from the test database; and unit testing is performed on the unit module under test according to the data records corresponding to the code functions.

7. A unit testing device, characterized in that: include: An acquisition module is used to obtain the historical interaction logs of the unit module under test; a processing module, configured to parse the non-database interaction log into a first database insert statement if the historical interaction log includes a non-database interaction log; and / or parse the database interaction log into a database query statement if the historical interaction log includes a database interaction log; convert the database query statement into a second database insert statement; and execute the first database insert statement and / or the second database insert statement on a test database; A test module, used for performing unit testing on the unit module to be tested according to the test database; The processing module is specifically used to, if the database query statement is a multi-table connection statement, use each layer of connection conditions in the multi-table connection statement as the query condition of the connection data table of the layer of connection conditions, to obtain a single-table compound query statement after the multi-table connection statement is converted; and obtain the second database insert statement according to the single-table compound query statement.

8. A computer device, characterized in that: The method comprises a program or an instruction, and when the program or the instruction is executed, the method according to any one of claims 1 to 6 is performed.

9. A storage medium, characterized in that: The method comprises a program or an instruction, and when the program or the instruction is executed, the method according to any one of claims 1 to 6 is performed.

Citation Information

Patent Citations

  • Method and system for preparing interface testing data

    CN103164312A