Test data generation method and apparatus, and related device
By inputting the vector sequence of the code to be tested into a pre-trained test data generation model, the Seq2Seq model is used to automatically generate test data, solving the problem of tedious and time-consuming manual test data construction and achieving efficient test data generation and code path coverage.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- CHINA TELECOM CORP LTD TECHNOLOGY INNOVATION CENTER
- Filing Date
- 2023-07-07
- Publication Date
- 2026-07-07
AI Technical Summary
Manually building test data is tedious and time-consuming in software development, especially in white-box testing, and it is difficult to cover all paths of the code.
By acquiring the code to be tested, preprocessing it, and then inputting its vector sequence into a pre-trained test data generation model, the test data is automatically generated using the Seq2Seq model.
It enables automated generation of test data, improves the efficiency of test data construction, and ensures maximum code path coverage.
Smart Images

Figure CN116893962B_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of artificial intelligence technology, and in particular to a test data generation method, apparatus, and related equipment. Background Technology
[0002] Software testing is a crucial part of software development and a vital step in ensuring software quality. Software testing requires building test data, but traditional manual test data creation involves testers designing test data, a tedious and time-consuming process. Among these, code coverage testing in white-box testing is the most cumbersome. This test requires testers to design test data that covers as many paths (branches) of the code as possible. However, because white-box code often contains numerous conditional statements and loops, resulting in a large number of branches, designing white-box test data is particularly challenging.
[0003] It should be noted that the information disclosed in the background section above is only used to enhance the understanding of the background of this disclosure, and therefore may include information that does not constitute related technology known to those skilled in the art. Summary of the Invention
[0004] This disclosure provides a test data generation method, apparatus, and related equipment, which at least to some extent overcomes the problem that manually designing test data is cumbersome and time-consuming in related technologies.
[0005] Other features and advantages of this disclosure will become apparent from the following detailed description, or may be learned in part from practice of this disclosure.
[0006] According to one aspect of this disclosure, a test data generation method is provided, comprising: acquiring code to be tested; preprocessing the code to be tested to obtain a vector sequence of the code to be tested; inputting the vector sequence of the code to be tested into a pre-trained test data generation model, and outputting test data corresponding to the code to be tested.
[0007] In some exemplary embodiments of this disclosure, based on the foregoing scheme, before inputting the vector sequence of the code to be tested into a pre-trained test data generation model and outputting the test data corresponding to the code to be tested, the method further includes: acquiring multiple sets of test codes and corresponding test data; preprocessing each set of test codes and corresponding test data to obtain a vector sequence of each set of test codes and a vector sequence of the test data corresponding to each set of test codes; training a pre-constructed Seq2Seq model based on the vector sequences of the multiple sets of test codes and the vector sequences of the corresponding test data to obtain a test data vector sequence generation model, wherein the test data vector sequence generation model is used to output the vector sequence corresponding to the test data.
[0008] In some exemplary embodiments of this disclosure, based on the foregoing scheme, each set of test code and the corresponding test data are preprocessed to obtain a vector sequence of each set of test code and a vector sequence of the test data corresponding to each set of test code. This includes: replacing blank lines and indentation characters in multiple sets of test code with spaces to obtain multiple sets of test code sequences; performing word segmentation on the multiple sets of test code sequences to obtain word sequences of multiple sets of test code; adding a first start identifier before the first word of the word sequence and adding a first end identifier after the last word of the word sequence; and performing vectorization on the word sequence to obtain a vector sequence of multiple sets of test code.
[0009] In some exemplary embodiments of this disclosure, based on the aforementioned scheme, each set of test code and the corresponding test data are preprocessed to obtain a vector sequence of each set of test code and a vector sequence of test data corresponding to each set of test code. This includes: dividing the test data corresponding to multiple sets of test code according to a preset rule to obtain multiple sets of test data sequences; adding a second start identifier before the first data in the first set of test data sequences; adding a separator identifier after the last data in each set of test data sequences; and adding a second end identifier after the last data in the last set of test data sequences to obtain a vector sequence of test data corresponding to each set of test code.
[0010] In some exemplary embodiments of this disclosure, based on the foregoing scheme, the test data generation model includes an encoding layer and a decoding layer. The vector sequence of the code to be tested is input into a pre-trained test data generation model, and test data corresponding to the code to be tested is output. This includes: encoding the vector sequence of the code to be tested through the encoding layer in the test data generation model to obtain an encoding result; decoding the encoding result through the decoding layer in the test data generation model to obtain a vector sequence of the test data; and transforming the vector sequence of the test data to obtain the test data corresponding to the code to be tested.
[0011] In some exemplary embodiments of this disclosure, based on the foregoing scheme, the code to be tested is preprocessed to obtain a vector sequence of the code to be tested, including: replacing blank lines and indentation characters in the code to be tested with spaces to obtain a sequence of code to be tested; performing word segmentation on the sequence of code to be tested to obtain a word sequence of the code to be tested; adding a third start identifier before the first word of the word sequence and adding a third end identifier after the last word of the word sequence; and performing vectorization on the word sequence to obtain a vector sequence of the code to be tested.
[0012] In some exemplary embodiments of this disclosure, the code to be tested is preprocessed based on the foregoing scheme, including: removing all comments in the code to be tested.
[0013] According to another aspect of this disclosure, a test data generation apparatus is also provided, comprising: a code acquisition module for acquiring code to be tested; a code preprocessing module for preprocessing the code to be tested to obtain a vector sequence of the code to be tested; and a test data generation module for inputting the vector sequence of the code to be tested into a pre-trained test data generation model and outputting test data corresponding to the code to be tested.
[0014] According to another aspect of this disclosure, an electronic device is also provided, comprising: a processor; and a memory for storing executable instructions of the processor; wherein the processor is configured to perform any of the above-described test data generation methods by executing the executable instructions.
[0015] According to another aspect of this disclosure, a computer-readable storage medium is also provided, on which a computer program is stored, which, when executed by a processor, implements any of the above-described test data generation methods.
[0016] The embodiments of this disclosure provide a test data generation method, apparatus, and related equipment. First, the code to be tested is acquired; then, the code to be tested is preprocessed to obtain a vector sequence of the code; finally, the vector sequence of the code to be tested is input into a pre-trained test data generation model to output the test data corresponding to the code. Compared to the tedious and time-consuming manual construction and design of test data in related technologies, the embodiments of this disclosure automatically generate the test data corresponding to the code by inputting the vector sequence of the code to be tested into a pre-trained test data generation model. This solves the problem of the tedious and time-consuming construction and design of test data in related technologies. Furthermore, in white-box testing, the pre-trained test data generation model can not only quickly obtain test data but also maximize code path coverage.
[0017] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and are not intended to limit this disclosure. Attached Figure Description
[0018] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this disclosure and, together with the description, serve to explain the principles of this disclosure. It is obvious that the drawings described below are merely some embodiments of this disclosure, and those skilled in the art can obtain other drawings based on these drawings without any inventive effort.
[0019] Figure 1This diagram illustrates the structure of a computer system according to an embodiment of the present disclosure;
[0020] Figure 2 This diagram illustrates a test data generation method according to an embodiment of the present disclosure.
[0021] Figure 3 This diagram illustrates a method for preprocessing code to be tested according to an embodiment of the present disclosure.
[0022] Figure 4 This diagram illustrates a Seq2Seq model structure according to an embodiment of the present disclosure.
[0023] Figure 5 This diagram illustrates a test data construction flowchart according to an embodiment of the present disclosure;
[0024] Figure 6 This diagram illustrates a test data generation apparatus according to an embodiment of the present disclosure.
[0025] Figure 7 A schematic diagram of an electronic device for applying a test data generation method according to an embodiment of this disclosure is shown. Detailed Implementation
[0026] Exemplary embodiments will now be described more fully with reference to the accompanying drawings. However, these exemplary embodiments can be implemented in many forms and should not be construed as limited to the examples set forth herein; rather, they are provided so that this disclosure will be more comprehensive and complete, and will fully convey the concept of the exemplary embodiments to those skilled in the art. The described features, structures, or characteristics may be combined in any suitable manner in one or more embodiments.
[0027] Furthermore, the described features, structures, or characteristics can be combined in any suitable manner in one or more embodiments. Numerous specific details are provided in the following description to give a thorough understanding of embodiments of this disclosure. However, those skilled in the art will recognize that the technical solutions of this disclosure can be practiced without one or more of the specific details, or other methods, components, apparatuses, steps, etc., can be employed. In other instances, well-known methods, apparatuses, implementations, or operations are not shown or described in detail to avoid obscuring various aspects of this disclosure.
[0028] The flowcharts shown in the accompanying drawings are merely illustrative and do not necessarily include all content and operations / steps, nor do they necessarily have to be performed in the described order. For example, some operations / steps can be broken down, while others can be combined or partially combined; therefore, the actual execution order may change depending on the specific circumstances.
[0029] Figure 1A schematic diagram of an exemplary application system architecture to which the test data generation method of the embodiments of this disclosure can be applied is shown. Figure 1 As shown, the system architecture may include terminal device 101, network 102 and server 103.
[0030] Network 102 is a medium used to provide a communication link between terminal device 101 and server 103, and can be a wired network or a wireless network.
[0031] Optionally, the aforementioned wireless or wired networks use standard communication technologies and / or protocols. The network is typically the Internet, but can also be any network, including but not limited to Local Area Networks (LANs), Metropolitan Area Networks (MANs), Wide Area Networks (WANs), mobile, wired or wireless networks, private networks, or any combination of virtual private networks. In some embodiments, technologies and / or formats including Hyper Text Markup Language (HTML), Extensible Markup Language (XML), etc., are used to represent data exchanged over the network. Furthermore, conventional encryption technologies such as Secure Socket Layer (SSL), Transport Layer Security (TLS), Virtual Private Networks (VPNs), and Internet Protocol Security (IPsec) can be used to encrypt all or some links. In other embodiments, custom and / or dedicated data communication technologies can be used to replace or supplement the aforementioned data communication technologies.
[0032] Terminal device 101 can be various electronic devices, including but not limited to smartphones, tablets, laptops, desktop computers, wearable devices, augmented reality devices, virtual reality devices, etc.
[0033] Optionally, the client of the application installed on different terminal devices 101 may be the same, or the client of the same type of application based on different operating systems. Depending on the terminal platform, the specific form of the application client may also be different; for example, the application client may be a mobile client, a PC client, etc.
[0034] Server 103 can be a server that generates test data, such as a backend management server that provides test data for devices operated by users using terminal device 101. The backend management server can analyze and process received requests and other data, and feed back the processing results, i.e., the test data, to the terminal device.
[0035] Optionally, the server can be a standalone physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN (Content Delivery Network), and big data and artificial intelligence platforms. The terminal can be a smartphone, tablet, laptop, desktop computer, smart speaker, smartwatch, etc., but is not limited to these. The terminal and server can be directly or indirectly connected via wired or wireless communication, and this disclosure does not impose any restrictions.
[0036] Those skilled in the art will know that Figure 1 The number of terminal devices, networks, and servers shown is merely illustrative; any number of terminal devices, networks, and servers can be included depending on actual needs. This disclosure does not limit the scope of the embodiments.
[0037] Under the above system architecture, this disclosure provides a test data generation method, which can be executed by any electronic device with computing capabilities.
[0038] In some embodiments, the test data generation method provided in this disclosure can be executed by a terminal device of the system architecture described above; in other embodiments, the test data generation method provided in this disclosure can be executed by a server in the system architecture described above; in still other embodiments, the test data generation method provided in this disclosure can be implemented by the terminal device and the server in the system architecture described above through interaction.
[0039] The following will describe in more detail each step of the test data generation method in this exemplary embodiment, with reference to the accompanying drawings and embodiments.
[0040] First, this disclosure provides a method for generating test data during software testing, which can be applied but is not limited to. Software testing is a crucial step in ensuring software quality; therefore, the design and construction of test data is particularly important. In related technologies, the design and construction of test data is generally done manually, especially since the code in white-box testing contains a large number of conditional statements and loop logic, as well as numerous branches. This requires testers to be not only very familiar with the code but also to be able to quickly construct test data. Therefore, this disclosure provides a method that quickly generates test data for the code under test by inputting the vector sequence of the code to be tested into a pre-trained test data generation model. This overcomes, to some extent, the problem of the tedious and time-consuming manual design of test data in related technologies.
[0041] Figure 2 This diagram illustrates a test data generation method according to an embodiment of the present disclosure, such as... Figure 2 As shown in the embodiments of this disclosure, the test data generation method includes the following steps:
[0042] S201, Get the code to be tested.
[0043] It should be noted that the code to be tested in the embodiments of this disclosure can be any kind of code to be tested. In some embodiments, it can be software test code. More specifically, the code to be tested in the embodiments of this disclosure can be code in white-box testing. The box in white-box testing refers to the software being tested. White-box means that the box is visible, that is, the contents of the box and how it works are clearly understood. In other words, the code to be tested in the embodiments of this disclosure can be code inside the software being tested that is clearly known to others. Here, the code includes multiple strings. Therefore, the code to be tested in the embodiments of this disclosure can be multiple strings inside the software being tested that are clearly known to others.
[0044] S202, preprocess the code to be tested to obtain a vector sequence of the code to be tested.
[0045] In some embodiments, the preprocessing of the acquired code to be tested in this disclosure may involve vectorizing the strings in the acquired code to be tested, so that the strings in the code to be tested are converted into corresponding vector data, thereby obtaining vector data corresponding to each string in the code to be tested. Each item in the vector sequence in this disclosure is the vector data corresponding to each string in the code to be tested.
[0046] S203: Input the vector sequence of the code to be tested into the pre-trained test data generation model, and output the test data corresponding to the code to be tested.
[0047] It should be noted that the test data generation model in this embodiment can be a model pre-trained using machine learning on various artificial intelligence algorithm models (e.g., neural network models) or combinations thereof. The test data generation model can automatically analyze and match the corresponding test data based on the vector sequence of the code to be tested. The input data of the test data generation model in this embodiment is the vector data of the code to be tested, and the output data is the test data associated with the code to be tested. In some embodiments, the test data in this embodiment is the input data provided to the software program during test execution. The test data can represent data that affects or is affected by the software execution during the test. The test data can be used for positive testing to verify that the function produces the expected results under a given input, or for negative testing to test the software's ability to handle abnormal or unexpected inputs.
[0048] This disclosure provides a test data generation method. First, the code to be tested is obtained; then, the code to be tested is preprocessed to obtain a vector sequence of the code; finally, the vector sequence of the code to be tested is input into a pre-trained test data generation model to output the test data corresponding to the code. Compared with the tedious and time-consuming manual construction and design of test data in related technologies, this disclosure can automatically generate the test data corresponding to the code by inputting the vector sequence of the code to be tested into a pre-trained test data generation model, thus solving the problem of the tedious and time-consuming construction and design of test data in related technologies.
[0049] In some embodiments, before inputting the vector sequence of the code to be tested into a pre-trained test data generation model and outputting the test data corresponding to the code to be tested, the method further includes: acquiring multiple sets of test codes and corresponding test data; preprocessing each set of test codes and corresponding test data to obtain a vector sequence of each set of test codes and a vector sequence of the test data corresponding to each set of test codes; training a pre-constructed sequence-to-sequence (Seq2Seq) model based on the vector sequences of multiple sets of test codes and the vector sequences of the corresponding test data to obtain a test data vector sequence generation model, which is used to output the vector sequence corresponding to the test data.
[0050] Specifically, the Seq2Seq model in this embodiment can automatically learn features from training data, that is, learn the corresponding test data feature information from multiple sets of test codes, which can greatly reduce the training workload. In addition, the Seq2Seq model can also simulate human language expression patterns, thereby enabling the interaction between machines and humans.
[0051] In some embodiments, this disclosure first constructs a Seq2Seq model. Seq2Seq is an important series of models in Natural Language Processing (NLP). When using the Seq2Seq model, an input vector sequence is required. Therefore, this disclosure collects multiple sets of previous test codes and corresponding test data, and preprocesses the multiple sets of test codes and corresponding test data to obtain a vector sequence for each set of test codes and a vector sequence for the corresponding test data. Then, the preprocessed vector sequences of multiple sets of test codes and the vector sequences of the corresponding test data are used to train the Seq2Seq model, so that a more accurate vector sequence of test code corresponding to test data can be obtained subsequently.
[0052] In some embodiments, each set of test code and the corresponding test data are preprocessed to obtain a vector sequence of each set of test code and a vector sequence of the test data corresponding to each set of test code. This includes: replacing blank lines and indentation characters in multiple sets of test code with spaces to obtain multiple sets of test code sequences; performing word segmentation on the multiple sets of test code sequences to obtain word sequences of multiple sets of test code; adding a first start identifier before the first word of the word sequence and adding a first end identifier after the last word of the word sequence; and vectorizing the word sequences to obtain a vector sequence of multiple sets of test code.
[0053] It should be noted that the first start identifier in this embodiment is an identifier used to mark the beginning of a sequence, and the first end identifier is an identifier used to mark the end of a sequence. The content of each line of code in the test code sequence can be clearly identified through the first start identifier and the first end identifier, which further facilitates the subsequent vectorization processing of each line of code to obtain multiple sets of test code vector sequences.
[0054] In some embodiments, preprocessing the code to be tested further includes: removing all comments from the code to be tested, such as... Figure 3 As shown, the comments in the obtained test code are removed. Specifically, in this embodiment, double forward slashes " / / " are added before each comment to remove the comments in the code. Next, blank lines and indentation characters in the test code are replaced with spaces. Then, the string in the code is segmented to obtain the word sequence of the test code. Next, a first start identifier and a first end identifier are added to the beginning and end of the word sequence of the test code. The first start identifier can be, but is not limited to, other than, the following: <sos>Logo, <sos>The identifier represents the start of a sequence, and the first end identifier can be, but is not limited to, the following: <eos>Logo, <eos>The identifier represents the end of a sequence; finally, the word sequence is transformed into a vector sequence using embedding techniques.
[0055] In some embodiments, each set of test code and the corresponding test data are preprocessed to obtain a vector sequence of each set of test code and a vector sequence of the test data corresponding to each set of test code. This includes: dividing the test data corresponding to multiple sets of test code according to a preset rule to obtain multiple sets of test data sequences; adding a second start identifier before the first data in the first set of test data sequences; adding a separator identifier after the last data in each set of test data sequences; and adding a second end identifier after the last data in the last set of test data sequences to obtain a vector sequence of the test data corresponding to each set of test code.
[0056] In some embodiments, the preprocessing of the test data corresponding to the test code in this disclosure involves mapping the test data to a vector space. Considering the diversity of string data and its potential for readability, this disclosure divides the string test data corresponding to each line of code into a sequence according to the character set. Then, a separator is added between different test data (strings). The separator in this disclosure can be, but is not limited to, other types of separators. <ios>The identifier, and the second start identifier and the second end identifier, wherein the second start identifier and the second end identifier in the embodiments of this disclosure may be, but are not limited to, identifiers. <sos>logo and <eos>Of course, those skilled in the art can flexibly set the second start identifier, separator identifier, and second end identifier in the embodiments of this disclosure according to the actual situation.
[0057] Specifically, in this embodiment of the disclosure, it is assumed that the test data corresponding to each line of the code to be tested are as follows:
[0058] {"2022 / 01 / 11"}, {"2022 / 02 / 30"}, {"2022 / 13 / 21"}, {"2022 / 13 / 244"}
[0059] After the preprocessing described in this embodiment, the following vector sequence of test data is obtained:
[0060] [ <sos>
[0061] "2","0","2","2"," / ","0","1"," / ","1","1", <ios>
[0062] "2","0","2","2"," / ","0","2"," / ","3","0", <ios>
[0063] "2","0","2","2"," / ","1","3"," / ","2","1", <ios>
[0064] "2","0","2","2","2"," / ","1","3","3"," / ","2","4","4", <eos>].
[0065] In some embodiments, the test data generation model includes an encoding layer and a decoding layer. The model inputs a vector sequence of the code to be tested into a pre-trained test data generation model and outputs test data corresponding to the code to be tested. This includes: encoding the vector sequence of the code to be tested through the encoding layer in the test data generation model to obtain an encoding result; decoding the encoding result through the decoding layer in the test data generation model to obtain a vector sequence of test data; and transforming the vector sequence of test data to obtain the test data corresponding to the code to be tested.
[0066] In some embodiments, this disclosure uses the Transformer model (a neural network consisting of an encoder layer and a decoder layer) in the Seq2Seq model. An example of the structure of this Transformer model is as follows: Figure 4 As shown, the left side is the encoding layer. The neural network of the encoding layer encodes the vector sequence of the code to be tested, and the encoding result is obtained. In this embodiment, the encoding result is the vector sequence of the code to be tested after code preprocessing. Useful information in the vector sequence of the code to be tested is extracted through this encoding. In the decoding layer, the encoding result of the encoding layer is decoded by the neural network to obtain the decoding result. In this embodiment, the decoding result is the vector sequence of test data. Finally, the vector sequence of test data is transformed to obtain the test data corresponding to the code to be tested.
[0067] In some embodiments, the code to be tested is preprocessed to obtain a vector sequence of the code to be tested, including: replacing blank lines and indentation characters in the code to be tested with spaces to obtain a sequence of code to be tested; performing word segmentation on the sequence of code to be tested to obtain a word sequence of the code to be tested; adding a third start identifier before the first word of the word sequence and adding a third end identifier after the last word of the word sequence; and vectorizing the word sequence to obtain a vector sequence of the code to be tested.
[0068] In some embodiments of this disclosure, the preprocessing of the code to be tested specifically includes: first, adding double forward slashes " / / " before each code comment to remove the comments in the code; second, replacing blank lines and indentation characters in the test code with spaces; then, performing word segmentation on the strings in the code to obtain the word sequence of the test code; next, adding a third start identifier and a third end identifier to the beginning and end of the word sequence of the test code, wherein the third start identifier may be, but is not limited to, a third start identifier. <sos>The third end marker can be, but is not limited to, the following: <eos>Finally, the word sequence is converted into a vector sequence using Embedding technology. Of course, those skilled in the art can flexibly set the third start identifier and the third end identifier in this embodiment according to the actual situation. The above-mentioned third start identifier and third end identifier are not limited in this embodiment.
[0069] More in detail, such as Figure 5 As shown, the method for generating test data in this embodiment of the disclosure specifically includes the following steps:
[0070] S501, multiple sets of test code and corresponding test data.
[0071] S502 preprocesses multiple sets of test code and corresponding test data to obtain vector sequences of multiple sets of test code and vector sequences of corresponding test data.
[0072] S503, construct a Seq2Seq model.
[0073] S504 trains a Seq2Seq model based on the vector sequences of multiple sets of test code and the corresponding vector sequences of test data.
[0074] S505, retrieve the code to be tested.
[0075] S506 preprocesses the code to be tested.
[0076] S507 uses a pre-trained Seq2Seq model to process the preprocessing results of the code to be tested, and obtains a vector sequence of the data to be tested.
[0077] S508 processes the vector sequence of the test data to obtain the test data corresponding to the code to be tested.
[0078] Based on the same inventive concept, this disclosure also provides a test data generation device, as shown in the following embodiment. Since the principle by which this device embodiment solves the problem is similar to that of the above-described method embodiment, the implementation of this device embodiment can refer to the implementation of the above-described method embodiment, and repeated details will not be described again.
[0079] Figure 6 This diagram illustrates a test data generation apparatus according to an embodiment of the present disclosure, such as... Figure 6 As shown, the device includes:
[0080] Code acquisition module 601 is used to acquire the code to be tested.
[0081] The code preprocessing module 602 is used to preprocess the code to be tested to obtain a vector sequence of the code to be tested;
[0082] The test data generation module 603 is used to input the vector sequence of the code to be tested into the pre-trained test data generation model and output the test data corresponding to the code to be tested.
[0083] The test data generation device provided in this embodiment acquires the code to be tested through a code acquisition module; preprocesses the code to be tested through a code preprocessing module to obtain a vector sequence of the code to be tested; and inputs the vector sequence of the code to be tested into a pre-trained test data generation model through a test data generation module to output the test data corresponding to the code to be tested. Compared with the tedious and time-consuming process of manually constructing and designing test data in related technologies, this embodiment can automatically generate the test data corresponding to the code to be tested by inputting the vector sequence of the code to be tested into a pre-trained test data generation model. This solves the problem of the tedious and time-consuming process of constructing and designing test data in related technologies. Furthermore, in white-box testing, the pre-trained test data generation model can not only quickly obtain test data but also maximize code path coverage.
[0084] In some embodiments, before inputting the vector sequence of the code to be tested into a pre-trained test data generation model and outputting the test data corresponding to the code to be tested, the test data generation device in this embodiment further includes: a test data acquisition module, used to acquire multiple sets of test codes and corresponding test data; a test data processing module, used to preprocess each set of test codes and corresponding test data to obtain a vector sequence of each set of test codes and a vector sequence of the test data corresponding to each set of test codes; and a Seq2Seq model training module, used to train a pre-constructed Seq2Seq model based on the vector sequences of multiple sets of test codes and the vector sequences of the corresponding test data to obtain a test data vector sequence generation model, which is used to output the vector sequence corresponding to the test data.
[0085] In some embodiments, this disclosure first constructs a Seq2Seq model, where Seq2Seq is an important series of models in NLP. When using the Seq2Seq model, an input vector sequence is required. Therefore, this disclosure collects multiple sets of previous test code and corresponding test data, and preprocesses the multiple sets of test code and corresponding test data to obtain a vector sequence for each set of test code and a vector sequence for the corresponding test data for each set of test code. Then, the preprocessed vector sequences of multiple sets of test code and the vector sequences of the corresponding test data are used to train the Seq2Seq model, so that a more accurate vector sequence of test code corresponding to test data can be obtained subsequently.
[0086] In some embodiments, the test data processing module in this disclosure is further configured to replace blank lines and indentation characters in multiple sets of test code with spaces to obtain multiple sets of test code sequences; perform word segmentation on the multiple sets of test code sequences to obtain word sequences of multiple sets of test code; add a first start identifier before the first word of the word sequence and add a first end identifier after the last word of the word sequence; and perform vectorization on the word sequence to obtain a vector sequence of multiple sets of test code.
[0087] More specifically, the test data processing module in this embodiment of the present disclosure preprocesses the code to be tested, further including: removing all comments from the code to be tested, such as... Figure 3 As shown, the comments in the obtained test code are removed. Specifically, in this embodiment, double forward slashes " / / " are added before each comment to remove the comments in the code. Next, blank lines and indentation characters in the test code are replaced with spaces. Then, the string in the code is segmented to obtain the word sequence of the test code. Finally, a first start identifier and a first end identifier are added to the beginning and end of the word sequence of the test code, wherein the first start identifier is... <sos>Logo, <sos>The identifier represents the beginning of a sequence, and the first end identifier is [identified]. <eos>Logo, <eos>The identifier represents the end of a sequence; finally, the word sequence is transformed into a vector sequence using embedding techniques.
[0088] In some embodiments, the test data processing module in this disclosure is further configured to divide the test data corresponding to multiple sets of test codes according to a preset rule to obtain multiple sets of test data sequences; add a second start identifier before the first data of the first set of test data sequences, add a separator identifier after the last data of each set of test data sequences, and add a second end identifier after the last data of the last set of test data sequences to obtain a vector sequence of test data corresponding to each set of test codes.
[0089] In some embodiments of this disclosure, the test data processing module preprocesses the test data corresponding to the test code by mapping the test data to a vector space. Considering the diversity of string data and its potential for limited readability, this disclosure embodiment divides the string test data corresponding to each line of code into a sequence according to the character set, and then adds separators between different test data (strings). <ios>The identifier, and the second start identifier and the second end identifier, wherein the second start identifier and the second end identifier in the embodiments of this disclosure may be, but are not limited to, identifiers. <sos>logo and <eos>Of course, those skilled in the art can flexibly set the second start identifier, separator identifier, and second end identifier in the embodiments of this disclosure according to the actual situation.
[0090] Specifically, in this embodiment of the disclosure, it is assumed that the test data corresponding to each line of the code to be tested are as follows:
[0091] {"2022 / 01 / 11"}, {"2022 / 02 / 30"}, {"2022 / 13 / 21"}, {"2022 / 13 / 244"}
[0092] After the preprocessing described in this embodiment, the following vector sequence of test data is obtained:
[0093] [ <sos>
[0094] "2","0","2","2"," / ","0","1"," / ","1","1", <ios>
[0095] "2","0","2","2"," / ","0","2"," / ","3","0", <ios>
[0096] "2","0","2","2"," / ","1","3"," / ","2","1", <ios>
[0097] "2","0","2","2","2"," / ","1","3","3"," / ","2","4","4", <eos>].
[0098] In some embodiments, the test data generation model in this disclosure includes an encoding layer and a decoding layer. The test data generation module is further configured to encode the vector sequence of the code to be tested through the encoding layer in the test data generation model to obtain an encoding result; decode the encoding result through the decoding layer in the test data generation model to obtain a vector sequence of test data; and transform the vector sequence of test data to obtain test data corresponding to the code to be tested.
[0099] More specifically, embodiments of this disclosure use the Transformer model in the Seq2Seq model, an example of the structure of which is shown below. Figure 4 As shown, the left side is the encoding layer. The neural network of the encoding layer encodes the vector sequence of the code to be tested, and the encoding result is obtained. In this embodiment, the encoding result is the vector sequence of the code to be tested after code preprocessing. Useful information in the vector sequence of the code to be tested is extracted through this encoding. In the decoding layer, the encoding result of the encoding layer is decoded by the neural network to obtain the decoding result. In this embodiment, the decoding result is the vector sequence of test data. Finally, the vector sequence of test data is transformed to obtain the test data corresponding to the code to be tested.
[0100] In some embodiments of this disclosure, the code preprocessing module is further configured to replace blank lines and indentation characters in the code to be tested with spaces to obtain a sequence of code to be tested; perform word segmentation on the sequence of code to be tested to obtain a word sequence of code to be tested; add a third start identifier before the first word of the word sequence and add a third end identifier after the last word of the word sequence; and perform vectorization on the word sequence to obtain a vector sequence of code to be tested.
[0101] In some embodiments of this disclosure, the code preprocessing module preprocesses the code to be tested, specifically including: adding double forward slashes " / / " before each code comment to remove comments from the code; replacing blank lines and indentation characters in the test code with spaces; performing word segmentation on the strings in the code to obtain a word sequence of the test code; adding a third start identifier and a third end identifier to the beginning and end of the word sequence of the test code, wherein the third start identifier can be... <sos>The third end marker can be... <eos>The word sequence is transformed into a vector sequence using Embedding technology. Of course, those skilled in the art can flexibly set the third start identifier and the third end identifier in this embodiment according to the actual situation. The third start identifier and the third end identifier are not limited in this embodiment.
[0102] In some embodiments of this disclosure, the code preprocessing module is further configured to remove all comments from the code to be tested.
[0103] Those skilled in the art will understand that various aspects of this disclosure can be implemented as a system, method, or program product. Therefore, various aspects of this disclosure can be specifically implemented in the following forms: a completely hardware implementation, a completely software implementation (including firmware, microcode, etc.), or a combination of hardware and software aspects, collectively referred to herein as a "circuit," "module," or "system."
[0104] The following reference Figure 7 To describe an electronic device 700 according to such an embodiment of the present disclosure. Figure 7 The electronic device 700 shown is merely an example and should not impose any limitation on the functionality and scope of use of the embodiments disclosed herein.
[0105] like Figure 7 As shown, the electronic device 700 is manifested in the form of a general-purpose computing device. The components of the electronic device 700 may include, but are not limited to: at least one processing unit 701, at least one storage unit 702, and a bus 703 connecting different system components (including storage unit 702 and processing unit 701).
[0106] The storage unit stores program code, which can be executed by the processing unit 701, causing the processing unit 701 to perform the steps described in the "Exemplary Methods" section above according to various exemplary embodiments of this disclosure.
[0107] In some embodiments, when an electronic device is used to control, for example, the knowledge graph-based question-answering method described above, the processing unit 701 may execute the following steps of the method embodiments described above:
[0108] Obtain the code to be tested.
[0109] The code to be tested is preprocessed to obtain a vector sequence of the code to be tested.
[0110] The vector sequence of the code to be tested is input into a pre-trained test data generation model, which outputs the test data corresponding to the code to be tested.
[0111] Storage unit 702 may include a readable medium in the form of a volatile storage unit, such as a random access memory unit (RAM) 7021 and / or a cache memory unit 7022, and may further include a read-only memory unit (ROM) 7023.
[0112] Storage unit 702 may also include a program / utility 7024 having a set (at least one) program module 7025, such program module 7025 including but not limited to: operating system, one or more application programs, other program modules and program data, each or some combination of these examples may include an implementation of a network environment.
[0113] Bus 703 can represent one or more of several types of bus structures, including a memory cell bus or memory cell controller, a peripheral bus, a graphics acceleration port, a processing unit, or a local bus using any of the various bus structures.
[0114] Electronic device 700 can also communicate with one or more external devices 704 (e.g., keyboard, pointing device, Bluetooth device, etc.), and with one or more devices that enable a user to interact with electronic device 700, and / or with any device that enables electronic device 700 to communicate with one or more other computing devices (e.g., router, modem, etc.). This communication can be performed via input / output (I / O) interface 705. Furthermore, electronic device 700 can also communicate with one or more networks (e.g., local area network (LAN), wide area network (WAN), and / or public networks, such as the Internet) via network adapter 706. As shown, network adapter 706 communicates with other modules of electronic device 700 via bus 703. It should be understood that, although not shown in the figures, other hardware and / or software modules can be used in conjunction with electronic device 700, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.
[0115] From the above description of the embodiments, those skilled in the art will readily understand that the exemplary embodiments described herein can be implemented by software or by combining software with necessary hardware. Therefore, the technical solutions according to the embodiments of this disclosure can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (such as a CD-ROM, USB flash drive, external hard drive, etc.) or on a network, including several instructions to cause a computing device (such as a personal computer, server, terminal device, or network device, etc.) to execute the methods according to the embodiments of this disclosure.
[0116] In particular, according to embodiments of this disclosure, the process described above with reference to the flowchart can be implemented as a computer program product, which includes a computer program that, when executed by a processor, implements the above-described test data generation method.
[0117] In exemplary embodiments of this disclosure, a computer-readable storage medium is also provided, which may be a readable signal medium or a readable storage medium. A program product capable of implementing the methods described above is stored thereon. In some possible implementations, various aspects of this disclosure may also be implemented as a program product including program code, which, when run on a terminal device, causes the terminal device to perform the steps described in the "Exemplary Methods" section of this specification according to various exemplary embodiments of this disclosure.
[0118] More specific examples of computer-readable storage media in this disclosure may include, but are not limited to: electrical connections having one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.
[0119] In this disclosure, a computer-readable storage medium may include a data signal propagated in baseband or as part of a carrier wave, carrying readable program code. Such propagated data signals may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A readable signal medium may also be any readable medium other than a readable storage medium, capable of transmitting, propagating, or transmitting a program for use by or in connection with an instruction execution system, apparatus, or device.
[0120] Optionally, the program code contained on the computer-readable storage medium may be transmitted using any suitable medium, including but not limited to wireless, wired, optical fiber, RF, etc., or any suitable combination thereof.
[0121] In practical implementation, program code for performing the operations of this disclosure can be written in any combination of one or more programming languages, including object-oriented programming languages such as Java and C++, and conventional procedural programming languages such as C or similar languages. The program code can execute entirely on the user's computing device, partially on the user's computing device, as a standalone software package, partially on the user's computing device and partially on a remote computing device, or entirely on a remote computing device or server. In cases involving remote computing devices, the remote computing device can be connected to the user's computing device via any type of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computing device (e.g., via the Internet using an Internet service provider).
[0122] It should be noted that although several modules or units for the device used to perform actions have been mentioned in the detailed description above, this division is not mandatory. In fact, according to embodiments of this disclosure, the features and functions of two or more modules or units described above can be embodied in one module or unit. Conversely, the features and functions of one module or unit described above can be further divided and embodied by multiple modules or units.
[0123] Furthermore, although the steps of the method in this disclosure are described in a specific order in the accompanying drawings, this does not require or imply that the steps must be performed in that specific order, or that all the steps shown must be performed to achieve the desired result. Additional or alternative steps may be omitted, multiple steps may be combined into one step, and / or a step may be broken down into multiple steps.
[0124] From the above description of the embodiments, those skilled in the art will readily understand that the exemplary embodiments described herein can be implemented by software or by combining software with necessary hardware. Therefore, the technical solutions according to the embodiments of this disclosure can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (such as a CD-ROM, USB flash drive, external hard drive, etc.) or on a network, including several instructions to cause a computing device (such as a personal computer, server, mobile terminal, or network device, etc.) to execute the methods according to the embodiments of this disclosure.
[0125] Other embodiments of this disclosure will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This disclosure is intended to cover any variations, uses, or adaptations of this disclosure that follow the general principles of this disclosure and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this disclosure are indicated by the appended claims.< / eos> < / sos> < / eos> < / ios> < / ios> < / ios> < / sos> < / eos> < / sos> < / ios> < / eos> < / eos> < / sos> < / sos> < / eos> < / sos> < / eos> < / ios> < / ios> < / ios> < / sos> < / eos> < / sos> < / ios> < / eos> < / eos> < / sos> < / sos>
Claims
1. A method for generating test data, characterized in that, include: Obtain the code to be tested; The code to be tested is preprocessed to obtain a vector sequence of the code to be tested; The vector sequence of the code to be tested is input into a pre-trained test data generation model, which outputs the test data corresponding to the code to be tested. The method further includes, before inputting the vector sequence of the code to be tested into a pre-trained test data generation model and outputting the test data corresponding to the code to be tested, the following steps before inputting the vector sequence of the code to be tested into a pre-trained test data generation model: acquiring multiple sets of test code and corresponding test data; preprocessing each set of test code and corresponding test data to obtain a vector sequence of each set of test code and a vector sequence of the test data corresponding to each set of test code; training a pre-constructed Seq2Seq model based on the vector sequences of the multiple sets of test code and the vector sequences of the corresponding test data to obtain a test data vector sequence generation model, which is used to output the vector sequence corresponding to the test data; wherein, preprocessing each set of test code and corresponding test data to obtain a vector sequence of each set of test code and a vector sequence of the test data corresponding to each set of test code includes: replacing blank lines and indentation characters in the multiple sets of test code with spaces. Multiple test code sequences are obtained; the multiple test code sequences are segmented to obtain multiple test code word sequences; a first start identifier is added before the first word of the word sequence, and a first end identifier is added after the last word of the word sequence; the word sequences are vectorized to obtain multiple test code vector sequences; the test data generation model includes an encoding layer and a decoding layer, and the vector sequence of the code to be tested is input into the pre-trained test data generation model to output the test data corresponding to the code to be tested, including: encoding the vector sequence of the code to be tested through the encoding layer in the test data generation model to obtain an encoding result; decoding the encoding result through the decoding layer in the test data generation model to obtain the vector sequence of the test data; and transforming the vector sequence of the test data to obtain the test data corresponding to the code to be tested.
2. The test data generation method according to claim 1, characterized in that, Each set of test code and its corresponding test data is preprocessed to obtain a vector sequence for each set of test code and a vector sequence for the corresponding test data, including: The test data corresponding to multiple sets of test code is divided according to preset rules to obtain multiple test data sequences; Add a second start marker before the first data in the first test data sequence, add a separator marker after the last data in each test data sequence, and add a second end marker after the last data in the last test data sequence to obtain a vector sequence of test data corresponding to each test code.
3. The test data generation method according to claim 1, characterized in that, The code to be tested is preprocessed to obtain a vector sequence of the code to be tested, including: Replace blank lines and indentation characters in the code to be tested with spaces to obtain the sequence of code to be tested; The code sequence to be tested is segmented into words to obtain the word sequence of the code to be tested; Add a third start marker before the first word of the word sequence and add a third end marker after the last word of the word sequence; The word sequence is vectorized to obtain the vector sequence of the code to be tested.
4. The test data generation method according to claim 1, characterized in that, The code to be tested is preprocessed, including removing all comments from the code to be tested.
5. A test data generation device, characterized in that, include: The code retrieval module is used to retrieve the code to be tested. The code preprocessing module is used to preprocess the code to be tested to obtain a vector sequence of the code to be tested; The test data generation module is used to input the vector sequence of the code to be tested into a pre-trained test data generation model and output the test data corresponding to the code to be tested. The test data acquisition module is used to acquire multiple sets of test code and corresponding test data; The test data processing module is used to preprocess each set of test code and the corresponding test data to obtain a vector sequence of each set of test code and a vector sequence of the test data corresponding to each set of test code. The Seq2Seq model training module is used to train a pre-built Seq2Seq model based on the vector sequences of multiple sets of test code and the corresponding vector sequences of test data, so as to obtain a test data vector sequence generation model. The test data vector sequence generation model is used to output the vector sequence corresponding to the test data. The test data processing module is further configured to: replace blank lines and indentation characters in multiple sets of test code with spaces to obtain multiple sets of test code sequences; perform word segmentation on the multiple sets of test code sequences to obtain word sequences of multiple sets of test code; add a first start identifier before the first word of the word sequence and add a first end identifier after the last word of the word sequence; and perform vectorization on the word sequence to obtain vector sequences of multiple sets of test code. The test data generation module is further configured to: encode the vector sequence of the code to be tested through the encoding layer in the test data generation model to obtain an encoding result; decode the encoding result through the decoding layer in the test data generation model to obtain a vector sequence of the test data; and transform the vector sequence of the test data to obtain the test data corresponding to the code to be tested.
6. An electronic device, characterized in that, include: processor; as well as Memory for storing the executable instructions of the processor; The processor is configured to execute the test data generation method of any one of claims 1 to 4 by executing the executable instructions.
7. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the test data generation method according to any one of claims 1 to 4.