Methods and apparatus for adaptive test case generation

By using an adaptive test case generation method, code text is segmented using header file identifiers and delimiters, the correspondence between content words is determined, and test cases are generated. This solves the problem of low test case generation efficiency in large software version upgrades and achieves efficient automatic test case generation.

CN117632690BActive Publication Date: 2026-11-17CHINA NAT PETROLEUM CORP +2
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210956630.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-08-10
Publication Date
2026-11-17
Estimated Expiration
2042-08-10

AI Technical Summary

Technical Problem

Generating test cases for new versions is inefficient, especially during version upgrades of large-scale industrial seismic data exploration platform software, where a large number of test cases need to be generated manually.

Method used

By using an adaptive test case generation method, the target code text is obtained, segmented and worded based on header file identifiers and delimiters, the correspondence between content words is determined, and test cases for regular values, pressure values, and outliers are generated using a geological exploration database.

Benefits of technology

This improved the efficiency of test case generation, reduced manual operations by technical personnel, and ensured comprehensive coverage and accuracy of test cases.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117632690B_ABST
    Figure CN117632690B_ABST
Patent Text Reader

Abstract

This application discloses a method, apparatus, device, and storage medium for adaptively generating test cases, belonging to the field of code testing technology. The method includes: obtaining target code text; segmenting the target code text based on header file identifiers to obtain multiple target code blocks; obtaining all content words in the target code text based on delimiters and a code content word dictionary within the target code blocks; determining whether the correspondences between pointer content words, class object content words, and structure content words included in the target code text all exist in a geological exploration database; when the correspondences all exist in the geological exploration database, obtaining the constraint data type corresponding to each structure content word defined in the target code text, and generating regular value test cases, stress value test cases, and outlier value test cases. Using this application can improve the efficiency of test case generation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of code testing technology, and in particular to a method and apparatus for adaptively generating test cases. Background Technology

[0002] Code testing is crucial for verifying software coding quality. When software versions change, code testing is necessary to identify errors in the new code and allow for corresponding improvements. For large-scale industrial seismic data exploration platform software, each version upgrade requires a large number of new test cases to test the new code.

[0003] Currently, when generating test cases for a new version, technical personnel usually manually generate test cases for the new version by comparing them with the test cases of the previous version, which is inefficient. Summary of the Invention

[0004] This application provides a method and apparatus for adaptively generating test cases, which can solve related problems. The technical solution is as follows: Firstly, a method for adaptively generating test cases is provided, the method comprising: Obtain the target code text; Based on the header file identifier in the target code text, the target code text is segmented to obtain multiple target code blocks; Based on the delimiters in the target code block, the target code block is segmented into words. Based on the code word dictionary, the words in each target code block are determined to obtain all the words in the target code text. The words in the target code text include pointer words, class object words, structure words, and interface name words. Each word includes at least one field, and adjacent fields are separated by a preset symbol. In the target code text, each pointer word corresponds to at least one class object word, each class object word corresponds to at least one structure word, and each structure word corresponds to one interface name word. The pointer word includes one field, the class object word includes two fields, one of which matches a field of the pointer word corresponding to the class object word, and the structure word includes at least three fields, two of which match two fields of the class object word corresponding to the structure word. The word dictionary includes a pointer word dictionary, a class object word dictionary, a structure word dictionary, and an interface name word dictionary. Determine whether the correspondences between the content words in the target code text, including pointer content words, class object content words, and structure content words, all exist in the geological exploration database; When the correspondence between pointer real words, class object real words and structure real words included in the target code text all exist in the geological exploration database, obtain the constraint data type corresponding to each structure real word defined in the target code text, and determine the interface name real word corresponding to each structure real word; For each structure content word corresponding to an interface name content word, generate data that matches the constraint data type corresponding to the structure content word and is within a specified numerical range, as a regular value test case for the interface corresponding to the interface name content word; generate data that matches the constraint data type corresponding to the structure content word but is outside the specified numerical range, as a stress value test case for the interface; generate data that does not match the constraint data type corresponding to the structure content word, as an outlier test case for the interface.

[0005] In one possible implementation, prior to obtaining the target code text, the process further includes: In the initial code text, find the comment start character. After finding each comment start character, determine the nearest comment end character in the initial code text after the comment start character, and delete the code from the comment start character to the comment end character. The initial code text after deletion is identified as the target code text.

[0006] In one possible implementation, the target code text is segmented based on the header file identifier in the target code text to obtain multiple target code blocks, including: Based on the header file identifier in the target code text, the target code text is divided into multiple initial code blocks; In multiple initial code blocks, the code of the preset target function is deleted to obtain multiple target code blocks.

[0007] In one possible implementation, the target function includes a parse function, a filter function, a loop function, and an encryption function.

[0008] In one possible implementation, determining whether the correspondences between the content words in the target code text, including pointer content words, class object content words, and structure content words, all exist in the geological exploration database includes: For each pointer content word and class object content word in the target code text, determine whether there is a data table with the class object content word as the table name stored in the storage space corresponding to the pointer content word; If the storage space corresponding to the pointer content word does not store a data table with the class object content word as the table name, then it is determined that the correspondence between the pointer content word, class object content word and structure content word included in the content word in the target code text does not all exist in the geological exploration database; If the storage space corresponding to the pointer content word contains a data table with the class object content word as the table name, then for each structure content word corresponding to the class object content word in the target code text, it is determined whether there is a page in the data table with each structure content word corresponding to the class object content word as the page name. If there is no page in the data table with any structure content word corresponding to the class object content word as the page name, it is determined that the correspondence between the pointer content word, class object content word, and structure content word included in the content words in the target code text does not all exist in the geological exploration database. If there is a page in the data table with each structure content word corresponding to the class object content word as the page name, it is determined that the correspondence between the pointer content word, class object content word, and structure content word included in the content words in the target code text all exist in the geological exploration database.

