Test method, device, electronic device, storage medium and program product
By performing static type testing of functional unit codes, using runnable and unrunable data types, we automatically detect the correctness of static types after refactoring of codes, and solve the problems of low detection efficiency and low accuracy in the existing technology, and realize the Efficient and accurate static type testing.
Patent Information
- Application Number
- CN202111341749.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-12
- Publication Date
- 2024-12-10
- Estimated Expiration
- 2041-11-12
AI Technical Summary
The prior art cannot accurately detect the static type accuracy of functional units after code reconstruction, resulting in low efficiency and low accuracy of test results.
By using two types of test data to perform static type testing of functional unit code, the static type test results are automatically obtained, and the runnable and unrunable data types are used to test separately to determine the correctness of static types.
It improves the efficiency and accuracy of static type testing of functional unit codes, realizes automatic acquisition of specific information on static type test results, and reduces manual intervention.
Smart Images

Figure CN113986757B_ABST
Abstract
Description
Background Art
[0002] During the code testing process, in order to optimize the code, some functional units in the code can be refactored. In order to determine whether the code refactoring destroys the function of the code, the functional units in the code can be tested after the code refactoring to detect the correctness of the business logic and static types of the functional units in the code.
[0003] In the related art, for the written code, before and after the code is refactored, the functional units in the code can be unit tested based on the test framework. However, for codes written in some programming languages, although the correctness of the business logic of the functional units in the code can be tested using the test framework, the correctness of the static type of the functional units cannot be accurately tested. Summary of the invention
[0004] The present disclosure provides a testing method, device, electronic device, storage medium and program product to at least solve the problem in the related art that the specific information of the static type test result of the functional unit cannot be determined, and the efficiency and accuracy of determining the static type correctness of the functional unit subcode are low. The technical solution of the present disclosure is as follows:
[0005] According to a first aspect of an embodiment of the present disclosure, a testing method is provided, including:
[0006] Obtaining a target code to be tested, wherein the target code includes a functional unit subcode;
[0007] Performing a first static type test on the functional unit subcode using first test data to obtain a current first test result, and performing a second static type test on the functional unit subcode using second test data to obtain a current second test result, wherein the data type of the first test data is a data type that can be run by the functional unit subcode, and the data type of the second test data is a data type that cannot be run by the functional unit subcode;
[0008] If it is determined that the current first test result is the expected test result corresponding to the first test data, and if it is determined that the current second test result is the expected test result corresponding to the second test data, then it is determined that the static type of the functional unit subcode is correct.
[0009] Optionally, determining that the current first test result is an expected test result corresponding to the first test data includes:
[0010] If the current first test result is an empty array, the first snapshot data is obtained, and the first snapshot data is the snapshot data of the historical first test result;
[0011] If the first snapshot data is an empty array, it is determined that the current first test result is an expected test result corresponding to the first test data.
[0012] Optionally, the test method also includes:
[0013] In response to an update operation on the first snapshot data, the first snapshot data is updated using the current first test result.
[0014] Optionally, determining that the current second test result is an expected test result corresponding to the second test data includes:
[0015] If the current second test result is a non-empty array, the second snapshot data is obtained, and the second snapshot data is the snapshot data of the historical second test result;
[0016] If it is determined that the second snapshot data is the same as the current second test result, the current second test result is determined to be the expected test result corresponding to the second test data.
[0017] Optionally, the second snapshot data is the same as the current second test result, including:
[0018] The second snapshot data is a non-empty array, the array corresponding to the second snapshot data has the same length as the array corresponding to the current second test result, and the character strings in the array corresponding to the second snapshot data and the array corresponding to the current second test result are consistent.
[0019] Optionally, the test method also includes:
[0020] In response to an update operation on the second snapshot data, the first snapshot data is updated using the current second test result.
[0021] Optionally, performing a first static type test on the functional unit subcode using the first test data to obtain a current first test result includes:
[0022] Run the first test script corresponding to the functional unit sub-code, call the functional unit sub-code with the first test data as a parameter to perform a first static type test, and obtain a current first test result.
[0023] Optionally, performing a second static type test on the functional unit subcode using the second test data to obtain a current second test result includes:
[0024] Run the second test script corresponding to the functional unit sub-code, call the functional unit sub-code with the second test data as a parameter to perform a second static type test, and obtain the current second test result.
[0025] According to a second aspect of an embodiment of the present disclosure, a testing device is provided, including:
[0026] An acquisition module is configured to acquire a target code to be tested, wherein the target code includes a functional unit subcode;
[0027] The testing module is configured to perform a first static type test on the functional unit subcode using first test data to obtain a current first test result, and to perform a second static type test on the functional unit subcode using second test data to obtain a current second test result, wherein the data type of the first test data is a data type that can be run by the functional unit subcode, and the data type of the second test data is a data type that cannot be run by the functional unit subcode;
[0028] The determination module is configured to determine that the static type of the functional unit subcode is correct if it is determined that the current first test result is the expected test result corresponding to the first test data and the current second test result is the expected test result corresponding to the second test data.
[0029] Optionally, determine the module, configured as:
[0030] If the current first test result is an empty array, the first snapshot data is obtained, and the first snapshot data is the snapshot data of the historical first test result;
[0031] If the first snapshot data is an empty array, it is determined that the current first test result is an expected test result corresponding to the first test data.
[0032] Optionally, the device further comprises:
[0033] The update module is configured to update the first snapshot data using the current first test result in response to an update operation on the first snapshot data.
[0034] Optionally, determine the module, configured as:
[0035] If the current second test result is a non-empty array, the second snapshot data is obtained, and the second snapshot data is the snapshot data of the historical second test result;
[0036] If it is determined that the second snapshot data is the same as the current second test result, the current second test result is determined to be the expected test result corresponding to the second test data.
[0037] Optionally, the determination module determines that the second snapshot data is the same as the current second test result, including: the second snapshot data is a non-empty array, the array corresponding to the second snapshot data is the same length as the array corresponding to the current second test result, and the array corresponding to the second snapshot data is consistent with the string in the array corresponding to the current second test result.
[0038] Optionally, the test device further includes:
[0039] The update module is configured to update the first snapshot data using the current second test result in response to an update operation on the second snapshot data.
[0040] Optionally, the test module is configured to:
[0041] Run the first test script corresponding to the functional unit sub-code, call the functional unit sub-code with the first test data as a parameter to perform a first static type test, and obtain a current first test result.
[0042] Optionally, the test module is configured to:
[0043] Run the second test script corresponding to the functional unit sub-code, call the functional unit sub-code with the second test data as a parameter to perform a second static type test, and obtain the current second test result.
[0044] According to a third aspect of an embodiment of the present disclosure, there is provided an electronic device, including:
[0045] processor;
[0046] a memory for storing processor-executable instructions;
[0047] The processor is configured to execute instructions to implement the testing method of the first aspect.
[0048] According to a fourth aspect of an embodiment of the present disclosure, a computer-readable storage medium is provided. When instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to perform the testing method of the first aspect.
[0049] According to a fifth aspect of an embodiment of the present disclosure, there is provided a computer program product, comprising a computer program, which implements the testing method of the first aspect when the computer program is executed by a processor.
[0050] The technical solution provided by the embodiments of the present disclosure brings at least the following beneficial effects:
[0051] The test method, device, electronic device, storage medium and program product provided by the embodiments of the present disclosure can use the test data that can be run by the functional unit subcode to perform static type detection on the functional unit subcode to obtain a first test result, and use the test data that cannot be run by the functional unit subcode to perform a second static type detection on the functional unit subcode to obtain a second test result, which can realize automatic acquisition of specific information of the static type test result of the functional unit subcode without manual determination, thereby improving the efficiency of determining the specific information of the static type test result of the functional unit subcode; at the same time, when it is determined that the functional unit subcode before and after code reconstruction can run the data type that it can run, and the functional unit subcode before and after code reconstruction cannot run the test data of the data type that it cannot run, and the second test results before and after code reconstruction are the same, it is determined that the static type of the functional unit subcode is correct, and the correctness of the static type of the functional unit subcode can be quickly determined through the two running results of the functional unit subcode on the data that it can run and the data that it cannot run, thereby improving the efficiency and accuracy of determining the correctness of the static type of the functional unit subcode.
[0052] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] The drawings herein are incorporated into and constitute a part of the specification, illustrate embodiments consistent with the present disclosure, and together with the description are used to explain the principles of the present disclosure, and do not constitute improper limitations on the present disclosure.
[0054] Figure 1 The figure is a flow chart of a testing method according to an exemplary embodiment.
[0055] Figure 2 The present invention is a flow chart showing a method for determining that a current first test result is an expected test result corresponding to first test data according to an exemplary embodiment.
[0056] Figure 3 The present invention is a flow chart showing a method for determining that a current second test result is an expected test result corresponding to second test data according to an exemplary embodiment.
[0057] Figure 4 is a flow chart showing another testing method according to an exemplary embodiment.
[0058] Figure 5 It is a schematic structural diagram of a testing device according to an exemplary embodiment.
[0059] Figure 6 The diagram is a schematic structural diagram of an electronic device according to an exemplary embodiment. DETAILED DESCRIPTION
[0060] In order to enable ordinary persons in the art to better understand the technical solutions of the present disclosure, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the accompanying drawings.
[0061] It should be noted that the terms "first", "second", etc. in the specification and claims of the present disclosure 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 interchanged where appropriate, so that the embodiments of the present disclosure described herein can be implemented in an order other than those illustrated or described herein. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.
[0062] In the related art, during the target code testing phase, if the functional unit subcode in the target unit is reconstructed, it is necessary to detect the static type and logic of the reconstructed functional unit subcode to ensure that the code reconstruction will not destroy the functional implementation of the functional unit subcode. Usually, the static type correctness of the functional unit subcode can be tested using a test tool. However, when detecting the static type of the functional unit subcode, the test tool used in the related art can only indicate whether there is a static type error in the functional unit subcode, but cannot determine the specific error information. For example, for the functional unit sub-code in the target code to be tested, the correctness of the static type of the functional unit sub-code can be detected based on the Jest testing framework, wherein the target code can be written in the TypeScript programming language, and the process of detecting the correctness of the static type of the functional unit sub-code based on the Jest testing framework (a code testing framework for the JavaScript programming language) can include: for the functional unit sub-code, adding JSDoc comments (JSDoc is a tool that generates application interface documents for JavaScript applications or modules based on comment information in JavaScript files), the JSDoc comments can be "@ts-expect-error" instructions, during the testing process of the code to be tested, the JSDoc parser can recognize the JSDoc comments, if the static type of the functional unit sub-code is correct, then the prompt information of the invalid instruction can be displayed, if the static type of the functional unit sub-code is incorrect, then the prompt information of the static type error can be displayed.
[0063] It can be seen that the method provided in the relevant technology for detecting the static type correctness of the functional unit sub-code in the target code is just a simple prompt whether there is a static type error in the functional unit sub-code, and it is unable to automatically determine the specific information of the static type test result. The tester is required to determine the specific information of the static type test result of the functional unit sub-code based on the prompt information, which is time-consuming and manpower-consuming.
[0064] In view of the above problems, the embodiments of the present disclosure provide a testing method, which can be applied to any code testing device, and the code testing device can be a personal computer or a desktop computer, etc., which is not limited by the embodiments of the present disclosure. Among them, the testing method can be applied to the testing link of the static type correctness of the functional unit sub-code in the target code, and the target code to be tested can be written in the TypeScript programming language. For the target code written in the TypeScript programming language, the code testing process can be implemented based on the test tool written in the Jest test framework and the detection engine of the TypeScript programming language. The application scenarios of the testing method include but are not limited to: the test tool responds to the selection operation of the target code to be tested, loads the pre-stored target code to obtain the target code to be tested, and calls the detection engine of the TypeScript programming language to perform code testing. The code testing process can be implemented in the Jest test framework. The test tool can be a tkit / type-test test tool written based on the Jest test framework.
[0065] Figure 1 is a flow chart of a testing method according to an exemplary embodiment. Figure 1 As shown, the testing method includes the following steps S101 to S103:
[0066] In step S101, the target code to be tested is obtained.
[0067] In the embodiment of the present disclosure, the target code includes a functional unit sub-code, which refers to a code module in the target code that is used to perform a certain task or realize a certain function.
[0068] In step S102, a first static type test is performed on the functional unit subcode using the first test data to obtain a current first test result, and a second static type test is performed on the functional unit subcode using the second test data to obtain a current second test result.
[0069] In the disclosed embodiment, the data type of the first test data is a data type that can be run by the functional unit subcode, the first static type test refers to making the functional unit subcode run the first test data, and the current first test result is a test result obtained by performing the first static type test on the functional unit subcode after the code is reconstructed using the first test data; the data type of the second test data is a data type that cannot be run by the functional unit subcode, the second static type test refers to making the functional unit subcode run the second test data, and the current second test result is a test result obtained by performing the second static type test on the functional unit subcode after the code is reconstructed using the second test data.
[0070] Among them, if the functional unit sub-code can run the first test data, the first test result does not include an array; if the functional unit sub-code cannot run the first test data, the first test result includes at least one array; similarly, if the functional unit sub-code can run the second test data, the second test result does not include an array; if the functional unit sub-code cannot run the second test data, the second test result includes at least one array. The character string in each array may include multiple field identifiers and field information corresponding to the field identifiers. The field identifier may include: an error type identifier, an error code identifier, a target file name identifier, and an error message identifier to record specific information of the test results.
[0071] In step S103, if it is determined that the current first test result is the expected test result corresponding to the first test data, and the current second test result is the expected test result corresponding to the second test data, then it is determined that the static type of the functional unit subcode is correct.
[0072] In the disclosed embodiment of the present invention: the expected test result corresponding to the first test data means that the first static type test is performed on the functional unit subcodes before and after the code reconstruction, and the functional unit subcodes before and after the code reconstruction can both run the first test data; the expected test result corresponding to the second test data means that the second static type test is performed on the functional unit subcodes before and after the code reconstruction, and the functional unit subcodes before and after the code reconstruction cannot run the second test data, and the arrays in the second test result obtained by performing the second static type test on the functional unit subcodes before and after the code reconstruction are the same.
[0073] In summary, the test method provided by the embodiment of the present disclosure can use the test data that can be run by the functional unit subcode to perform static type detection on the functional unit subcode in the target code to obtain a first test result, and use the test data that cannot be run by the functional unit subcode to perform a second static type detection on the functional unit subcode to obtain a second test result. The specific information of the static type test result of the functional unit subcode can be automatically obtained without manual determination, thereby improving the efficiency of determining the specific information of the static type test result of the functional unit subcode; at the same time, when it is determined that the functional unit subcode before and after code reconstruction can run the data type that it can run, and the functional unit subcode before and after code reconstruction cannot run the test data of the data type that it cannot run, and the second test results before and after code reconstruction are the same, it is determined that the static type of the functional unit subcode is correct, and the correctness of the static type of the functional unit subcode can be quickly determined through the two running results of the functional unit subcode on the data that it can run and the data that it cannot run, thereby improving the efficiency and accuracy of determining the correctness of the static type of the functional unit subcode.
[0074] In an optional embodiment, in the above step S101, the testing tool may obtain the target code to be tested.
[0075] In an embodiment of the present disclosure, the target code includes a functional unit sub-code, and the target code may be written in the TypeScript programming language. For example, the functional unit sub-code may be a code module in the target code corresponding to a function for performing a task.
[0076] In an optional implementation, the process of the test tool acquiring the target code to be tested may include: the test tool may acquire the target code to be tested in response to a load operation of the target code pre-stored in the code test device.
[0077] In an optional embodiment, in the above step S102, the testing tool can use the first test data to perform a first static type test on the functional unit subcode to obtain the current first test result, and use the second test data to perform a second static type test on the functional unit subcode to obtain the current second test result.
[0078] In the embodiment of the present disclosure, the data type of the first test data is the data type that the functional unit subcode can run, and the data type of the second test data is the data type that the functional unit subcode cannot run. For example, if the data type that a certain functional unit subcode can run is number type data, the first test data is number type test data, and the second test data is non-number type test data, for example, the second test data may include string type test data.
[0079] It should be noted that in the embodiments of the present disclosure, for the functional unit sub-code, a first test script and a second test script corresponding to the functional unit sub-code can be pre-written, and the first test script is used to perform a first static type test on the functional unit sub-code using first test data to obtain a first test result; the second test script is used to perform a second static type test on the functional unit sub-code using second test data to obtain a second test result, wherein the test results are saved in an array form to facilitate intuitively obtaining specific information on the static type test results for the functional unit sub-code, and static type tests of the corresponding functional unit sub-codes can be performed by using two pre-written test scripts corresponding to different functional unit sub-codes, thereby improving test efficiency.
[0080] It is understandable that in the process of code writing, the tasks to be performed or the functions to be implemented are various, and the code modules corresponding to the tasks or functions are also various. For the code modules corresponding to each task or function, the first test script and the second test script corresponding thereto can be pre-written to obtain a test script library. In the process of code testing, the first test script and the second test script corresponding to the functional unit subcode in the target code can be called in the test script library to test the functional unit subcode in the target code to be tested. Among them, each first test script or second test script in the test script library can include the functional unit identifier of its testable functional unit subcode.
[0081] In an optional embodiment, the process in which the testing tool uses the first test data to perform a first static type test on the functional unit sub-code to obtain the current first test result may include: running the first test script corresponding to the functional unit sub-code, calling the functional unit sub-code with the first test data as a parameter to perform a first static type test, and obtaining the current first test result.
[0082] Among them, the process of running the first test script corresponding to the functional unit sub-code may include: determining the functional unit identifier of the functional unit sub-code, determining the first test script corresponding to the functional unit identifier in the test script library according to the functional unit identifier, obtaining the first test script corresponding to the functional unit sub-code, and running the first test script corresponding to the functional unit sub-code.
[0083] Among them, the process of calling the functional unit subcode with the first test data as a parameter to perform the first static type test and obtain the current first test result can be implemented based on the first test script to improve the efficiency of determining the first test result. The process may include: running the first test script so that the first test script generates the first test data, and using the first test data to call the functional unit subcode, monitoring the running result of the functional unit subcode on the first test data, and generating the current first test result according to the running result. Among them, if it is monitored that the functional unit subcode is successfully called by the first test data, the current first test result is generated, and the current first test result is converted into JSON (JavaScript Object Notation, JavaScript Object Notation) format, and the current first test result does not include an array; if the monitoring functional unit subcode cannot be successfully called by the first test data, the current first test result is generated, and the current first test result is converted into JSON format, and the current first test result includes at least one array, and the string in each array can include multiple field identifiers and field information corresponding to the field identifier, and the field identifier can include: error type identifier, error code identifier, target file name identifier, and error information identifier.
[0084] It should be noted that the process of running the code includes: assigning parameter values to the code so that the code executes the data processing logic therein according to the parameter values, and obtaining the execution result of the code on the parameter values. The test data is the parameter value assigned to the code during the code testing phase. The process of calling the functional unit sub-code using the first test data refers to: assigning the first test data to the functional unit sub-code so that the functional unit sub-code executes the data processing logic therein according to the first test data, and obtaining the execution result of the first test data.
[0085] In an optional embodiment, the process in which the testing tool uses the second test data to perform a second static type test on the functional unit sub-code to obtain the current second test result may include: running a second test script corresponding to the functional unit sub-code, calling the functional unit sub-code with the second test data as a parameter to perform a second static type test, and obtaining the current second test result.
[0086] Among them, the process of running the second test script corresponding to the functional unit sub-code may include: determining the functional unit identifier of the functional unit sub-code, determining the second test script corresponding to the functional unit identifier in the test script library according to the functional unit identifier, obtaining the second test script corresponding to the functional unit sub-code, and running the second test script corresponding to the functional unit sub-code.
[0087] Among them, the process of calling the functional unit subcode with the second test data as a parameter to perform the second static type test and obtaining the current second test result can be implemented based on the second test script to improve the efficiency of determining the second test result. The process may include: running the second test script so that the second test script generates the second test data, calling the functional unit subcode using the second test data, monitoring the running result of the functional unit subcode on the second test data, and generating the current second test result according to the running result. Among them, if it is monitored that the functional unit subcode is successfully called by the second test data, the current second test result is generated, and the current second test result is converted into JSON format, and the current second test result does not include an array; if the monitoring functional unit subcode cannot be successfully called by the second test data, the current second test result is generated, and the current second test result is converted into JSON format, and the current second test result includes at least one array, and the string in each array can include multiple field identifiers and field information corresponding to the field identifier, and the field identifier can include: error type identifier, error code identifier, target file name identifier, and error information identifier.
[0088] Similarly, the process of calling the functional unit subcode using the second test data refers to: assigning the second test data to the functional unit subcode so that the functional unit subcode executes the data processing logic therein according to the second test data to obtain the execution result of the second test data.
[0089] For example, assume that a second static type test is performed on a functional unit subcode and the current second test result is [{"category":1,"code":2322,"fileName":"cmSamples / testCMFail.ts","messageText":"Type'number'is not assignable to type'string'.",}], where the current second test result includes an array {"category":1,"code":2322,"fileName":"cmSamples / testCMFail.ts","messageText":"Type'number'is not assignable to type'string'.",}, in which category represents an error type identifier, 1 represents the error type; code represents an error code identifier, 2322 represents the error code; fileName represents a file name identifier of a target file, cmSamples / testCMFail.ts represents the file name of the target file; messageText represents an error message identifier, and Type'number'is not assignableto type'string' represents the error message. The current second test result indicates that a TS2322 error of error type 1 occurs in the functional unit subcode of the target code of the file name cmSamples / testCMFail.ts, and the error message is that the functional unit subcode that can run 'number' type data cannot run 'string' type data.
[0090] In an optional embodiment, in the above step S103, if the testing tool determines that the current first test result is the expected test result corresponding to the first test data, and determines that the current second test result is the expected test result corresponding to the second test data, then the static type of the functional unit subcode is determined to be correct.
[0091] In an embodiment of the present disclosure, if the current first test result is an empty array, indicating that the functional unit subcode can successfully run the test data of the data type that it can run; and the historical first test result is an empty array, indicating that the functional unit subcode before the code reconstruction can also successfully run the test data of the data type that it can run, it can be determined that the test tool determines that the current first test result is the expected test result corresponding to the first test data, wherein the current first test result is the test result obtained by performing a first static type test on the functional unit subcode after the code reconstruction using the first test data, and the historical first test result is the first test result obtained by performing a first static type test on the functional unit subcode before the code reconstruction using the first test data.
[0092] If the current second test result is a non-empty array, indicating that the functional unit subcode cannot successfully run the test data of the data type that it cannot run; and the historical second test result is a non-empty array, indicating that the functional unit subcode before the code reconstruction cannot successfully run the test data of the data type that it cannot run, then it can be determined that the current second test result is the expected test result corresponding to the second test data, wherein the current second test result is the test result obtained by performing a second static type test on the functional unit subcode after the code reconstruction using the second test data, and the historical second test result is the second test result obtained by performing a second static type test on the functional unit subcode before the code reconstruction using the second test data.
[0093] In an optional embodiment, if Figure 2 As shown, the process of the test tool determining that the current first test result is the expected test result corresponding to the first test data may include the following steps S201 to S203:
[0094] Step S201: Determine whether the current first test result is an empty array.
[0095] In an optional embodiment, the process of the testing tool determining whether the current first test result is an empty array may include: obtaining the current first test result, determining whether there is an array in the current first test result, if there is an array in the current first test result, determining that the current first test result is a non-empty array, indicating that the functional unit sub-code cannot run data of the data type it can run, and the code refactoring destroys the static type of the functional unit sub-code, then a prompt message indicating that the static type test of the functional unit sub-code has failed may be displayed, and the process may be terminated; if there is no array in the current first test result, determining that the current first test result is an empty array, indicating that the functional unit sub-code cannot run data of the data type it can run, then continuing to execute step S202.
[0096] For example, the process of the test tool determining whether the current first test result is an empty array can be implemented by calling the toHaveLength(number) matcher, which is used to determine the number of arrays in the test result; wherein, if the current first test result is [{…},{…}], since the current first test result includes two arrays {…}, the current first test result output by the toHaveLength(number) matcher is toHaveLength(2), indicating that the current first test result is a non-empty array; if the current first test result is [], since there is no array in the current first test result, the current first test result output by the toHaveLength(number) matcher is toHaveLength(0), indicating that the current first test result is an empty array.
[0097] Step S202: If the current first test result is an empty array, obtain first snapshot data.
[0098] In the disclosed embodiment, the snapshot data is the backup data of the test results, and the first snapshot data is the snapshot data of the historical first test result. The generation process of the first snapshot data may include: in the process of static type testing the functional unit sub-code before code reconstruction, after using the first test data to perform a first static type test on the functional unit sub-code before code reconstruction to obtain the first test result, a snapshot data file of the first test result can be generated to obtain the first snapshot data.
[0099] In an optional implementation, the process of the test tool acquiring the first snapshot data may include: acquiring the first snapshot data in a snapshot data file.
[0100] Step S203: If the first snapshot data is an empty array, determine that the current first test result is an expected test result corresponding to the first test data.
[0101] In an optional implementation, the process of the test tool determining whether the current first test result is the expected test result corresponding to the first test data may include: the test tool may compare the first snapshot data with the current first test result to determine whether the first snapshot data is also an empty array. If the first snapshot data is an empty array, it means that the functional unit subcode before the code reconstruction can successfully run the data of the data type that it can run, and then the current first test result can be determined to be the expected test result corresponding to the first test data; if the first snapshot data is a non-empty array, it means that the functional unit subcode cannot run the data of the data type that it can run, and the code reconstruction destroys the static type of the functional unit subcode, then it can be determined that the current first test result is not the expected test result corresponding to the first test data, and the process can be terminated. Among them, the process of the test tool determining whether the current first test result is the expected test result corresponding to the first test data can be implemented by calling the toMatchSnapshot() matcher, and the toMatchSnapshot() matcher can compare the data content of the first snapshot data and the current first test result. If the first snapshot data and the current first test result are consistent, it is determined that the current first test result is the expected test result corresponding to the first test data.
[0102] It should be noted that in the disclosed embodiment of the present invention, when it is determined that the current first test result is a non-empty array, the process can be terminated and a prompt message indicating that the static type test of the functional unit sub-code has failed can be directly displayed. This can reduce the waste of data processing resources caused by subsequent processes such as obtaining the first snapshot data and determining whether the first snapshot data is an empty array, and improve the efficiency of confirming the static type correctness of the functional unit sub-code.
[0103] In an optional embodiment, if Figure 3 As shown, the process of the test tool determining that the current second test result is the expected test result corresponding to the second test data may include the following steps S301 to S303:
[0104] Step S301: Determine whether the current second test result is a non-empty array.
[0105] In an optional embodiment, the process of the test tool determining whether the current second test result is an empty array may include: obtaining the current second test result, determining whether there is an array in the current second test result, if there is no array in the current second test result, determining that the current second test result is an empty array, indicating that the functional unit sub-code can run data of a data type that it cannot run, and the code refactoring destroys the static type of the functional unit sub-code, then a prompt message indicating that the static type test of the functional unit sub-code has failed may be displayed, and the process may be terminated; if there is an array in the current second test result, determining that the current second test result is a non-empty array, indicating that the functional unit sub-code cannot run data of a data type that it cannot run, then continuing to execute step S202.
[0106] For example, the process of the test tool determining whether the current second test result is an empty array can be implemented by calling the toHaveLength(number) matcher, wherein, if the current second test result is [{…}], since the current second test result includes an array {…}, the current second test result output by the toHaveLength(number) matcher is toHaveLength(1), indicating that the current second test result is a non-empty array; if the current second test result is [], since there is no array in the current second test result, the current second test result output by the toHaveLength(number) matcher is toHaveLength(0), indicating that the current second test result is an empty array.
[0107] Step S302: If the current second test result is a non-empty array, obtain the second snapshot data.
[0108] In an embodiment of the present disclosure, the second snapshot data is snapshot data of a historical second test result. The generation process of the second snapshot data may include: in a static type test process on a functional unit subcode before code reconstruction, after using the second test data to perform a second static type test on the functional unit subcode before code reconstruction to obtain a second test result, a snapshot data file of the second test result may be generated to obtain the second snapshot data.
[0109] In an optional implementation, the process of the test tool acquiring the second snapshot data may include: acquiring the second snapshot data in a snapshot data file.
[0110] Step S303: If it is determined that the second snapshot data is the same as the current second test result, then determine that the current second test result is the expected test result corresponding to the second test data.
[0111] In an optional embodiment, the process by which the testing tool determines that the second snapshot data is the same as the current second test result may include: if the second snapshot data is a non-empty array, determining whether the array corresponding to the second snapshot data is the same as the array length corresponding to the current second test result; if the array corresponding to the second snapshot data is not the same as the array length corresponding to the current second test result, determining that the second snapshot data is not the same as the current second test result, determining that the current second test result is not the expected test result corresponding to the second test data, and ending the process, which can reduce the amount of data processing.
[0112] If the length of the array corresponding to the second snapshot data is the same as the length of the array corresponding to the current second test result, then continue to determine whether the strings in the array corresponding to the second snapshot data and the array corresponding to the current second test result are consistent; if the array corresponding to the second snapshot data is consistent with the string in the array corresponding to the current second test result, then determine that the second snapshot data is the same as the current second test result, and when it is determined that the specific information of the second snapshot data and the current second test result are consistent, determine that the second snapshot data is the same as the current second test result, so as to improve the accuracy of determining the correctness of the static type of the functional unit subcode. Among them, the process of the test tool determining whether the strings in the array corresponding to the second snapshot data and the array corresponding to the current second test result are consistent can be implemented by calling the toMatchSnapshot() matcher, and the toMatchSnapshot() matcher can compare the data content of the second snapshot data and the current second test result. If the second snapshot data is consistent with the current second test result, determine that the current second test result is the expected test result corresponding to the second test data. It can be understood that if the second snapshot data is an empty array, it means that the static type of the functional unit subcode before the code reconstruction is incorrect, and a corresponding prompt information can be displayed, which can be an error prompt information.
[0113] It should be noted that in the embodiment of the present disclosure, when it is determined that the current second test result is an empty array, the process can be terminated, and it can be directly determined that the static type of the functional unit sub-code is incorrect. This can reduce the waste of data processing resources caused by subsequent acquisition of the second snapshot data and determination of whether the second snapshot data is the same as the current second test result, and improve the efficiency of confirming the static type correctness of the functional unit sub-code.
[0114] In an optional implementation, in the embodiment of the present disclosure, if the testing tool determines that the static type of the functional unit subcode is incorrect, it can display a static type error prompt message of the functional unit subcode, and the tester can determine whether the current first test result and the current second test result meet the actual expectations by viewing the current first test result and the current second test result. If it is determined that the current first test result and the current second test result do not meet the actual expectations, it can be determined that the static type detection for the functional unit subcode has failed. If it is determined that the current first test result and the current second test result meet the actual expectations, the first snapshot data and the second snapshot data can be updated. The testing tool can respond to the update operation of the first snapshot data by updating the first snapshot data using the current first test result; and respond to the update operation of the second snapshot data by updating the first snapshot data using the current second test result.
[0115] Among them, the process of updating the first snapshot data using the current first test result may include: determining the storage location of the first snapshot data, and replacing the first snapshot data in the first snapshot data storage location with the current first test result; the process of updating the first snapshot data using the current second test result may include: determining the storage location of the second snapshot data, and replacing the second snapshot data in the second snapshot data storage location with the current second test result.
[0116] In an optional implementation, the test tool may receive a snapshot update instruction, and in response to the snapshot update instruction, update the first snapshot data to the current first test result, and update the second snapshot data to the current second test result. It is understandable that after the functional unit subcode in the target code is reconstructed again, the correctness of the static type of the reconstructed functional unit subcode may be determined by using the test results and the updated snapshot data during the next code test.
[0117] For example, assume that the test tool written based on the Jest test framework is tkit / type-test, the target code to be tested is written in the TypeScript programming language, the file name of the target code is test.ts, and the target code test.ts contains a functional unit subcode T1 corresponding to a function for performing a task or realizing a function. The functional unit subcode T1 needs to be tested, where the data type that the functional unit subcode T1 can run is the number type. Figure 4 As shown, the process may include:
[0118] Step S401, the testing tool tkit / type-test obtains the target code test.ts to be tested;
[0119] Step S402: The test tool tkit / type-test calls the TypeScript detection engine;
[0120] Step S403, the test tool tkit / type-test runs the first test script ok.ts corresponding to the functional unit subcode T1 based on the TypeScript detection engine, calls the functional unit subcode T1 with the first test data "1" as a parameter to perform a first static type test, and obtains the current first test result A1; and runs the second test script corresponding to the functional unit subcode T1, calls the functional unit subcode T1 with the second test data "string" as a parameter to perform a second static type test, and obtains the current second test result A2;
[0121] Step S404, the test tool tkit / type-test calls the toHaveLength(number) matcher to determine whether the current first test result A1 is an empty array, and determines whether the current second test result A2 is a non-empty array;
[0122] Step S405, if the current first test result A1 is an empty array and the current second test result A2 is a non-empty array, the test tool tkit / type-test calls the toMatchSnapshot() matcher to determine that the first snapshot data A1' is also an empty array, and determines that the second snapshot data A2' is also a non-empty array, and the character string in the non-empty array of the second snapshot data A2' is consistent with the character string in the non-empty array of the current second test result A2, then it is determined that the static type of the functional unit subcode is correct.
[0123] Step S406: If the test tool tkit / type-test determines that the static type of the functional unit subcode is incorrect, a static type error prompt message of the functional unit subcode may be displayed.
[0124] In the disclosed embodiment, the tester can view the current first test result and the current second test result based on the static type error prompt information of the functional unit sub-code, and determine whether the current first test result and the current second test result meet the actual expectations. If they meet the actual expectations, a snapshot update instruction can be entered in the test tool.
[0125] Step S407 : After receiving the snapshot update instruction, the test tool tkit / type-test updates the first snapshot data A1 ′ to the current first test result A1 and updates the second snapshot data A2 ′ to the current second test result A2 in response to the snapshot update instruction.
[0126] Figure 5 is a block diagram of a test device according to an exemplary embodiment. Figure 5 The testing device 500 includes an acquisition module 501 , a testing module 502 and a determination module 503 .
[0127] An acquisition module 501 is configured to acquire a target code to be tested, wherein the target code includes a functional unit subcode;
[0128] The testing module 502 is configured to perform a first static type test on the functional unit subcode using first test data to obtain a current first test result, and to perform a second static type test on the functional unit subcode using second test data to obtain a current second test result, wherein the data type of the first test data is a data type that can be run by the functional unit subcode, and the data type of the second test data is a data type that cannot be run by the functional unit subcode;
[0129] The determination module 503 is configured to determine that the static type of the functional unit subcode is correct if it is determined that the current first test result is the expected test result corresponding to the first test data and the current second test result is the expected test result corresponding to the second test data.
[0130] Optionally, the determination module 503 is configured to:
[0131] If the current first test result is an empty array, the first snapshot data is obtained, and the first snapshot data is the snapshot data of the historical first test result;
[0132] If the first snapshot data is an empty array, it is determined that the current first test result is an expected test result corresponding to the first test data.
[0133] Optional, such as Figure 5 As shown, the testing device 500 further includes:
[0134] The updating module 504 is configured to update the first snapshot data using the current first test result in response to an update operation on the first snapshot data.
[0135] Optionally, the determination module 503 is configured to:
[0136] If the current second test result is a non-empty array, the second snapshot data is obtained, and the second snapshot data is the snapshot data of the historical second test result;
[0137] If it is determined that the second snapshot data is the same as the current second test result, the current second test result is determined to be the expected test result corresponding to the second test data.
[0138] Optionally, determination module 503 determines that the second snapshot data is the same as the current second test result, including: the second snapshot data is a non-empty array, the array corresponding to the second snapshot data is the same length as the array corresponding to the current second test result, and the array corresponding to the second snapshot data is consistent with the string in the array corresponding to the current second test result.
[0139] Optionally, the updating module 504 is configured to update the first snapshot data using the current second test result in response to an update operation on the second snapshot data.
[0140] Optionally, the test module 502 is configured to:
[0141] Run the first test script corresponding to the functional unit sub-code, call the functional unit sub-code with the first test data as a parameter to perform a first static type test, and obtain a current first test result.
[0142] Optionally, the test module 502 is configured to:
[0143] Run the second test script corresponding to the functional unit sub-code, call the functional unit sub-code with the second test data as a parameter to perform a second static type test, and obtain the current second test result.
[0144] Regarding the device in the above embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated here.
[0145] The exemplary embodiment of the present disclosure also provides an electronic device, which may be a code testing device. Figure 6 The electronic device is described. It should be understood that the following reference Figure 6 The electronic device is described. It should be understood that Figure 6 The electronic device 600 shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0146] like Figure 6 As shown, the electronic device 600 is in the form of a general computing device. The components of the electronic device 600 may include but are not limited to: at least one processing unit 610, at least one storage unit 620, and a bus 630 connecting different system components (including the storage unit 620 and the processing unit 610).
[0147] The storage unit stores program codes, which can be executed by the processing unit 610, so that the processing unit 610 performs the steps according to various exemplary embodiments of the present invention described in the above “Exemplary Method” section of this specification. For example, the processing unit 610 can perform the following steps: Figure 2 The method steps shown, etc.
[0148] The storage unit 620 may include a volatile storage unit, such as a random access storage unit (RAM) 621 and / or a cache storage unit 622 , and may further include a read-only storage unit (ROM) 623 .
[0149] The storage unit 620 may also include a program / utility 624 having a set (at least one) of program modules 625, such program modules 625 including but not limited to: an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment.
[0150] The bus 630 may include a data bus, an address bus, and a control bus.
[0151] The electronic device 600 may also communicate with one or more external devices 700 (e.g., keyboards, pointing devices, Bluetooth devices, etc.), and such communication may be performed via an input / output (I / O) interface 640. The electronic device 600 may also communicate with one or more networks (e.g., local area networks (LANs), wide area networks (WANs), and / or public networks, such as the Internet) via a network adapter 650. As shown, the network adapter 650 communicates with other modules of the electronic device 600 via a bus 630. It should be understood that, although not shown in the figure, other hardware and / or software modules may be used in conjunction with the electronic device 600, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems, etc.
[0152] It should be noted that, although several modules or units of the device for action execution are mentioned in the above detailed description, this division is not mandatory. In fact, according to the exemplary embodiments of the present 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 into multiple modules or units to be embodied.
[0153] It will be appreciated by those skilled in the art that various aspects of the present disclosure may be implemented as systems, methods or program products. Therefore, various aspects of the present disclosure may be specifically implemented in the following forms, namely: complete hardware implementation, complete software implementation (including firmware, microcode, etc.), or implementations combining hardware and software aspects, which may be collectively referred to herein as "circuit", "module" or "system". Those skilled in the art will readily think of other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. The present application is intended to cover any variations, uses or adaptive changes of the present disclosure, which follow the general principles of the present disclosure and include common knowledge or customary technical means in the art that are not disclosed in the present disclosure. The specification and implementation are intended to be exemplary only, and the true scope and spirit of the present disclosure are indicated by the claims.
[0154] In addition, the present disclosure also provides a computer-readable storage medium. When instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device can execute the test method provided in the above embodiment.
[0155] In addition, the present disclosure also provides a computer program product, including computer instructions, which, when executed on an electronic device, causes the electronic device to execute the test method provided in the above embodiment.
[0156] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary techniques in the art that are not disclosed in the present disclosure. The specification and examples are intended to be exemplary only, and the true scope and spirit of the present disclosure are indicated by the following claims.
[0157] It should be understood that the present disclosure is not limited to the exact structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.
Claims
1. A testing method, characterized in that: include: Obtaining a target code to be tested, wherein the target code includes a functional unit subcode; Performing a first static type test on the functional unit subcode using first test data to obtain a current first test result, and performing a second static type test on the functional unit subcode using second test data to obtain a current second test result, wherein the data type of the first test data is a data type that can be run by the functional unit subcode, and the data type of the second test data is a data type that cannot be run by the functional unit subcode; If it is determined that the current first test result is the expected test result corresponding to the first test data, and it is determined that the current second test result is the expected test result corresponding to the second test data, then it is determined that the static type of the functional unit subcode is correct; Among them, determining that the current first test result is the expected test result corresponding to the first test data includes: if the current first test result is an empty array, obtaining first snapshot data, the first snapshot data being the snapshot data of the historical first test result; if the first snapshot data is an empty array, determining that the current first test result is the expected test result corresponding to the first test data.
2. The testing method according to claim 1, characterized in that: The method further comprises: In response to an update operation on the first snapshot data, the first snapshot data is updated using the current first test result.
3. The testing method according to claim 1, characterized in that: The determining that the current second test result is an expected test result corresponding to the second test data includes: If the current second test result is a non-empty array, obtaining second snapshot data, where the second snapshot data is snapshot data of the historical second test result; If it is determined that the second snapshot data is the same as the current second test result, the current second test result is determined to be an expected test result corresponding to the second test data.
4. The testing method according to claim 3, characterized in that: The second snapshot data is the same as the current second test result, including: The second snapshot data is a non-empty array, the array corresponding to the second snapshot data has the same length as the array corresponding to the current second test result, and the character strings in the array corresponding to the second snapshot data and the array corresponding to the current second test result are consistent.
5. The testing method according to claim 3, characterized in that: The method further comprises: In response to an update operation on the second snapshot data, the first snapshot data is updated using the current second test result.
6. The testing method according to claim 1, characterized in that: The step of performing a first static type test on the functional unit subcode using the first test data to obtain a current first test result includes: Run a first test script corresponding to the functional unit sub-code, call the functional unit sub-code with the first test data as a parameter to perform a first static type test, and obtain a current first test result.
7. The testing method according to claim 1, characterized in that: The performing a second static type test on the functional unit subcode using the second test data to obtain a current second test result includes: Run a second test script corresponding to the functional unit sub-code, call the functional unit sub-code with the second test data as a parameter to perform a second static type test, and obtain a current second test result.
8. A testing device, characterized in that: include: An acquisition module is configured to acquire a target code to be tested, wherein the target code includes a functional unit subcode; a test module configured to perform a first static type test on the functional unit subcode using first test data to obtain a current first test result, and to perform a second static type test on the functional unit subcode using second test data to obtain a current second test result, wherein a data type of the first test data is a data type that can be run by the functional unit subcode, and a data type of the second test data is a data type that cannot be run by the functional unit subcode; a determination module configured to determine that the static type of the functional unit subcode is correct if it is determined that the current first test result is an expected test result corresponding to the first test data, and if it is determined that the current second test result is an expected test result corresponding to the second test data; Among them, determining that the current first test result is the expected test result corresponding to the first test data includes: if the current first test result is an empty array, obtaining first snapshot data, the first snapshot data being the snapshot data of the historical first test result; if the first snapshot data is an empty array, determining that the current first test result is the expected test result corresponding to the first test data.
9. The testing device according to claim 8, characterized in that: The device also includes: The updating module is configured to update the first snapshot data using the current first test result in response to an update operation on the first snapshot data.
10. The testing device according to claim 8, characterized in that: The determination module is configured to: If the current second test result is a non-empty array, obtaining second snapshot data, where the second snapshot data is snapshot data of the historical second test result; If it is determined that the second snapshot data is the same as the current second test result, the current second test result is determined to be an expected test result corresponding to the second test data.
11. The testing device according to claim 10, characterized in that: The determination module determines that the second snapshot data is the same as the current second test result, including: the second snapshot data is a non-empty array, the array corresponding to the second snapshot data is the same length as the array corresponding to the current second test result, and the array corresponding to the second snapshot data is consistent with the character string in the array corresponding to the current second test result.
12. The testing device according to claim 10, characterized in that: The device also includes: The updating module is configured to update the first snapshot data using the current second test result in response to an update operation on the second snapshot data.
13. The testing device according to claim 8, characterized in that: The test module is configured as follows: Run a first test script corresponding to the functional unit sub-code, call the functional unit sub-code with the first test data as a parameter to perform a first static type test, and obtain a current first test result.
14. The testing device according to claim 8, characterized in that: The test module is configured as follows: Run a second test script corresponding to the functional unit sub-code, call the functional unit sub-code with the second test data as a parameter to perform a second static type test, and obtain a current second test result.
15. An electronic device, characterized in that: include: processor; a memory for storing instructions executable by the processor; The processor is configured to execute the instructions to implement the testing method according to any one of claims 1 to 7.
16. A computer-readable storage medium, characterized in that: When the instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to perform the testing method according to any one of claims 1 to 7.
17. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the testing method according to any one of claims 1 to 7 is implemented.
