Generation method and device of test case of Modbus RTU protocol and computer equipment
By generating and testing the combined fields, the test case accuracy and coverage of the ModbusRTU protocol are improved, and the problems of low test case accuracy and long manual operation in the prior art are solved.
Patent Information
- Application Number
- CN202411157053.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-20
- Publication Date
- 2024-12-13
AI Technical Summary
In the prior art, in the communication detection of ModbusRTU protocol, the test cases of the communication equipment are relatively low, and it takes a long time to operate manually, and the labor cost is high, making it difficult to ensure coverage.
By obtaining the set data frame, a mapping group set is generated according to the result type of each field, the field data is selected and combined, a combination field that meets the test conditions is generated, and it is tested, and it is determined as a test case.
It improves the comprehensiveness and accuracy of the test cases of the ModbusRTU protocol, reduces manual operation time, reduces labor costs, and ensures the coverage of test cases.
Smart Images

Figure CN119127685A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a method, device and computer equipment for generating test cases for a ModbusRTU protocol. Background Art
[0002] The ModbusRTU protocol is a variant of the Modbus protocol and an implementation of the Modbus protocol. Among them, RTU (Remote Terminal Unit) is a remote terminal unit. The ModbusRTU protocol defines the specification of using binary format for data transmission in serial communication. The ModbusRTU protocol is commonly used in the 485 bus. During the communication process of the communication device, abnormal data frames or interference data frames may appear due to bus interference. Due to the reliability requirement, when the communication device receives an abnormal data frame, it does not respond to the data frame; when it receives an interference data frame, it should respond normally. In order to meet this reliability requirement, it is necessary to exhaustively enumerate the normal data frames and abnormal data frames of the communication device based on the ModbusRTU protocol. Usually, the exhaustive enumeration of normal data frames and abnormal data frames requires manual operation by testers, which takes a long time, has high labor costs, and is difficult to ensure coverage. Summary of the invention
[0003] The purpose of this application is to provide a method, device and computer equipment for generating test cases of the ModbusRTU protocol, so as to solve the problem of low accuracy of test cases of communication equipment in communication detection of the ModbusRTU protocol.
[0004] In order to achieve the above-mentioned object, the first aspect of the present application provides a method for generating a test case of the ModbusRTU protocol, the method comprising:
[0005] Acquire a set first data frame, where the first data frame includes multiple fields;
[0006] Generate a mapping group set corresponding to each field according to the result type of each field, the mapping group set includes multiple mapping groups, each mapping group corresponds to a result type, and the mapping group includes a mapping relationship between the result type and multiple field data;
[0007] Select the field data in any mapping group of each field and combine them to obtain a combined field;
[0008] The combination fields are tested, and the combination fields that meet the test conditions are determined as test cases.
[0009] In the embodiment of the present application, the result type includes a first type and a second type, and a mapping group set corresponding to each field is generated according to the result type of each field, including:
[0010] For each field, obtain the set field conditions of the field;
[0011] Determine the field data that meets the set field condition as first field data corresponding to the first type, and determine the field data that does not meet the set field condition as second field data corresponding to the second type;
[0012] Establishing a first mapping group according to a mapping relationship between the first type and the first field data;
[0013] Establishing a second mapping group according to a mapping relationship between the second type and the second field data;
[0014] A mapping group set of fields is established based on the first mapping group and the second mapping group.
[0015] In the embodiment of the present application, field data in any mapping group of each field is selected and combined to obtain a combined field, including:
[0016] Select the first field data or the second field data in any mapping group of each field for iterative combination until the set iteration condition is met;
[0017] If each field of the combination field is the first field data, the combination field is determined as the first combination field;
[0018] If at least one field in the combination field is the second field data, the combination field is determined as the second combination field.
[0019] In the embodiment of the present application, the combination fields are tested, and the combination fields that meet the test conditions are determined as test cases, including:
[0020] Sending each first combination field to the communication device in sequence to perform a communication test;
[0021] If a feedback data frame for the first combination field is received from the communication device within a set time, the first combination field is determined as a test case.
[0022] In the embodiment of the present application, the combination fields are tested, and the combination fields that meet the test conditions are determined as test cases, which also includes:
[0023] If the feedback data frame is not received within the set time, determining whether the first combination field passes the communication verification;
[0024] If the first combination field passes the communication verification, marking the first combination field;
[0025] If the first combination field fails the communication verification, the test of the first combination field is stopped.
[0026] In the embodiment of the present application, the combination fields are tested, and the combination fields that meet the test conditions are determined as test cases, including:
[0027] Sending each second combination field to the communication device in sequence to perform a communication test;
[0028] If no feedback data frame for the second combination field is received from the communication device within the set time, an abnormality detection is performed on the second combination field;
[0029] If the second combination field passes the anomaly detection, determining whether the second combination field passes the communication verification;
[0030] If the second combination field passes the communication verification, the second combination field is determined as a test case.
[0031] In the embodiment of the present application, the combination fields are tested, and the combination fields that meet the test conditions are determined as test cases, which also includes:
[0032] If the feedback data frame is received within the set time, determining whether the second combination field passes the communication verification;
[0033] If the second combination field passes the communication verification, marking the second combination field;
[0034] If the second combination field fails the communication verification, the test of the second combination field is stopped.
[0035] In the embodiment of the present application, before the step of sequentially performing communication tests on the second combination fields, the method further includes:
[0036] Determining whether the second combination field complies with the set communication protocol;
[0037] If the second combination field complies with the set communication protocol, the second combination field is removed.
[0038] A second aspect of the present application provides a device for generating a test case of a ModbusRTU protocol, comprising:
[0039] A generating module, used for acquiring a set first data frame, where the first data frame includes a plurality of fields;
[0040] A mapping module, used to generate a mapping group set corresponding to each field according to the result type of each field, the mapping group set includes multiple mapping groups, each mapping group corresponds to a result type, and the mapping group includes a mapping relationship between the result type and multiple field data;
[0041] A combination module is used to select field data in any mapping group of each field and combine them to obtain a combined field;
[0042] The test module is used to test the combination fields and determine the combination fields that meet the test conditions as test cases.
[0043] A third aspect of the present application provides a computer device, including:
[0044] a memory configured to store instructions; and
[0045] The processor is configured to call instructions from the memory and implement the above-mentioned method for generating test cases of the ModbusRTU protocol when executing the instructions.
[0046] A fourth aspect of the present application provides a machine-readable storage medium having instructions stored thereon, which, when executed by a processor, configure the processor to execute the above-mentioned method for generating test cases for the ModbusRTU protocol.
[0047] The present application first obtains a set first data frame, which includes multiple fields. Then, a mapping group set corresponding to each field is generated according to the result type of each field. In the mapping group combination, each mapping group corresponds to a result type, and the mapping group includes a mapping relationship between the result type and multiple field data. Next, the field data in any mapping group of each field is selected and combined to obtain a combined field. Finally, the combined field is tested, and the combined field that meets the test conditions is determined as a test case. The present application selects the field data in any mapping group of each field in the first data frame, and then combines the field data selected from each field to obtain a combined field, and tests the combined field to obtain a test case that meets the test conditions. In this way, generating test cases based on the result type of the field can make the generated test cases of the ModbusRTU protocol more comprehensive and more accurate.
[0048] Other features and advantages of the present application will be described in detail in the subsequent detailed description. BRIEF DESCRIPTION OF THE DRAWINGS
[0049] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0050] Figure 1 A schematic diagram of an application scenario of a method for generating a test case of the ModbusRTU protocol provided in an embodiment of the present application;
[0051] Figure 2A flow chart of a method for generating a test case of the ModbusRTU protocol provided in an embodiment of the present application;
[0052] Figure 3 A schematic diagram of fields of a first data frame provided in an embodiment of the present application;
[0053] Figure 4 A flowchart of a method for testing a combination field provided in a specific embodiment of the present application;
[0054] Figure 5 A schematic diagram of the structure of a device for generating a test case of the ModbusRTU protocol provided in an embodiment of the present application;
[0055] Figure 6 This is a structural block diagram of a computer device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0056] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of this application.
[0057] In the description of the present application, it should be understood that the terms "first" and "second" are used only for descriptive purposes and cannot be understood as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Thus, the features defined as "first" and "second" may explicitly or implicitly include one or more of the features. In the description of the present application, the meaning of "multiple" is two or more, unless otherwise clearly and specifically defined. In the present application, the word "exemplary" is used to mean "used as an example, illustration or description". Any embodiment described as "exemplary" in the present application is not necessarily interpreted as being more preferred or more advantageous than other embodiments. In order to enable any technician in the field to implement and use the present application, the following description is given. In the following description, details are listed for the purpose of explanation. It should be understood that a person of ordinary skill in the art can recognize that the present application can also be implemented without using these specific details. In other examples, well-known structures and processes will not be elaborated in detail to avoid unnecessary details that make the description of the present application obscure. Therefore, the present application is not intended to be limited to the embodiments shown, but is consistent with the widest range of principles and features disclosed in the present application.
[0058] In the communication process based on ModbusRTU protocol, communication equipment may have abnormal data frames or interference data frames due to bus interference. And because in the related art, test cases are usually selected by test personnel through manual exhaustive enumeration for abnormal data frames or interference data frames, so that it is easy to respond to abnormal data frames or respond to interference data frames. The wrong response result is that it is difficult to ensure the comprehensiveness of the test case. Based on this, the embodiment of the present application provides a method for generating test cases of ModbusRTU protocol, so that the test cases of communication equipment are more comprehensive and accurate. In an example, the communication equipment in the embodiment of the present application can be a slave device, and the slave device can communicate with the host device through the bus. Wherein, the host device and the slave device can communicate based on the ModbusRTU protocol.
[0059] The method for generating a test case of the ModbusRTU protocol in the embodiment of the present application is applied to a device for generating a test case of the ModbusRTU protocol, and the device is arranged in a computer device. Figure 1 As shown, Figure 1 The present invention is a schematic diagram of an application scenario of a method for generating test cases for the ModbusRTU protocol in an embodiment of the present application. The application scenario of the method for generating test cases for the ModbusRTU protocol in an embodiment of the present application includes a computer device 100 for the method for generating test cases for the ModbusRTU protocol. A test case generating device is integrated in the computer device 100 to run a machine-readable storage medium corresponding to the method for generating test cases for the ModbusRTU protocol to execute the steps of the method for generating test cases for the ModbusRTU protocol.
[0060] Understandably, Figure 1 The computer device in the application scenario of the method for generating test cases for the ModbusRTU protocol shown, or the device included in the computer device, does not constitute a limitation on the embodiments of the present application, that is, the number of devices and types of devices included in the application scenario of the method for generating test cases, or the number of devices and types of devices included in each device do not affect the overall implementation of the technical solution in the embodiments of the present application, and can all be regarded as equivalent replacements or derivatives of the technical solution claimed to be protected in the embodiments of the present application.
[0061] The computer device 100 in the embodiment of the present application may be an independent device, or a device network or device cluster composed of devices. For example, the computer device 100 described in the embodiment of the present application includes but is not limited to a computer, a network host, a single network device, a set of multiple network devices, or a cloud device composed of multiple devices. The cloud device is composed of a large number of computers or network devices based on cloud computing.
[0062] Those skilled in the art will understand that Figure 1 The application scenario shown in the figure is only an application scenario corresponding to the technical solution of the present application, and does not constitute a limitation on the application scenario of the technical solution of the present application. Other application scenarios may also include Figure 1 More or fewer computer devices shown in, or computer device network connection relationships, such as Figure 1 Only one computer device is shown in the figure. It can be understood that the scenario of the test case generation method can also include one or more other computer devices, which is not specifically limited here.
[0063] In addition, in the application scenario of the method for generating test cases of the ModbusRTU protocol in the embodiment of the present application, the computer device 100 may be provided with a display device, or the computer device 100 may not be provided with a display device and may be communicated with an external display device 200, and the display device 200 is used to output the result of the execution of the method for generating test cases in the computer device 100. The computer device 100 may access a backend database 300. The backend database 300 may be a local memory of the computer device 100, or may be a cloud database set in the cloud, and the backend database 300 stores information related to the method for generating test cases.
[0064] It should be noted that Figure 1 The application scenario of the test case generation method for the ModbusRTU protocol shown is merely an example. The application scenario of the test case generation method described in the embodiment of the present application is to more clearly illustrate the technical solution of the embodiment of the present application, and does not constitute a limitation on the technical solution provided in the embodiment of the present application.
[0065] Based on the application scenario of the method for generating test cases of the ModbusRTU protocol, an embodiment of the method for generating test cases of the ModbusRTU protocol is proposed, which is described in detail below with reference to the accompanying drawings.
[0066] Figure 2 The following is a flow chart of a method for generating a test case of the ModbusRTU protocol provided in an embodiment of the present application. Figure 2 As shown, the method for generating a test case of the ModbusRTU protocol in the embodiment of the present application can execute steps 201-204 and other steps through the above-mentioned computer device 100, which is described in detail below.
[0067] Step 201: Acquire a set first data frame, wherein the first data frame includes multiple fields.
[0068] In an embodiment of the present application, the first data frame refers to a correct data frame that the communication device complies with the ModbusRTU protocol, which can be called a correct frame or a test seed frame. In different communication device tests, due to differences in device addresses, supported function codes, register address spaces, etc., it is necessary to use the correct frame as the basic configuration of the test, and to split and set it according to each field of the correct frame. The first data frame in the embodiment of the present application can be customized according to the requirements of the communication protocol, and the first data frame can include multiple fields, and the composition structure and field definition, i.e., the content of the field can be configured in advance. In an example, the frame format, field definition, and field content related to the communication protocol can be determined according to the requirements. Then define the basic structure of the first data frame, including the required fields and data types. Then, assign an initial value to each field, thereby obtaining the set first data frame.
[0069] Figure 3 Schematic diagram of the fields of a first data frame provided in an embodiment of the present application. Figure 3 Taking the communication device as a slave device in the ModbusRTU communication protocol as an example, the slave device can receive the first data frame sent by the host device. The fields in the first data frame may include: a pre-frame interference field, a post-frame interference field, a device address field, a function code field, a content field, and a CRC16 check field.
[0070] Wherein, the interference field before the frame and the interference field after the frame include interference data that often occurs in communication, and the communication device needs to filter out the required data from the data with interference as much as possible. In the Modbus communication address, device address, broadcast address, multicast address, etc. may be included. The embodiment of the present application takes the field representing the address segment as the field corresponding to the device address as an example. The function code field is used to define the data type and operation mode of the first data frame, including function code data. Based on the function code data, the type of communication request can be obtained. In addition, the CRC16 check field is the check information specified in the ModbusRTU standard protocol. When the interference field before the frame is empty or has random length and random content, or at least a part is consistent with the first data frame but not completely consistent, it can be determined as an interference data field, which has no effect on the correctness of the data frame. Similarly, the interference field after the frame may be empty or has random length and random content, or when there is a CRC16 check field, the interference after the frame is greater than obtaining a certain byte, such as 2 bytes, and the CRC16 of the first data frame is satisfied, which has no effect on the correctness of the data frame. Therefore, such data frames can be judged as interference data frames that should be responded to normally. For the device address field and function code field, there may be a situation where the field is empty or the field data is set. When it is empty, it means that the field is an abnormal field. When it is the set field data, it means that the field is a correct field. There are three situations for the CRC16 check field: no such field, correct CRC16, and incorrect CRC16. When the CRC16 is incorrect, it means that the field is not a correct field. All other situations are abnormal fields.
[0071] In addition, the content field in the embodiment of the present application matches the function code field. Take function codes 01, 03, 05, 06, 0F and 10 as examples. Among them, the communication request corresponding to function code 01 is a read coil request. The communication request corresponding to function code 03 is a read register request. The communication request corresponding to function code 05 is a write single coil request. The communication request corresponding to function code 06 is a write single register request. The communication request corresponding to function code 0F is a write multiple coil request. The communication request corresponding to function code 10 is a write multiple register request. Each function code field has a corresponding content field that can correctly respond to the communication request, and each content field can include multiple subfields. By determining the structure of the subfield in the content field and the length of each subfield, it can be determined whether the content field meets the field length requirement. For example, the read coil request with function code 01, the corresponding content field includes the starting address and the number of coils. Therefore, the field data corresponding to the function code field can be 0×01 [1 byte], indicating that the function code field occupies 1 byte. The field data corresponding to the content field can be the starting address [2 bytes] and the number of coils [2 bytes]. It means that the starting address occupies 2 bytes and the number of coils occupies 2 bytes. The starting address and the number of coils can be obtained based on the first data frame received.
[0072] In the first data frame, if there is interference data in the interference field before the frame and the interference field after the frame, a normal response should also be performed. If the device address field, function code field, content field and CRC16 check field have empty field data or are different from the set field data, that is, when an abnormal data frame appears, no response should be made, which is specifically manifested as a communication timeout. At the same time, no abnormal response should occur, such as abnormal action, crash, restart and other responses. Based on this, the embodiment of the present application needs to generate interference data frames that have interference data but should respond normally and abnormal data frames that should not respond normally based on the first data frame.
[0073] Step 202: Generate a mapping group set corresponding to each field according to the result type of each field, wherein the mapping group set includes multiple mapping groups, each mapping group corresponds to a result type, and the mapping group includes a mapping relationship between the result type and multiple field data.
[0074] As can be seen from the above, each field can be set with a set field condition, and the set field condition indicates the judgment condition for the field data of the field to be correct field data. If the set field condition is met, the current field is correct field data, and if the set field condition is not met, it indicates that the field data of the current field is abnormal field data. In the embodiment of the present application, the result type of each field refers to the possible situation of the field data of the field, such as meeting the set field condition or not meeting the set field condition.
[0075] Taking the result type of the field as satisfying the set field condition as an example, it means that the field matches the field data set for the field in the first data frame. For example, suppose the field data set for the device address field set in the first data frame is 07. If the number that appears in the device address field is 07, it means that the field data is correct field data. On the contrary, taking the result type of the field as not satisfying the set field condition as an example, if the number that appears in the device address field is other numbers or is empty, it means that the field data is abnormal field data.
[0076] Therefore, the embodiment of the present application can generate a corresponding mapping group set based on the result type of each field. The mapping group set represents the combination of the mapping relationship between the result type and the field data. Each mapping group includes a mapping relationship between a result type and multiple field data. And each mapping group corresponds to a result type. In this way, the specific field data content can be grouped based on the possible situations of the field data of each field, so that the field data of different result types can be randomly combined later.
[0077] Step 203: Select field data in any mapping group of each field and combine them to obtain a combined field.
[0078] In the embodiment of the present application, in order to ensure the coverage of the test case, after the result type of each field is classified, the different result types of each field can be combined in a loop iteration manner to realize the traversal of the combination field composed of multiple fields. Wherein, the combination field refers to a data frame obtained by combining any one field data from any mapping group of each field in the first data frame. Taking the device address field, function code field, content field and CRC16 check field as an example, assuming that the device address field selects the correct field data, the function code field selects the wrong field data, the content field selects the correct field data, and the CRC16 check field also selects the correct field data, the formed field combination is a data frame with abnormal field data. And assuming that the device address field, function code field, content field and CRC16 check field all select the correct field data that meets the set field conditions, the field combination is a data frame without abnormal field data. The embodiment of the present application combines the field data corresponding to the result type of each field through an iterative method to realize the traversal of the permutation and combination of each field result type, thereby covering the combination of the result type of the field and improving the comprehensiveness of the combination field.
[0079] Step 204: Test the combination fields, and determine the combination fields that meet the test conditions as test cases.
[0080] In the embodiment of the present application, the generated combination field may be a data frame with abnormal field data, or may be a data frame without abnormal field data. In order to determine whether the generated combination field satisfies the response result of the communication device, it is necessary to further test the combination field to determine whether the generated combination field can be used as a test case, wherein the test case refers to the data that can verify whether the communication device is correct in the software test. The test case of the ModbusRTU protocol in the embodiment of the present application may include an interference data frame and an abnormal data frame. The interference data frame should be responded normally, that is, it should be responded by the communication device within the set time. The abnormal data frame cannot be responded, that is, the communication device should not respond within the set time. In one example, if the generated combination field is a data frame with abnormal field data, the data frame cannot be responded, if a response occurs. For example, it is responded within the set time or other abnormal responses occur, indicating that the data frame cannot be used as an abnormal data frame. In another example, if the generated combination field is a data frame without abnormal field data, the data frame should be responded normally. If no response occurs within the set time, it means that the data frame cannot be used as an interference data frame. By testing the combined fields, the correctness of the combined fields can be improved, thereby improving the accuracy of the test cases.
[0081] In summary, the embodiment of the present application selects field data in any mapping group of each field in the first data frame, and then combines the field data selected for each field to obtain a combined field, and tests the combined field to obtain a test case that meets the test conditions. In this way, by generating a test case based on the result type of the field, the generated test case of the ModbusRTU protocol can be more comprehensive and more accurate.
[0082] In an embodiment of the present application, the result type may include a first type and a second type. The first type may refer to a result type that satisfies a set field condition, and the second type may refer to a result type that does not satisfy the set field condition. In step 202, the set field condition of the field may be obtained for each field. Then, the field data that satisfies the set field condition is determined as the first field data corresponding to the first type, and the field data that does not satisfy the set field condition is determined as the second field data corresponding to the second type. Next, a first mapping group is established based on the mapping relationship between the first type and the first field data, and a second mapping group is established based on the mapping relationship between the second type and the second field data. Finally, a mapping group set for the field is established based on the first mapping group and the second mapping group.
[0083] In the embodiment of the present application, each result type may correspond to multiple field data. Still taking the above device address field as an example, assume that the field condition is set to 07. The field data corresponding to the first type of the device address field is 07, and the field data corresponding to the second type may be empty, 01, 02, 03, and other field data that is not 07. Therefore, the first mapping group may be a mapping relationship between the first type and 07, and the second mapping group may be a mapping relationship between the second type and other codes such as empty, 01, 02, 03, and the like.
[0084] Take the function code field and the content field as an example again, the field data corresponding to the first type of the function code field can be the set field data, and the field data corresponding to the second type can also be empty or other non-set field data. The content field matches the function code field, and the content field corresponding to each function code field may be different. The function codes 01, 03, 05, 06, 0F and 10 are taken as examples below. Please refer to Table 1, which is an example of a mapping group in the function code and content field in the embodiment of the present application. The mapping group includes a mapping relationship between the result type and the function code field and the content field.
[0085] Table 1
[0086]
[0087]
[0088]
[0089] Among them, the correct field data of the field corresponding to the first type is the field data that satisfies the field conditions set for each field. The field data with abnormal field length errors and field errors in the second type are field data generated randomly. It can be seen that there can be countless cases of abnormal field data corresponding to the second type. In order to ensure the normal operation of the server, the embodiment of the present application can also limit the randomly generated field data by limiting the number of random generation and random data combination. Ensure that the randomly generated field data is within the set range, thereby reducing the computing pressure of the server. Take the combination of field data for the three fields A, B, and C as an example, assuming that A and B are abnormal field data, and C is normal field data. First, set a random length range of a combined field, assuming it is 0-250. First, randomly generate the length of one of the fields. For example, if the length of the field generated by A is 100, the random length range of B is 0-150, and the field length generated by B is in 0-150, so that the total field length of AB generated will not exceed the set value of 250. In addition, if the randomly generated field length exceeds the field length of the first data frame, the filled data is randomly generated data. In this way, the computing pressure of the server can be reduced and the efficiency of random generation can be improved by limiting the length range of the randomly generated abnormal fields.
[0090] In step 203 of the embodiment of the present application, the first field data or the second field data in any mapping group of each field can be selected for iterative combination until the set iteration condition is met. If each field of the combination field is the first field data, the combination field is determined as the first combination field. If at least one field in the combination field is the second field data, the combination field is determined as the second combination field.
[0091] In an embodiment of the present application, an iterative method is used to achieve the purpose of traversing the combined fields after the permutations and combinations based on the result type of the field. Among them, setting the iteration condition refers to the condition for terminating the iteration. For example, it can be set to complete the iteration after traversing all the permutations and combinations of the result types. Taking the fields A and B as an example, assuming that each field includes a first type and a second type, respectively A1, A2, B1 and B2, wherein A1 and B1 represent the correct field data that meets the set field conditions, and A2 and B2 represent abnormal field data that does not meet the set field conditions, which can be divided into two cases of empty fields or error fields, wherein the error field can be a randomly generated data field. Assuming that the empty field data is represented by 1 and the error field data is represented by 2, the abnormal field can include A21, A22, B21 and B22. If the iteration condition is set to require all cases of different fields to be permuted and combined to complete the iteration. It is necessary to complete the traversal of the field combination: A1B1, A1B21, A1B22, A21B1, A21B21, A21B22, A22B1, A22B21 and A22B22. In an embodiment of the present application, the combination field may include a first combination field and a second combination field. The first combination field is a combination field in which each field is a correct field, and the second combination field is a combination field in which an abnormal field exists. For example, A1B1 may be the first combination field, and the other combination fields are all second combination fields. In this way, different result types of each field can be combined to obtain a corresponding combination field, thereby improving the comprehensiveness of the combination field.
[0092] In step 204 of the embodiment of the present application, the first combination field and the second combination field may be subjected to communication tests respectively. In order to determine whether the generated combination field satisfies the response result of the communication device, it is necessary to perform further communication tests on the combination field to determine whether the generated combination field can be used as a test case.
[0093] The following first describes the test of the first combination field. In step 204, each first combination field can be sent to the communication device in sequence for communication testing. The first combination field is a combination field that should respond normally. Therefore, if a feedback data frame for the first combination field is received from the communication device within a set time, the first combination field is determined as a test case. On the contrary, if the feedback data frame is not received within the set time, it means that a communication anomaly occurs in the first combination field. At this time, it is necessary to further determine whether the cause of the communication anomaly is the first combination field itself or a problem with the communication device. Therefore, it is also necessary to determine whether the first combination field passes the communication verification. If the first combination field passes the communication verification, the first combination field is marked. If the first combination field fails to pass the communication verification, the test of the first combination field is stopped. Among them, the communication verification refers to sending a data frame opposite to the response result of the current combination field to the communication device, and determining whether the cause of the anomaly is an abnormality of the communication device according to the response result. If the communication verification is passed, it means that the communication device is not abnormal and is in a normal operating state. If the communication verification is not passed, it means that the communication device is abnormal. For example, for the first combination field, communication verification refers to sending an abnormal data frame that should not be responded to to the communication device. If the abnormal data frame is responded to, it means that there is a problem with the communication device, and the first combination field has not passed the communication verification, and it is necessary to stop the test of the communication device, that is, stop the test of the first combination field. If the abnormal data frame is not responded to, it means that the communication device is operating normally and an error occurs in the first combination field. Therefore, it is necessary to mark the first combination field to remind the tester that the first combination field is wrong. Then, you can choose to terminate the test of the first combination field or continue the test of the first combination field according to the configuration. In this way, by performing a communication test on the first combination field, it can be ensured that the first combination field is a test case that can be responded to, and the accuracy of the interference data frame is guaranteed.
[0094] The following is a description of the test of the second combination field. In step 204, each second combination field can be sent to the communication device in sequence for communication testing. The second combination field is a combination field that should not respond. If the feedback data frame of the communication device for the second combination field is not received within the set time, it means that the second combination field is a suspected abnormal data frame. However, it is also necessary to determine whether the second combination field causes other abnormal responses. Therefore, it is also necessary to perform abnormal detection on the second combination field. For example, determine whether there is an abnormal action, abnormal response of crash or restart, etc. If the second combination field passes the abnormal detection, that is, there is no other abnormal response, further determine whether the second combination field passes the communication verification to ensure that the failure to respond is not caused by the failure of the communication device. At this time, a correct data frame can be sent to the communication device. If a normal response is received within the set time, it can be determined that the second combination field passes the communication verification, and the second combination field can be determined as a test case.
[0095] In addition, if a feedback data frame based on the second combination field is received within the set time, it is also necessary to further determine whether it is an error feedback caused by a communication device failure. Therefore, it is possible to further determine whether the second combination field has passed the communication verification. If the second combination field does not pass the communication verification, stop the test of the second combination field. If the second combination field passes the communication verification, mark the second combination field. Remind the tester that the second combination field is incorrect. Then, you can choose to terminate the test of the second combination field or continue the test of the second combination field according to the configuration. In this way, by performing a communication test on the second combination field, it can be ensured that the second combination field is a test case that does not respond, thereby ensuring the accuracy of the abnormal data frame.
[0096] In the embodiment of the present application, for the second combination field, there may be a situation where the combination field after the combination of multiple abnormal fields can be responded by the communication device. For example, the content field corresponding to the function code field 03 is M, and the content field corresponding to the function code field 06 is N. Both 03M and 06N can be responded by the communication device. Assuming that the function code field generated based on the second type of function code 03 and the second type of content field is 06N, although it is not 03M, it can still be responded by the communication device. Therefore, before the communication test of the second combination field, it is also necessary to detect whether the second combination field conforms to the communication protocol in advance. Therefore, in step 204 of the embodiment of the present application, before the step of performing communication test on the second combination field in turn, it can be judged whether the second combination field conforms to the set communication protocol. If the second combination field conforms to the set communication protocol, the second combination field is eliminated. Taking the Modbu communication protocol as an example, the Modbus protocol check can be run on the tool side of the computer device to ensure that the second combination field does not conform to any function code supported by the communication device, and the second combination field can be further tested for communication. By detecting the communication protocol of the second combination field, the situation where the second combination field is normally responded can be reduced, and the accuracy of the second combination field can be improved. It should be noted that the above-mentioned setting time can be customized by the tester and is not limited here.
[0097] Figure 4 This is a flow chart of a method for testing a combination field provided in a specific embodiment of the present application. Figure 4 In a specific embodiment of the present application, the method for testing the combined field may include steps 401-414.
[0098] Step 401: Set the communication parameters of the communication device. The communication parameters include a first data frame. By setting the correct communication parameters, it can be ensured that the communication device can respond to the correct data frame.
[0099] Step 402 , determine whether the first data frame passes the communication verification, if so, proceed to step 403 , otherwise, return to step 401 .
[0100] Among them, the communication verification of the first data frame can first generate an interference data frame based on the first data frame, and determine whether the interference data frame is fed back normally, that is, responds normally within the set time. If so, it can be determined as passing the communication verification, otherwise, it is determined as failing the communication verification.
[0101] Step 403: Select field data of any mapping group of each field in the first data frame and combine them to obtain a first combined field and a second combined field.
[0102] Step 404: Test the first combination fields one by one.
[0103] Step 405: determine whether it is a normal response, if so, proceed to step 412: determine it as a test case, otherwise proceed to step 406. Wherein, a normal response means that the feedback data frame is received within a set time.
[0104] Step 406, determine whether the communication verification is passed, if so, proceed to step 413, mark, otherwise, proceed to step 414 to terminate the test.
[0105] Step 407: Test the second combination fields one by one.
[0106] Step 408 , determine whether the communication has timed out, if so, proceed to step 409 , otherwise, proceed to step 411 .
[0107] Step 409, determine whether the abnormality detection is passed, if so, proceed to step 410, otherwise, proceed to step 413 and mark.
[0108] Step 410, determine whether the communication verification is passed, if so, proceed to step 412, determine it as a test case, otherwise, proceed to step 414, terminate the test.
[0109] Step 411, determine whether the communication verification is passed, if so, proceed to step 413, mark, otherwise, proceed to step 414, terminate the test.
[0110] The embodiment of the present application selects field data in any mapping group of each field in the first data frame, and then combines the field data selected for each field to obtain a combined field, and tests the combined field to obtain a test case that meets the test conditions. In this way, by generating a test case based on the result type of the field, the generated test case of the ModbusRTU protocol can be more comprehensive and more accurate.
[0111] Figure 5This is a schematic diagram of the structure of a device for generating a test case of the ModbusRTU protocol provided in an embodiment of the present application. Figure 5 , the generating device 500 may include a generating module 501, a mapping module 502, a combining module 503 and a testing module 504. The generating module 501 is used to obtain a set first data frame, and the first data frame includes multiple fields. The mapping module 502 is used to generate a mapping group set corresponding to each field according to the result type of each field. The mapping group set includes multiple mapping groups, each mapping group corresponds to a result type, and the mapping group includes a mapping relationship between the result type and multiple field data. The combining module 503 is used to select the field data in any mapping group of each field for combination to obtain a combined field. The testing module 504 is used to test the combined field, and determine the combined field that meets the test conditions as a test case.
[0112] Among them, the generation module 501, the mapping module 502, the combination module 503 and the test module 504 can be respectively used to execute steps 201-204 in the embodiment corresponding to the method for generating test cases of the above-mentioned ModbusRTU protocol. For the specific implementation methods of these modules and more details, please refer to the corresponding method part, which will not be repeated here one by one.
[0113] Figure 6 600 is a structural block diagram of a computer device 600 provided in an embodiment of the present application. Figure 6 The embodiment of the present application provides a computer device 600, which may include a memory 610 and a processor 620. The memory 610 is configured to store instructions. The processor 620 is configured to call instructions from the memory and implement the above-mentioned method for generating test cases of the ModbusRTU protocol when executing the instructions.
[0114] An embodiment of the present application also provides a machine-readable storage medium, on which instructions are stored. When the instructions are executed by a processor, the processor is configured to execute the method for generating test cases for the above-mentioned ModbusRTU protocol.
[0115] Since the instructions stored in the computer device and the machine-readable storage medium can execute the steps in any test case generation method provided in the embodiments of the present application, the beneficial effects that can be achieved by any test case generation method provided in the embodiments of the present application can be achieved. Please refer to the previous embodiments for details and will not be repeated here.
[0116] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.
[0117] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems) and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0118] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0119] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0120] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0121] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.
[0122] Computer readable media include permanent and non-permanent, removable and non-removable media, and can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated communication signals and carrier waves.
[0123] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.
[0124] The above are only embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included within the scope of the claims of the present application.
Claims
1. A method for generating test cases for the ModbusRTU protocol. It is characterized in that The method comprises: Acquire a set first data frame, where the first data frame includes a plurality of fields; Generate a mapping group set corresponding to each field according to the result type of each field, the mapping group set including multiple mapping groups, each mapping group corresponding to a result type, and the mapping group including a mapping relationship between the result type and multiple field data; Selecting field data in any mapping group of each of the fields and combining them to obtain a combined field; The combination fields are tested, and the combination fields that meet the test conditions are determined as test cases.
2. The generation method according to claim 1, It is characterized in that The result type includes a first type and a second type, and generating a mapping group set corresponding to each field according to the result type of each field includes: For each of the fields, obtaining a set field condition of the field; Determine the field data that meets the set field condition as first field data corresponding to the first type, and determine the field data that does not meet the set field condition as second field data corresponding to the second type; Establishing a first mapping group according to a mapping relationship between the first type and the first field data; Establishing a second mapping group according to a mapping relationship between the second type and the second field data; A mapping group set for the field is established based on the first mapping group and the second mapping group.
3. The generation method according to claim 2, It is characterized in that The selecting and combining the field data in any mapping group of each of the fields to obtain a combined field includes: Selecting the first field data or the second field data in any mapping group of each of the fields for iterative combination until a set iteration condition is met; If each of the fields of the combination field is the first field data, determining the combination field as the first combination field; If at least one of the fields in the combination field is the second field data, the combination field is determined as the second combination field.
4. The generation method according to claim 3, It is characterized in that The step of testing the combination fields and determining the combination fields that meet the test conditions as test cases includes: Sending each of the first combination fields to the communication device in sequence to perform a communication test; If a feedback data frame from the communication device for the first combination field is received within a set time, the first combination field is determined as the test case.
5. The generation method according to claim 4, It is characterized in that The testing of the combination fields and determining the combination fields that meet the test conditions as test cases also includes: If the feedback data frame is not received within the set time, determining whether the first combination field passes the communication verification; If the first combination field passes the communication verification, marking the first combination field; If the first combination field fails the communication verification, the test of the first combination field is stopped.
6. The generation method according to claim 3, It is characterized in that The step of testing the combination fields and determining the combination fields that meet the test conditions as test cases includes: Sending each of the second combination fields to the communication device in sequence to perform a communication test; If no feedback data frame for the second combination field is received from the communication device within a set time, performing an abnormality detection on the second combination field; If the second combination field passes the anomaly detection, determining whether the second combination field passes the communication verification; If the second combination field passes the communication verification, the second combination field is determined as the test case.
7. The generation method according to claim 6, It is characterized in that The testing of the combination fields and determining the combination fields that meet the test conditions as test cases also includes: If the feedback data frame is received within the set time, determining whether the second combination field passes the communication verification; If the second combination field passes the communication verification, marking the second combination field; If the second combination field fails the communication verification, the test of the second combination field is stopped.
8. The generation method according to claim 6, It is characterized in that Before the step of sequentially performing communication tests on the second combination fields, the method further includes: Determining whether the second combination field complies with a set communication protocol; If the second combination field complies with the set communication protocol, the second combination field is removed.
9. A device for generating test cases for ModbusRTU protocol, It is characterized in that include: A generating module, configured to obtain a set first data frame, wherein the first data frame includes a plurality of fields; A mapping module, used to generate a mapping group set corresponding to each field according to the result type of each field, wherein the mapping group set includes a plurality of mapping groups, each mapping group corresponds to a result type, and the mapping group includes a mapping relationship between the result type and a plurality of field data; A combination module, used for selecting field data in any mapping group of each of the fields for combination to obtain a combined field; The test module is used to test the combination fields and determine the combination fields that meet the test conditions as test cases.
10. A computer device, It is characterized in that include: a memory configured to store instructions; as well as A processor is configured to call the instruction from the memory and implement the method for generating a test case of the ModbusRTU protocol according to any one of claims 1 to 8 when executing the instruction.
11. A machine-readable storage medium, It is characterized in that The machine-readable storage medium stores instructions, which, when executed by a processor, enable the processor to be configured to execute a method for generating a test case for the ModbusRTU protocol according to any one of claims 1 to 8.
