Method, testing method, device and storage medium for generating test code
Patent Information
- Application Number
- CN202310997656.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-08-09
- Publication Date
- 2026-09-25
- Estimated Expiration
- 2043-08-09
AI Technical Summary
[0004]在现有的方案中,由开发人员基于Layout的布局,人工计算掩码并填入测试脚本中,存在效率低和易于出错的问题
[0017]本发明实施例,直接基于从DBC文件中提取的报文信息来计算报文所占用的各个字节对应的掩码,该掩码可以指示各个字节中未使用的位,并基于计算的掩码自动生成测试代码。此种方式,直接基于DBC文件中的信息进行掩码计算和测试代码生成,无需人工参与,因此准确性和效率均能够得到提高。
Smart Images

Figure CN116866235B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to software testing technology, and more particularly to a method, testing method, apparatus, and storage for generating test code. Background Technology
[0002] In vehicles, CAN communication is extensive, and DBC (Database for CAN) files are typically used to describe the CAN communication of related products. After product development (which can be a device or a program product) is complete, it is generally necessary to test whether the product can correctly send CAN signals based on the DBC description. One aspect of this testing involves unused bit testing, specifically testing whether the values of unused bits in the CAN signals sent by the product are at their default values. This verifies the correctness of the CAN signals sent by the product and thus verifies the product's functionality. To perform unused bit testing, the unused bit information needs to be written into the test script (code) so that the program can identify which bits are unused during testing.
[0003] In existing solutions, developers can use the Layout function in Vector's DBC development tool to identify unused bits in the CAN signal. Based on the layout, they can manually calculate a mask to indicate the unused bits and then write the calculated mask into the test script. For example, such as Figure 7 The diagram shows the layout of a specific message in a CAN signal (a CAN signal can contain multiple messages). The vertical axis represents bytes 0 to n, and the horizontal axis represents bits 0 to 7. As shown, this message contains n bytes. In byte 0, bits 0 to 5 are used (shown in gray), while bits 6 and 7 are unused. Therefore, the mask for byte 0 can be manually calculated as "00111111", where 0 represents unused bits and 1 represents used bits. Similarly, the mask for byte 1 can be calculated as "01100111", and so on, until all bytes are masked. The calculated masks are then manually entered into a test script, and the script is used for testing to verify the functionality of the unused bits.
[0004] In existing solutions, developers manually calculate masks based on layouts and input them into test scripts, which is inefficient and prone to errors. Therefore, there is a need to improve existing technologies. Summary of the Invention
[0005] In view of this, embodiments of the present invention provide a method, testing method, apparatus and storage medium for generating test code, which can improve efficiency and accuracy.
[0006] This invention discloses a method for generating test code. When executed, the test code is used to test whether the value of unused bits in a CAN signal sent by a device under test is a default value. The CAN signal includes at least one message, and each message includes at least one signal. The method includes: acquiring a DBC file describing CAN communication; extracting message information from the DBC file; calculating a mask for each byte occupied by the message based on the extracted message information, wherein the mask is used to indicate unused bits in the bytes occupied by the message; and generating the test code based on the calculated mask.
[0007] The message information includes at least one of the following: message identifier, message length, and the start bit, signal length, and signal type of each signal in the message.
[0008] The step of calculating the mask of each byte occupied by the message based on the extracted message information specifically includes: distinguishing each message based on the message identifier; and for each message, determining the information of the usage bits of each signal in the message based on the start bit, signal length, and signal type of each signal in the message; determining the bytes occupied by the message based on the length of the message; and calculating the mask of each byte occupied by the message based on the bytes occupied by the message and the information of the usage bits.
[0009] The step of determining the usage information of each signal in the message based on the start bit, signal length, and signal type of each signal in the message specifically includes: for each signal in the message, determining the byte order of the signal as Intel or Motorola based on the signal type; when the byte order of the signal is Intel, directly determining the usage information of the signal based on the start bit and signal length of the signal; when the byte order of the signal is Motorola, determining whether the signal spans bytes based on the start bit and signal length of the signal; and determining the usage information of the signal based on whether the signal spans bytes.
[0010] Specifically, a list is used to record the information of the bits used for each signal in the message.
[0011] The step of calculating the mask of each byte occupied by the message based on the information of the bytes occupied by the message and the bits used specifically includes: for each byte occupied by the message, determining whether each bit of each byte is in the list; and calculating the mask of each byte based on the determination result.
[0012] The step of generating the test code based on the calculated mask specifically includes: extracting a test code template; and modifying the test code template based on the calculated mask to generate the test code.
[0013] An embodiment of the present invention provides a testing method that performs an unused bit function test on a CAN signal sent by a device under test (DUT) by executing test code. The test code is generated at least partially using the test code generation method of the present invention. When executing the test code, the method performs the following steps: acquiring the CAN signal sent by the DUT; acquiring a mask in the test code; determining unused bit information in each message of the CAN signal based on the acquired mask; extracting data of corresponding bits in each message based on the unused bit information; and generating a test result by determining whether the extracted data is a preset value.
[0014] An embodiment of the present invention provides an apparatus for generating test code. The generated test code is used to test whether the value of unused bits in a CAN signal sent by a device under test is a default value. The CAN signal includes at least one message, and each message includes at least one signal. The apparatus includes: an acquisition module for acquiring a DBC file describing CAN communication; an information extraction module for extracting message information from the DBC file; a mask calculation module for calculating a mask of each byte occupied by the message based on the extracted message information, wherein the mask is used to indicate unused bits in the bytes occupied by the message; and a code generation module for generating the test code based on the calculated mask.
[0015] An embodiment of the present invention provides a computer-readable storage medium having a computer program stored thereon, the computer program including executable instructions that, when executed by a processor, implement the method according to an embodiment of the present invention.
[0016] Beneficial effects of the embodiments of the present invention:
[0017] In this embodiment of the invention, the mask corresponding to each byte occupied by the message is calculated directly based on the message information extracted from the DBC file. This mask can indicate the unused bits in each byte, and test code is automatically generated based on the calculated mask. This method directly calculates the mask and generates the test code based on the information in the DBC file, without manual intervention, thus improving both accuracy and efficiency. Attached Figure Description
[0018] Other details and advantages of the invention will become apparent from the detailed description provided below. It should be understood that the following drawings are merely illustrative and should not be considered as limiting the invention. The following detailed description will refer to the drawings, in which:
[0019] Figure 1 This is a flowchart illustrating an embodiment of the method for generating test code according to the present invention;
[0020] Figure 2 yes Figure 1 A flowchart illustrating an embodiment of step S14;
[0021] Figure 3A and 3B This is a flowchart illustrating the method for determining the bit usage of each signal in a message according to an embodiment of the present invention.
[0022] Figure 4 This is a flowchart of the method for calculating the mask according to an embodiment of the present invention;
[0023] Figure 5 This is a schematic flowchart of an embodiment of the testing method of the present invention;
[0024] Figure 6 This is a schematic diagram of an embodiment of the apparatus for generating test code according to the present invention;
[0025] Figure 7 This is a schematic diagram of the message layout displayed by the tool. Detailed Implementation
[0026] To make the technical problems, technical solutions, and beneficial effects of this invention clearer and more understandable, the invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the invention.
[0027] In the description of this invention, it should be understood that the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature. Furthermore, the terms "first," "second," etc., are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such terms can be used interchangeably where appropriate so that embodiments of the invention described herein can be implemented in orders other than those illustrated or described herein.
[0028] like Figure 1The diagram shown is a flowchart illustrating an embodiment of the method for generating test code according to the present invention. When the test code generated using this embodiment is executed, for example by a processor, it can test whether the value of unused bits in the CAN signal sent by the device under test is the default value, thus performing a functional test on the unused bits. The CAN signal includes at least one message, which can be distinguished from other messages by a message ID, and each message includes at least one signal.
[0029] Specifically, Figure 1 The method for generating test code includes the following steps:
[0030] Step S10: Obtain the DBC file used to describe CAN communication.
[0031] For details regarding DBC files, please refer to the "DCB File Format Document" published by Vector Corporation; it will not be elaborated upon here.
[0032] Step S12: Extract message information from the DBC file.
[0033] The information extracted in step S12 mainly relates to bit usage. This information may include at least one of the following: message identifier, message length, and the start bit, signal length, and signal type of each signal in the message. The message identifier, or message ID, is mainly used to distinguish different messages. In this embodiment, the bit usage is calculated for each message separately, and the calculation method for each message is similar and will not be described repeatedly. The message length refers to the number of bytes occupied by the message. In the DBC specification, a message can occupy multiple bytes. A mask needs to be calculated for each byte of the message, and then these masks are combined to obtain the bit usage of the message. A message may include multiple signals. The start bit, signal length, and signal type of each signal can be used to determine the bit usage of that signal. The signal type refers to the byte order used when encoding the signal, which is divided into Intel (little-endian, i.e., low-order byte stored at low address) and Motorola (big-endian, i.e., high-order byte stored at low address).
[0034] Step S14: Calculate the mask of each byte occupied by the message based on the extracted message information. The mask is used to indicate unused bits or used bits in the corresponding byte.
[0035] The flow of the embodiment of step S14 can be referred to Figure 2 As shown, it will not be elaborated upon here.
[0036] Step S16: Generate test code based on the calculated mask.
[0037] In this process, a test code template can be predefined. Therefore, in step S16, the test code template is extracted, and then modified based on the calculated mask to generate the test code. For example, the value of the mask variable in the test code template is replaced with the calculated mask to write the calculated mask into the test code.
[0038] In this embodiment, the mask is calculated directly based on the information in the DBC file, and test code is automatically generated based on the mask. This process does not require manual intervention; the mask and test code can be automatically calculated and generated simply by inputting the DBC file. Therefore, the accuracy of the mask and the efficiency of test code generation can be improved.
[0039] like Figure 2 As shown, is Figure 1 A flowchart illustrating an embodiment of step S14. It should be noted that since the mask calculation method is consistent for each message, therefore... Figure 2 In this example, we will use only one message as an example. Figure 2 As shown, the method flow includes the following steps:
[0040] Step S20: Based on the start bit, signal length, and signal type of each signal in the message, determine the information of the bits used by each signal in the message.
[0041] In this embodiment, a list can be used to store / record the bit information used by each signal. Using a list, compared to using variables, reduces the processing power requirements of the computer. For example, some messages can occupy 64 bytes, or 512 bits; many commonly used computers cannot calculate variables of such a large number of bits, but using a list can solve this problem.
[0042] Specifically, step S20 may include: for each signal in the message, first determining whether the signal's byte order is Intel or Motorola based on the signal type. If the signal's byte order is Intel, the information on the used bits of the signal can be directly determined based on the signal's start bit and signal length, for example, by referring to... Figure 3A As shown. When the signal's byte order is Motorola, it first determines whether the signal spans multiple bytes based on the signal's start bit and length; then, based on whether the signal spans multiple bytes, it determines the information of the bits used by the signal; for example, refer to... Figure 3B As shown. The following will discuss... Figure 3A and 3B Specific details will not be elaborated here.
[0043] Step S22: Determine the number of bytes occupied by the message based on the message length.
[0044] Step S24: Calculate the mask of each byte occupied by the message based on the information of the bytes occupied by the message and the bits used.
[0045] Specifically, step S24 may include: determining whether each bit of each byte occupied by the message is in the list; and calculating the mask of each byte based on the determination result. In step S24, for example, if the list records [0, 1, 2, 11...], it means that in byte 0, bits 0, 1, and 2 are used, while bits 3 to 7 are not used. Based on this, the mask of byte 0 can be determined to be 11111000, where 1 represents unused and 0 represents used.
[0046] A specific example of steps S22 and S24 can be found by referring to Figure 4 As shown, it will not be elaborated upon here.
[0047] In this embodiment, the bit usage of each signal is first determined based on the start bit, signal length, and signal type of each signal in the message. Then, based on the bit usage and the message length, the mask of each byte occupied by the message is determined. This method does not rely on manual intervention, thus offering high efficiency and accuracy. Furthermore, a list can be used to record the bit usage, further reducing the processing power requirements of the computing device.
[0048] like Figure 3A and 3B The diagram shown is a flowchart illustrating an embodiment of calculating the bit usage of a message. The method includes the following steps:
[0049] Step S300: Create an empty list List[]. This list will eventually contain the set of used bits for a single message, which can reflect the bit usage of the message.
[0050] Step S302: Based on the signal type, determine whether the byte order used by the signal is Intel. If yes, proceed to step S304; otherwise (if no, then proceed to step S301).
[0051] Step S304: Set i = LSB, j = 0. In Intel format, the start bit of a signal is the least significant bit, therefore the initial value of i is set to LSB, and the initial value of j is set to 0.
[0052] Step S306: Add i to List[].
[0053] Step S308: Let i = i+1, j = j+1;
[0054] Step S310: Determine if j is less than the signal length. If yes, proceed to step S306; otherwise, the signal calculation is complete, and proceed to step S312.
[0055] Step S312: determining whether all signals under the message have been traversed; if yes, ending the process; if no, performing step S302 to process the next signal.
[0056] Step S301: when the byte order of a signal is Motorola, setting the initial value of i to MSB and the initial value of j to 0. In the Motorola format, the starting bit of a signal is MSB (most significant bit).
[0057] Step S303: determining whether the result of MSB divided by 8 (integer division) is equal to the result of (MSB minus j) divided by 8 (integer division), that is, whether MSB / / 8 equals i / / 8, wherein the symbol / / represents integer division, i.e., rounding after division. If yes, performing step S305; if no, performing step S307. The essence of the determination in step S303 is to determine whether i crosses bytes; when i crosses bytes, performing step S307, and when i does not cross bytes, performing step S305.
[0058] Step S305: adding i to List[].
[0059] Step S307: adding i+2×8×((MSB / / 8)-(i / / 8)) to List[].
[0060] Step S309: setting i=i-1, j=j+1.
[0061] Step S311: determining whether j is less than the signal length; if yes, performing step S312; if no, returning to perform step S303.
[0062] As Figure 4 shown, it is a schematic flow chart of an embodiment for calculating a mask value, which includes the following steps:
[0063] Step S401: setting variables k and P, and setting the initial values of both k and P to 0, wherein k represents the k-th byte of the message.
[0064] Step S402: setting Q(k)=0xff, wherein Q(k) represents the mask of the k-th byte of the message. Additionally, setting j=k*8.
[0065] Step S403: determining whether j is in List[]; if yes, performing step S404; if no, performing step S405.
[0066] Step S404: setting P=P|(1<<j%8). Wherein, the symbol | represents bitwise OR, << represents left shift, and % represents taking the remainder after division.
[0067] Step S405: setting j=j+1.
[0068] Step S406: Determine if j is less than (k+1)×8. If yes, proceed to step S403; otherwise, proceed to step S407.
[0069] Step S407: Set Q(k) = Q(k)^P, where the symbol ^ represents the XOR operation, save this Q(k) value, and this Q(k) value is the mask value of the k-th byte in the final calculation.
[0070] Step S408: Set k=k+1; P=0.
[0071] Step S409: Determine if k is less than the message length. If yes, proceed to step S402. Otherwise, end the process, which means the calculation of the mask for this message is complete, and proceed to the calculation of the mask for the next message.
[0072] like Figure 5 The diagram shown is a flowchart illustrating an embodiment of the testing method of the present invention. This testing method performs an unused bit function test on the CAN signal sent by the device under test by executing the aforementioned generated test code. The testing method includes the following steps:
[0073] Step S50: Obtain the CAN signal sent by the device under test.
[0074] Step S52: Obtain the mask in the test code.
[0075] Step S54: Based on the obtained mask, determine the information of the unused bits of each message in the CAN signal.
[0076] Step S56: Based on the information of unused bits, extract the data of the corresponding bits in each message.
[0077] Step S58: Generate test results by determining whether the extracted data is a preset value.
[0078] like Figure 6 The diagram shown is a structural schematic of an embodiment of the device 6 for generating test code according to the present invention. The generated test code is used to test whether the value of unused bits in a received CAN signal is a default value. The CAN signal includes at least one message, and each message includes at least one signal. The device 6 includes: an acquisition module 60 for acquiring a DBC file describing CAN communication; an information extraction module 62 for extracting message information from the DBC file; a mask calculation module 64 for calculating a mask for each byte occupied by the message based on the extracted message information, wherein the mask indicates unused bits in the bytes occupied by the message; and a code generation module 66 for generating the test code based on the calculated mask.
[0079] In addition, embodiments of the present invention also provide a computer-readable storage medium storing a computer program thereon, the computer program including executable instructions that, when executed by a processor, implement the method according to embodiments of the present invention.
[0080] The descriptions of the storage medium and device embodiments above are similar to those of the method embodiments above, and have similar beneficial effects. For technical details not disclosed in the storage medium and device embodiments of this application, please refer to the descriptions of the method embodiments of this application for understanding.
[0081] The aforementioned processor can be at least one of the following: Application Specific Integrated Circuit (ASIC), Digital Signal Processor (DSP), Digital Signal Processing Device (DSPD), Programmable Logic Device (PLD), Field Programmable Gate Array (FPGA), Central Processing Unit (CPU), controller, microcontroller, microprocessor, etc. It is understood that other electronic devices can also implement the functions of the aforementioned processor, and this application does not specifically limit the specific implementation.
[0082] The aforementioned computer storage media / memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), magnetic random access memory (FRAM), flash memory, magnetic surface memory, optical disc, or compact disc read-only memory (CD-ROM), etc.; or it can be various terminals that include one or any combination of the above-mentioned memories, such as mobile phones, computers, tablet devices, personal digital assistants, etc.
[0083] It should be noted that the above description is merely illustrative and not intended to limit the invention. In other embodiments of the invention, the method may have more, fewer, or different steps, and the order, inclusion, and functional relationships between the steps may differ from those described and illustrated. For example, multiple steps may typically be combined into a single step, or a single step may be split into multiple steps. For those skilled in the art, variations in the order of the steps are also within the scope of protection of this invention without inventive effort.
[0084] The technical solution of the present invention, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. The 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.) or processor or microcontroller to execute all or part of the steps of the methods described in the various embodiments of the present invention.
[0085] Those skilled in the art will understand that all or part of the steps of the above-described method embodiments can be implemented by hardware associated with program instructions. The aforementioned program can be stored in a computer-readable storage medium. When executed, the program performs the steps of the above-described method embodiments.
[0086] While the present invention has been disclosed above with reference to preferred embodiments, it is not limited thereto. Any modifications and alterations made by those skilled in the art without departing from the spirit and scope of the invention should be included within the scope of protection of the invention. Therefore, the scope of protection of the invention should be determined by the scope defined in the claims.
Claims
1. A method for generating test code, which, when executed, is used to test whether the value of unused bits in a CAN signal sent by the device under test is a default value, wherein, CAN signals include: at least one message, each message including: at least one signal, characterized in that the method includes: Obtain the DBC file used to describe CAN communication; Extract message information from the DBC file. The message information includes: message identifier, message length, and start bit, signal length, and signal type of each signal in the message. Each message is distinguished based on the message identifier; For each signal in each message, the byte order of the signal is determined to be either Intel or Motorola based on the signal type; When the byte order of the signal is Intel, the information of the used bits of the signal is determined based on the start bit and the signal length. When the byte order of the signal is Motorola, based on the start bit and length of the signal, it is determined whether the signal spans bytes, and based on whether the signal spans bytes, the information of the used bits of the signal is determined; Specifically, a list is used to record the information of the bits used for each signal in the message; The number of bytes occupied by the message is determined based on the length of the message; For each byte occupied by the message, determine whether each bit of each byte is in the list; and Based on the judgment result, calculate the mask for each byte; and The test code is generated based on the calculated mask.
2. The method for generating test code as described in claim 1, characterized in that, The steps for generating the test code based on the calculated mask specifically include: Extract test code templates; and Based on the calculated mask, the test code template is modified to generate the test code.
3. A testing method, characterized in that, A test is performed on the CAN signals sent by the device under test by executing test code, wherein the test code is generated at least in part by the method described in any one of claims 1 to 2, and when the test code is executed, the method performs the following steps: Acquire the CAN signal sent by the device under test; Obtain the mask from the test code; Based on the obtained mask, determine the information of unused bits in each message of the CAN signal; Based on the information of the unused bits, extract the data of the corresponding bits from each message; and Test results are generated by determining whether the extracted data is a preset value.
4. A computer-readable storage medium having a computer program stored thereon, the computer program including executable instructions that, when executed by a processor, implement the method according to any one of claims 1 to 2.
Citation Information
Patent Citations
Controller CAN signal test method and system
CN106850372A