[0009] In one possible implementation, the method includes: Set the root node of the AST (Abstract Syntax Tree), and set each pointer content word in the target code text as a child node of the root node; For each pointer content word, set the class object content word corresponding to the pointer content word in the target code text as a child node of the pointer content word; For each class object content word, set the structure content word corresponding to the class object content word in the target code text as a child node of the class object content word; The correspondence between each pointer term and class object term in the target code text includes: For each pair of pointer real words and class object real words that have a parent-child relationship in the AST abstract syntax tree; The phrase for each structure word corresponding to the class object word in the target code text includes: For each child node of the class object real word in the AST abstract syntax tree, there is a corresponding structure real word.

[0010] In one possible implementation, the constraint data type includes: At least one of integer, floating-point, character, and time types, and at least one of empty and non-empty types.

[0011] Secondly, an apparatus for adaptively generating test cases is provided, the apparatus comprising: The acquisition module is used to acquire the target code text; The segmentation module is used to segment the target code text based on the header file identifier in the target code text to obtain multiple target code blocks; The word segmentation module is used to segment the target code block based on the delimiters in the target code block, and to determine the content words in each target code block based on the code content word dictionary to obtain all the content words in the target code text. The content words in the target code text include pointer content words, class object content words, structure content words, and interface name content words. Each content word includes at least one field, and adjacent fields are separated by a preset symbol. In the target code text, each pointer content word corresponds to at least one class object content word, each class object content word corresponds to at least one structure content word, and each structure content word corresponds to one interface name content word. The pointer content word includes one field, the class object content word includes two fields, one of which matches a field of the pointer content word corresponding to the class object content word, and the structure content word includes at least three fields, two of which match two fields of the class object content word corresponding to the structure content word. The content word dictionary includes a pointer content word dictionary, a class object content word dictionary, a structure content word dictionary, and an interface name content word dictionary. The first determining module is used to determine whether the correspondence between the real words in the target code text, including pointer real words, class object real words and structure real words, all exist in the geological exploration database; The second determining module is used to obtain the constraint data type corresponding to each structure real word defined in the target code text and determine the interface name real word corresponding to each structure real word when the correspondence between the pointer real words, class object real words and structure real words included in the real words in the target code text all exist in the geological exploration database. The generation module is used to generate, for each structure content word corresponding to an interface name content word, data that matches the constraint data type corresponding to the structure content word and is within a specified numerical range, as a regular value test case for the interface corresponding to the interface name content word; generate data that matches the constraint data type corresponding to the structure content word but is outside the specified numerical range, as a stress value test case for the interface; and generate data that does not match the constraint data type corresponding to the structure content word, as an outlier value test case for the interface.

[0012] In one possible implementation, the acquisition module is further configured to: In the initial code text, find the comment start character. After finding each comment start character, determine the nearest comment end character in the initial code text after the comment start character, and delete the code from the comment start character to the comment end character. The initial code text after deletion is identified as the target code text.

[0013] In one possible implementation, the segmentation module is used for: Based on the header file identifier in the target code text, the target code text is divided into multiple initial code blocks; In multiple initial code blocks, the code of the preset target function is deleted to obtain multiple target code blocks.

[0014] In one possible implementation, the target function includes a parse function, a filter function, a loop function, and an encryption function.

[0015] In one possible implementation, the first determining module is configured to: For each pointer content word and class object content word in the target code text, determine whether there is a data table with the class object content word as the table name stored in the storage space corresponding to the pointer content word; If the storage space corresponding to the pointer content word does not store a data table with the class object content word as the table name, then it is determined that the correspondence between the pointer content word, class object content word and structure content word included in the content word in the target code text does not all exist in the geological exploration database; If the storage space corresponding to the pointer content word contains a data table with the class object content word as the table name, then for each structure content word corresponding to the class object content word in the target code text, it is determined whether there is a page in the data table with each structure content word corresponding to the class object content word as the page name. If there is no page in the data table with any structure content word corresponding to the class object content word as the page name, it is determined that the correspondence between the pointer content word, class object content word, and structure content word included in the content words in the target code text does not all exist in the geological exploration database. If there is a page in the data table with each structure content word corresponding to the class object content word as the page name, it is determined that the correspondence between the pointer content word, class object content word, and structure content word included in the content words in the target code text all exist in the geological exploration database.

[0016] In one possible implementation, the first determining module is configured to: Set the root node of the AST (Abstract Syntax Tree), and set each pointer content word in the target code text as a child node of the root node; For each pointer content word, set the class object content word corresponding to the pointer content word in the target code text as a child node of the pointer content word; For each class object content word, set the structure content word corresponding to the class object content word in the target code text as a child node of the class object content word; The correspondence between each pointer term and class object term in the target code text includes: For each pair of pointer real words and class object real words that have a parent-child relationship in the AST abstract syntax tree; The phrase for each structure word corresponding to the class object word in the target code text includes: For each child node of the class object real word in the AST abstract syntax tree, there is a corresponding structure real word.

[0017] In one possible implementation, the constraint data type includes: At least one of integer, floating-point, character, and time types, and at least one of empty and non-empty types.

