CN112860585B - Test script assertion generation method and device - Google Patents

Test script assertion generation method and device Download PDF

Info

Publication number
CN112860585B
CN112860585B CN202110350690.3A CN202110350690A CN112860585B CN 112860585 B CN112860585 B CN 112860585B CN 202110350690 A CN202110350690 A CN 202110350690A CN 112860585 B CN112860585 B CN 112860585B
Authority
CN
China
Prior art keywords
assertion
statement
unit
code
generating
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202110350690.3A
Other languages
Chinese (zh)
Other versions
CN112860585A (en
Inventor
朱伟健
吴能斌
黄菁
利德新
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Industrial and Commercial Bank of China Ltd ICBC
Original Assignee
Industrial and Commercial Bank of China Ltd ICBC
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Industrial and Commercial Bank of China Ltd ICBC filed Critical Industrial and Commercial Bank of China Ltd ICBC
Priority to CN202110350690.3A priority Critical patent/CN112860585B/en
Publication of CN112860585A publication Critical patent/CN112860585A/en
Application granted granted Critical
Publication of CN112860585B publication Critical patent/CN112860585B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3604Software analysis for verifying properties of programs
    • G06F11/3608Software analysis for verifying properties of programs using formal methods, e.g. model checking, abstract interpretation
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3688Test management for test execution, e.g. scheduling of test suites
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Computer Hardware Design (AREA)
  • Quality & Reliability (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Software Systems (AREA)
  • Investigating Or Analysing Biological Materials (AREA)

Abstract

The embodiment of the application provides a test script assertion generating method and device, which can be used in the technical field of information security, and the method comprises the following steps: analyzing a target source code and generating a data blood relationship of the target source code; generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method which is used for being called when the source code is tested. The method and the device avoid the situation that the absolutely speaking data are inaccurate, and ensure the comprehensiveness of the absolutely speaking fields by checking the assertion coverage result.

Description

Test script assertion generation method and device
Technical Field
The application relates to the technical field of computers, in particular to a test script assertion generation method and device.
Background
In the software testing process, automated testing is a common testing means. The assertion of the automated test script is typically generated by a tool or written manually by a tester. For automatic test script assertion generated by a tool, generally, only simple assertion is performed on a transaction result, or assertion of a specific combination is generated by pre-configuration, and no accurate assertion is performed on a test scene; for assertions written manually by a tester, if the tester is not familiar with the function under test, then missing critical field assertions, or miswriting assertions of the expected result may occur.
Disclosure of Invention
Aiming at the problems in the prior art, the application provides a test script assertion generating method and device, wherein the method comprises the following steps: analyzing a target source code and generating a data blood relationship of the target source code; generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method which is used for being called when the source code is tested. The method and the device avoid the situation that the absolutely speaking data are inaccurate, and ensure the comprehensiveness of the absolutely speaking fields by checking the assertion coverage result.
In one aspect of the present invention, a test script assertion generating method is provided, including:
analyzing a target source code and generating a data blood relationship of the target source code;
generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method which is used for being called when the source code is tested.
In a preferred embodiment, the parsing the target source code, generating the data blood relationship of the target source code, includes:
performing lexical analysis on the target source code;
and establishing a data blood relationship of the target source code according to the sentence types obtained by the lexical analysis.
In a preferred embodiment, further comprising: dividing the target source code into a plurality of code units; the lexical analysis of the target source code includes: and respectively performing lexical analysis on each code unit.
In a preferred embodiment, the code unit is a line of codes, or the code unit comprises a set character length.
In a preferred embodiment, the establishing the data blood relationship of the target source code according to the sentence types obtained by the lexical analysis includes:
judging the statement type of each code unit one by one according to the sequence of the target source code of each code unit;
and executing at least one construction operation according to a set rule based on the statement type to obtain the data blood relationship of the target source code.
In a preferred embodiment, the data lineage relationship includes a plurality of data lineage cells, each data lineage cell including: judging a correct list, judging an error list and judging a conditional statement; executing at least one construction operation according to a set rule based on the sentence type, including:
if the statement type of the code unit is an assignment statement, inserting the code unit into a judging correct list in the relative data blood-edge unit of the previous level;
If the statement type of the code unit is a judging statement, creating a relative next-level data blood-edge unit, recording the code unit to a judging condition statement of the next-level data blood-edge unit, inserting the next-level data blood-edge unit into the relative previous-level data unit, inserting the branch judged to be correct in the code unit into a judging correct list of the next-level data blood-edge unit, and inserting the branch judged to be incorrect into a judging error list of the next-level data blood-edge unit.
In a preferred embodiment, the generating an assertion class according to the data blood relationship includes:
traversing all data blood edge units in the data blood edge relationship;
and generating corresponding assertion statement according to the statement type recorded in the data blood-edge unit, and further generating the assertion class.
In a preferred embodiment, the generating a corresponding assertion statement according to the statement type recorded in the data blood edge unit includes:
if the statement type is an assignment statement, the assignment statement is used as a corresponding assertion statement;
if the statement type is a judging statement, taking the recorded judging condition statement as a judging condition of the asserting statement, taking the recorded judging correct list as a branch of the asserting statement, and taking the recorded judging error list as a branch of the asserting statement error;
And if the statement type is a database operation statement, generating a corresponding assertion statement according to the database operation type.
In a preferred embodiment, the generating a corresponding assertion statement according to the database operation type includes:
if the database operation type is insertion, adding a query record with the same name as a key in a source code database mapping file into a preset assertion mapping file, taking the record number of a return table as an actual value of assertion, taking 1 as an expected value of assertion, and generating a corresponding assertion statement;
if the operation type of the database is update, adding a query record with the same name as a key in a source code database mapping file into a preset assertion mapping file, taking the record number of a return table as an actual value of assertion, taking a variable represented by the key value in a database statement corresponding to the source code as an expected value of assertion, and sequentially generating corresponding assertion statements according to query condition fields;
if the database operation type is deletion, adding a query record with the same name as a key in the source code database mapping file into a preset assertion mapping file, taking the record number of the return table as the actual value of assertion, taking 0 as the expected value of assertion, and generating a corresponding assertion statement.
In still another aspect of the present invention, there is provided a test script assertion generating apparatus comprising:
the data blood-edge relation generating module analyzes the target source code and generates the data blood-edge relation of the target source code;
and the assertion class generating module is used for generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method which is used for being called when the source code is tested.
In a preferred embodiment, the data blood relationship generation module includes:
the lexical analysis unit is used for lexically analyzing the target source codes;
and the data blood-edge relation establishing unit establishes the data blood-edge relation of the target source code according to the sentence types obtained by the lexical analysis.
In a preferred embodiment, further comprising: the code unit dividing unit is specifically used for dividing the target source code into a plurality of code units; the lexical analysis unit is specifically configured to perform lexical analysis on each code unit.
In a preferred embodiment, the code unit is a line of codes, or the code unit comprises a set character length.
In a preferred embodiment, the data blood relationship establishing unit includes:
The code unit type judging unit judges the statement type of each code unit one by one according to the sequence of the target source code of each code unit;
and the data blood relationship construction unit is used for executing at least one construction operation according to a set rule based on the statement type to obtain the data blood relationship of the target source code.
In a preferred embodiment, the data lineage relationship includes a plurality of data lineage cells, each data lineage cell including: judging a correct list, judging an error list and judging a conditional statement; the data blood relationship construction unit includes:
the assignment statement construction unit is used for inserting the code unit into a judging correct list in the relative data blood-edge unit of the previous level if the statement type of the code unit is an assignment statement;
and the judging statement construction unit is used for creating a corresponding next-level data blood-edge unit if the statement type of the code unit is a judging statement, recording the code unit to a judging condition statement of the next-level data blood-edge unit, inserting the next-level data blood-edge unit into a corresponding previous-level data unit, inserting the branch judged to be correct in the code unit into a judging correct list of the next-level data blood-edge unit, and inserting the branch judged to be wrong into a judging error list of the next-level data blood-edge unit.
In a preferred embodiment, the assertion class generating module includes:
the traversing unit traverses all the data blood edge units in the data blood edge relationship;
and the assertion generating unit is used for generating corresponding assertion statements according to statement types recorded in the data blood-edge unit, so as to generate the assertion class.
In a preferred embodiment, the assertion generating unit includes:
the assignment statement assertion generating unit is used for taking the assignment statement as a corresponding assertion statement if the statement type is the assignment statement;
the judging statement assertion generating unit takes the recorded judging condition statement as the judging condition of the assertion statement, takes the recorded judging correct list as the branch of the assertion statement, and takes the recorded judging error list as the branch of the assertion statement error if the statement type is the judging statement;
and the database operation statement assertion generating unit generates a corresponding assertion statement according to the database operation type if the statement type is the database operation statement.
In a preferred embodiment, the database operation statement assertion generating unit includes:
an insertion operation assertion generating unit, if the database operation type is insertion, adding a query record with the same name as a key in a source code database mapping file into a preset assertion mapping file, taking the record number of a return table as an actual value of assertion, taking 1 as an expected value of assertion, and generating a corresponding assertion statement;
An updating operation assertion generating unit, if the database operation type is updating, adding a query record with the same name as a key in a source code database mapping file in a preset assertion mapping file, taking the record number of a return table as an actual value of assertion, taking a variable represented by a key value in a database statement corresponding to a source code as an expected value of assertion, and sequentially generating corresponding assertion statements according to a query condition field;
and if the database operation type is deleting, adding a query record with the same name as a key in the source code database mapping file into a preset assertion mapping file, taking the record number of the return table as the actual value of the assertion, taking 0 as the expected value of the assertion, and generating a corresponding assertion statement.
In yet another aspect of the present invention, an electronic device is provided that includes a memory, a processor, and a computer program stored on the memory and executable on the processor, the processor implementing the test script assertion generating method when executing the program.
In yet another aspect of the present invention, a computer-readable storage medium is provided, on which a computer program is stored, which when executed by a processor implements the test script assertion generating method.
According to the technical scheme, the test script assertion generating method comprises the following steps: analyzing a target source code and generating a data blood relationship of the target source code; generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method which is used for being called when the source code is tested. The method and the device avoid the situation that the absolutely speaking data are inaccurate, and ensure the comprehensiveness of the absolutely speaking fields by checking the assertion coverage result.
Drawings
In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings that are required in the embodiments or the description of the prior art will be briefly described, and it is obvious that the drawings in the following description are some embodiments of the present application, and other drawings may be obtained according to these drawings without inventive effort for a person skilled in the art.
FIG. 1 is a flow diagram of a test script assertion generation method.
FIG. 2 is a schematic diagram of a data blood relationship generation flow.
Fig. 3 is a schematic diagram of an assertion method generation flow.
FIG. 4 is a schematic diagram of data blood relationship in an embodiment.
FIG. 5 is a schematic diagram of a test script assertion generating apparatus.
Fig. 6 is a schematic structural diagram of an electronic device in an embodiment of the present application.
Detailed Description
For the purposes of making the objects, technical solutions and advantages of the embodiments of the present application more clear, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is apparent that the described embodiments are some embodiments of the present application, but not all embodiments. All other embodiments, which can be made by one of ordinary skill in the art based on the embodiments herein without making any inventive effort, are intended to be within the scope of the present application.
It should be noted that, the test script assertion generating method and device disclosed in the present application may be used in the information security field, and may also be used in any field other than the information security field, and the application field of the test script assertion generating method and device disclosed in the present application is not limited.
In the software testing process, automated testing is a common testing means. The assertion of the automated test script is typically generated by a tool or written manually by a tester. For automatic test script assertion generated by a tool, generally, only simple assertion is performed on a transaction result, or assertion of a specific combination is generated by pre-configuration, and no accurate assertion is performed on a test scene; for assertions written manually by a tester, if the tester is not familiar with the function under test, then missing critical field assertions, or miswriting assertions of the expected result may occur.
In order to solve at least one of the existing problems, the application provides a test script assertion generating method and device, wherein the method comprises the following steps: analyzing a target source code and generating a data blood relationship of the target source code; generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method which is used for being called when the source code is tested. The method and the device avoid the situation that the absolutely speaking data are inaccurate, and ensure the comprehensiveness of the absolutely speaking fields by checking the assertion coverage result.
The test script assertion generating method and device provided by the invention are described in detail below with reference to the accompanying drawings.
In a specific embodiment, the present application provides a test script assertion generating method, as shown in fig. 1, including:
s1, analyzing a target source code and generating a data blood relationship of the target source code;
specifically, a relationship is naturally formed between data from generation, processing fusion, circulation and circulation to final extinction. This relationship between data is referred to as the blood relationship of the data by reference to a similar relationship in human society. Through the data blood relationship of the source code, the source and the processing process of any variable can be traced, so that the assertion of the test script can be accurately generated. The parsing the target source code, generating the data blood relationship of the target source code, as shown in fig. 2, includes:
S11, performing lexical analysis on the target source code;
specifically, for the establishment of data blood relationship, the lexical analysis of the code is relied on. In a specific embodiment, the target source code is divided into a plurality of code units; and respectively performing lexical analysis on each code unit. Wherein the code unit is a line of codes, or the code unit comprises a set character length. In a specific embodiment, the code unit includes a set character length, which is understood to include a plurality of lines of codes, where the lines of codes belong to the same syntax structure, such as a cyclic syntax, and a judgment syntax, and the syntax structures include a plurality of lines of source codes. For example, assume that the target source code includes the following statements: int a=1; int b=2; if (a > b) { a=0 } else { b=0 }; dividing the source code into three code units, namely a unit one: int a=1 unit two: int b=2 units three: if (a > b) { a=0 } else { b=0 }. After dividing a source code into a plurality of code units, respectively performing lexical analysis on each code unit to obtain the statement type of the corresponding code unit. The lexical analysis shows that the sentence types corresponding to the three code units are as follows: the first code unit is an assignment statement, the second code unit is an assignment statement, and the third code unit is a judgment statement.
And S12, establishing a data blood relationship of the target source code according to the sentence types obtained by the lexical analysis.
Specifically, through lexical analysis of the source code, the sentence type of the code can be obtained, and the data blood relationship of the target code is established according to the sentence type. It can be understood that there are two ways to generate the data blood-edge relationship, one way is to parse the sentence types of one code unit and then add the corresponding information in the data blood-edge relationship, and the other way is to obtain the sentence types of all code units completely and then establish the data blood-edge relationship. Both approaches are feasible and effective. The establishing the data blood relationship of the target source code according to the sentence types obtained by the lexical analysis comprises the following steps:
judging the statement type of each code unit one by one according to the sequence of the target source code of each code unit, and executing at least one construction operation according to a set rule based on the statement type to obtain the data blood-edge relation of the target source code. The data lineage relationship includes a plurality of data lineage cells, each data lineage cell including: judging a correct list, judging an error list and judging a conditional statement, and executing at least one construction operation according to a set rule based on the statement type, wherein the construction operation comprises the following steps:
If the statement type of the code unit is an assignment statement, inserting the code unit into a judging correct list in the relative data blood-edge unit of the previous level;
if the statement type of the code unit is a judging statement, creating a relative next-level data blood-edge unit, recording the code unit to a judging condition statement of the next-level data blood-edge unit, inserting the next-level data blood-edge unit into the relative previous-level data unit, inserting the branch judged to be correct in the code unit into a judging correct list of the next-level data blood-edge unit, and inserting the branch judged to be incorrect into a judging error list of the next-level data blood-edge unit.
In a specific embodiment, it is assumed that the target source code has four code units, where the order in which they are located is named code unit one, two, three, four. The method comprises the steps of obtaining a code unit I as a judgment statement through lexical analysis, obtaining a code unit II as an assignment statement, obtaining a code unit III as a judgment statement, and obtaining a code unit IV as a judgment statement, wherein the code units I, II and III are of the same level, and the code unit IV is of the next level in the code units III. When the data blood relationship is established, an initial data blood cell analysis class0 is firstly generated, and as the code cell I is a judgment statement, a data blood cell analysis class1 is newly established, the analysis class1 is inserted into a judgment correct list of the analysis class0, then the specific content of the analysis class1 is filled, the judgment condition of the code cell I is inserted into the judgment condition statement of the analysis class1, the judgment correct branch of the code cell I is inserted into the judgment correct list of the analysis class1, and the judgment incorrect branch of the code cell I is inserted into the judgment incorrect list of the analysis class 1. And (3) completing the operation of the first code unit, starting to analyze the second code unit, wherein the second code unit is an assignment statement, directly inserting the assignment statement of the second code unit into a correct list of analysis class0, and completing the operation of the second code unit. As for the code unit III, since the code unit III is a judgment statement, a data blood edge unit analytical class2 is newly established, analytical class2 is inserted into a judgment correct list of analytical class0, then the analytical class2 is filled according to the content of the code unit III, the judgment condition of the code unit III is inserted into a judgment condition statement of the analytical class2, the corresponding correct branch is inserted into a judgment correct list of the analytical class2, and the corresponding error branch is inserted into a judgment error list of the analytical class 2. In the filling process, the corresponding correct branch comprises a code unit IV which is also a judgment statement type, so that a data blood-source unit analysis class3 is newly established, the analysis class3 is inserted into a judgment correct list of the analysis class2, and then the analysis class3 is filled according to the content of the code unit IV. And finally, data blood relationship analysis of the whole target source code is completed, and the data blood relationship is generated.
S2, generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method which is used for being called when the source code is tested.
Specifically, after the data blood relationship of the target source code is generated, the sources and the processing process of all data in the source code are clear. The data blood relationship can accurately conduct assertion, and an assertion class used for assertion is generated, wherein the assertion class comprises an assertion method, and the assertion method comprises assertion of all data operations of the target source code. When the test script is generated, the assertion method of the assertion class can be directly called to assert the data operation of the target source code. In a specific embodiment, the generating an assertion class according to the data blood relationship, as shown in fig. 3, includes:
s21, traversing all data blood edge units in the data blood edge relationship;
specifically, the data blood-edge relation comprises a plurality of data blood-edge units, and the data blood-edge units record data statement information of the source codes, so that traversing all the data blood-edge units in the data blood-edge relation can be understood as traversing the whole target source codes, and further, the following method can cover the data of all the target source codes.
S22, generating corresponding assertion statement according to statement types recorded in the data blood-edge unit, and further generating the assertion class.
Specifically, for different statement types, the corresponding predicate statements are different. In a specific embodiment, if the statement type is an assignment statement, the assignment statement is used as a corresponding assertion statement. For example, if the assignment statement of the source code is int example=6, the assertion statement written in the assertion method is int example=6, i.e. the assertion statement is consistent with the source code statement. If the statement type is a judging statement, taking the recorded judging condition statement as a judging condition of the asserting statement, taking the recorded judging correct list as a branch of the asserting statement, and taking the recorded judging error list as a branch of the asserting statement error. For example, in the source code, the judgment statement is if (a > b) { a=0; printf (a) } else { b=100; printf (b) }, the judgment condition statement of the judgment statement is a > b, the statement recorded in the judgment correct list should be a=0, the statement recorded in the judgment error list should be b=100, so the corresponding generated assertion statement of the judgment statement should be if (a > b) { a=0 } else { b=100; printf (b).
In a specific embodiment, if the statement type is a database operation statement, a corresponding assertion statement is generated according to the database operation type. Wherein, the statement type is database operation statement, which needs to be further subdivided according to the specific database operation type: if the operation type of the database is insertion, adding a query record with the same name as a key in a source code database mapping file into a preset assertion mapping file, taking the record number of a return table as an actual value of assertion, taking 1 as an expected value of assertion, and generating a corresponding assertion statement. For example, the database operation statement in the source code is dbexecutor. Insert (atdbmapper. Insert_medialimttable, input), and the corresponding database statement is:
the inserted key name is insert_MedLimtTable, so the corresponding generated query record in the predicate mapping file is
If the insertion is successful, the number of the returned table records of the query record is 1, and if the insertion is unsuccessful, the number of the returned table records is 0, so that the number of the returned table records is taken as the actual value of the assertion, 1 is taken as the expected value of the assertion, and the following assertion statement is generated:
Assert.assertEquals(DBExecutor.insert(“AtDBMapper.insert_MedLimitTable”,input),1)
if the operation type of the database is update, adding a query record with the same name as a key in a source code database mapping file into a preset assertion mapping file, taking the record number of a return table as an actual value of assertion, taking a variable represented by the key value in a database statement corresponding to the source code as an expected value of assertion, and sequentially generating corresponding assertion statements according to query condition fields. For example, the database operation statement in the source code is DBExecutor.update (AtDBM applicator.update_medium_with_cardno, input), and the database statement corresponding to the statement is
Its key name is update_MedLimitTable_by_cardno, so the corresponding query generated in the assertion mapping file is recorded as
If the updating is successful, the values of all the queried fields are consistent with the input values, so the variables represented by the key values are used as expected values of the assertion, and corresponding assertion sentences are sequentially generated according to the query condition fields, as follows:
tmpBean=DBExecutor.update(“AtDBMapper.insert_MedLimitTable”,input);
Assert.assertEquals(tmpBean.limit_amout,input.limit_amout);
Assert.assertEquals(tmpBean.status,input.status);
if the database operation type is deletion, adding a query record with the same name as a key in the source code database mapping file into a preset assertion mapping file, taking the record number of the return table as the actual value of assertion, taking 0 as the expected value of assertion, and generating a corresponding assertion statement. For example, the database operation statement in the source code is dbexecutor. Delete (atdbmapper. Delete_medialimttable, input), and the corresponding database statement is:
the inserted key name is delete_MedLimtTable, so the query record correspondingly generated in the assertion mapping file is
If the deletion is successful, the number of the returned table records of the query record is 0, and if the deletion is unsuccessful, the number of the returned table records is 1, so that the number of the returned table records is taken as the actual value of the assertion, 0 is taken as the expected value of the assertion, and the following assertion statement is generated:
Assert.assertEquals(DBExecutor.delete(“AtDBMapper.insert_MedLimitTable”,input),0)
The invention is further described in connection with a specific scenario where an object source code is present from which corresponding test script assertions are generated for testing the object source code. According to the method provided by the application, firstly, the target source code is subjected to lexical analysis, the target source code is divided into a plurality of code units, each code unit can be one line of codes or a plurality of lines of codes, in the embodiment, the target source code is divided into five code units, wherein the first code unit and the second code unit are in the same layer, and the third code unit, the fourth code unit and the fifth code unit are in the next layer. And respectively performing lexical analysis on each code unit to obtain the statement type of each code unit, wherein the first code unit is an assignment statement, the second code unit is a judgment statement, the third code unit is a database operation statement, the fourth code unit is a judgment statement, and the fifth code unit is a database operation statement. According to the step of generating the data blood-edge relation provided by the application, an initialized data blood-edge unit analysis class0 is firstly established, and as the code unit I is an assignment statement, the judgment condition and the judgment error list of the analysis class0 are set to be null values, and the assignment statement of the code unit I is inserted into the judgment correct list of the analysis class 0. Then judging the statement type of the next code unit, if the code unit II is a judging statement, then establishing a new data blood edge unit analytical class1, inserting the whole data blood edge unit analytical class1 into a judging correct list of analytical class0, filling the content of the analytical class1, placing the judging condition in the code unit II into the judging condition of the analytical class1, inserting the branch which is judged to be correct in the code unit II into the judging correct list of the analytical class1, and inserting the branch which is judged to be incorrect in the code unit II into the judging error list of the analytical class 1. The code unit III is in the correct branch of the code unit II, which is the database operation statement, and then the database operation statement is directly inserted into the judging correct list of the analysis class 1. Code element four is in the error branch of code element two, which is an assignment statement inserted directly into the error branch of analysclass 1. And if the code unit five is in the error branch of the code unit two and is of the database operation statement type, the database operation statement of the code unit five is directly inserted into the judgment error list of the analysis class 1. The data blood relationship of the source code may be generated so far as shown in fig. 4.
After the data blood edge relation is generated, traversing all the data blood edge units in the data blood edge relation, and generating an assertion class containing an assertion method according to statement types recorded by the data blood edge units. In this implementation scenario, starting from analytical class0, the first piece of the record is an assignment statement, so the assignment statement is directly written into the assertion method. The second item recorded is analysis class1 and corresponds to a judgment statement, so that judgment conditions recorded in analysis class1 are written into judgment conditions of the assertion statement, contents recorded in a judgment correct list of analysis class1 are written into a correct branch of the assertion statement, and a judgment error list is written into an error branch of the assertion statement. By analogy, the following predicate classes are ultimately generated:
Public class AssertDemoClass{
Public viod assertFunction(DemoClassBean input){
Input.limit_amout=input.limit_amout+100;
If(input.operflag==1){
Assert.assertEquals(DBExecutor.insert(“AtDBMapper.insert_MedLimitTable”,input),1);
Else
{
Input.status=1;
DemoClassBean
tmpBean=DBExcutor.update(“AtDBMapper.insert_MedLimitTable”,input);
Assert.assertEquals(tmpBean.limit_amout,input.limit_amout);
Assert.assertEquals(tmpBean.status,input.status);
}
}
}
}
as can be seen from the above description, the method for generating test script assertion provided by the present invention includes: analyzing a target source code and generating a data blood relationship of the target source code; generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method which is used for being called when the source code is tested. The method and the device avoid the situation that the absolutely speaking data are inaccurate, and ensure the comprehensiveness of the absolutely speaking fields by checking the assertion coverage result.
From the software aspect, the application provides an embodiment of a test script assertion generating device for executing all or part of the content in the test script assertion generating method, referring to fig. 5, the test script assertion generating device specifically includes the following contents:
the data blood-edge relation generating module 1 analyzes the target source code and generates the data blood-edge relation of the target source code;
and the assertion class generating module 2 generates an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method which is used for being called when the source code is tested.
As can be seen from the above description, the test script assertion generating device provided by the present invention first analyzes a target source code and generates a data blood relationship of the target source code; and then generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method which is used for being called when the source code is tested. The method and the device avoid the situation that the absolutely speaking data are inaccurate, and ensure the comprehensiveness of the absolutely speaking fields by checking the assertion coverage result.
In a specific embodiment, the present application provides a test script assertion generating device, including:
The data blood relationship generating module 1 is used for executing analysis target source codes and generating data blood relationship of the target source codes;
specifically, a relationship is naturally formed between data from generation, processing fusion, circulation and circulation to final extinction. This relationship between data is referred to as the blood relationship of the data by reference to a similar relationship in human society. Through the data blood relationship of the source code, the source and the processing process of any variable can be traced, so that the assertion of the test script can be accurately generated. In a specific embodiment, the data blood relationship generating module specifically executes the following steps:
s11, performing lexical analysis on the target source code;
specifically, for the establishment of data blood relationship, the lexical analysis of the code is relied on. In a specific embodiment, the target source code is divided into a plurality of code units; and respectively performing lexical analysis on each code unit. Wherein the code unit is a line of codes, or the code unit comprises a set character length. In a specific embodiment, the code unit includes a set character length, which is understood to include a plurality of lines of codes, where the lines of codes belong to the same syntax structure, such as a cyclic syntax, and a judgment syntax, and the syntax structures include a plurality of lines of source codes. For example, assume that the target source code includes the following statements: int a=1; int b=2; if (a > b) { a=0 } else { b=0 }; dividing the source code into three code units, namely a unit one: int a=1 unit two: int b=2 units three: if (a > b) { a=0 } else { b=0 }. After dividing a source code into a plurality of code units, respectively performing lexical analysis on each code unit to obtain the statement type of the corresponding code unit. The lexical analysis shows that the sentence types corresponding to the three code units are as follows: the first code unit is an assignment statement, the second code unit is an assignment statement, and the third code unit is a judgment statement.
And S12, establishing a data blood relationship of the target source code according to the sentence types obtained by the lexical analysis.
Specifically, through lexical analysis of the source code, the sentence type of the code can be obtained, and the data blood relationship of the target code is established according to the sentence type. It can be understood that there are two ways to generate the data blood-edge relationship, one way is to parse the sentence types of one code unit and then add the corresponding information in the data blood-edge relationship, and the other way is to obtain the sentence types of all code units completely and then establish the data blood-edge relationship. Both approaches are feasible and effective. The establishing the data blood relationship of the target source code according to the sentence types obtained by the lexical analysis comprises the following steps:
judging the statement type of each code unit one by one according to the sequence of the target source code of each code unit, and executing at least one construction operation according to a set rule based on the statement type to obtain the data blood-edge relation of the target source code. The data lineage relationship includes a plurality of data lineage cells, each data lineage cell including: judging a correct list, judging an error list and judging a conditional statement, and executing at least one construction operation according to a set rule based on the statement type, wherein the construction operation comprises the following steps:
If the statement type of the code unit is an assignment statement, inserting the code unit into a judging correct list in the relative data blood-edge unit of the previous level;
if the statement type of the code unit is a judging statement, creating a relative next-level data blood-edge unit, recording the code unit to a judging condition statement of the next-level data blood-edge unit, inserting the next-level data blood-edge unit into the relative previous-level data unit, inserting the branch judged to be correct in the code unit into a judging correct list of the next-level data blood-edge unit, and inserting the branch judged to be incorrect into a judging error list of the next-level data blood-edge unit.
In a specific embodiment, it is assumed that the target source code has four code units, where the order in which they are located is named code unit one, two, three, four. The method comprises the steps of obtaining a code unit I as a judgment statement through lexical analysis, obtaining a code unit II as an assignment statement, obtaining a code unit III as a judgment statement, and obtaining a code unit IV as a judgment statement, wherein the code units I, II and III are of the same level, and the code unit IV is of the next level in the code units III. When the data blood relationship is established, an initial data blood cell analysis class0 is firstly generated, and as the code cell I is a judgment statement, a data blood cell analysis class1 is newly established, the analysis class1 is inserted into a judgment correct list of the analysis class0, then the specific content of the analysis class1 is filled, the judgment condition of the code cell I is inserted into the judgment condition statement of the analysis class1, the judgment correct branch of the code cell I is inserted into the judgment correct list of the analysis class1, and the judgment incorrect branch of the code cell I is inserted into the judgment incorrect list of the analysis class 1. And (3) completing the operation of the first code unit, starting to analyze the second code unit, wherein the second code unit is an assignment statement, directly inserting the assignment statement of the second code unit into a correct list of analysis class0, and completing the operation of the second code unit. As for the code unit III, since the code unit III is a judgment statement, a data blood edge unit analytical class2 is newly established, analytical class2 is inserted into a judgment correct list of analytical class0, then the analytical class2 is filled according to the content of the code unit III, the judgment condition of the code unit III is inserted into a judgment condition statement of the analytical class2, the corresponding correct branch is inserted into a judgment correct list of the analytical class2, and the corresponding error branch is inserted into a judgment error list of the analytical class 2. In the filling process, the corresponding correct branch comprises a code unit IV which is also a judgment statement type, so that a data blood-source unit analysis class3 is newly established, the analysis class3 is inserted into a judgment correct list of the analysis class2, and then the analysis class3 is filled according to the content of the code unit IV. And finally, data blood relationship analysis of the whole target source code is completed, and the data blood relationship is generated.
It may be understood that the step performed by the data blood-edge relationship generating module may be understood as including a lexical analysis unit and a data blood-edge relationship establishing unit, where the lexical analysis unit performs step S11 and the data blood-edge relationship establishing unit performs step S12, and the related embodiments will not be described in detail.
And the assertion class generating module 2 is used for executing the generation of an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method which is called when the source code is tested.
Specifically, after the data blood relationship of the target source code is generated, the sources and the processing process of all data in the source code are clear. The data blood relationship can accurately conduct assertion, and an assertion class used for assertion is generated, wherein the assertion class comprises an assertion method, and the assertion method comprises assertion of all data operations of the target source code. When the test script is generated, the assertion method of the assertion class can be directly called to assert the data operation of the target source code. In a specific embodiment, the assertion class generating module specifically executes the following steps:
s21, traversing all data blood edge units in the data blood edge relationship;
Specifically, the data blood-edge relation comprises a plurality of data blood-edge units, and the data blood-edge units record data statement information of the source codes, so that traversing all the data blood-edge units in the data blood-edge relation can be understood as traversing the whole target source codes, and further, the following method can cover the data of all the target source codes.
S22, generating corresponding assertion statement according to statement types recorded in the data blood-edge unit, and further generating the assertion class.
Specifically, for different statement types, the corresponding predicate statements are different. In a specific embodiment, if the statement type is an assignment statement, the assignment statement is used as a corresponding assertion statement. For example, if the assignment statement of the source code is int example=6, the assertion statement written in the assertion method is int example=6, i.e. the assertion statement is consistent with the source code statement. If the statement type is a judging statement, taking the recorded judging condition statement as a judging condition of the asserting statement, taking the recorded judging correct list as a branch of the asserting statement, and taking the recorded judging error list as a branch of the asserting statement error. For example, in the source code, the judgment statement is if (a > b) { a=0; printf (a) } else { b=100; printf (b) }, the judgment condition statement of the judgment statement is a > b, the statement recorded in the judgment correct list should be a=0, the statement recorded in the judgment error list should be b=100, so the corresponding generated assertion statement of the judgment statement should be if (a > b) { a=0 } else { b=100; printf (b).
In a specific embodiment, if the statement type is a database operation statement, a corresponding assertion statement is generated according to the database operation type. Wherein, the statement type is database operation statement, which needs to be further subdivided according to the specific database operation type: if the operation type of the database is insertion, adding a query record with the same name as a key in a source code database mapping file into a preset assertion mapping file, taking the record number of a return table as an actual value of assertion, taking 1 as an expected value of assertion, and generating a corresponding assertion statement. For example, the database operation statement in the source code is dbexecutor. Insert (atdbmapper. Insert_medialimttable, input), and the corresponding database statement is:
the inserted key name is insert_MedLimtTable, so the corresponding generated query record in the predicate mapping file is
If the insertion is successful, the number of the returned table records of the query record is 1, and if the insertion is unsuccessful, the number of the returned table records is 0, so that the number of the returned table records is taken as the actual value of the assertion, 1 is taken as the expected value of the assertion, and the following assertion statement is generated:
Assert.assertEquals(DBExecutor.insert(“AtDBMapper.insert_MedLimitTable”,input),1)
if the operation type of the database is update, adding a query record with the same name as a key in a source code database mapping file into a preset assertion mapping file, taking the record number of a return table as an actual value of assertion, taking a variable represented by the key value in a database statement corresponding to the source code as an expected value of assertion, and sequentially generating corresponding assertion statements according to query condition fields. For example, the database operation statement in the source code is DBExecutor.update (AtDBM applicator.update_medium_with_cardno, input), and the database statement corresponding to the statement is
Its key name is update_MedLimitTable_by_cardno, so the corresponding query generated in the assertion mapping file is recorded as
If the updating is successful, the values of all the queried fields are consistent with the input values, so the variables represented by the key values are used as expected values of the assertion, and corresponding assertion sentences are sequentially generated according to the query condition fields, as follows:
tmpBean=DBExecutor.update(“AtDBMapper.insert_MedLimitTable”,input);
Assert.assertEquals(tmpBean.limit_amout,input.limit_amout);
Assert.assertEquals(tmpBean.status,input.status);
if the database operation type is deletion, adding a query record with the same name as a key in the source code database mapping file into a preset assertion mapping file, taking the record number of the return table as the actual value of assertion, taking 0 as the expected value of assertion, and generating a corresponding assertion statement. For example, the database operation statement in the source code is dbexecutor. Delete (atdbmapper. Delete_medialimttable, input), and the corresponding database statement is:
the inserted key name is delete_MedLimtTable, so the query record correspondingly generated in the assertion mapping file is
If the deletion is successful, the number of the returned table records of the query record is 0, and if the deletion is unsuccessful, the number of the returned table records is 1, so that the number of the returned table records is taken as the actual value of the assertion, 0 is taken as the expected value of the assertion, and the following assertion statement is generated:
Assert.assertEquals(DBExecutor.delete(“AtDBMapper.insert_MedLimitTable”,input),0)
The invention is further described in connection with a specific scenario where an object source code is present from which corresponding test script assertions are generated for testing the object source code. According to the method provided by the application, firstly, the target source code is subjected to lexical analysis, the target source code is divided into a plurality of code units, each code unit can be one line of codes or a plurality of lines of codes, in the embodiment, the target source code is divided into five code units, wherein the first code unit and the second code unit are in the same layer, and the third code unit, the fourth code unit and the fifth code unit are in the next layer. And respectively performing lexical analysis on each code unit to obtain the statement type of each code unit, wherein the first code unit is an assignment statement, the second code unit is a judgment statement, the third code unit is a database operation statement, the fourth code unit is a judgment statement, and the fifth code unit is a database operation statement. According to the step of generating the data blood-edge relation provided by the application, an initialized data blood-edge unit analysis class0 is firstly established, and as the code unit I is an assignment statement, the judgment condition and the judgment error list of the analysis class0 are set to be null values, and the assignment statement of the code unit I is inserted into the judgment correct list of the analysis class 0. Then judging the statement type of the next code unit, if the code unit II is a judging statement, then establishing a new data blood edge unit analytical class1, inserting the whole data blood edge unit analytical class1 into a judging correct list of analytical class0, filling the content of the analytical class1, placing the judging condition in the code unit II into the judging condition of the analytical class1, inserting the branch which is judged to be correct in the code unit II into the judging correct list of the analytical class1, and inserting the branch which is judged to be incorrect in the code unit II into the judging error list of the analytical class 1. The code unit III is in the correct branch of the code unit II, which is the database operation statement, and then the database operation statement is directly inserted into the judging correct list of the analysis class 1. Code element four is in the error branch of code element two, which is an assignment statement inserted directly into the error branch of analysclass 1. And if the code unit five is in the error branch of the code unit two and is of the database operation statement type, the database operation statement of the code unit five is directly inserted into the judgment error list of the analysis class 1. The data blood relationship of the source code may be generated so far as shown in fig. 4.
After the data blood edge relation is generated, traversing all the data blood edge units in the data blood edge relation, and generating an assertion class containing an assertion method according to statement types recorded by the data blood edge units. In this implementation scenario, starting from analytical class0, the first piece of the record is an assignment statement, so the assignment statement is directly written into the assertion method. The second item recorded is analysis class1 and corresponds to a judgment statement, so that judgment conditions recorded in analysis class1 are written into judgment conditions of the assertion statement, contents recorded in a judgment correct list of analysis class1 are written into a correct branch of the assertion statement, and a judgment error list is written into an error branch of the assertion statement. By analogy, the following predicate classes are ultimately generated:
Public class AssertDemoClass{
Public viod assertFunction(DemoClassBean input){
Input.limit_amout=input.limit_amout+100;
If(input.operflag==1){
Assert.assertEquals(DBExecutor.insert(“AtDBMapper.insert_MedLimitTable”,input),1);
Else
{
Input.status=1;
DemoClassBean
tmpBean=DBExcutor.update(“AtDBMapper.insert_MedLimitTable”,input);
Assert.assertEquals(tmpBean.limit_amout,input.limit_amout);
Assert.assertEquals(tmpBean.status,input.status);
}
}
}
}
as can be seen from the above description, the test script assertion generating device provided by the present invention is configured to execute the following steps: analyzing a target source code and generating a data blood relationship of the target source code; generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method which is used for being called when the source code is tested. The method and the device avoid the situation that the absolutely speaking data are inaccurate, and ensure the comprehensiveness of the absolutely speaking fields by checking the assertion coverage result.
From the hardware aspect, the application provides an embodiment of an electronic device for implementing all or part of contents in a test script assertion generating method, where the electronic device specifically includes the following contents:
fig. 6 is a schematic block diagram of a system configuration of an electronic device 9600 of an embodiment of the present application. As shown in fig. 6, the electronic device 9600 may include a central processor 9100 and a memory 9140; the memory 9140 is coupled to the central processor 9100. Notably, this fig. 6 is exemplary; other types of structures may also be used in addition to or in place of the structures to implement telecommunications functions or other functions.
In one embodiment, the test script assertion generating functionality may be integrated into the central processor. Wherein the central processor may be configured to control:
s1, analyzing a target source code and generating a data blood relationship of the target source code;
s2, generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method which is used for being called when the source code is tested.
From the above description, the electronic device provided by the embodiment of the application avoids the situation that the assertion data is inaccurate, and ensures the comprehensiveness of the assertion field by checking the assertion coverage result.
In another embodiment, the test script assertion generating device may be configured separately from the central processor 9100, for example, the test script assertion generating device may be configured as a chip connected to the central processor 9100, and the test script assertion generating function is implemented by control of the central processor.
As shown in fig. 6, the electronic device 9600 may further include: a communication module 9110, an input unit 9120, an audio processor 9130, a display 9160, and a power supply 9170. It is noted that the electronic device 9600 need not include all of the components shown in fig. 6; in addition, the electronic device 9600 may further include components not shown in fig. 6, and reference may be made to the related art.
As shown in fig. 6, the central processor 9100, sometimes referred to as a controller or operational control, may include a microprocessor or other processor device and/or logic device, which central processor 9100 receives inputs and controls the operation of the various components of the electronic device 9600.
The memory 9140 may be, for example, one or more of a buffer, a flash memory, a hard drive, a removable media, a volatile memory, a non-volatile memory, or other suitable device. The information about failure may be stored, and a program for executing the information may be stored. And the central processor 9100 can execute the program stored in the memory 9140 to realize information storage or processing, and the like.
The input unit 9120 provides input to the central processor 9100. The input unit 9120 is, for example, a key or a touch input device. The power supply 9170 is used to provide power to the electronic device 9600. The display 9160 is used for displaying display objects such as images and characters. The display may be, for example, but not limited to, an LCD display.
The memory 9140 may be a solid state memory such as Read Only Memory (ROM), random Access Memory (RAM), SIM card, etc. But also a memory which holds information even when powered down, can be selectively erased and provided with further data, an example of which is sometimes referred to as EPROM or the like. The memory 9140 may also be some other type of device. The memory 9140 includes a buffer memory 9141 (sometimes referred to as a buffer). The memory 9140 may include an application/function storage portion 9142, the application/function storage portion 9142 storing application programs and function programs or a flow for executing operations of the electronic device 9600 by the central processor 9100.
The memory 9140 may also include a data store 9143, the data store 9143 for storing data, such as contacts, digital data, pictures, sounds, and/or any other data used by an electronic device. The driver storage portion 9144 of the memory 9140 may include various drivers of the electronic device for communication functions and/or for performing other functions of the electronic device (e.g., messaging applications, address book applications, etc.).
The communication module 9110 is a transmitter/receiver 9110 that transmits and receives signals via an antenna 9111. A communication module (transmitter/receiver) 9110 is coupled to the central processor 9100 to provide input signals and receive output signals, as in the case of conventional mobile communication terminals.
Based on different communication technologies, a plurality of communication modules 9110, such as a cellular network module, a bluetooth module, and/or a wireless local area network module, etc., may be provided in the same electronic device. The communication module (transmitter/receiver) 9110 is also coupled to a speaker 9131 and a microphone 9132 via an audio processor 9130 to provide audio output via the speaker 9131 and to receive audio input from the microphone 9132 to implement usual telecommunications functions. The audio processor 9130 can include any suitable buffers, decoders, amplifiers and so forth. In addition, the audio processor 9130 is also coupled to the central processor 9100 so that sound can be recorded locally through the microphone 9132 and sound stored locally can be played through the speaker 9131.
The embodiments of the present application also provide a computer-readable storage medium capable of implementing all the steps in the test script assertion generating method in the above embodiments, where the computer-readable storage medium stores a computer program that, when executed by a processor, implements all the steps in the test script assertion generating method in the above embodiments in which an execution subject is a server or a client, for example, the processor implements the following steps when executing the computer program:
S1, analyzing a target source code and generating a data blood relationship of the target source code;
s2, generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method which is used for being called when the source code is tested.
As can be seen from the above description, the computer readable storage medium provided in the embodiments of the present application avoids the situation that the assertion data is inaccurate, and ensures the comprehensiveness of the assertion field by checking the assertion coverage result.
It will be apparent to those skilled in the art that embodiments of the present invention may be provided as a method, apparatus, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present invention may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present invention is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (devices), and computer program products according to embodiments of the invention. It will be understood that each flow and/or block of the flowchart illustrations and/or block diagrams, and combinations of flows and/or blocks in the flowchart illustrations and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
The principles and embodiments of the present invention have been described in detail with reference to specific examples, which are provided to facilitate understanding of the method and core ideas of the present invention; meanwhile, as those skilled in the art will have variations in the specific embodiments and application scope in accordance with the ideas of the present invention, the present description should not be construed as limiting the present invention in view of the above.

Claims (14)

1. A test script assertion generation method, comprising:
performing lexical analysis on the target source code;
judging the statement type of each code unit one by one according to the sequence of the target source code of each code unit;
executing at least one construction operation according to a set rule based on the statement type to obtain a data blood relationship of the target source code;
traversing all data blood edge units in the data blood edge relationship;
generating a corresponding assertion statement according to the statement type recorded in the data blood edge unit, and further generating an assertion class, wherein the assertion class comprises an assertion method which is called when the source code is tested, the data blood edge relation comprises a plurality of data blood edge units, and each data blood edge unit comprises: judging a correct list, judging an error list and judging a conditional statement.
2. The test script assertion generating method of claim 1, further comprising: dividing the target source code into a plurality of code units; the lexical analysis of the target source code includes: and respectively performing lexical analysis on each code unit.
3. The test script assertion generating method of claim 2, characterized in that the code unit is a line of codes or the code unit includes a set character length.
4. The test script assertion generating method of claim 1, characterized in that performing at least one build operation according to a set rule based on the statement type, comprises:
if the statement type of the code unit is an assignment statement, inserting the code unit into a judging correct list in the relative data blood-edge unit of the previous level;
if the statement type of the code unit is a judging statement, creating a relative next-level data blood-edge unit, recording the code unit to a judging condition statement of the next-level data blood-edge unit, inserting the next-level data blood-edge unit into the relative previous-level data unit, inserting the branch judged to be correct in the code unit into a judging correct list of the next-level data blood-edge unit, and inserting the branch judged to be incorrect into a judging error list of the next-level data blood-edge unit.
5. The test script assertion generating method of claim 1, wherein the generating a corresponding assertion statement according to a statement type recorded in the data blood edge unit includes:
If the statement type is an assignment statement, the assignment statement is used as a corresponding assertion statement;
if the statement type is a judging statement, taking the recorded judging condition statement as a judging condition of the asserting statement, taking the recorded judging correct list as a branch of the asserting statement, and taking the recorded judging error list as a branch of the asserting statement error;
and if the statement type is a database operation statement, generating a corresponding assertion statement according to the database operation type.
6. The test script assertion generating method of claim 5, wherein the generating a corresponding assertion statement according to a database operation type includes:
if the database operation type is insertion, adding a query record with the same name as a key in a source code database mapping file into a preset assertion mapping file, taking the record number of a return table as an actual value of assertion, taking 1 as an expected value of assertion, and generating a corresponding assertion statement;
if the operation type of the database is update, adding a query record with the same name as a key in a source code database mapping file into a preset assertion mapping file, taking the record number of a return table as an actual value of assertion, taking a variable represented by the key value in a database statement corresponding to the source code as an expected value of assertion, and sequentially generating corresponding assertion statements according to query condition fields;
If the database operation type is deletion, adding a query record with the same name as a key in the source code database mapping file into a preset assertion mapping file, taking the record number of the return table as the actual value of assertion, taking 0 as the expected value of assertion, and generating a corresponding assertion statement.
7. A test script assertion generating apparatus, comprising:
the data blood-edge relation generating module analyzes the target source code and generates the data blood-edge relation of the target source code;
the assertion class generation module generates an assertion class according to the data blood edge relation, the assertion class comprises an assertion method, the assertion method is used for being called when the source code is tested, the data blood edge relation comprises a plurality of data blood edge units, and each data blood edge unit comprises: judging a correct list, judging an error list and judging a conditional statement;
the data blood relationship generating module comprises:
the lexical analysis unit is used for lexically analyzing the target source codes;
the data blood-edge relation establishing unit establishes the data blood-edge relation of the target source code according to the sentence types obtained by the lexical analysis;
the data blood relationship establishing unit comprises:
The code unit type judging unit judges the statement type of each code unit one by one according to the sequence of the target source code of each code unit;
the data blood-edge relation construction unit is used for executing at least one construction operation according to a set rule based on the statement type to obtain the data blood-edge relation of the target source code;
the assertion class generating module includes:
the traversing unit traverses all the data blood edge units in the data blood edge relationship;
and the assertion generating unit is used for generating corresponding assertion statements according to statement types recorded in the data blood-edge unit, so as to generate the assertion class.
8. The test script assertion generating device of claim 7, further comprising: the code unit dividing unit is specifically used for dividing the target source code into a plurality of code units; the lexical analysis unit is specifically configured to perform lexical analysis on each code unit.
9. The test script assertion generating device of claim 8, wherein the code unit is a line of codes or the code unit includes a set character length.
10. The test script assertion generating device according to claim 7, characterized in that the data blood relationship constructing unit includes:
The assignment statement construction unit is used for inserting the code unit into a judging correct list in the relative data blood-edge unit of the previous level if the statement type of the code unit is an assignment statement;
and the judging statement construction unit is used for creating a corresponding next-level data blood-edge unit if the statement type of the code unit is a judging statement, recording the code unit to a judging condition statement of the next-level data blood-edge unit, inserting the next-level data blood-edge unit into a corresponding previous-level data unit, inserting the branch judged to be correct in the code unit into a judging correct list of the next-level data blood-edge unit, and inserting the branch judged to be wrong into a judging error list of the next-level data blood-edge unit.
11. The test script assertion generating apparatus of claim 7, characterized in that the assertion generating unit includes:
the assignment statement assertion generating unit is used for taking the assignment statement as a corresponding assertion statement if the statement type is the assignment statement;
the judging statement assertion generating unit takes the recorded judging condition statement as the judging condition of the assertion statement, takes the recorded judging correct list as the branch of the assertion statement, and takes the recorded judging error list as the branch of the assertion statement error if the statement type is the judging statement;
And the database operation statement assertion generating unit generates a corresponding assertion statement according to the database operation type if the statement type is the database operation statement.
12. The test script assertion generating device of claim 11, characterized in that the database operation statement assertion generating unit includes:
an insertion operation assertion generating unit, if the database operation type is insertion, adding a query record with the same name as a key in a source code database mapping file into a preset assertion mapping file, taking the record number of a return table as an actual value of assertion, taking 1 as an expected value of assertion, and generating a corresponding assertion statement;
an updating operation assertion generating unit, if the database operation type is updating, adding a query record with the same name as a key in a source code database mapping file in a preset assertion mapping file, taking the record number of a return table as an actual value of assertion, taking a variable represented by a key value in a database statement corresponding to a source code as an expected value of assertion, and sequentially generating corresponding assertion statements according to a query condition field;
and if the database operation type is deleting, adding a query record with the same name as a key in the source code database mapping file into a preset assertion mapping file, taking the record number of the return table as the actual value of the assertion, taking 0 as the expected value of the assertion, and generating a corresponding assertion statement.
13. An electronic device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, wherein the processor implements the test script assertion generating method of any one of claims 1 to 6 when the program is executed by the processor.
14. A computer readable storage medium having stored thereon a computer program, characterized in that the computer program, when executed by a processor, implements the test script assertion generating method of any one of claims 1 to 6.
CN202110350690.3A 2021-03-31 2021-03-31 Test script assertion generation method and device Active CN112860585B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202110350690.3A CN112860585B (en) 2021-03-31 2021-03-31 Test script assertion generation method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202110350690.3A CN112860585B (en) 2021-03-31 2021-03-31 Test script assertion generation method and device

Publications (2)

Publication Number Publication Date
CN112860585A CN112860585A (en) 2021-05-28
CN112860585B true CN112860585B (en) 2024-01-26

Family

ID=75991957

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202110350690.3A Active CN112860585B (en) 2021-03-31 2021-03-31 Test script assertion generation method and device

Country Status (1)

Country Link
CN (1) CN112860585B (en)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115629999A (en) * 2022-12-22 2023-01-20 云账户技术(天津)有限公司 Assertion detection method and device

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109614432A (en) * 2018-12-05 2019-04-12 北京百分点信息科技有限公司 A kind of system and method for the acquisition data genetic connection based on syntactic analysis
CN110377519A (en) * 2019-07-22 2019-10-25 中国工商银行股份有限公司 Performance capability test method, device, equipment and the storage medium of big data system
CN111538743A (en) * 2020-04-22 2020-08-14 电子科技大学 SQL-based data blood relationship analysis method and system
CN111782265A (en) * 2020-06-28 2020-10-16 中国工商银行股份有限公司 Software resource system based on field level blood relationship and establishment method thereof

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109614432A (en) * 2018-12-05 2019-04-12 北京百分点信息科技有限公司 A kind of system and method for the acquisition data genetic connection based on syntactic analysis
CN110377519A (en) * 2019-07-22 2019-10-25 中国工商银行股份有限公司 Performance capability test method, device, equipment and the storage medium of big data system
CN111538743A (en) * 2020-04-22 2020-08-14 电子科技大学 SQL-based data blood relationship analysis method and system
CN111782265A (en) * 2020-06-28 2020-10-16 中国工商银行股份有限公司 Software resource system based on field level blood relationship and establishment method thereof

Also Published As

Publication number Publication date
CN112860585A (en) 2021-05-28

Similar Documents

Publication Publication Date Title
CN111159049B (en) Automatic interface testing method and system
CN110941563B (en) Automatic script testing method and device
CN112783793B (en) Automatic interface test system and method
CN112905459B (en) Service interface testing method and device, electronic equipment and storage medium
CN111797015B (en) Dynamic byte code based test method and device
CN112463634B (en) Software testing method and device under micro-service architecture
CN108089985B (en) Method, device and equipment for verifying interface return parameters and readable storage medium
CN105786695A (en) Data test method and system
CN111930617A (en) Automatic testing method and device based on data objectification
CN112988601A (en) Test script development method and device
CN109408372A (en) Common component test method, device, computer equipment and storage medium
CN110990274A (en) Data processing method, device and system for generating test case
CN111897738B (en) Automatic testing method and device based on atomic service
CN113051163A (en) Unit testing method, unit testing device, electronic equipment and storage medium
CN108062276A (en) The generation method and device of test case and the generation method and device of test report
CN112860585B (en) Test script assertion generation method and device
CN112860264B (en) Method and device for reconstructing abstract syntax tree
CN106990974B (en) APP updating method and device and electronic equipment
CN112800063A (en) Automatic label passing method and device based on data structure
CN114138633A (en) Method, device and equipment for testing software based on data driving and readable medium
US8423849B2 (en) Device test data reuse for device simulation
CN114443039A (en) Input parameter verification method and device, electronic equipment and storage medium
CN112905461A (en) Method and device for executing automatic interface test case
CN115470152A (en) Test code generation method, test code generation device, and storage medium
CN113297076B (en) Service change identification method and device based on call chain diagram

Legal Events

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