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

Test script assertion generation method and device Download PDF

Info

Publication number
CN112860585A
CN112860585A CN202110350690.3A CN202110350690A CN112860585A CN 112860585 A CN112860585 A CN 112860585A CN 202110350690 A CN202110350690 A CN 202110350690A CN 112860585 A CN112860585 A CN 112860585A
Authority
CN
China
Prior art keywords
assertion
statement
unit
code
source code
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.)
Granted
Application number
CN202110350690.3A
Other languages
Chinese (zh)
Other versions
CN112860585B (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

Images

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 generation method and a test script assertion generation device, which can be used in the technical field of information security, and the method comprises the following steps: analyzing a target source code to generate a data blood relationship of the target source code; and generating 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. The method and the device avoid the condition that the assertion data are inaccurate, and ensure the comprehensiveness of the assertion field 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. Assertions of automated test scripts are typically generated by tools or written manually by testers. For the automatic test script assertion generated by the tool, generally, only simple assertion is performed on a transaction result, or assertion of a specific combination is generated through pre-configuration, and accurate assertion is not performed on a test scene; for assertions written manually by a tester, if the tester is not familiar enough with the function under test, it may happen that the critical field assertions are omitted, or the expected results of the assertions are written incorrectly.
Disclosure of Invention
For the problems in the prior art, the application provides a test script assertion generation method and device, and the method comprises the following steps: analyzing a target source code to generate a data blood relationship of the target source code; and generating 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. The method and the device avoid the condition that the assertion data are inaccurate, and ensure the comprehensiveness of the assertion field by checking the assertion coverage result.
One aspect of the present invention provides a test script assertion generation method, including:
analyzing a target source code to generate a data blood relationship of the target source code;
and generating 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.
In a preferred embodiment, the parsing the target source code and generating the data consanguinity 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 statement type 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 performing lexical analysis on each code unit respectively.
In a preferred embodiment, the code unit is a line code or the code unit comprises a set character length.
In a preferred embodiment, the establishing a data consanguinity relationship of the target source code according to the statement type 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 units, each data lineage unit including: judging a correct list, a wrong list and a condition statement; 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 correct judgment list in a corresponding upper-level data blood margin unit;
if the statement type of the code unit is a judgment statement, a relative next-level data blood margin unit is established, the code unit is recorded in a judgment condition statement of the next-level data blood margin unit, the next-level data blood margin unit is inserted into a relative previous-level data unit, a branch which is judged to be correct in the code unit is inserted into a judgment correct list of the next-level data blood margin unit, and a branch which is judged to be wrong is inserted into a judgment wrong list of the next-level data blood margin unit.
In a preferred embodiment, the generating an assertion class according to the data context includes:
traversing all data blood margin units in the data blood margin relation;
and generating corresponding assertion statements according to the statement types recorded in the data blood margin unit, and further generating the assertion classes.
In a preferred embodiment, the generating a corresponding assertion statement according to the statement type recorded in the data consanguinity unit includes:
if the statement type is an assignment statement, taking the assignment statement as a corresponding assertion statement;
if the statement type is a judgment statement, taking the recorded judgment condition statement as a judgment condition of an assertion statement, taking the recorded judgment correct list as a branch of the assertion statement, and taking the recorded judgment wrong list as a branch of the assertion statement;
and if the statement type is the 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 operation type of the database is insertion, adding a query record which is identical to the key in the mapping file of the source code database in name in a preset assertion mapping file, taking the number of records in 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 updated, adding a query record which is identical to the key in the source code database mapping file in name in a preset assertion mapping file, taking the number of returned table records 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;
if the operation type of the database is deletion, adding a query record which is identical to the key in the source code database mapping file in name in a preset assertion mapping file, taking the number of records in a return table as an actual value of assertion, taking 0 as an expected value of assertion, and generating a corresponding assertion statement.
In another aspect of the present invention, a test script assertion generating apparatus is provided, including:
the data blood relationship generation module analyzes the target source code and generates the data blood relationship of the target source code;
and the assertion class generation module generates an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method, and the assertion method is called when the source code is tested.
In a preferred embodiment, the data blood relationship generation module includes:
a lexical analysis unit which performs lexical analysis on the target source code;
and the data blood relationship establishing unit is used for establishing the data blood relationship of the target source code according to the statement type obtained by the lexical analysis.
In a preferred embodiment, further comprising: a code unit dividing unit, specifically configured to divide 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 code 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 is used for 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 the data blood relationship construction unit executes 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 units, each data lineage unit including: judging a correct list, a wrong list and a condition statement; the data blood relationship construction unit comprises:
the assignment statement construction unit is used for inserting the code unit into a correct judgment list in a corresponding upper-level data blood margin unit if the statement type of the code unit is an assignment statement;
and the judgment statement construction unit is used for creating a relative next-level data blood margin unit if the statement type of the code unit is a judgment statement, recording the code unit to a judgment condition statement of the next-level data blood margin unit, inserting the next-level data blood margin unit into a relative previous-level data unit, inserting the branch judged to be correct in the code unit into a judgment correct list of the next-level data blood margin unit, and inserting the branch judged to be wrong in a judgment wrong list of the next-level data blood margin unit.
In a preferred embodiment, the assertion class generating module includes:
the traversal unit traverses all data blood margin units in the data blood margin relation;
and the assertion generating unit is used for generating corresponding assertion statements according to the statement types recorded in the data blood margin unit so as to generate the assertion classes.
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 an assignment statement;
a judgment statement assertion generating unit, which takes the recorded judgment condition statement as a judgment condition of the assertion statement, takes the recorded judgment correct list as a branch of the assertion statement, and takes the recorded judgment wrong list as a branch of the assertion statement error if the statement type is the judgment statement;
and if the statement type is the database operation statement, generating a corresponding assertion statement according to the database operation type.
In a preferred embodiment, the database operation statement assertion generating unit includes:
an insertion operation assertion generating unit, which adds a query record having the same name as a key in a source code database mapping file in a preset assertion mapping file if the database operation type is insertion, takes the number of records in a return table as an actual value of assertion, takes 1 as an expected value of assertion, and generates a corresponding assertion statement;
if the database operation type is updating, adding a query record which is identical to the key in the source code database mapping file in name in a preset assertion mapping file, taking the number of the returned table records as an actual value of assertion, taking a variable represented by a key value in a database statement corresponding to the source code as an expected value of the assertion, and sequentially generating corresponding assertion statements according to the query condition field;
and if the database operation type is deletion, adding a query record which is identical to the key in the source code database mapping file in name to a preset assertion mapping file, taking the number of records in the return table as an actual value of assertion, taking 0 as an expected value of assertion, and generating a corresponding assertion statement.
In another aspect of the present invention, the present application provides an electronic device, which includes a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the test script assertion generating method when executing the program.
In yet another aspect of the present invention, the present application provides a computer-readable storage medium having stored thereon a computer program which, when executed by a processor, implements the test script assertion generation method.
According to the technical scheme, the test script assertion generation method provided by the application comprises the following steps: analyzing a target source code to generate a data blood relationship of the target source code; and generating 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. The method and the device avoid the condition that the assertion data are inaccurate, and ensure the comprehensiveness of the assertion field 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 needed to be used in the description of the embodiments or the prior art will be briefly introduced below, and it is obvious that the drawings in the following description are some embodiments of the present application, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without creative efforts.
FIG. 1 is a flow diagram of a test script assertion generation method.
FIG. 2 is a schematic diagram of a data relationship generation process.
FIG. 3 is a schematic diagram of an assertion method generation flow.
FIG. 4 is a graph illustrating data relationship in one embodiment.
Fig. 5 is a schematic structural diagram of a test script assertion generating device.
Fig. 6 is a schematic structural diagram of an electronic device in an embodiment of the present application.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present application clearer, 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 obvious that the described embodiments are some embodiments of the present application, but not all embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
It should be noted that the test script assertion generating method and apparatus disclosed in the present application may be used in the field of information security, and may also be used in any field other than the field of information security.
In the software testing process, automated testing is a common testing means. Assertions of automated test scripts are typically generated by tools or written manually by testers. For the automatic test script assertion generated by the tool, generally, only simple assertion is performed on a transaction result, or assertion of a specific combination is generated through pre-configuration, and accurate assertion is not performed on a test scene; for assertions written manually by a tester, if the tester is not familiar enough with the function under test, it may happen that the critical field assertions are omitted, or the expected results of the assertions are written incorrectly.
In order to solve at least one of the existing problems, the application provides a test script assertion generation method and device, and the method comprises the following steps: analyzing a target source code to generate a data blood relationship of the target source code; and generating 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. The method and the device avoid the condition that the assertion data are inaccurate, and ensure the comprehensiveness of the assertion field by checking the assertion coverage result.
The following describes the test script assertion generating method and apparatus provided by the present invention in detail with reference to the accompanying drawings.
In a specific embodiment, the present application provides a test script assertion generation method, as shown in fig. 1, including:
s1, analyzing the target source code to generate the data blood relationship of the target source code;
specifically, the data generation, processing and fusion, circulation and circulation are carried out until the data are finally lost, and a relationship is naturally formed between the data. The relationship between data is expressed by referring to a similar relationship in human society, which is called the blood-related relationship of data. 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 generated accurately. The analyzing the target source code to generate 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, the establishment of the data consanguinity relationship depends on the lexical analysis of the codes. In a specific embodiment, the target source code is divided into a plurality of code units; and performing lexical analysis on each code unit respectively. Wherein the code unit is a line of codes, or the code unit includes a set character length. In a specific embodiment, the code unit includes a set character length, which is understood to include multiple lines of code belonging to the same syntax structure, such as a circular syntax, a decision syntax, which contains multiple lines of source code. By way of 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 unit three: if (a > b) { a ═ 0} else { b ═ 0 }. After the source code is divided into a plurality of code units, lexical analysis is carried out on each code unit respectively to obtain the statement type of the corresponding code unit. Through lexical analysis, the statement types corresponding to the three code units are as follows: the code unit I is an assignment statement, the code unit II is an assignment statement, and the code unit III is a judgment statement.
And S12, establishing the data blood relationship of the target source code according to the statement type obtained by the lexical analysis.
Specifically, through lexical analysis of the source code, the statement type of the code can be obtained, and the data consanguinity relation of the target code is established according to the statement type. It can be understood that there may be two ways for generating the data context, one way is to analyze the statement type of a code unit and add corresponding information to the data context, and the other way is to obtain all statement types of all code units and then establish the data context. Both approaches are feasible and effective. Establishing a data blood relationship of a target source code according to the statement type obtained by the lexical analysis, wherein the establishing comprises the following steps:
judging the statement type of each code unit one by one according to the sequence of the target source code in which each code unit is positioned, 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. The data consanguinity relationship comprises a plurality of data consanguinity units, each data consanguinity unit comprising: judging a correct list, judging an incorrect 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 correct judgment list in a corresponding upper-level data blood margin unit;
if the statement type of the code unit is a judgment statement, a relative next-level data blood margin unit is established, the code unit is recorded in a judgment condition statement of the next-level data blood margin unit, the next-level data blood margin unit is inserted into a relative previous-level data unit, a branch which is judged to be correct in the code unit is inserted into a judgment correct list of the next-level data blood margin unit, and a branch which is judged to be wrong is inserted into a judgment wrong list of the next-level data blood margin unit.
In a specific embodiment, it is assumed that the target source code has four code units, wherein the order of the four code units is named code unit one, code unit two, code unit three and code unit four. And obtaining a first code unit as a judgment statement, a second code unit as an assignment statement, a third code unit as a judgment statement and a fourth code unit as a judgment statement through lexical analysis, wherein the first, second and third code units are in the same layer, and the fourth code unit is the next layer in the third code unit. When a data blood relationship is established, an initial data blood edge unit analysisclass 0 is generated firstly, as a code unit I is a judgment statement, a data blood edge unit analysisclass 1 is newly established, the analysisclass 1 is inserted into a judgment correct list of the analysisclass 0, then specific contents of the analysisclass 1 are filled, a judgment condition of the code unit I is inserted into a judgment condition statement of the analysisclass 1, a judgment correct branch of the code unit I is inserted into a judgment correct list of the analysisclass 1, and a judgment wrong branch of the code unit I is inserted into a judgment wrong list of the analysisclass 1. And then, completing the operation of the code unit I, starting to analyze the code unit II, wherein the code unit II is an assignment statement, and directly inserting the assignment statement of the code unit II into a correct list of analysis class0 to complete the operation of the code unit II. For the third code unit, as the third code unit is a judgment statement, a data consanguinity class2 is newly established, the analysisclass 2 is inserted into a judgment correct list of the analysisclass 0, then the analysisclass 2 is filled according to the content of the third code unit, the judgment condition of the third code unit is inserted into the judgment condition statement of the analysisclass 2, the corresponding correct branch is inserted into the judgment correct list of the analysisclass 2, and the corresponding error branch is inserted into a judgment error list of the analysisclass 2. In the filling process, the corresponding correct branch comprises a code unit four, and the code unit four is also a judgment statement type, so that a data edge unit analysisclass 3 is newly created, the analysisclass 3 is inserted into a judgment correct list of the analysisclass 2, and then the analysisclass 3 is filled according to the content of the code unit four. And finally, the data blood relationship analysis of the whole target source code is completed, and the data blood relationship is generated.
And S2, generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method, and the assertion method 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 processes of all data in the source code are clear. The assertion can be accurately predicated through the data blood relationship, and an assertion class for the assertion is generated, wherein the assertion class comprises an assertion method, and the assertion method comprises the 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 context, as shown in fig. 3, includes:
s21, traversing all data blood margin units in the data blood margin relation;
specifically, the data consanguinity relationship includes a plurality of data consanguinity units, and the data consanguinity units record data statement information of the source code, so that traversing all the data consanguinity units in the data consanguinity relationship can be understood as traversing the whole target source code, and further, it is ensured that a subsequent assertion method can cover data of all the target source codes.
And S22, generating corresponding assertion statements according to the statement types recorded in the data blood margin unit, and further generating the assertion classes.
Specifically, for different statement types, the corresponding assertion statements are different. In a specific embodiment, if the statement type is an assignment statement, the assignment statement is used as its corresponding assertion statement. For example, if the assignment statement of the source code is int example ═ 6, then the assertion statement written into the assertion method is int example ═ 6, that is, the assertion statement is consistent with the source code statement. If the statement type is a judgment statement, the recorded judgment condition statement is used as a judgment condition of the assertion statement, the recorded judgment correct list is used as a correct branch of the assertion statement, and the recorded judgment wrong list is used as a wrong branch of the assertion statement. For example, if (a > b) { a ═ 0; printf (a) } else { b ═ 100; printf (b), the judgment condition statement of the judgment statement is a > b, it judges that the statement recorded in the correct list should be a ═ 0, and judges that the statement recorded in the error list should be b ═ 100, so the assertion statement generated corresponding to 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 further subdivided according to the concrete database operation type for the database operation statement: if the operation type of the database is insertion, adding a query record which is identical to the key in the source code database mapping file in name in a preset assertion mapping file, taking the number of records in 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, a database operation statement in the source code is dbexecutor.insert (attdapper.insert _ medlimtstable, input), and a corresponding database statement is:
Figure BDA0003002020930000091
the inserted key name is insert _ MedLimtTable, so the corresponding generated query in the assertion mapping file is recorded as
Figure BDA0003002020930000101
If the insertion is successful, the number of the return table records of the query record is 1, and if the insertion is unsuccessful, the number of the return table records is 0, so that the number of the return table records is used as an actual value of the assertion, 1 is used as an expected value of the assertion, and the following assertion statements are generated:
Assert.assertEquals(DBExecutor.insert(“AtDBMapper.insert_MedLimitTable”,input),1)
if the operation type of the database is updated, adding a query record which is identical to the key in the source code database mapping file in name in a preset assertion mapping file, taking the number of the returned table records as an actual value of assertion, taking a variable represented by a 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 the query condition field. For example, the database operation statement in the source code is dbexecute. update (at dbmupper. update _ medium _ with _ card no), and the database statement corresponding to the statement is dbexecute
Figure BDA0003002020930000102
The key name is update _ MedLimitTable _ by _ cardno, so the corresponding generated query record in the assertion mapping file is
Figure BDA0003002020930000103
If the updating is successful, the queried field values are consistent with the input values, so that variables represented by key values serve as expected values of the assertion, and corresponding assertion statements 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 operation type of the database is deletion, adding a query record which is identical to the key in the source code database mapping file in name in a preset assertion mapping file, taking the number of records in a return table as an actual value of assertion, taking 0 as an expected value of assertion, and generating a corresponding assertion statement. For example, a database operation statement in the source code is dbexecute.
Figure BDA0003002020930000111
The inserted key name is delete _ MedLimtTable, so the corresponding generated query record in the assertion mapping file is
Figure BDA0003002020930000112
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 used as an actual value of the assertion, 0 is used as an expected value of the assertion, and the following assertion statements are generated:
Assert.assertEquals(DBExecutor.delete(“AtDBMapper.insert_MedLimitTable”,input),0)
the present invention is further described in conjunction with a particular scenario in which a target source code is used to generate a corresponding test script assertion for testing the target source code. According to the method provided by the application, lexical analysis is firstly performed on the target source code, the target source code is divided into a plurality of code units, each code unit can be a 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 of the second code unit. And performing lexical analysis on each code unit to obtain the statement type of each code unit, wherein a code unit I is an assignment statement, a code unit II is a judgment statement, a code unit III is a database operation statement, a code unit IV is a judgment statement, and a code unit V is a database operation statement. According to the steps for generating the data consanguinity relationship provided by the application, an initialized data consanguinity class0 is established, and since the code unit 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 one is inserted into the judgment correct list of the analysis class 0. Then, judging the statement type of the next code unit, if the second code unit is a judgment statement, establishing a new data consanguinity class1, inserting the whole of the new data consanguinity class1 into a judgment correct list of the emulation class0, then filling the content of the emulation class1, placing the judgment condition in the second code unit into the judgment condition of the emulation class1, inserting the branch which is judged to be correct in the second code unit into the judgment correct list of the emulation class1, and inserting the branch which is judged to be wrong in the second code unit into a judgment error list of the emulation class 1. And the code unit three is in the correct branch of the code unit two, and is a database operation statement, and the database operation statement is directly inserted into the judgment correct list of the analysisclass 1. Code unit four is in the error branch of code unit two, which is an assignment statement inserted directly into the error branch of analysysclass 1. And the code unit five is in the error branch of the code unit two and is of a database operation statement type, and the database operation statement of the code unit five is directly inserted into the judgment error list of the analysis class 1. By this, the data context of the source code can be generated as shown in FIG. 4.
And after the data blood relationship is generated, traversing all the data blood relationship units in the data blood relationship, and generating an assertion class containing an assertion method according to the statement type recorded by the data blood relationship units. In this implementation scenario, starting with analystclass 0, the first entry in the record is an assignment statement, which is written directly into the assertion method. The second record is anysysclass 1, which corresponds to a judgment statement, so the judgment condition recorded in anysysclass 1 is written in the judgment condition of the assertion statement, the content recorded in the judgment correct list of the anysysclass 1 is written in the correct branch of the assertion statement, and the judgment error list is written in the error branch of the assertion statement. By analogy, the following assertion classes are finally 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 generation method provided by the present invention includes: analyzing a target source code to generate a data blood relationship of the target source code; and generating 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. The method and the device avoid the condition that the assertion data are inaccurate, and ensure the comprehensiveness of the assertion field by checking the assertion coverage result.
From the aspect of software, the present application provides an embodiment of a test script assertion generating device for executing all or part of the contents in the test script assertion generating method, and referring to fig. 5, the test script assertion generating device specifically includes the following contents:
the data blood relationship generation module 1 analyzes a target source code and generates a data blood relationship of the target source code;
and the assertion class generating module 2 is used for generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method, and the assertion method is called when the source code is tested.
According to the test script assertion generating device provided by the invention, firstly, the device analyzes the target source code to generate the data blood relationship of the target source code; and generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method, and the assertion method is called when the source code is tested. The method and the device avoid the condition that the assertion data are inaccurate, and ensure the comprehensiveness of the assertion field 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 generation module 1 is used for executing analysis of a target source code and generating a data blood relationship of the target source code;
specifically, the data generation, processing and fusion, circulation and circulation are carried out until the data are finally lost, and a relationship is naturally formed between the data. The relationship between data is expressed by referring to a similar relationship in human society, which is called the blood-related relationship of data. 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 generated accurately. 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, the establishment of the data consanguinity relationship depends on the lexical analysis of the codes. In a specific embodiment, the target source code is divided into a plurality of code units; and performing lexical analysis on each code unit respectively. Wherein the code unit is a line of codes, or the code unit includes a set character length. In a specific embodiment, the code unit includes a set character length, which is understood to include multiple lines of code belonging to the same syntax structure, such as a circular syntax, a decision syntax, which contains multiple lines of source code. By way of 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 unit three: if (a > b) { a ═ 0} else { b ═ 0 }. After the source code is divided into a plurality of code units, lexical analysis is carried out on each code unit respectively to obtain the statement type of the corresponding code unit. Through lexical analysis, the statement types corresponding to the three code units are as follows: the code unit I is an assignment statement, the code unit II is an assignment statement, and the code unit III is a judgment statement.
And S12, establishing the data blood relationship of the target source code according to the statement type obtained by the lexical analysis.
Specifically, through lexical analysis of the source code, the statement type of the code can be obtained, and the data consanguinity relation of the target code is established according to the statement type. It can be understood that there may be two ways for generating the data context, one way is to analyze the statement type of a code unit and add corresponding information to the data context, and the other way is to obtain all statement types of all code units and then establish the data context. Both approaches are feasible and effective. Establishing a data blood relationship of a target source code according to the statement type obtained by the lexical analysis, wherein the establishing comprises the following steps:
judging the statement type of each code unit one by one according to the sequence of the target source code in which each code unit is positioned, 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. The data consanguinity relationship comprises a plurality of data consanguinity units, each data consanguinity unit comprising: judging a correct list, judging an incorrect 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 correct judgment list in a corresponding upper-level data blood margin unit;
if the statement type of the code unit is a judgment statement, a relative next-level data blood margin unit is established, the code unit is recorded in a judgment condition statement of the next-level data blood margin unit, the next-level data blood margin unit is inserted into a relative previous-level data unit, a branch which is judged to be correct in the code unit is inserted into a judgment correct list of the next-level data blood margin unit, and a branch which is judged to be wrong is inserted into a judgment wrong list of the next-level data blood margin unit.
In a specific embodiment, it is assumed that the target source code has four code units, wherein the order of the four code units is named code unit one, code unit two, code unit three and code unit four. And obtaining a first code unit as a judgment statement, a second code unit as an assignment statement, a third code unit as a judgment statement and a fourth code unit as a judgment statement through lexical analysis, wherein the first, second and third code units are in the same layer, and the fourth code unit is the next layer in the third code unit. When a data blood relationship is established, an initial data blood edge unit analysisclass 0 is generated firstly, as a code unit I is a judgment statement, a data blood edge unit analysisclass 1 is newly established, the analysisclass 1 is inserted into a judgment correct list of the analysisclass 0, then specific contents of the analysisclass 1 are filled, a judgment condition of the code unit I is inserted into a judgment condition statement of the analysisclass 1, a judgment correct branch of the code unit I is inserted into a judgment correct list of the analysisclass 1, and a judgment wrong branch of the code unit I is inserted into a judgment wrong list of the analysisclass 1. And then, completing the operation of the code unit I, starting to analyze the code unit II, wherein the code unit II is an assignment statement, and directly inserting the assignment statement of the code unit II into a correct list of analysis class0 to complete the operation of the code unit II. For the third code unit, as the third code unit is a judgment statement, a data consanguinity class2 is newly established, the analysisclass 2 is inserted into a judgment correct list of the analysisclass 0, then the analysisclass 2 is filled according to the content of the third code unit, the judgment condition of the third code unit is inserted into the judgment condition statement of the analysisclass 2, the corresponding correct branch is inserted into the judgment correct list of the analysisclass 2, and the corresponding error branch is inserted into a judgment error list of the analysisclass 2. In the filling process, the corresponding correct branch comprises a code unit four, and the code unit four is also a judgment statement type, so that a data edge unit analysisclass 3 is newly created, the analysisclass 3 is inserted into a judgment correct list of the analysisclass 2, and then the analysisclass 3 is filled according to the content of the code unit four. And finally, the data blood relationship analysis of the whole target source code is completed, and the data blood relationship is generated.
It is understood that the data blood relationship generation module executing the above steps may be understood as including a lexical analysis unit and a data blood relationship establishment unit, wherein the lexical analysis unit executes step S11, and the data blood relationship establishment unit executes step S12, and the following related embodiments are not repeated herein.
And the assertion class generating module 2 is used for generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method, and the assertion method 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 processes of all data in the source code are clear. The assertion can be accurately predicated through the data blood relationship, and an assertion class for the assertion is generated, wherein the assertion class comprises an assertion method, and the assertion method comprises the 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 margin units in the data blood margin relation;
specifically, the data consanguinity relationship includes a plurality of data consanguinity units, and the data consanguinity units record data statement information of the source code, so that traversing all the data consanguinity units in the data consanguinity relationship can be understood as traversing the whole target source code, and further, it is ensured that a subsequent assertion method can cover data of all the target source codes.
And S22, generating corresponding assertion statements according to the statement types recorded in the data blood margin unit, and further generating the assertion classes.
Specifically, for different statement types, the corresponding assertion statements are different. In a specific embodiment, if the statement type is an assignment statement, the assignment statement is used as its corresponding assertion statement. For example, if the assignment statement of the source code is int example ═ 6, then the assertion statement written into the assertion method is int example ═ 6, that is, the assertion statement is consistent with the source code statement. If the statement type is a judgment statement, the recorded judgment condition statement is used as a judgment condition of the assertion statement, the recorded judgment correct list is used as a correct branch of the assertion statement, and the recorded judgment wrong list is used as a wrong branch of the assertion statement. For example, if (a > b) { a ═ 0; printf (a) } else { b ═ 100; printf (b), the judgment condition statement of the judgment statement is a > b, it judges that the statement recorded in the correct list should be a ═ 0, and judges that the statement recorded in the error list should be b ═ 100, so the assertion statement generated corresponding to 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 further subdivided according to the concrete database operation type for the database operation statement: if the operation type of the database is insertion, adding a query record which is identical to the key in the source code database mapping file in name in a preset assertion mapping file, taking the number of records in 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, a database operation statement in the source code is dbexecutor.insert (attdapper.insert _ medlimtstable, input), and a corresponding database statement is:
Figure BDA0003002020930000171
the inserted key name is insert _ MedLimtTable, so the corresponding generated query in the assertion mapping file is recorded as
Figure BDA0003002020930000172
If the insertion is successful, the number of the return table records of the query record is 1, and if the insertion is unsuccessful, the number of the return table records is 0, so that the number of the return table records is used as an actual value of the assertion, 1 is used as an expected value of the assertion, and the following assertion statements are generated:
Assert.assertEquals(DBExecutor.insert(“AtDBMapper.insert_MedLimitTable”,input),1)
if the operation type of the database is updated, adding a query record which is identical to the key in the source code database mapping file in name in a preset assertion mapping file, taking the number of the returned table records as an actual value of assertion, taking a variable represented by a 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 the query condition field. For example, the database operation statement in the source code is dbexecute. update (at dbmupper. update _ medium _ with _ card no), and the database statement corresponding to the statement is dbexecute
Figure BDA0003002020930000173
Figure BDA0003002020930000181
The key name is update _ MedLimitTable _ by _ cardno, so the corresponding generated query record in the assertion mapping file is
Figure BDA0003002020930000182
If the updating is successful, the queried field values are consistent with the input values, so that variables represented by key values serve as expected values of the assertion, and corresponding assertion statements 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 operation type of the database is deletion, adding a query record which is identical to the key in the source code database mapping file in name in a preset assertion mapping file, taking the number of records in a return table as an actual value of assertion, taking 0 as an expected value of assertion, and generating a corresponding assertion statement. For example, a database operation statement in the source code is dbexecute.
Figure BDA0003002020930000183
The inserted key name is delete _ MedLimtTable, so the corresponding generated query record in the assertion mapping file is
Figure BDA0003002020930000184
Figure BDA0003002020930000191
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 used as an actual value of the assertion, 0 is used as an expected value of the assertion, and the following assertion statements are generated:
Assert.assertEquals(DBExecutor.delete(“AtDBMapper.insert_MedLimitTable”,input),0)
the present invention is further described in conjunction with a particular scenario in which a target source code is used to generate a corresponding test script assertion for testing the target source code. According to the method provided by the application, lexical analysis is firstly performed on the target source code, the target source code is divided into a plurality of code units, each code unit can be a 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 of the second code unit. And performing lexical analysis on each code unit to obtain the statement type of each code unit, wherein a code unit I is an assignment statement, a code unit II is a judgment statement, a code unit III is a database operation statement, a code unit IV is a judgment statement, and a code unit V is a database operation statement. According to the steps for generating the data consanguinity relationship provided by the application, an initialized data consanguinity class0 is established, and since the code unit 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 one is inserted into the judgment correct list of the analysis class 0. Then, judging the statement type of the next code unit, if the second code unit is a judgment statement, establishing a new data consanguinity class1, inserting the whole of the new data consanguinity class1 into a judgment correct list of the emulation class0, then filling the content of the emulation class1, placing the judgment condition in the second code unit into the judgment condition of the emulation class1, inserting the branch which is judged to be correct in the second code unit into the judgment correct list of the emulation class1, and inserting the branch which is judged to be wrong in the second code unit into a judgment error list of the emulation class 1. And the code unit three is in the correct branch of the code unit two, and is a database operation statement, and the database operation statement is directly inserted into the judgment correct list of the analysisclass 1. Code unit four is in the error branch of code unit two, which is an assignment statement inserted directly into the error branch of analysysclass 1. And the code unit five is in the error branch of the code unit two and is of a database operation statement type, and the database operation statement of the code unit five is directly inserted into the judgment error list of the analysis class 1. By this, the data context of the source code can be generated as shown in FIG. 4.
And after the data blood relationship is generated, traversing all the data blood relationship units in the data blood relationship, and generating an assertion class containing an assertion method according to the statement type recorded by the data blood relationship units. In this implementation scenario, starting with analystclass 0, the first entry in the record is an assignment statement, which is written directly into the assertion method. The second record is anysysclass 1, which corresponds to a judgment statement, so the judgment condition recorded in anysysclass 1 is written in the judgment condition of the assertion statement, the content recorded in the judgment correct list of the anysysclass 1 is written in the correct branch of the assertion statement, and the judgment error list is written in the error branch of the assertion statement. By analogy, the following assertion classes are finally 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 present invention provides a test script assertion generating apparatus, which is configured to perform the following steps: analyzing a target source code to generate a data blood relationship of the target source code; and generating 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. The method and the device avoid the condition that the assertion data are inaccurate, and ensure the comprehensiveness of the assertion field by checking the assertion coverage result.
In terms of hardware, the present application provides an embodiment of an electronic device for implementing all or part of contents in a test script assertion generation 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 according to an embodiment of the present application. As shown in fig. 6, the electronic device 9600 can 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 structure to implement telecommunications or other functions.
In one embodiment, the test script assertion generation functionality may be integrated into a central processor. Wherein the central processor may be configured to control:
s1, analyzing the target source code to generate the data blood relationship of the target source code;
and S2, generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method, and the assertion method is called when the source code is tested.
From the above description, the electronic device provided in the embodiment of the present application avoids the situation that the assertion data is inaccurate, and guarantees comprehensiveness of the assertion field by checking the assertion coverage result.
In another embodiment, the test script assertion generating apparatus may be configured separately from the central processor 9100, for example, the test script assertion generating apparatus may be configured as a chip connected to the central processor 9100, and the test script assertion generating function is realized by the 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 also does not necessarily include all of the components shown in fig. 6; further, the electronic device 9600 may further include components not shown in fig. 6, which may be referred to in the art.
As shown in fig. 6, a central processor 9100, sometimes referred to as a controller or operational control, can include a microprocessor or other processor device and/or logic device, which central processor 9100 receives input and controls the operation of the various components of the electronic device 9600.
The memory 9140 can 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 relating to the failure may be stored, and a program for executing the information may be stored. And the central processing unit 9100 can execute the program stored in the memory 9140 to realize information storage or processing, or 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. Power supply 9170 is used to provide power to electronic device 9600. The display 9160 is used for displaying display objects such as images and characters. The display may be, for example, an LCD display, but is not limited thereto.
The memory 9140 can be a solid state memory, e.g., Read Only Memory (ROM), Random Access Memory (RAM), a SIM card, or the like. There may also be a memory that holds information even when power is off, can be selectively erased, and is provided with more data, an example of which is sometimes called an EPROM or the like. The memory 9140 could also be some other type of device. 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 being used for storing application programs and function programs or for executing a flow of operations of the electronic device 9600 by the central processor 9100.
The memory 9140 can also include a data store 9143, the data store 9143 being used to store 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 for the electronic device for communication functions and/or for performing other functions of the electronic device (e.g., messaging applications, contact book applications, etc.).
The communication module 9110 is a transmitter/receiver 9110 that transmits and receives signals via an antenna 9111. The communication module (transmitter/receiver) 9110 is coupled to the central processor 9100 to provide input signals and receive output signals, which may be the same as in the case of a conventional mobile communication terminal.
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, 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 receive audio input from the microphone 9132, thereby implementing ordinary telecommunications functions. The audio processor 9130 may include any suitable buffers, decoders, amplifiers and so forth. In addition, the audio processor 9130 is also coupled to the central processor 9100, thereby enabling recording locally through the microphone 9132 and enabling locally stored sounds to be played through the speaker 9131.
Embodiments of the present application further provide a computer-readable storage medium capable of implementing all steps in the test script assertion generating method in the foregoing embodiments, where the computer-readable storage medium stores thereon a computer program, and when the computer program is executed by a processor, the computer program implements all steps of the test script assertion generating method in which an execution subject is a server or a client in the foregoing embodiments, for example, when the processor executes the computer program, the processor implements the following steps:
s1, analyzing the target source code to generate the data blood relationship of the target source code;
and S2, generating an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method, and the assertion method is 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 guarantees comprehensiveness of the assertion field by checking the assertion coverage result.
As will be appreciated by one skilled in the art, 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 flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams 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 principle and the implementation mode of the invention are explained by applying specific embodiments in the invention, and the description of the embodiments is only used for helping to understand the method and the core idea of the invention; meanwhile, for a person skilled in the art, according to the idea of the present invention, there may be variations in the specific embodiments and the application scope, and in summary, the content of the present specification should not be construed as a limitation to the present invention.

Claims (20)

1. A test script assertion generation method, comprising:
analyzing a target source code to generate a data blood relationship of the target source code;
and generating 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.
2. The method according to claim 1, wherein the parsing the target source code to generate the data context of the target source code comprises:
performing lexical analysis on the target source code;
and establishing a data blood relationship of the target source code according to the statement type obtained by the lexical analysis.
3. The test script assertion generation method of claim 2, further comprising: dividing the target source code into a plurality of code units; the lexical analysis of the target source code includes: and performing lexical analysis on each code unit respectively.
4. The test script assertion generation method of claim 3 wherein the code unit is a line of code or the code unit includes a set character length.
5. The method according to claim 2, wherein the establishing a data context of the target source code according to the type of the sentence obtained by the lexical analysis comprises:
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.
6. The method of generating a test script assertion as claimed in claim 5 wherein the data lineage relationship includes a plurality of data lineage units, each data lineage unit including: judging a correct list, a wrong list and a condition statement; 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 correct judgment list in a corresponding upper-level data blood margin unit;
if the statement type of the code unit is a judgment statement, a relative next-level data blood margin unit is established, the code unit is recorded in a judgment condition statement of the next-level data blood margin unit, the next-level data blood margin unit is inserted into a relative previous-level data unit, a branch which is judged to be correct in the code unit is inserted into a judgment correct list of the next-level data blood margin unit, and a branch which is judged to be wrong is inserted into a judgment wrong list of the next-level data blood margin unit.
7. The method according to claim 1, wherein generating an assertion class according to the data lineage relationship comprises:
traversing all data blood margin units in the data blood margin relation;
and generating corresponding assertion statements according to the statement types recorded in the data blood margin unit, and further generating the assertion classes.
8. The method according to claim 7, wherein generating corresponding assertion statements according to the statement types recorded in the data consanguinity unit comprises:
if the statement type is an assignment statement, taking the assignment statement as a corresponding assertion statement;
if the statement type is a judgment statement, taking the recorded judgment condition statement as a judgment condition of an assertion statement, taking the recorded judgment correct list as a branch of the assertion statement, and taking the recorded judgment wrong list as a branch of the assertion statement;
and if the statement type is the database operation statement, generating a corresponding assertion statement according to the database operation type.
9. The method according to claim 8, wherein generating corresponding assertion statements according to the database operation type comprises:
if the operation type of the database is insertion, adding a query record which is identical to the key in the mapping file of the source code database in name in a preset assertion mapping file, taking the number of records in 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 updated, adding a query record which is identical to the key in the source code database mapping file in name in a preset assertion mapping file, taking the number of returned table records 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;
if the operation type of the database is deletion, adding a query record which is identical to the key in the source code database mapping file in name in a preset assertion mapping file, taking the number of records in a return table as an actual value of assertion, taking 0 as an expected value of assertion, and generating a corresponding assertion statement.
10. A test script assertion generating apparatus, comprising:
the data blood relationship generation module analyzes the target source code and generates the data blood relationship of the target source code;
and the assertion class generation module generates an assertion class according to the data blood relationship, wherein the assertion class comprises an assertion method, and the assertion method is called when the source code is tested.
11. The test script assertion generating device of claim 10, wherein the data context generating module comprises:
a lexical analysis unit which performs lexical analysis on the target source code;
and the data blood relationship establishing unit is used for establishing the data blood relationship of the target source code according to the statement type obtained by the lexical analysis.
12. The test script assertion generating apparatus of claim 11, further comprising: a code unit dividing unit, specifically configured to divide 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.
13. The test script assertion generating apparatus of claim 12, wherein the unit of code is a line of code or the unit of code includes a set character length.
14. The test script assertion generating apparatus as claimed in claim 11, wherein the data consanguinity relationship establishing unit comprises:
the code unit type judging unit is used for 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 the data blood relationship construction unit executes 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.
15. The test script assertion generating device of claim 14, wherein the data consanguinity relationship comprises a plurality of data consanguinity cells, each data consanguinity cell comprising: judging a correct list, a wrong list and a condition statement; the data blood relationship construction unit comprises:
the assignment statement construction unit is used for inserting the code unit into a correct judgment list in a corresponding upper-level data blood margin unit if the statement type of the code unit is an assignment statement;
and the judgment statement construction unit is used for creating a relative next-level data blood margin unit if the statement type of the code unit is a judgment statement, recording the code unit to a judgment condition statement of the next-level data blood margin unit, inserting the next-level data blood margin unit into a relative previous-level data unit, inserting the branch judged to be correct in the code unit into a judgment correct list of the next-level data blood margin unit, and inserting the branch judged to be wrong in a judgment wrong list of the next-level data blood margin unit.
16. The test script assertion generating apparatus of claim 10, wherein the assertion class generating module comprises:
the traversal unit traverses all data blood margin units in the data blood margin relation;
and the assertion generating unit is used for generating corresponding assertion statements according to the statement types recorded in the data blood margin unit so as to generate the assertion classes.
17. The test script assertion generating apparatus of claim 16, wherein the assertion generating unit comprises:
the assignment statement assertion generating unit is used for taking the assignment statement as a corresponding assertion statement if the statement type is an assignment statement;
a judgment statement assertion generating unit, which takes the recorded judgment condition statement as a judgment condition of the assertion statement, takes the recorded judgment correct list as a branch of the assertion statement, and takes the recorded judgment wrong list as a branch of the assertion statement error if the statement type is the judgment statement;
and if the statement type is the database operation statement, generating a corresponding assertion statement according to the database operation type.
18. The test script assertion generating apparatus of claim 17, wherein the database operation statement assertion generating unit comprises:
an insertion operation assertion generating unit, which adds a query record having the same name as a key in a source code database mapping file in a preset assertion mapping file if the database operation type is insertion, takes the number of records in a return table as an actual value of assertion, takes 1 as an expected value of assertion, and generates a corresponding assertion statement;
if the database operation type is updating, adding a query record which is identical to the key in the source code database mapping file in name in a preset assertion mapping file, taking the number of the returned table records as an actual value of assertion, taking a variable represented by a key value in a database statement corresponding to the source code as an expected value of the assertion, and sequentially generating corresponding assertion statements according to the query condition field;
and if the database operation type is deletion, adding a query record which is identical to the key in the source code database mapping file in name to a preset assertion mapping file, taking the number of records in the return table as an actual value of assertion, taking 0 as an expected value of assertion, and generating a corresponding assertion statement.
19. 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 generation method of any of claims 1 to 9 when executing the program.
20. A computer-readable storage medium, on which a computer program is stored, which, when being executed by a processor, carries out a test script assertion generating method as claimed in any one of claims 1 to 9.
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 true CN112860585A (en) 2021-05-28
CN112860585B 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)

Cited By (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

Cited By (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

Also Published As

Publication number Publication date
CN112860585B (en) 2024-01-26

Similar Documents

Publication Publication Date Title
CN111159049B (en) Automatic interface testing method and system
CN107193593B (en) Upgrading method of upgradable file, set top box and storage medium
CN112463634B (en) Software testing method and device under micro-service architecture
CN105786695A (en) Data test method and system
CN111797015B (en) Dynamic byte code based test method and device
CN112905459B (en) Service interface testing method and device, electronic equipment and storage medium
CN109408372A (en) Common component test method, device, computer equipment and storage medium
CN110990274A (en) Data processing method, device and system for generating test case
CN111930617A (en) Automatic testing method and device based on data objectification
CN113051163A (en) Unit testing method, unit testing device, electronic equipment and storage medium
CN111897738B (en) Automatic testing method and device based on atomic service
CN111949491B (en) SQL extraction method and device of MyBatis application program
CN111913889A (en) Test system building method and test system
CN105404574B (en) Smart card and mobile terminal consistency test method and device
CN112988601A (en) Test script development method and device
CN112860585B (en) Test script assertion generation method and device
CN111400169A (en) Method and system for automatically generating netlist file for testing software and hardware
CN114510425A (en) Test case generation method and device
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
CN107229650B (en) Test method and device
CN114385493A (en) Performance test method and device, electronic equipment and storage medium
CN113886221A (en) Test script generation method and device, storage medium and electronic equipment
CN113986263A (en) Code automation test method, device, electronic equipment and storage medium
CN113419957A (en) Rule-based big data offline batch processing performance capacity scanning method and device

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