[0018] Thirdly, a computer device is provided, the computer device including a processor and a memory, the memory storing at least one instruction, the instruction being loaded and executed by the processor to implement the first aspect and its possible implementations.

[0019] Fourthly, a computer-readable storage medium is provided, wherein at least one instruction is stored therein, the instruction being loaded and executed by a processor to implement the first aspect and a method thereof.

[0020] Fifthly, a computer program product is provided, the computer program product including at least one instruction, the at least one instruction being loaded and executed by a processor to implement the first aspect and its possible implementations.

[0021] The beneficial effects of the technical solutions provided in this application are: In this embodiment, all content words in the target code text are first determined. Then, it is determined whether the correspondence between pointer content words, class object content words, and structure content words included in all content words in the target code text exists in the geological exploration database. When the correspondence between pointer content words, class object content words, and structure content words included in all content words in the target code text exists in the geological exploration database, the constraint data type corresponding to each structure content word defined in the target code text is obtained, and the interface name content word corresponding to each structure content word is determined. Based on the constraint data type and the specified numerical range, normal value test cases, stress value test cases, and abnormal value test cases are generated. This eliminates the need for technicians to manually generate test cases, thus improving the efficiency of test case generation. Attached Figure Description

[0022] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0023] Figure 1 This is a flowchart of a method for adaptively generating test cases provided in an embodiment of this application; Figure 2 This is a partial schematic diagram of an initial code text provided in an embodiment of this application; Figure 3 This is a partial schematic diagram of a target code text provided in an embodiment of this application; Figure 4 This is a partial schematic diagram of a content word dictionary provided in an embodiment of this application; Figure 5 This is a partial schematic diagram of an abstract syntax tree provided in an embodiment of this application; Figure 6 This is a schematic diagram of a constrained data type for structured real words provided in an embodiment of this application; Figure 7 This is a schematic diagram of the structure of an adaptive test case generation device provided in an embodiment of this application; Figure 8 This is a structural block diagram of a server provided in an embodiment of this application. Detailed Implementation

[0024] To make the objectives, technical solutions, and advantages of this application clearer, the embodiments of this application will be described in further detail below with reference to the accompanying drawings.

[0025] This application provides a method for adaptively generating test cases. This method can be implemented by a computer device, which can be a server or a terminal. The server can be a single server or a server cluster consisting of multiple servers.

[0026] A server may include a processor, memory, communication components, etc., with the processor connected to the memory and communication components respectively.

[0027] The processor can be a CPU (central processing unit). The processor can be used to read instructions and process data, for example, to obtain target code text; to segment the target code text based on header file identifiers in the target code text to obtain multiple target code blocks; to perform word segmentation on the target code blocks based on delimiters in the target code blocks; to determine the content words in each target code block based on a code content word dictionary to obtain all content words in the target code text; to determine whether the correspondences between pointer content words, class object content words, and structure content words included in the target code text all exist in a geological exploration database; to obtain the constraint data type corresponding to each structure content word defined in the target code text, and to determine the interface name content word corresponding to each structure content word, and so on.

[0028] Memory can include ROM (read-only memory), RAM (random access memory), CD-ROM (compact disc read-only memory), hard disks, optical data storage devices, etc. Memory can be used for data storage, such as storing acquired target code text, storing multiple target code blocks, storing all the content words in the target code text, storing the correspondences between pointer content words, class object content words, and structure content words in the target code text, storing generated regular value test cases, stress value test cases, and outlier value test cases, etc.

[0029] Communication components can be wired network connectors, Wi-Fi (Wireless Fidelity) modules, Bluetooth modules, cellular network communication modules, etc. Communication components can be used to receive and transmit signals.

[0030] Figure 1 This is a flowchart illustrating an adaptive test case generation method provided in an embodiment of this application. See also... Figure 1 This embodiment includes: 101. Obtain the target code text.

[0031] 102. Based on the header file identifier in the target code text, the target code text is segmented to obtain multiple target code blocks.

[0032] 103. Based on the delimiters in the target code block, segment the target code block into words. Based on the code content word dictionary, determine the content words in each target code block to obtain all the content words in the target code text.

[0033] 104. Determine whether the correspondences between the content words in the target code text, including pointer content words, class object content words, and structure content words, all exist in the geological exploration database.

[0034] 105. When the correspondence between pointer real words, class object real words and structure real words included in the target code text all exist in the geological exploration database, obtain the constraint data type corresponding to each structure real word defined in the target code text, and determine the interface name real word corresponding to each structure real word.

[0035] 106. For each structure content word corresponding to an interface name content word, generate data that matches the constraint data type corresponding to the structure content word and is within the specified numerical range, as the regular value test case of the interface corresponding to the interface name content word; generate data that matches the constraint data type corresponding to the structure content word but is outside the specified numerical range, as the stress value test case of the interface; generate data that does not match the constraint data type corresponding to the structure content word, as the outlier value test case of the interface.

[0036] Before executing step 101, the target code text needs to be determined based on the initial code text. The steps to determine the target code text can be as follows: obtain the initial code text; then, find the comment start character; after finding each comment start character, determine the nearest comment end character in the initial code text; and delete the code from the comment start character to the comment end character. The initial code text after deletion is then determined as the target code text.

[0037] The code between the comment start symbol and the comment end symbol is the comment code used to interpret the target code text. Each comment start symbol, the nearest comment end symbol after the comment start symbol, and the comment code constitute a group of comments. The initial code text includes the target code text and all comments.

