Data testing method and device, storage medium and computer program product
The natural language processing model analyzes and generates unit test code and executes it, solving the problems of low efficiency and poor coverage of traditional unit test methods, realizing automated testing and rapid error positioning, and improving code quality and stability.
Patent Information
- Application Number
- CN202411260021.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-09
- Publication Date
- 2024-11-12
AI Technical Summary
Traditional unit testing methods are inefficient and have poor coverage, and cannot accurately determine code errors, resulting in inefficient testing.
By obtaining the initial code file and target prompt statements, the natural language processing model is used to analyze and process the generated unit test code, and compile and execute it, determining the code error location and type, and automatically generating and executing unit tests.
Improves the efficiency and coverage of unit testing, can automatically generate unit testing code, quickly locate and fix code errors, and improves code quality and stability.
Smart Images

Figure CN118939559A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a data testing method, device, storage medium and computer program product. Background Art
[0002] In the process of software development, unit testing is an important quality assurance method that can effectively detect the quality and correctness of the code. However, traditional unit testing work usually requires developers to manually write a large amount of unit test code, which is not only time-consuming and labor-intensive, but also may result in low test coverage due to human factors. In the related art, unit test code is mainly automatically generated through testing tools and frameworks. Although the problem of manually writing test code is solved to a certain extent, developers are usually required to participate manually during the testing process, such as selecting methods and objects to be tested, setting test parameters, etc., thereby reducing the efficiency of automation. When dealing with complex logic and polymorphic situations, the test methods in the related art may have problems such as inaccurate generated test code or low coverage. In addition, the test methods in the related art also have certain limitations in discovering code defects, that is, they cannot accurately locate the specific error location, resulting in low test efficiency.
[0003] To address the above-mentioned problems, no effective solution has been proposed yet. Summary of the invention
[0004] The embodiments of the present application provide a data testing method, device, storage medium and computer program product to at least solve the technical problems of low testing efficiency and poor testing coverage in the unit testing process in the related art.
[0005] According to one aspect of the embodiments of the present application, a data testing method is provided, including: obtaining an initial code file and a target prompt statement, wherein the target prompt statement is used to prompt the test type information and dependency content corresponding to the unit test code, and the unit test code is used to verify whether multiple preset program units in the initial code file meet the expected functional settings; using a natural language processing model to analyze and process the initial code file and the target prompt statement to generate unit test code; compiling the unit test code to obtain a compilation result, wherein the compilation result is used to determine whether the unit test code passes the compilation; in response to determining that the unit test code passes the compilation based on the compilation result, executing the unit test code to obtain a test execution result, wherein the test execution result is used to determine the code error location and code error type in the initial code file.
[0006] Optionally, obtaining the target prompt statement includes: using the program structure interface to analyze and process the initial code file to obtain target test data, wherein the target test data includes: code data to be tested and associated code data; filling in a preset prompt template based on the target test data to obtain the target prompt statement.
[0007] Optionally, the initial code file is analyzed and processed using the program structure interface to obtain target test data, including: parsing the initial code file using the program structure interface to obtain syntax tree structure information corresponding to the initial code file, wherein the syntax tree structure information is used to determine the syntax structure and calling relationship of the initial code file; and obtaining target test data based on the syntax tree structure information.
[0008] Optionally, filling in a preset prompt template based on target test data to obtain a target prompt statement includes: determining test type information and dependency content corresponding to the unit test code based on the target test data; and filling in a preset prompt template using the test type information and dependency content to obtain a target prompt statement.
[0009] Optionally, the data testing method also includes: in response to determining that the unit test code has failed compilation based on the compilation result, obtaining compilation prompt information based on the compilation result, wherein the compilation prompt information is used to prompt the compilation failure reason corresponding to the unit test code; and updating the unit test code using the compilation prompt information and a natural language processing model until the updated unit test code passes the compilation.
[0010] Optionally, the cause of the compilation failure includes at least one of the following: code syntax error, reference data error, file path error, and compiler configuration error.
[0011] Optionally, the initial code file and the target prompt statement are analyzed and processed using a natural language processing model to generate unit test code, including: using the natural language processing model to identify the initial code file to obtain an identification result, and using the natural language processing model to parse the target prompt statement to obtain a parsing result; generating a target test case based on the recognition result and the parsing result; and generating unit test code according to the target test case.
[0012] According to one aspect of the embodiments of the present application, a data testing device is also provided, including: an acquisition module, used to acquire an initial code file and a target prompt statement, wherein the target prompt statement is used to prompt the test type information and dependency content corresponding to the unit test code, and the unit test code is used to verify whether multiple preset program units in the initial code file meet the expected functional settings; a generation module, used to analyze and process the initial code file and the target prompt statement using a natural language processing model to generate unit test code; a compilation module, used to compile the unit test code to obtain a compilation result, wherein the compilation result is used to determine whether the unit test code has passed the compilation; an execution module, used to respond to determining that the unit test code has passed the compilation based on the compilation result, execute the unit test code, and obtain a test execution result, wherein the test execution result is used to determine the code error location and code error type in the initial code file.
[0013] Optionally, the acquisition module is also used to: analyze and process the initial code file using the program structure interface to obtain target test data, wherein the target test data includes: code data to be tested and associated code data; fill in a preset prompt template based on the target test data to obtain a target prompt statement.
[0014] Optionally, the acquisition module is also used to: use the program structure interface to parse the initial code file to obtain syntax tree structure information corresponding to the initial code file, wherein the syntax tree structure information is used to determine the syntax structure and calling relationship of the initial code file; and obtain target test data based on the syntax tree structure information.
[0015] Optionally, the acquisition module is further used to: determine the test type information and dependency content corresponding to the unit test code based on the target test data; and fill in a preset prompt template using the test type information and dependency content to obtain a target prompt statement.
[0016] Optionally, the data testing device also includes: a processing module, used to respond to determining based on the compilation result that the unit test code has failed to pass the compilation, and obtain compilation prompt information based on the compilation result, wherein the compilation prompt information is used to prompt the compilation failure reason corresponding to the unit test code; an update module, used to use the compilation prompt information and the natural language processing model to update the unit test code until the updated unit test code passes the compilation.
[0017] Optionally, the cause of the compilation failure includes at least one of the following: code syntax error, reference data error, file path error, and compiler configuration error.
[0018] Optionally, the generation module is also used to: use a natural language processing model to identify and process the initial code file to obtain an identification result, and use a natural language processing model to parse the target prompt statement to obtain a parsing result; generate a target test case based on the recognition result and the parsing result; and generate a unit test code according to the target test case.
[0019] According to one aspect of the embodiments of the present application, an electronic device is provided, including: a memory storing an executable program; and a processor for running the program, wherein when the program is running, any one of the data testing methods in the embodiments of the present application is executed.
[0020] According to one aspect of the embodiments of the present application, a computer-readable storage medium is provided, the computer-readable storage medium including a stored executable program, wherein when the executable program is running, the device where the storage medium is located is controlled to execute any one of the data testing methods in the embodiments of the present application.
[0021] According to one aspect of the embodiments of the present application, a computer program product is provided, including a computer program, which implements the data testing method according to any one of the embodiments of the present application when executed by a processor.
[0022] In an embodiment of the present application, an initial code file and a target prompt statement are obtained, and then the initial code file and the target prompt statement are analyzed and processed using a natural language processing model to generate unit test code, which is then compiled to obtain a compilation result. In response, the unit test code is determined to have passed compilation based on the compilation result, and the unit test code is executed to obtain a test execution result to determine the code error location and code error type in the initial code file, thereby achieving the purpose of automatically generating unit test code and executing unit testing, thereby achieving the technical effect of improving the test efficiency and test coverage of unit testing, and thereby solving the technical problems of low test efficiency and poor test coverage in the unit testing process in the related technology. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0024] Figure 1 is a schematic diagram of a data testing method according to an embodiment of the present application;
[0025] Figure 2 is a flow chart of another data testing method according to an embodiment of the present application;
[0026] Figure 3It is a structural block diagram of a data testing device according to an embodiment of the present application. DETAILED DESCRIPTION
[0027] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present application.
[0028] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0029] According to an embodiment of the present application, a method embodiment of data testing is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0030] The method embodiment can be executed in an electronic device or a similar computing device including a memory and a processor. Taking running on a computer terminal as an example, the computer terminal may include one or more processors (processors may include but are not limited to central processing units (CPU), graphics processing units (GPU), digital signal processing (DSP) chips, microprocessors (MCU), programmable logic devices (Field Programmable Gate Array, FPGA), neural network processors (Neural-network Processor Unit, NPU), tensor processors (TensorProcessing Unit, TPU), artificial intelligence (Artificial Intelligence, AI) type processors and other processing devices) and a memory for storing data. Optionally, the above-mentioned computer terminal may also include a transmission device, an input and output device, and a display device for communication functions. It can be understood by those of ordinary skill in the art that the above-mentioned structural description is only for illustration, and it does not limit the structure of the above-mentioned computer terminal. For example, the computer terminal may also include more or fewer components than the above-mentioned structural description, or have a configuration different from the above-mentioned structural description.
[0031] The memory can be used to store computer programs, for example, software programs and modules of application software, such as the computer program corresponding to the data testing method in the embodiment of the present application. The processor executes various functional applications and data processing by running the computer program stored in the memory, that is, realizing the above-mentioned data testing method. The memory may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory may further include a memory remotely arranged relative to the processor, and these remote memories may be connected to the mobile terminal via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0032] The transmission device is used to receive or send data via a network. The specific example of the above network may include a wireless network provided by a communication provider of the mobile terminal. In one example, the transmission device includes a network adapter (Network Interface Controller, referred to as NIC), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the transmission device can be a radio frequency (Radio Frequency, referred to as RF) module, which is used to communicate with the Internet wirelessly.
[0033] The display device may be, for example, a touch screen type liquid crystal display (LCD) and a touch display (also referred to as a "touch screen" or "touch display screen"). The liquid crystal display may enable a user to interact with a user interface of a mobile terminal. In some embodiments, the mobile terminal has a graphical user interface (GUI), and a user may interact with the GUI by finger contacts and / or gestures on a touch-sensitive surface, wherein the human-computer interaction functions here may optionally include the following interactions: creating web pages, drawing, word processing, making electronic documents, games, video conferencing, instant messaging, sending and receiving emails, call interfaces, playing digital videos, playing digital music and / or web browsing, etc. The executable instructions for executing the above human-computer interaction functions are configured / stored in a computer program product or readable storage medium executable by one or more processors.
[0034] Figure 1 is a flow chart of a data testing method according to an embodiment of the present application, such as Figure 1 As shown, the method comprises the following steps:
[0035] Step S11, obtaining an initial code file and a target prompt statement, wherein the target prompt statement is used to prompt the test type information and dependency content corresponding to the unit test code, and the unit test code is used to verify whether multiple preset program units in the initial code file meet the expected function settings;
[0036] Step S12, using a natural language processing model to analyze and process the initial code file and the target prompt statement to generate a unit test code;
[0037] Step S13, compiling the unit test code to obtain a compilation result, wherein the compilation result is used to determine whether the unit test code passes the compilation;
[0038] Step S14, in response to determining that the unit test code passes compilation based on the compilation result, the unit test code is executed to obtain a test execution result, wherein the test execution result is used to determine the code error location and code error type in the initial code file.
[0039] The above-mentioned initial code file is a source code file written in a preset programming language, such as Java source code. The initial code file usually contains the logic, functions, variables and other contents of the program, and executes the program through compilation or interpreter. The initial code file can be edited, saved, compiled, run and other operations to achieve specific functions or solve problems.
[0040] The above target prompt statement is used to prompt the test type information and dependency content corresponding to the unit test code. The unit test code is used to verify whether the multiple preset program units in the initial code file meet the expected functional settings to ensure their normal operation. The unit test code can simulate input data, call functions or methods, and verify whether the output results meet expectations. By writing and executing unit test code, developers can find and fix potential code errors early and improve code quality and stability.
[0041] The test type information corresponding to the above-mentioned unit test code plays an important guiding role in the management and execution of the unit test code, and helps to improve the quality and efficiency of the test. Specifically, the test type information of the unit test code can identify the scope of the test, guide the test strategy, evaluate the test coverage, and assist in problem location. The test type information can clearly understand the functional modules or classes involved in the unit test code, helping developers and testers to quickly locate problems. The test type information can guide testers to determine the design and execution strategies of the test cases to ensure that different paths and boundary conditions of the code are covered. The test coverage can also be evaluated through the test type information to help developers and testers understand which code paths have been covered and which need further testing. When a test fails, the test type information can help developers locate the problem more quickly, thereby fixing the error more quickly.
[0042] The dependencies corresponding to the above unit test code include but are not limited to the single function, method or module being tested, and usually need to simulate or replace other dependencies, such as external services, databases or file systems, etc. The dependency content is to ensure that the unit test code can run correctly and effectively test the tested code.
[0043] The above-mentioned natural language processing model is a model for processing and understanding human natural language. It can analyze and process text data through machine learning and deep learning algorithms to achieve tasks such as language understanding, language generation, text classification, and named entity recognition. In the field of software development, natural language processing models can be used to analyze initial code files and target prompt statements, generate unit test code, and help developers write test cases and verify the correctness of code logic more quickly. Using natural language processing models can improve the efficiency and quality of software development. In addition to existing natural language processing models, other machine learning or deep learning models can also be explored. Different models will have different effects on code generation and error repair.
[0044] Furthermore, the unit test code is compiled to obtain the compilation result. The compilation result is an important indicator of whether the unit test code has passed the compilation, which can help developers find errors and problems in the code as early as possible. During the compilation process, the compiler will check whether the code syntax and semantics are correct. If there are errors or non-standard places in the code, the compiler will report an error and stop compiling, prompting the developer to make corrections. The compilation results usually include the following situations: pass the compilation, compilation error, compilation warning, and compilation time is too long. Specifically, when the unit test code is compiled successfully, there is no error or warning, and it can be executed normally, that is, it passes the compilation; when there are syntax errors or logical errors in the unit test code, resulting in compilation failure, the developer needs to make corrections, then the compilation error is prompted. When the unit test code is compiled successfully, but there are some non-standard places that may affect the quality of the code and require the developer to pay attention to and deal with it, a compilation warning will appear. When the unit test code compilation time is too long, it may be due to the large amount of code or poor compiler performance. When the code needs to be optimized or the compilation environment needs to be adjusted, it will be prompted that the compilation time is too long. By analyzing the compilation results, developers can promptly discover and solve problems in the code to ensure the quality and stability of the unit test code.
[0045] When the unit test code is determined to have passed the compilation based on the compilation result, the unit test code is executed to obtain the test execution result. The test execution result is used to determine the code error location and code error type in the initial code file, thereby ensuring the functional correctness of the initial code file and improving the quality and stability of the code. When executing unit tests, it is usually necessary to check whether the input and output of the code meet expectations, and whether the code can correctly handle various boundary conditions and abnormal conditions. If the test execution result shows that the test passes, it means that the code is correct within the scope of the test; if the test fails, it is necessary to locate the error in the code according to the test results and fix it. By continuously executing unit test code, the quality of the code can be gradually improved, the errors in the code can be reduced, and the maintainability and scalability of the code can be improved. Unit testing can also help developers find and solve problems faster, improve development efficiency, and reduce the time cost of debugging and fixing errors.
[0046] Based on the above steps S11 to S14, by obtaining the initial code file and the target prompt statement, and then using the natural language processing model to analyze and process the initial code file and the target prompt statement, a unit test code is generated, and then the unit test code is compiled to obtain a compilation result. In response, it is determined based on the compilation result that the unit test code passes the compilation, and the unit test code is executed to obtain a test execution result to determine the code error location and code error type in the initial code file, thereby achieving the purpose of automatically generating unit test code and executing unit testing, thereby realizing the technical effect of improving the test efficiency and test coverage of unit testing, and thus solving the technical problems of low test efficiency and poor test coverage in the unit testing process in the related technology.
[0047] The data testing method in the embodiment of the present application is further introduced below.
[0048] In an optional embodiment, in step S11, obtaining the target prompt sentence includes:
[0049] Step S111, using the program structure interface to analyze and process the initial code file to obtain target test data, wherein the target test data includes: code data to be tested and associated code data;
[0050] Step S112, filling in the preset prompt template based on the target test data to obtain the target prompt sentence.
[0051] Specifically, the Program Structure Interface (PSI) in the integrated development environment is used to analyze the Java source code to obtain the target test data, which includes the code data to be tested and the associated code data, that is, the source code of the method to be tested and the source code of the associated method. The Java source code is statically analyzed through the PSI plug-in to extract all possible methods and objects to be tested for subsequent unit test generation.
[0052] For example, for a test where the code data to be tested is a function, and the associated code data is the input parameters and expected output values of the function, the preset prompt template can be filled based on these data to generate the following target prompt statement: "Unit test code should include tests for the input parameters and expected output values of the function, and the dependencies are the correctness of the function's implementation code and related auxiliary functions." The target prompt statement can help testers understand the content to be tested and related dependencies more clearly when writing unit test code, thereby improving the quality and coverage of the test code.
[0053] Based on the above optional embodiment, by using the program structure interface to analyze and process the code file, the target test data can be quickly and accurately obtained, thereby generating targeted prompt statements, thereby realizing automated code analysis and prompt statement generation, saving time and energy for manual analysis and processing, helping developers locate and solve problems more quickly, and improving code quality and development efficiency. At the same time, the automated analysis and generation process also reduces the possibility of human error and improves the accuracy and reliability of test results.
[0054] In an optional embodiment, in step S111, the initial code file is analyzed and processed using the program structure interface to obtain target test data including:
[0055] Step S1111, using the program structure interface to parse the initial code file to obtain syntax tree structure information corresponding to the initial code file, wherein the syntax tree structure information is used to determine the syntax structure and call relationship of the initial code file;
[0056] Step S1112, obtaining target test data based on the syntax tree structure information.
[0057] Specifically, in the process of parsing the initial code file using the program structure interface, the PSI is used to read the initial code file and convert it into syntax tree structure information. Specifically, a specific syntax parser can be used to parse the code file and convert it into a syntax tree. The syntax tree structure information can be used to determine the syntax structure and call relationship of the initial code file. By analyzing the nodes, subnodes, and the relationship between nodes in the syntax tree, the relationship between the various parts in the initial code file can be determined. For example, the relationship between function definition and call, the relationship between class definition and member function, etc. can be found. The obtained syntax tree structure information is saved in a data structure to obtain the target test data, so that the code file can be further analyzed and processed later.
[0058] Based on the above optional embodiments, the initial code file is parsed by utilizing the program structure interface to obtain the syntax tree structure information corresponding to the initial code file, and then the target test data is obtained based on the syntax tree structure information. Thus, the code file can be automatically analyzed and processed to obtain the syntax tree structure information of the code. Based on the syntax tree structure information, it can be used for code analysis, optimization and test data generation to further improve the test efficiency.
[0059] In an optional embodiment, in step S112, the preset prompt template is filled based on the target test data to obtain the target prompt sentence including:
[0060] Step S1121, determining the test type information and dependency content corresponding to the unit test code based on the target test data;
[0061] Step S1122: fill in the preset prompt template using the test type information and dependency content to obtain a target prompt sentence.
[0062] Specifically, by determining the test type information and dependency content from the target test data, the preset prompt template can be filled more accurately, making the generated prompt statement more specific and targeted. This can help developers better understand the logic and purpose of the unit test code and improve the quality and efficiency of the unit test code. At the same time, automatically filling in the preset prompt template can also save developers time and energy and improve development efficiency.
[0063] Based on the above optional embodiments, the test type information and dependency content corresponding to the unit test code are determined based on the target test data, and then the preset prompt template is filled in with the test type information and dependency content to obtain the target prompt statement, thereby improving the maintainability and readability of the unit test code.
[0064] In an optional embodiment, the data testing method in the embodiment of the present application also includes: in response to determining that the unit test code has failed compilation based on the compilation result, obtaining compilation prompt information based on the compilation result, wherein the compilation prompt information is used to prompt the compilation failure reason corresponding to the unit test code; using the compilation prompt information and the natural language processing model to update the unit test code until the updated unit test code passes the compilation.
[0065] Specifically, use the Idea compiler plug-in to compile the code. When the editing fails, obtain the compilation prompt information based on the compilation result, and submit it to the natural language processing model for regeneration together with the compilation prompt information obtained based on the compilation result. In this process, if the generated unit test code cannot be compiled in the Idea compiler, then these error messages will be fed back to the natural language processing model, and the natural language processing model will regenerate the unit test code based on this information until it passes the compilation. By automatically analyzing the compilation results and prompt information, and combining the natural language processing model to update the unit test code, it can help developers find and fix errors in the code more quickly, and improve the stability and maintainability of the code.
[0066] Based on the above optional embodiments, in response to determining that the unit test code has failed compilation based on the compilation result, compilation prompt information is obtained according to the compilation result, and then the unit test code is updated using the compilation prompt information and a natural language processing model until the updated unit test code passes compilation, thereby improving the quality and reliability of the unit test code.
[0067] In an optional embodiment, the cause of the compilation failure includes at least one of the following: code syntax error, reference data error, file path error, and compiler configuration error.
[0068] Specifically, code syntax errors are usually caused by spelling errors, missing semicolons or brackets, incorrect syntax usage, etc. in the code. The compiler cannot correctly parse these errors, resulting in compilation failure. If the code references data such as non-existent variables, functions, or classes, or references incorrect data types, the compiler will not be able to compile the code correctly, resulting in compilation failure, resulting in a reference data error. If the file path referenced in the code is incorrect or the file does not exist, the compiler cannot find the required file for compilation, resulting in compilation failure, resulting in a file path error. Compiler configuration errors include compiler version mismatches, incorrect compiler parameter settings, and other issues. If the compiler cannot correctly configure or recognize the code, compilation will fail.
[0069] In an optional embodiment, in step S12, the initial code file and the target prompt statement are analyzed and processed by using a natural language processing model to generate a unit test code, including:
[0070] Step S121, using a natural language processing model to identify the initial code file to obtain an identification result, and using the natural language processing model to parse the target prompt sentence to obtain a parsing result;
[0071] Step S122, generating a target test case based on the recognition result and the parsing result;
[0072] Step S123, generating unit test code according to the target test case.
[0073] The natural language processing model is used to identify and process the initial code file to obtain the recognition result, which includes information such as variable names, function names, and comments. At the same time, the natural language processing model is used to parse the target prompt statement to obtain the parsing result, that is, to understand the function or requirement to be tested in the target prompt statement. The target test case is generated based on the recognition result and the parsing result. The target test case can cover various situations to ensure the correctness and completeness of the code. Unit test code is generated according to the target test case. The unit test code can be used to verify whether the function of the initial code file is correctly implemented as required, thereby effectively testing and verifying the code and improving the code quality and stability.
[0074] Based on the above optional embodiments, the initial code file is recognized and processed by using a natural language processing model to obtain a recognition result, and the target prompt statement is parsed by using a natural language processing model to obtain a parsing result, and then a target test case is generated based on the recognition result and the parsing result, and finally a unit test code is generated according to the target test case, thereby further improving the testing efficiency.
[0075] Figure 2 is a flow chart of another data testing method according to an embodiment of the present application, such as Figure 2 As shown, the method comprises the following steps:
[0076] Step S201, obtaining an initial code file;
[0077] Step S202, using the program structure interface to analyze and process the initial code file to obtain target test data, wherein the target test data includes: code data to be tested and associated code data;
[0078] Step S203, filling the preset prompt template based on the target test data to obtain a target prompt sentence;
[0079] Step S204, using a natural language processing model to analyze and process the initial code file and the target prompt statement to generate a unit test code;
[0080] Step S205, compiling the unit test code to obtain a compilation result, wherein the compilation result is used to determine whether the unit test code passes the compilation;
[0081] Step S206, in response to determining that the unit test code passes compilation based on the compilation result, executing the unit test code to obtain a test execution result, wherein the test execution result is used to determine a code error location and a code error type in the initial code file;
[0082] Step S207, in response to determining that the unit test code fails to be compiled based on the compilation result, obtaining compilation prompt information according to the compilation result, wherein the compilation prompt information is used to prompt the compilation failure reason corresponding to the unit test code;
[0083] Step S208, updating the unit test code using the compilation prompt information and the natural language processing model until the updated unit test code passes the compilation.
[0084] Based on the above steps S201 to S208, by obtaining the initial code file and the target prompt statement, and then using the natural language processing model to analyze and process the initial code file and the target prompt statement, a unit test code is generated, and then the unit test code is compiled to obtain a compilation result. In response, it is determined based on the compilation result that the unit test code passes the compilation, and the unit test code is executed to obtain a test execution result to determine the code error location and code error type in the initial code file, thereby achieving the purpose of automatically generating unit test code and executing unit testing, thereby realizing the technical effect of improving the test efficiency and test coverage of unit testing, and thus solving the technical problems of low test efficiency and poor test coverage in the unit testing process in the related technology.
[0085] In the embodiment of the present application, the java source code is analyzed by the Idea compiler, the source code of the method to be tested and the associated method and the object is obtained, and the source code is submitted to the natural language processing model together with a prompt to generate a unit test code, which greatly improves the degree of automation and reduces the workload of developers. The unit test code is automatically generated by the natural language processing model, and each line of code in the source code can be more comprehensively covered, thereby improving the test coverage and ensuring the quality and correctness of the code. The code is compiled by the Idea compiler, and the natural language processing model is submitted together with the error message for regeneration if the editing fails, so that the specific error location can be more accurately located, which is convenient for developers to solve problems quickly. By running unit tests automatically in the background, code defects are found in real time, further improving the quality and reliability of the code.
[0086] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes a number of instructions for a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods described in each embodiment of the present application.
[0087] In the embodiments of the present application, a data testing device is also provided, which is used to implement the above-mentioned embodiments and preferred implementation modes, and the descriptions that have been made will not be repeated. As used below, the term "module" can implement a combination of software and / or hardware of a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, the implementation of hardware, or a combination of software and hardware is also possible and conceived.
[0088] Figure 3 is a structural block diagram of a data testing device according to an embodiment of the present application, such as Figure 3 As shown, the device comprises:
[0089] An acquisition module 301 is used to acquire an initial code file and a target prompt statement, wherein the target prompt statement is used to prompt the test type information and dependency content corresponding to the unit test code, and the unit test code is used to verify whether multiple preset program units in the initial code file meet the expected function settings;
[0090] A generation module 302 is used to analyze and process the initial code file and the target prompt statement using a natural language processing model to generate unit test code;
[0091] A compiling module 303 is used to compile the unit test code to obtain a compilation result, wherein the compilation result is used to determine whether the unit test code passes the compilation;
[0092] The execution module 304 is used to execute the unit test code in response to determining that the unit test code passes the compilation based on the compilation result, and obtain the test execution result, wherein the test execution result is used to determine the code error location and code error type in the initial code file.
[0093] Optionally, the acquisition module 301 is also used to: analyze and process the initial code file using the program structure interface to obtain target test data, wherein the target test data includes: code data to be tested and associated code data; fill in the preset prompt template based on the target test data to obtain the target prompt statement.
[0094] Optionally, the acquisition module 301 is also used to: parse the initial code file using the program structure interface to obtain syntax tree structure information corresponding to the initial code file, wherein the syntax tree structure information is used to determine the syntax structure and calling relationship of the initial code file; and obtain target test data based on the syntax tree structure information.
[0095] Optionally, the acquisition module 301 is further used to: determine the test type information and dependency content corresponding to the unit test code based on the target test data; and fill in a preset prompt template using the test type information and dependency content to obtain a target prompt statement.
[0096] Optionally, the data testing device also includes: a processing module 305, which is used to respond to determining that the unit test code has failed to pass the compilation based on the compilation result, and obtain compilation prompt information based on the compilation result, wherein the compilation prompt information is used to prompt the compilation failure reason corresponding to the unit test code; an update module 306, which is used to use the compilation prompt information and the natural language processing model to update the unit test code until the updated unit test code passes the compilation.
[0097] Optionally, the cause of the compilation failure includes at least one of the following: code syntax error, reference data error, file path error, and compiler configuration error.
[0098] Optionally, the generation module 302 is also used to: use a natural language processing model to identify and process the initial code file to obtain an identification result, and use a natural language processing model to parse the target prompt statement to obtain a parsing result; generate a target test case based on the recognition result and the parsing result; and generate a unit test code according to the target test case.
[0099] It should be noted that the above modules can be implemented by software or hardware. For the latter, it can be implemented in the following ways, but not limited to: the above modules are all located in the same processor; or the above modules are located in different processors in any combination.
[0100] According to another aspect of an embodiment of the present application, a computer-readable storage medium is further provided, the computer-readable storage medium including a stored executable program, wherein when the executable program runs, the device where the storage medium is located is controlled to execute the above-mentioned data testing method.
[0101] Optionally, in this embodiment, the storage medium may be configured to store a computer program for performing the following steps:
[0102] S1, obtaining an initial code file and a target prompt statement, wherein the target prompt statement is used to prompt the test type information and dependency content corresponding to the unit test code, and the unit test code is used to verify whether multiple preset program units in the initial code file meet the expected function settings;
[0103] S2, using the natural language processing model to analyze and process the initial code file and the target prompt statement to generate unit test code;
[0104] S3, compile the unit test code to obtain a compilation result, wherein the compilation result is used to determine whether the unit test code passes the compilation;
[0105] S4, in response to determining that the unit test code passes compilation based on the compilation result, the unit test code is executed to obtain a test execution result, wherein the test execution result is used to determine the code error location and code error type in the initial code file.
[0106] Optionally, in this embodiment, the above-mentioned storage medium may include but is not limited to: a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk, and other media that can store computer programs.
[0107] According to another aspect of an embodiment of the present application, there is further provided an electronic device, comprising: a memory storing an executable program; and a processor for running the program, wherein the above-mentioned data testing method is executed when the program is running.
[0108] Optionally, in this embodiment, the processor may be configured to perform the following steps through a computer program:
[0109] S1, obtaining an initial code file and a target prompt statement, wherein the target prompt statement is used to prompt the test type information and dependency content corresponding to the unit test code, and the unit test code is used to verify whether multiple preset program units in the initial code file meet the expected function settings;
[0110] S2, using the natural language processing model to analyze and process the initial code file and the target prompt statement to generate unit test code;
[0111] S3, compile the unit test code to obtain a compilation result, wherein the compilation result is used to determine whether the unit test code passes the compilation;
[0112] S4, in response to determining that the unit test code passes compilation based on the compilation result, the unit test code is executed to obtain a test execution result, wherein the test execution result is used to determine the code error location and code error type in the initial code file.
[0113] According to another aspect of an embodiment of the present application, a computer program product is also provided, including a computer program, and the computer program implements the above-mentioned data testing method when executed by a processor.
[0114] Optionally, in this embodiment, the computer program product may be configured as a computer program for executing the following steps:
[0115] S1, obtaining an initial code file and a target prompt statement, wherein the target prompt statement is used to prompt the test type information and dependency content corresponding to the unit test code, and the unit test code is used to verify whether multiple preset program units in the initial code file meet the expected function settings;
[0116] S2, using the natural language processing model to analyze and process the initial code file and the target prompt statement to generate unit test code;
[0117] S3, compile the unit test code to obtain a compilation result, wherein the compilation result is used to determine whether the unit test code passes the compilation;
[0118] S4, in response to determining that the unit test code passes compilation based on the compilation result, the unit test code is executed to obtain a test execution result, wherein the test execution result is used to determine the code error location and code error type in the initial code file.
[0119] The serial numbers of the above-mentioned embodiments of the present application are for description only and do not represent the advantages or disadvantages of the embodiments.
[0120] In the above embodiments of the present application, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, please refer to the relevant description of other embodiments.
[0121] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only schematic. For example, the division of the units can be a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.
[0122] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple units. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0123] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0124] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application, in essence, or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), mobile hard disk, disk or optical disk and other media that can store program codes.
[0125] The above is only a preferred implementation of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.
Claims
1. A data testing method, characterized in that: include: Obtaining an initial code file and a target prompt statement, wherein the target prompt statement is used to prompt the test type information and dependency content corresponding to the unit test code, and the unit test code is used to verify whether multiple preset program units in the initial code file meet the expected function settings; Analyzing and processing the initial code file and the target prompt statement using a natural language processing model to generate the unit test code; Compiling the unit test code to obtain a compilation result, wherein the compilation result is used to determine whether the unit test code passes the compilation; In response to determining that the unit test code passes compilation based on the compilation result, the unit test code is executed to obtain a test execution result, wherein the test execution result is used to determine a code error location and a code error type in the initial code file.
2. The data testing method according to claim 1, characterized in that: Obtaining the target prompt sentence includes: Analyzing and processing the initial code file using a program structure interface to obtain target test data, wherein the target test data includes: code data to be tested and associated code data; A preset prompt template is filled based on the target test data to obtain the target prompt sentence.
3. The data testing method according to claim 2, characterized in that: The initial code file is analyzed and processed using the program structure interface to obtain target test data including: Utilizing the program structure interface to parse the initial code file, obtaining syntax tree structure information corresponding to the initial code file, wherein the syntax tree structure information is used to determine the syntax structure and calling relationship of the initial code file; The target test data is acquired based on the syntax tree structure information.
4. The data testing method according to claim 2, characterized in that: Filling the preset prompt template based on the target test data to obtain the target prompt sentence includes: Determine the test type information and the dependency content corresponding to the unit test code based on the target test data; The preset prompt template is filled using the test type information and the dependency content to obtain the target prompt sentence.
5. The data testing method according to claim 1, characterized in that: The method further comprises: In response to determining that the unit test code fails to be compiled based on the compilation result, obtaining compilation prompt information according to the compilation result, wherein the compilation prompt information is used to prompt the compilation failure reason corresponding to the unit test code; The unit test code is updated using the compilation prompt information and the natural language processing model until the updated unit test code passes the compilation.
6. The data testing method according to claim 5, characterized in that: The reasons for the compilation failure include at least one of the following: code syntax error, reference data error, file path error, and compiler configuration error.
7. The data testing method according to claim 1, characterized in that: Analyzing and processing the initial code file and the target prompt statement using the natural language processing model to generate the unit test code includes: Using the natural language processing model to perform recognition processing on the initial code file to obtain a recognition result, and using the natural language processing model to parse the target prompt sentence to obtain a parsing result; Generate a target test case based on the recognition result and the parsing result; The unit test code is generated according to the target test case.
8. A data testing device, characterized in that: include: An acquisition module, used to acquire an initial code file and a target prompt statement, wherein the target prompt statement is used to prompt the test type information and dependency content corresponding to the unit test code, and the unit test code is used to verify whether multiple preset program units in the initial code file meet the expected function settings; A generation module, used to analyze and process the initial code file and the target prompt statement using a natural language processing model to generate the unit test code; A compiling module, used to compile the unit test code to obtain a compiling result, wherein the compiling result is used to determine whether the unit test code passes the compilation; An execution module is used to execute the unit test code in response to determining that the unit test code passes compilation based on the compilation result to obtain a test execution result, wherein the test execution result is used to determine the code error location and code error type in the initial code file.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored executable program, wherein when the executable program is run, the device where the storage medium is located is controlled to execute the data testing method according to any one of claims 1 to 7.
10. A computer program product, characterized in that The invention comprises a computer program, which implements the data testing method according to any one of claims 1 to 7 when being executed by a processor.
Citation Information
Cited By
Code generation method, computing device, electronic device and storage medium
CN119883939A
Unit test code generation method based on large language model, and related device
WO2026130029A1
Code generation method, computing device, electronic device and storage medium
WO2026200036A1
