A test case generation method and apparatus
By slicing and traversing the code of the urban rail interlocking software, test cases are automatically generated, which solves the problem of tedious and expensive manual test case setting in white-box testing, and improves the efficiency and reliability of testing.
Patent Information
- Application Number
- CN202210220682.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-08
- Publication Date
- 2025-12-02
- Estimated Expiration
- 2042-03-08
AI Technical Summary
In white-box testing of urban rail interlocking software, test case writing is cumbersome and prone to errors, regression testing is difficult when multiple people collaborate, and manual setup is labor-intensive and expensive.
By determining the test case format of the testing tool, the interlocking software code is sliced using the testing tool to determine multiple unit conditions. Each unit condition is then traversed based on the test case format to generate test cases, including generating unit test sequences and preset conditions. Regular expressions are used to extract the test case format.
It simplifies manual operations, reduces the time testers spend manually setting up test cases, improves the reliability of test cases, prevents missed tests, and reduces labor costs.
Smart Images

Figure CN114721939B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a method and apparatus for generating test cases. Background Technology
[0002] Currently, when conducting white-box testing of urban rail interlocking software, testers need to create sequences based on the tools they use and manually write test cases.
[0003] If the test code is long and the logic is complex, the process of writing test cases will be extremely tedious and prone to errors. Furthermore, when there are multiple testers, different testing habits will make subsequent regression testing more difficult even after the test is completed.
[0004] White-box testing of urban rail interlocking codes requires manual test case setup, which consumes a lot of manpower and is expensive. Summary of the Invention
[0005] To address the problems existing in the prior art, embodiments of the present invention provide a test case generation method and apparatus.
[0006] This invention provides a test case generation method, comprising: determining the test case format of a test tool;
[0007] The test tool is used to slice the code of the interlocking software to determine multiple unit conditions of the interlocking software;
[0008] Based on the test case format, the test tool is used to iterate through the conditions of each unit to determine the test cases for the interlocking software.
[0009] According to a test case generation method provided by the present invention, the step of determining test cases for the interlocking software by traversing each unit condition using the test tool based on the test case format includes:
[0010] The testing tool is used to iterate through each unit condition to determine the preset conditions for each unit condition.
[0011] Based on the test case format, and according to the preset conditions of each unit condition, the test cases for the interlocking software are determined.
[0012] According to a test case generation method provided by the present invention, the step of using the testing tool to traverse each unit condition and determine the preset conditions of each unit condition includes:
[0013] Based on the function of each unit condition, the test tool is used to generate a unit test sequence for each unit condition;
[0014] The conditions of each unit are traversed using the unit test sequence to determine the preset conditions of each unit.
[0015] According to a test case generation method provided by the present invention, the step of using the testing tool to slice the code of interlocking software and determine multiple unit conditions of the interlocking software includes:
[0016] Input the code of the interlocking software into the testing tool;
[0017] Based on the conditions in the interlocking software code, the testing tool is used to divide the interlocking software code into multiple code segments.
[0018] Based on the multiple code segments, multiple unit conditions of the interlocking software are determined.
[0019] According to a test case generation method provided by the present invention, determining the test case format of the test tool includes:
[0020] Regular expressions are used to extract the format of test cases from historical test sequences to obtain the test case format; the historical test sequences are generated by the testing tool.
[0021] The use case formats include: stub function assignment, global variable creation, or global variable assignment.
[0022] According to a test case generation method provided by the present invention, after determining the test cases for the interlocking software, the method further includes:
[0023] Based on the aforementioned testing tool, the interlocking software is tested using the aforementioned test cases, and test results are obtained.
[0024] If the test results meet the preset conditions, the interlocking software test is deemed complete.
[0025] The present invention also provides a test case generation apparatus, comprising:
[0026] The module is used to determine the test case format for the testing tools;
[0027] The slicing module is used to slice the code of the interlocking software using the testing tool to determine multiple unit conditions of the interlocking software.
[0028] The traversal module is used to traverse each unit condition based on the test case format and the test tool to determine the test cases for the interlocking software.
[0029] The present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the test case generation method as described above.
[0030] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the test case generation method as described above.
[0031] The present invention also provides a computer program product, including a computer program that, when executed by a processor, implements the test case generation method as described above.
[0032] The test case generation method and apparatus provided by this invention automatically set the pre-conditions for test cases for each logical path corresponding to the code by slicing and traversing the code. This simplifies manual operation, reduces the time spent by testers in manually setting test cases, and prevents testers from missing tests during manual testing, thereby improving the reliability of test cases. Attached Figure Description
[0033] To more clearly illustrate the technical solutions in this invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0034] Figure 1 This is one of the flowcharts illustrating the test case generation method provided by the present invention;
[0035] Figure 2 This is the second flowchart of the test case generation method provided by the present invention;
[0036] Figure 3 This is a schematic diagram of the test case generation device provided by the present invention;
[0037] Figure 4 This is a schematic diagram of the structure of the electronic device provided by the present invention. Detailed Implementation
[0038] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.
[0039] It should be noted that in the description of the embodiments of the present invention, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element. The terms "upper," "lower," etc., indicating orientation or positional relationships based on the orientation or positional relationships shown in the accompanying drawings, are only for the convenience of describing the present invention and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation, and therefore should not be construed as a limitation of the present invention. Unless otherwise expressly specified and limited, the terms "installed," "connected," and "linked" should be interpreted broadly, for example, they can refer to a fixed connection, a detachable connection, or an integral connection; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; they can refer to the internal communication of two elements. Those skilled in the art can understand the specific meaning of the above terms in this invention according to the specific circumstances.
[0040] White-box testing, also known as structural testing, transparent box testing, logic-driven testing, or code-based testing, is a test case design methodology. The "box" refers to the software being tested, and "white-box" means the box is visible, meaning its internal components and how it functions are clearly understood.
[0041] The "white-box" method provides a comprehensive understanding of the program's internal logical structure and tests all logical paths. The "white-box" method is an exhaustive path testing approach. When using this method, testers must examine the program's internal structure, starting with its logic, to derive test data. The number of independent paths traversing the program is astronomical.
[0042] Computer Interlocking (CI) is an automatic control system that integrates computer technology, network communication technology, and modern control technology to centrally operate power switches and color light signals and other signaling equipment through electronic information transmission, thereby realizing the function of controlling the signaling system of a station.
[0043] A test case is a description of a specific software product testing task, reflecting the test plan, methods, techniques, and strategies. Its content includes test objectives, test environment, input data, test steps, expected results, test scripts, etc., ultimately forming a document. As software functionality increases, the number of required test cases also increases.
[0044] To address the issues of high cost, excessive testing time, and inconsistent formats associated with white-box testing, this invention provides a method for large-scale automatic test case generation. By traversing the code, it automatically sets pre-defined conditions for test cases for each logical path corresponding to a given condition, which are then reviewed and approved by testers. This approach aims to reduce the significant time spent by testers manually setting test cases.
[0045] The following is combined with Figures 1 to 4 This invention describes the test case generation method and apparatus provided in the embodiments of the present invention.
[0046] Figure 1 This is one of the flowcharts illustrating the test case generation method provided by this invention, such as... Figure 1 As shown, including but not limited to the following steps:
[0047] First, in step S1, the test case format of the test tool is determined.
[0048] The testing tool is a white-box testing tool; the test case format is a fixed format of the existing sequence of the testing tool. Before generating white-box test cases for the urban rail interlocking software (hereinafter referred to as: interlocking software), it is necessary to confirm the white-box testing tool to be used and organize the test cases according to the format of the test sequence generated by the tool. Generally, the test cases generated by the testing tool are in a uniform format. If they are in a uniform format, this method can be used to generate subsequent test cases.
[0049] For testing tools used to test interlocking software, the test cases in existing historical test sequences generated by the testing tool are analyzed to confirm that the format of the preconditions in the historical test sequences is a test case format, such as stub function assignment, global variable creation, and global variable assignment. The preconditions are located at fixed positions in the test sequences and can be extracted using regular expressions.
[0050] The test case format should be one that the testing tool can recognize, such as txt format.
[0051] Furthermore, in step S2, the test tool is used to slice the code of the interlocking software to determine multiple unit conditions of the interlocking software.
[0052] The code of the interlocking software is input into the testing tool. The testing tool is used to search and judge the if conditions in the code of the interlocking software. Based on the searched if conditions, the code of the interlocking software is split into multiple code blocks, and each code block is determined as a unit condition, thus obtaining multiple unit conditions.
[0053] Further, in step S3, based on the test case format, the test tool is used to traverse the conditions of each unit to determine the test cases for the interlocking software.
[0054] In the testing tool, the test sequence is used to iterate through the conditions of each unit, and the results of the iteration are placed in a location that the testing tool can read. The testing tool then generates test cases for testing the interlocking software.
[0055] The test case generation method provided by this invention automatically sets pre-conditions for test cases for each logical path corresponding to the code by slicing and traversing the code. This simplifies manual operation, reduces the time spent by testers in manually setting test cases, and prevents testers from missing tests during manual testing, thereby improving the reliability of the test.
[0056] Optionally, determining the test case format for the testing tool includes:
[0057] Regular expressions are used to extract the format of test cases from historical test sequences to obtain the test case format; the historical test sequences are generated by the testing tool.
[0058] The use case formats include: stub function assignment, global variable creation, or global variable assignment.
[0059] Specifically, to ensure that the test cases exported by the testing tool are files that can be opened with a TXT text document, it is necessary to import the files generated by the testing tool into the testing tool to unify the format of the final test cases. Preconditions are located at fixed positions in the test sequence and can be extracted using regular expressions.
[0060] Test cases can be in .txt format.
[0061] The test case generation method provided by this invention can effectively solve the problem of inconsistent test formats by standardizing the test format.
[0062] Optionally, the step of using the testing tool to slice the code of the interlocking software and determine multiple unit conditions of the interlocking software includes:
[0063] Input the code of the interlocking software into the testing tool;
[0064] Based on the conditions in the interlocking software code, the testing tool is used to divide the interlocking software code into multiple code segments.
[0065] Based on the multiple code segments, multiple unit conditions of the interlocking software are determined.
[0066] White-box testing uses multi-carrier phased coding (MCPC) to iterate through all possibilities of the software under test. If the interlocking software code contains if conditions, it's necessary to iterate through all possible combinations of those if conditions.
[0067] Input the interlocking software code into the testing tool and retrieve the content of each if condition in the code. Conditions such as OR and AND can be split into multiple cases, that is, split into multiple unit conditions. The splitting rules can follow the splitting rules commonly used in white-box testing, such as statement coverage, decision coverage, decision condition coverage, and path coverage.
[0068] Statement coverage is a test case that ensures that every executable statement in a program is executed once.
[0069] Decision coverage, also known as branch coverage, requires that when designing test cases for decision statements, two cases must be designed: the result of the decision statement is true and the result is false.
[0070] Fixed condition coverage, also known as branch condition coverage, is a test case design that ensures that all possible results of each conditional expression in a decision statement occur at least once, and that all possible results of each decision statement itself also occur at least once.
[0071] Path coverage refers to designing test cases that cover all possible execution paths in the program.
[0072] For example, if we use two conditions, A and B, to perform a calculation, for if(A and B), according to permutations and combinations, we need to traverse 4 cases.
[0073] Optionally, the step of determining test cases for the interlocking software by traversing each unit condition using the testing tool based on the test case format includes:
[0074] The testing tool is used to iterate through each unit condition to determine the preset conditions for each unit condition.
[0075] Based on the test case format, and according to the preset conditions of each unit condition, the test cases for the interlocking software are determined.
[0076] Optionally, the step of using the testing tool to traverse each unit condition and determine the preset conditions for each unit condition includes:
[0077] Based on the function of each unit condition, the test tool is used to generate a unit test sequence for each unit condition;
[0078] The conditions of each unit are traversed using the unit test sequence to determine the preset conditions of each unit.
[0079] For example, if(A and B and C) needs to automatically set 4 cases, namely (T and T and T), (T and T and F), (T and F and X), (F and X && X), where A, B, and C are the unit conditions in the interlocking software code, T represents the condition is true (TRUE), F represents the condition is false (FALSE), and X represents no need to set.
[0080] The preconditions for each unit condition are generated according to the test case format, and the preconditions for each unit condition are placed in a location that the test tool can read, so that the test tool can generate test cases.
[0081] The test case generation method provided by this invention can iterate through the required test conditions one by one, preventing testers from missing tests when manually testing, reducing the workload of testers, and saving resources.
[0082] Optionally, after determining the test cases for the interlocking software, the method further includes:
[0083] Based on the aforementioned testing tool, the interlocking software is tested using the aforementioned test cases, and test results are obtained.
[0084] If the test results meet the preset conditions, the interlocking software test is deemed complete.
[0085] White-box testing requires iterating through all the code and conditions during the testing process to achieve the testing objective.
[0086] However, due to the complexity of the interlocking software code and the potential presence of restricted areas, supplementary testing and verification of these restricted areas are necessary to identify and address any gaps until the test results meet preset conditions. These preset conditions may include: 100% code coverage, 100% branch coverage, and 100% correction decision condition coverage (MC / DC).
[0087] The test case generation method provided by this invention can effectively avoid missed tests of interlocking software and ensure the reliability of the test.
[0088] Figure 2 This is the second flowchart of the test case generation method provided by the present invention, as shown below. Figure 2 As shown, it includes:
[0089] First, analyze the existing sequences of the testing tool. Existing sequences can be historical test sequences generated by the testing tool.
[0090] Furthermore, if the existing sequence is not in a fixed format, it is determined that the test case generation method provided by this invention cannot be used; if the existing sequence is in a fixed format, it is determined that the format to which the existing sequence belongs is a preset condition of the test tool.
[0091] Further, the code of the interlocking software to be tested is input into the testing tool;
[0092] Furthermore, the input code is sliced to obtain multiple unit conditions, and each unit condition is iterated to obtain the preset conditions of each unit condition.
[0093] Furthermore, based on the pre-defined conditions of each unit condition, the test tools are used to bring the pre-defined conditions into the test sequence to generate test cases.
[0094] Furthermore, testers will use the test cases to identify and address any shortcomings in the interlocking software testing until the interlocking software testing is completed.
[0095] The test case generation apparatus provided by the present invention is described below. The test case generation apparatus described below and the test case generation method described above can be referred to in correspondence.
[0096] Figure 3 This is a schematic diagram of the test case generation device provided by the present invention, as shown below. Figure 3 As shown, it includes at least:
[0097] Module 301 is used to determine the test case format for the testing tools;
[0098] The slicing module 302 is used to slice the code of the interlocking software using the test tool to determine multiple unit conditions of the interlocking software;
[0099] The traversal module 303 is used to traverse each unit condition based on the test case format and the test tool to determine the test cases for the interlocking software.
[0100] First, module 301 determines the test case format for the test tool.
[0101] The testing tool is a white-box testing tool; the test case format follows the fixed format of the tool's existing sequences. Before generating white-box test cases for interlocking software, it is necessary to confirm the white-box testing tool being used and organize the test cases according to the format of the test sequences generated by the tool. Generally, the test cases generated by the testing tool are in a uniform format. If they are in a uniform format, this method can be used for subsequent test case generation.
[0102] For the testing tool used to test interlocking software, the test cases in the existing test sequences generated by the tool are analyzed to confirm that the format of the preconditions in the existing test sequences is a test case format, such as stub function assignment, global variable creation, and global variable assignment. The preconditions are located at fixed positions in the test sequences and can be extracted using regular expressions.
[0103] The test case format should be one that the testing tool can recognize, such as txt format.
[0104] Furthermore, the slicing module 302 uses the testing tool to slice the code of the interlocking software to determine multiple unit conditions of the interlocking software.
[0105] The code of the interlocking software is input into the testing tool. The testing tool is used to search and judge the if conditions in the code of the interlocking software. Based on the searched if conditions, the code of the interlocking software is split into multiple code blocks, and each code block is determined as a unit condition, thus obtaining multiple unit conditions.
[0106] Furthermore, based on the test case format, the traversal module 303 uses the test tool to traverse each unit condition to determine the test cases for the interlocking software.
[0107] In the testing tool, the test sequence is used to iterate through the conditions of each unit, and the results of the iteration are placed in a location that the testing tool can read. The testing tool then generates test cases for testing the interlocking software.
[0108] The test case generation device provided by this invention automatically sets pre-conditions for test cases for each logical path corresponding to the code by slicing and traversing the code. This simplifies manual operation, reduces the time spent by testers in manually setting test cases, and prevents testers from missing tests during manual testing, thereby improving the reliability of the test.
[0109] It should be noted that the test case generation device provided in this embodiment of the invention can be implemented based on the test case generation method described in any of the above embodiments during specific execution, and this embodiment will not elaborate on this.
[0110] Figure 4This is a schematic diagram of the structure of the electronic device provided by the present invention, such as... Figure 4 As shown, the electronic device may include a processor 410, a communications interface 420, a memory 430, and a communication bus 440, wherein the processor 410, communications interface 420, and memory 430 communicate with each other via the communication bus 440. The processor 410 can call logical instructions in the memory 430 to execute a test case generation method, which includes: determining the test case format of the testing tool; using the testing tool to slice the code of the interlocking software to determine multiple unit conditions of the interlocking software; and based on the test case format, using the testing tool to traverse each unit condition to determine the test cases for the interlocking software.
[0111] Furthermore, the logical instructions in the aforementioned memory 430 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, essentially, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0112] On the other hand, the present invention also provides a computer program product, the computer program product comprising a computer program stored on a non-transitory computer-readable storage medium, the computer program comprising program instructions, wherein when the program instructions are executed by a computer, the computer is able to execute the test case generation method provided by the above methods, the method comprising: determining the test case format of a test tool; using the test tool to slice the code of interlocking software to determine multiple unit conditions of the interlocking software; and based on the test case format, using the test tool to traverse each unit condition to determine test cases for the interlocking software.
[0113] In another aspect, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon. When executed by a processor, the computer program implements the test case generation method provided in the above embodiments. The method includes: determining a test case format for a testing tool; using the testing tool to slice the code of interlocking software to determine multiple unit conditions of the interlocking software; and based on the test case format, using the testing tool to traverse each unit condition to determine test cases for the interlocking software.
[0114] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.
[0115] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.
[0116] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A test case generation method, characterized in that, include: Determine the test case format for the testing tools; The testing tool is a white-box testing tool; The test tool is used to slice the code of the interlocking software to determine multiple unit conditions of the interlocking software; Specifically, this includes: inputting the code of the interlocking software into the testing tool; dividing the code of the interlocking software into multiple code segments using the testing tool based on the conditions in the code of the interlocking software; and determining multiple unit conditions of the interlocking software based on the multiple code segments. Based on the test case format, the test tool is used to traverse each unit condition to determine the test cases for the interlocking software; specifically, this includes: generating a unit test sequence for each unit condition based on its function using the test tool; traversing each unit condition using the unit test sequence to determine the preset conditions for each unit condition; and determining the test cases for the interlocking software based on the test case format and the preset conditions for each unit condition.
2. The test case generation method according to claim 1, characterized in that, The determination of the test case format for the testing tool includes: Regular expressions are used to extract the format of test cases from historical test sequences to obtain the test case format; the historical test sequences are generated by the testing tool. The use case formats include: stub function assignment, global variable creation, or global variable assignment.
3. The test case generation method according to claim 1, characterized in that, After determining the test cases for the interlocking software, the method further includes: Based on the aforementioned testing tool, the interlocking software is tested using the aforementioned test cases, and test results are obtained. If the test results meet the preset conditions, the interlocking software test is deemed complete.
4. A test case generation device, characterized in that, include: The module is used to determine the test case format for the testing tools; The testing tool is a white-box testing tool; The slicing module is used to slice the code of the interlocking software using the testing tool to determine multiple unit conditions of the interlocking software. Specifically, this includes: inputting the code of the interlocking software into the testing tool; dividing the code of the interlocking software into multiple code segments using the testing tool based on the conditions in the code of the interlocking software; and determining multiple unit conditions of the interlocking software based on the multiple code segments. The traversal module is used to traverse each unit condition based on the test case format using the testing tool to determine the test cases for the interlocking software. Specifically, it includes: generating a unit test sequence for each unit condition based on its function using the testing tool; traversing each unit condition using the unit test sequence to determine the preset conditions for each unit condition; and determining the test cases for the interlocking software based on the test case format and the preset conditions for each unit condition.
5. An electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the test case generation method as described in any one of claims 1 to 3.
6. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the test case generation method as described in any one of claims 1 to 3.
7. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the test case generation method as described in any one of claims 1 to 3.
Citation Information
Patent Citations
Automatic testing method and device for servo system control software and storage medium
CN112506759A
Method for automatically generating Ice interface test case and test script
CN113419946A