[0038] In implementation, the programming language used is C++ / C, for example, such as... Figure 2 As shown ( Figure 2 (This is part of the initial code text). Line 5 of the initial code text, "virtual GPK getPK()=0;", defines an interface. Lines 0-4 of the initial code text contain comments explaining this interface. In the initial code text, " / " is the comment start character, " The slash " / " is the comment terminator. "and" The code between " / " is comment code. Comment code is used to explain specific code in the initial code, such as the interface mentioned above. The comment start character and comment end character indicate the position of the comment code in the initial code text. All comments in the initial code text are deleted to obtain the target code text.

[0039] Due to the diverse nature of software functionality, the amount of code used in coding is often very large, potentially reaching millions of lines. This code requires numerous comments to explain and annotate the code, facilitating subsequent modifications and understanding. However, since comments are used to explain the meaning of specific code rather than to implement specific functions, they are not typically tested during code testing. Therefore, before testing, comments are usually removed from the initial code text, and this revised initial text becomes the target code text. This eliminates the need to revisit the comments during testing, reducing the time spent on code traversal.

[0040] After obtaining the target code text, test cases can be generated, and the corresponding processing is as follows: Regarding step 101: Obtain the target code text.

[0041] The target code text is a collection of code texts composed of all functional code and all structural code. Functional code is the code that implements specific functions of the software; it has testability and can be considered content words in the target code. Structural code is the code used to call functions or make declarations according to the rules of the programming language; it does not have testability and can be considered function words in the target code.

[0042] For example, such as Figure 3 As shown in the diagram, line 1, "virtual void setPK(GPK nld)=0;", defines an interface. Here, "virtual void" and "=0" are structural code and have no testing significance. "virtual void" is a declaration statement used to declare that the called function is a virtual function. The final "=0" does not indicate that the function returns 0; it only serves a formal purpose, telling the compiler that the called function is a pure virtual function. "setPK(GPK nld)" defines an interface that may implement some functionality in the software and has testing significance.

[0043] In practice, when coding, technicians need to store each function code as a content word in a content word dictionary to prepare for subsequent traversal of all content words in the target code text.

[0044] Regarding step 102: Based on the header file identifier in the target code text, the target code text is segmented to obtain multiple target code blocks.

[0045] Each target code block includes a header file identifier, each target code block corresponds to a header file, and corresponds to at least one implementation file.

[0046] In implementation, taking C++ / C programs as an example, header files are frequently used. Typically, each object code block consists of a header file and implementation files, with one header file corresponding to multiple implementation files. Header files generally serve only a descriptive purpose; they are carrier files containing function declarations and data interface declarations. Their main function is to store the definitions and declarations of software functions and prevent definition conflicts. Implementation files are used to implement specific functions of the software. C++ / C program header files usually have the ".h" suffix, as shown in Table 1 (…). Figure 4 (For several common header file identifiers), #include<assert.h> #include<ctype.h> and #include<errno.h> These are all header file identifiers.

[0047] Table 1 – Header File Identifier Table (Partial)

[0048] During code segmentation, the target code text can be traversed based on a pre-stored table of header file identifiers to identify all header file identifiers within the target code text. Then, the code between two adjacent header file identifiers is segmented to obtain multiple target code blocks. These multiple target code blocks can then be processed simultaneously, thereby improving the efficiency of test case generation.

[0049] Optionally, the target code text can be segmented based on the implementation file identifier in the target code text to obtain multiple target code blocks.

[0050] In implementation, the target code text can be traversed based on a pre-stored table of implementation file identifiers to determine all header implementation file identifiers within the target code text. Then, the target code text is split at the end of the line preceding each implementation file identifier, resulting in multiple target code blocks. These multiple target code blocks can then be processed simultaneously, thereby improving the efficiency of test case generation.

[0051] Optionally, the target code text can be divided into multiple initial code blocks based on the header file identifier in the target code text. Within these initial code blocks, the code of a predefined target function can be deleted to obtain multiple target code blocks.

[0052] The objective function includes an extraction function, a filtering function, a loop function, and an encryption function.

[0053] In implementation, the target code text may contain many functions, each with different functionalities. For example, loop functions can avoid excessive code duplication, making the target code text concise; filtering functions can filter seismic waves received by the detection instrument, removing clutter from the seismic wave data. From the software's actual functional perspective, these functions have no practical testing significance. Therefore, based on target function markers, such as "for," "if," and "case," these functions without practical testing significance can be deleted, leaving only the code with testing significance, thereby improving the efficiency of obtaining all content words in the target code text. The specific steps for determining the target function can be as follows: traverse the target function markers in the target code text, determine if the adjacent character after the target function marker is a prefix bracket "{". If the adjacent character after the target function marker is a prefix bracket "{", then determine the nearest closing bracket "}" after that prefix bracket "{". If there are no other target function markers between the closing bracket "}" and the aforementioned target function marker, then the target function marker, the prefix brackets, and the code between the prefix brackets are determined as the target function code. Finally, delete the preset target function code to obtain the target code block.

[0054] Regarding step 103: Based on the delimiters in the target code block, the target code block is segmented into words. Based on the code content word dictionary, the content words in each target code block are determined, and all content words in the target code text are obtained.

[0055] The vocabulary dictionary includes a pointer vocabulary dictionary, a class object vocabulary dictionary, a structure vocabulary dictionary, and an interface name vocabulary dictionary. The separator in the target code block is the space character. The vocabulary in the target code text includes pointer vocabulary, class object vocabulary, structure vocabulary, and interface name vocabulary. Each vocabulary includes at least one field, with adjacent fields separated by a preset symbol. In the target code text, each pointer vocabulary corresponds to at least one class object vocabulary, each class object vocabulary corresponds to at least one structure vocabulary, and each structure vocabulary corresponds to one interface name vocabulary.

[0056] In implementation, the target code block consists of lines of code, with each word separated by a space character within each line. When segmenting the target code block, each space character is iterated through, dividing each line of code into multiple words, thus obtaining all the words in the target code block. After segmentation, all the words in the target code block are compared with all the content words stored in the code vocabulary dictionary to determine all the content words in each target code block, thereby obtaining all the content words in the target code text.

[0057] like Figure 4 As shown, the code terminology dictionary includes a pointer terminology dictionary, a class object terminology dictionary, a structure terminology dictionary, and an interface name terminology dictionary. Each terminology dictionary stores the corresponding terms. Pointer terms have one field, class object terms have two fields, one of which matches a field of the pointer terminology corresponding to the class object terminology. Structure terms have at least three fields, two of which match two fields of the class object terminology corresponding to the structure terminology. Pointer terms include point1, point2, point3, etc. Class object terms include point1_well, point1_fault, point2_geometry, point2_business, point3_data, etc. Structure terms include point1_well_owner, point1_well_location_id, point1_well_name_id, point2_geometry_line, etc.

[0058] Regarding step 104: Determine whether the correspondences between the content words in the target code text, including pointer content words, class object content words, and structure content words, all exist in the geological exploration database.

[0059] The geological exploration database comprises multiple storage spaces, each with a specific address name. Within each storage space, corresponding data tables are stored, each with a specific header name. Each data table contains multiple corresponding data pages, each with a specific page name.

[0060] In practice, the following process can be used to determine whether the above correspondences all exist in the geological exploration database: (1) For each pointer real word and class object real word in the target code text, determine whether there is a data table with the class object real word as the table name stored in the storage space corresponding to the pointer real word.

[0061] (2) If the storage space corresponding to the pointer content word does not store a data table with the class object content word as the table name, then it is determined that the correspondence between the pointer content word, class object content word and structure content word included in the target code text does not exist in the geological exploration database.

[0062] (3) If the storage space corresponding to the pointer content word contains a data table with the class object content word as the table name, then for each structure content word corresponding to the class object content word in the target code text, determine whether there is a page in the data table with each structure content word corresponding to the class object content word as the page name. If there is no page in the data table with any structure content word corresponding to the class object content word as the page name, then determine that the correspondence between the pointer content word, class object content word and structure content word included in the content word in the target code text does not exist in the geological exploration database. If there is a page in the data table with each structure content word corresponding to the class object content word as the page name, then determine that the correspondence between the pointer content word, class object content word and structure content word included in the content word in the target code text exists in the geological exploration database.

[0063] In this way, after determining whether the correspondences between pointer words, class object words, and structure words in the target code text all exist in the geological exploration database, it can be guaranteed that the subsequently generated test cases cover all interfaces in the target code, preventing any omission in testing for a certain interface.

[0064] Optionally, after determining that the correspondences between pointer words, class object words, and structure words in the target code text all exist in the geological exploration database, the computer equipment can number all the words and establish a word numbering table. Table 2 shows a partial word numbering table.

[0065] Table 2 – Content Word Numbering Table (Partial)

[0066] Then, the numbers of pointer instances, class object instances, and structure instances can be added sequentially to the end of the storage space address name, the table header name, and the page name of the data page in the geological exploration database. For example, adding the corresponding number to the end of the name of the data table with the header name "point1_well_owner" will change the table header name to "point1_well_owner1.1.1".

[0067] Optionally, after numbering all content words, an Abstract Syntax Tree (AST) can be constructed based on the correspondence between all content words. A partial AST is shown below. Figure 5 As shown. The specific process of constructing the abstract syntax tree is as follows: (1) Set the root node of the abstract syntax tree and set each pointer word in the target code text as a child node of the root node. The root node of the abstract syntax tree is a node with no practical meaning.

[0068] (2) For each pointer content, set the class object content corresponding to the pointer content in the target code text as a child node of the pointer content.

[0069] (3) For each class object content word, set the structure content word corresponding to the class object content word in the target code text as a child node of the class object content word.

[0070] In this way, all the content words in the target code text can be displayed in a tree structure, thus more intuitively showing the relationships between all the content words in the target code text.

[0071] Optionally, when constructing the abstract syntax tree, the interface name content word corresponding to the structure content word in the target code text can be set as a child node of the structure content word.

[0072] Regarding step 105: When the correspondence between pointer real words, class object real words and structure real words included in the target code text all exist in the geological exploration database, obtain the constraint data type corresponding to each structure real word defined in the target code text, and determine the interface name real word corresponding to each structure real word.

[0073] The data types subject to constraints include at least one of integer, floating-point, character, and time types, and at least one of empty and non-empty types. Integer means that integers can be entered; floating-point means that decimals can be entered; character means that a combination of letters and numbers can be entered; and time means that dates can be entered. Empty means that the input data can be empty; and non-empty means that the input data cannot be empty.

[0074] In implementation, the data type stored in each data page may differ. For example, the data page corresponding to `point1_well_owner` (the well owner) might store multiple names, while the data page corresponding to `point1_well_location` (the well location) might store multiple latitude and longitude coordinates. Therefore, as... Figure 6As shown, corresponding constraint data types can be set for each structure content term, establishing a correspondence graph between structure content terms and constraint data types. Here, `bigint` represents integers, `double precision` represents floating-point numbers, `character varying` represents characters, and `timestamp without time zone` represents time. `not null` represents non-nullables, and `not null` (annotation) represents nullables. When a user queries data in the data page corresponding to a structure content term, input is required into the interface. Only when the input data conforms to the constraint data type of the structure content term can the user retrieve the corresponding data. If the input data does not conform to the constraint data type of the structure content term, the interface will output an error message, prompting the user to input data that conforms to the constraint data type of the structure content term. Therefore, after completing step 104, when the correspondences between pointer content terms, class object content terms, and structure content terms included in the target code text all exist in the geological exploration database, the computer device can obtain the constraint data type corresponding to each structure content term defined in the target code text and determine the interface name content term corresponding to each structure content term, preparing for the subsequent generation of test cases.

[0075] Regarding step 106: For each structure content word corresponding to an interface name content word, generate data that matches the constraint data type corresponding to the structure content word and is within the specified numerical range, as the regular value test case for the interface corresponding to the interface name content word; generate data that matches the constraint data type corresponding to the structure content word but is outside the specified numerical range, as the stress value test case for the interface; and generate data that does not match the constraint data type corresponding to the structure content word, as the outlier value test case for the interface.

[0076] The specified numerical range is a preset range, which can be set by technicians according to actual needs.

[0077] In implementation, taking the structure `point1_well_name` as an example, the constraint data type of this structure is character, and the corresponding interface name can be `get point1_well_name`. The corresponding data page stores the well name data. The naming convention for wells is generally oilfield name + number, such as `tlm001` (Tarim 00001). Based on existing data, the largest oilfield in my country has approximately 5000 wells. Based on this data, technicians can set the specified value range to 10000. When the test cases generated by the computer device are "tlm00001"-"tlm10000", they can be considered as regular value test cases for the interface corresponding to the interface name. When the test cases generated by the computer device are "tlm100000000", they can be considered as stress value test cases for the interface corresponding to the interface name. When the test cases generated by the computer device do not conform to the constraint data type of the structure content corresponding to the interface name, they can be considered as abnormal value test cases for the interface corresponding to the interface name, such as "%%¥@…#&!@".

[0078] For other structure content words corresponding to interface name content words, the specific process of generating regular value test cases, stress value test cases and abnormal value test cases is the same as the above process, and will not be repeated here.

[0079] After completing step 106, the generated test cases for regular values, stress values, and outliers can be used to test the interface. If the interface returns correct data when a regular value test case is input, and also returns correct data when a stress value test case is input (although the return time is longer), and fails to return correct data when an outlier test case is input, then the interface can be considered to have passed the test. For each interface in the target code text, test it using regular value test cases, stress value test cases, and outlier test cases respectively. If each interface passes the test, then all interfaces in the target code text can be considered error-free, and further testing or actual use can be performed on the target code text.

[0080] In this embodiment, all real words in the target code text are first determined. Then, it is determined whether the correspondence between pointer real words, class object real words, and structure real words included in all real words in the target code text exists in the geological exploration database. When the correspondence between pointer real words, class object real words, and structure real words included in all real words in the target code text exists in the geological exploration database, the constraint data type corresponding to each structure real word defined in the target code text is obtained, and the interface name real word corresponding to each structure real word is determined. Based on the constraint data type and the specified numerical range, regular value test cases, stress value test cases, and abnormal value test cases for each interface are generated, without the need for technicians to manually generate test cases, thus improving the efficiency of test case generation.

[0081] This application provides an apparatus for adaptively generating test cases. This apparatus can be the computer device described in the above embodiments, such as... Figure 7 As shown, the device includes: Module 710 is used to acquire target code text; The segmentation module 720 is used to segment the target code text based on the header file identifier in the target code text to obtain multiple target code blocks; The word segmentation module 730 is used to segment the target code block based on the delimiters in the target code block, determine the content words in each target code block based on the code content word dictionary, and obtain all the content words in the target code text. The content words in the target code text include pointer content words, class object content words, structure content words, and interface name content words. Each content word includes at least one field, and adjacent fields are separated by a preset symbol. In the target code text, each pointer content word corresponds to at least one class object content word, each class object content word corresponds to at least one structure content word, and each structure content word corresponds to one interface name content word. The pointer content word includes one field, the class object content word includes two fields, one of which matches a field of the pointer content word corresponding to the class object content word, and the structure content word includes at least three fields, two of which match two fields of the class object content word corresponding to the structure content word. The content word dictionary includes a pointer content word dictionary, a class object content word dictionary, a structure content word dictionary, and an interface name content word dictionary. The first determining module 740 is used to determine whether the correspondence between the real words in the target code text, including pointer real words, class object real words and structure real words, all exist in the geological exploration database; The second determining module 750 is used to obtain the constraint data type corresponding to each structure real word defined in the target code text and determine the interface name real word corresponding to each structure real word when the correspondence between the real words in the target code text, including pointer real words, class object real words and structure real words, all exist in the geological exploration database. The generation module 760 is used to generate, for each structure content word corresponding to an interface name content word, data that matches the constraint data type corresponding to the structure content word and is within a specified numerical range, as a regular value test case for the interface corresponding to the interface name content word; generate data that matches the constraint data type corresponding to the structure content word but is outside the specified numerical range, as a stress value test case for the interface; and generate data that does not match the constraint data type corresponding to the structure content word, as an outlier value test case for the interface.

[0082] In one possible implementation, the acquisition module 710 is further configured to: In the initial code text, find the comment start character. After finding each comment start character, determine the nearest comment end character in the initial code text after the comment start character, and delete the code from the comment start character to the comment end character. The initial code text after deletion is identified as the target code text.

[0083] In one possible implementation, the segmentation module 720 is used for: Based on the header file identifier in the target code text, the target code text is divided into multiple initial code blocks; In multiple initial code blocks, the code of the preset target function is deleted to obtain multiple target code blocks.

[0084] In one possible implementation, the target function includes a parse function, a filter function, a loop function, and an encryption function.

[0085] In one possible implementation, the first determining module 740 is configured to: For each pointer content word and class object content word in the target code text, determine whether there is a data table with the class object content word as the table name stored in the storage space corresponding to the pointer content word; If the storage space corresponding to the pointer content word does not store a data table with the class object content word as the table name, then it is determined that the correspondence between the pointer content word, class object content word and structure content word included in the content word in the target code text does not all exist in the geological exploration database; If the storage space corresponding to the pointer content word contains a data table with the class object content word as the table name, then for each structure content word corresponding to the class object content word in the target code text, it is determined whether there is a page in the data table with each structure content word corresponding to the class object content word as the page name. If there is no page in the data table with any structure content word corresponding to the class object content word as the page name, it is determined that the correspondence between the pointer content word, class object content word, and structure content word included in the content words in the target code text does not all exist in the geological exploration database. If there is a page in the data table with each structure content word corresponding to the class object content word as the page name, it is determined that the correspondence between the pointer content word, class object content word, and structure content word included in the content words in the target code text all exist in the geological exploration database.

[0086] In one possible implementation, the first determining module 740 is configured to: Set the root node of the abstract syntax tree, and set each pointer content word in the target code text as a child node of the root node; For each pointer content word, set the class object content word corresponding to the pointer content word in the target code text as a child node of the pointer content word; For each class object content word, set the structure content word corresponding to the class object content word in the target code text as a child node of the class object content word; The correspondence between each pointer term and class object term in the target code text includes: For each pair of pointer real words and class object real words that have a parent-child relationship in the abstract syntax tree; The phrase for each structure word corresponding to the class object word in the target code text includes: For each child node of the class object real word in the abstract syntax tree, there is a corresponding structure real word.

[0087] In one possible implementation, the constraint data type includes: At least one of integer, floating-point, character, and time types, and at least one of empty and non-empty types.

[0088] It should be noted that the adaptive test case generation apparatus provided in the above embodiments is only illustrated by the division of the above functional modules. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the apparatus can be divided into different functional modules to complete all or part of the functions described above. Furthermore, the adaptive test case generation apparatus and the adaptive test case generation method embodiments provided in the above embodiments belong to the same concept, and their specific implementation process is detailed in the method embodiments, which will not be repeated here.

[0089] Figure 8 This is a schematic diagram of the structure of a computer device provided in an embodiment of this application. The computer device can be a server or a terminal. The computer device 800 can vary significantly due to differences in configuration or performance, and may include one or more processors 801 and one or more memories 802. The memory 802 stores at least one instruction, which is loaded and executed by the processor 801 to implement the methods provided in the above-described method embodiments. Of course, the server may also have wired or wireless network interfaces, a keyboard, and input / output interfaces for input and output. The server may also include other components for implementing device functions, which will not be elaborated upon here.

[0090] In an exemplary embodiment, a computer-readable storage medium is also provided, such as a memory including instructions that can be executed by a processor in a terminal to complete the method for adaptively generating test cases in the above embodiments. The computer-readable storage medium may be non-transitory. For example, the computer-readable storage medium may be ROM (read-only memory), RAM (random access memory), CD-ROM, magnetic tape, floppy disk, and optical data storage device, etc.

[0091] Those skilled in the art will understand that all or part of the steps of the above embodiments can be implemented by hardware or by a program instructing related hardware. The program can be stored in a computer-readable storage medium, such as a read-only memory, a disk, or an optical disk.

[0092] The above description is merely an optional embodiment of this application and is not intended to limit this application. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the protection scope of this application.

Claims

1. A method for adaptively generating test cases, characterized in that, The method includes: Obtain the target code text; Based on the header file identifier in the target code text, the target code text is segmented to obtain multiple target code blocks; Based on the delimiters in the target code block, the target code block is segmented into words. Based on the code word dictionary, the words in each target code block are determined to obtain all the words in the target code text. The words in the target code text include pointer words, class object words, structure words, and interface name words. Each word includes at least one field, and adjacent fields are separated by a preset symbol. In the target code text, each pointer word corresponds to at least one class object word, each class object word corresponds to at least one structure word, and each structure word corresponds to one interface name word. The pointer word includes one field, the class object word includes two fields, one of which matches a field of the pointer word corresponding to the class object word, and the structure word includes at least three fields, two of which match two fields of the class object word corresponding to the structure word. The word dictionary includes a pointer word dictionary, a class object word dictionary, a structure word dictionary, and an interface name word dictionary. Determine whether the correspondences between the content words in the target code text, including pointer content words, class object content words, and structure content words, all exist in the geological exploration database; When the correspondence between the pointer real words, class object real words and structure real words included in the target code text all exist in the geological exploration database, the constraint data type corresponding to each structure real word defined in the target code text is obtained, and the interface name real word corresponding to each structure real word is determined. The constraint data type includes at least one of integer, floating point, character, and time types, and at least one of empty and non-empty types. For each structure content word corresponding to an interface name content word, generate data that matches the constraint data type corresponding to the structure content word and is within a specified numerical range, as a regular value test case for the interface corresponding to the interface name content word; generate data that matches the constraint data type corresponding to the structure content word but is outside the specified numerical range, as a stress value test case for the interface; generate data that does not match the constraint data type corresponding to the structure content word, as an outlier test case for the interface.

2. The method according to claim 1, characterized in that, Before obtaining the target code text, the process also includes: In the initial code text, find the comment start character. After finding each comment start character, determine the nearest comment end character in the initial code text after the comment start character, and delete the code from the comment start character to the comment end character. The initial code text after deletion is identified as the target code text.

3. The method according to claim 1, characterized in that, The target code text is segmented based on the header file identifier in the target code text to obtain multiple target code blocks, including: Based on the header file identifier in the target code text, the target code text is divided into multiple initial code blocks; In multiple initial code blocks, the code of the preset target function is deleted to obtain multiple target code blocks.

4. The method according to claim 3, characterized in that, The target function includes a parse function, a filter function, a loop function, and an encryption function.

5. The method according to claim 1, characterized in that, The determination of whether the correspondences between the content words in the target code text, including pointer content words, class object content words, and structure content words, all exist in the geological exploration database includes: For each pointer content word and class object content word in the target code text, determine whether there is a data table with the class object content word as the table name stored in the storage space corresponding to the pointer content word; If the storage space corresponding to the pointer content word does not store a data table with the class object content word as the table name, then it is determined that the correspondence between the pointer content word, class object content word and structure content word included in the content word in the target code text does not all exist in the geological exploration database; If the storage space corresponding to the pointer content word contains a data table with the class object content word as the table name, then for each structure content word corresponding to the class object content word in the target code text, it is determined whether there is a page in the data table with each structure content word corresponding to the class object content word as the page name. If there is no page in the data table with any structure content word corresponding to the class object content word as the page name, it is determined that the correspondence between the pointer content word, class object content word, and structure content word included in the content words in the target code text does not all exist in the geological exploration database. If there is a page in the data table with each structure content word corresponding to the class object content word as the page name, it is determined that the correspondence between the pointer content word, class object content word, and structure content word included in the content words in the target code text all exist in the geological exploration database.

6. The method according to claim 5, characterized in that, The method includes: Set the root node of the AST (Abstract Syntax Tree), and set each pointer content word in the target code text as a child node of the root node; For each pointer content word, set the class object content word corresponding to the pointer content word in the target code text as a child node of the pointer content word; For each class object content word, set the structure content word corresponding to the class object content word in the target code text as a child node of the class object content word; The correspondence between each pointer term and class object term in the target code text includes: For each pair of pointer real words and class object real words that have a parent-child relationship in the AST abstract syntax tree; The phrase for each structure word corresponding to the class object word in the target code text includes: For each child node of the class object real word in the AST abstract syntax tree, there is a corresponding structure real word.

7. An apparatus for adaptively generating test cases, characterized in that, The device includes: The acquisition module is used to acquire the target code text; The segmentation module is used to segment the target code text based on the header file identifier in the target code text to obtain multiple target code blocks; The word segmentation module is used to segment the target code block based on the delimiters in the target code block, and to determine the content words in each target code block based on the code content word dictionary to obtain all the content words in the target code text. The content words in the target code text include pointer content words, class object content words, structure content words, and interface name content words. Each content word includes at least one field, and adjacent fields are separated by a preset symbol. In the target code text, each pointer content word corresponds to at least one class object content word, each class object content word corresponds to at least one structure content word, and each structure content word corresponds to one interface name content word. The pointer content word includes one field, the class object content word includes two fields, one of which matches a field of the pointer content word corresponding to the class object content word, and the structure content word includes three fields, two of which match two fields of the class object content word corresponding to the structure content word. The content word dictionary includes a pointer content word dictionary, a class object content word dictionary, a structure content word dictionary, and an interface name content word dictionary. The first determining module is used to determine whether the correspondence between the real words in the target code text, including pointer real words, class object real words and structure real words, all exist in the geological exploration database; The second determining module is used to obtain the constraint data type corresponding to each structure real word defined in the target code text and determine the interface name real word corresponding to each structure real word when the correspondence between the pointer real words, class object real words and structure real words included in the real words in the target code text all exist in the geological exploration database. The generation module is used to generate, for each structure content word corresponding to an interface name content word, data that matches the constraint data type corresponding to the structure content word and is within a specified numerical range, as a regular value test case for the interface corresponding to the interface name content word; generate data that matches the constraint data type corresponding to the structure content word but is outside the specified numerical range, as a stress value test case for the interface; and generate data that does not match the constraint data type corresponding to the structure content word, as an outlier value test case for the interface.

8. A computer device, characterized in that, The computer device includes a processor and a memory, the memory storing at least one instruction, which is loaded and executed by the processor to perform the operations performed by the method for adaptively generating test cases as described in any one of claims 1 to 6.

9. A computer-readable storage medium, characterized in that, The storage medium stores at least one instruction, which is loaded and executed by a processor to perform the operations of the adaptive test case generation method as described in any one of claims 1 to 6.

Citation Information

Patent Citations

  • Code testing method and device, storage medium and electronic device

    CN109446078A

  • Method for generating modularized test case

    CN111124938A