Code testing method, computer equipment and storage medium

By performing risk code detection and empty-time protection detection on the target program code, the problem of inability to accurately identify program crash problems in the existing technology is solved, efficient identification and elimination in the pre-test stage is achieved, and the stability and reliability of the program are improved.

CN120336158APending Publication Date: 2025-07-18TENCENT MUSIC ENTERTAINMENT TECH (SHENZHEN) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510331573.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-03-20
Publication Date
2025-07-18

AI Technical Summary

Technical Problem

There is a lack of effective pre-test methods in the prior art to identify application crash problems, resulting in extended resolution time for crash problems and affecting user experience.

Method used

By performing risk code detection and null protection detection on the target program code, code blocks that may cause program crashes are identified and eliminated, including abstract syntax tree analysis and detection of null protection structures.

Benefits of technology

Accurately identify and eliminate program crash-program code blocks in the pre-test stage, improving the stability and reliability of program code and reducing problem-solving time.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120336158A_ABST
    Figure CN120336158A_ABST
Patent Text Reader

Abstract

The invention relates to a code testing method and device, a storage medium and a computer program product. The method comprises the steps of obtaining a to-be-tested target program code; performing risk code detection processing on the target program code to obtain a risk code detection result of the target program code; the risk code detection result is used for representing whether the target program code contains a code block meeting a risk structure or not; the risk structure is used for representing a called and unrealized code structure; performing null judgment protection detection processing on the target program code to obtain a null judgment protection detection result of the target program code; the null judgment protection detection result is used for representing whether the target program code contains a code block meeting a null judgment protection structure or not; and obtaining a code test result of the target program code according to the risk code detection result and the null judgment protection detection result. By adopting the method, the problem code block causing the program crash can be identified in the pre-test stage, and the stability and the reliability of the target program code are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of software testing, and particularly to a code testing method, apparatus, computer device, storage medium, and computer program product. Background Art

[0002] In the application development of the client side, ensuring the stability and reliability of the application is crucial. However, if the application crashes during operation, it may have a serious impact on the user experience and the overall performance of the application. For example, the iOS client error signal "SIGSEGV" will cause the application to exit unexpectedly during operation due to accessing an illegal memory address.

[0003] Currently, the solutions for pre-testing and detecting crash problems in the market are still relatively scarce. Many development teams often rely on post-crash log analysis and user feedback to troubleshoot such problems. This passive approach not only increases the time to solve the problem but also may cause users to continue to encounter crashes without timely repair. Therefore, there is an urgent need for a method that can accurately identify program crash problems in applications during the pre-testing stage. Summary of the Invention

[0004] Based on this, in view of the above technical problems, it is necessary to provide a code testing method, apparatus, computer device, computer-readable storage medium, and computer program product that can accurately identify program crash problems in applications during the pre-testing stage.

[0005] In a first aspect, the present application provides a code testing method. The method includes:

[0006] Obtain target program code to be tested;

[0007] Perform risk code detection processing on the target program code to obtain a risk code detection result of the target program code; the risk code detection result is used to represent whether the target program code contains a code block that meets a risk structure; the risk structure is used to represent a code structure that is called and not implemented;

[0008] Perform null pointer protection detection processing on the target program code to obtain a null pointer protection detection result of the target program code; the null pointer protection detection result is used to represent whether the target program code contains a code block that meets a null pointer protection structure;

[0009] Obtain a code testing result of the target program code according to the risk code detection result and the null pointer protection detection result; the code testing result is used to indicate whether there is a code block in the target program code that causes the program to crash.

[0010] In one embodiment, risk code detection processing is performed on the target program code to obtain a risk code detection result of the target program code, including:

[0011] From the target program code, a first code block, a second code block, a third code block, and a fourth code block are determined; the first code block is a called code block; the second code block is a code block belonging to the attribute declaration of a class; the third code block is a code block containing code representing method definitions; the fourth code block is a code block that has parameters passed in when called.

[0012] Based on the first code block, the second code block, the third code block, and the fourth code block, the risk code detection result is obtained.

[0013] In one embodiment, the second code block is obtained in the following manner:

[0014] From the abstract syntax tree of the target program code, a first abstract syntax tree is determined;

[0015] From the abstract syntax tree of the target program code, an abstract syntax tree that meets a preset condition is determined and set as a second abstract syntax tree; the preset condition indicates that the code block of the abstract syntax tree has a method definition after being used as an attribute declaration of a class.

[0016] Based on the difference structure between the first abstract syntax tree and the second abstract syntax tree, the second code block is obtained.

[0017] In one embodiment, based on the first code block, the second code block, the third code block, and the fourth code block, obtaining the risk code detection result includes:

[0018] Determine the intersection code block between the first code block and the second code block;

[0019] Remove the code blocks in the intersection code block that match the third code block and / or the fourth code block to obtain the risk code detection result.

[0020] In one embodiment, null protection detection processing is performed on the target program code to obtain a null protection detection result of the target program code, including:

[0021] From the target program code, a code block that meets the null protection structure is determined to obtain a null protection code block in the target program code; the null protection structure is processed based on conditional null information, arithmetic null information, and pre-assignment information.

[0022] Based on the null-check protection code block, obtain the null-check protection detection result.

[0023] In one embodiment, based on the risk code detection result and the null-check protection detection result, obtain the code test result of the target program code, including:

[0024] Perform consistency detection processing on the risk code block in the risk code detection result and the null-check protection code block in the null-check protection detection result to obtain a consistency detection result;

[0025] Based on the consistency detection result, determine the difference code block between the risk code block and the null-check protection code block;

[0026] Based on the difference code block, obtain the code test result.

[0027] In one embodiment, perform consistency detection processing on the risk code block in the risk code detection result and the null-check protection code block in the null-check protection detection result to obtain a consistency detection result, including:

[0028] Perform consistency detection processing on the method information corresponding to the risk code block and the method information corresponding to the null-check protection code block to obtain a method detection result;

[0029] Perform consistency detection processing on the name information of the risk code block and the name information of the null-check protection code block to obtain a name detection result;

[0030] Compare the line number information where the risk code block is located with the line number information where the null-check protection code block is located to obtain a line number comparison result;

[0031] Based on the method detection result, the name detection result, and the line number comparison result, obtain the consistency detection result.

[0032] In one embodiment, obtain the target program code to be tested, including:

[0033] Upload the project to the project management library;

[0034] If a code update message triggered for the project in the project management library is detected, obtain the updated code corresponding to the code update message;

[0035] Set the updated code as the target program code to be tested.

[0036] In a second aspect, the present application also provides a code testing device. The device includes:

[0037] A test code acquisition module for acquiring the target program code to be tested;

[0038] A risk code detection module for performing risk code detection processing on the target program code to obtain a risk code detection result of the target program code; the risk code detection result is used to characterize whether the target program code contains a code block that meets the risk structure; the risk structure is used to represent a code structure that is called and not implemented;

[0039] A null check protection detection module for performing null check protection detection processing on the target program code to obtain a null check protection detection result of the target program code; the null check protection detection result is used to characterize whether the target program code contains a code block that meets the null check protection structure;

[0040] A test result acquisition module for obtaining a code test result of the target program code according to the risk code detection result and the null check protection detection result; the code test result is used to indicate whether there is a code block in the target program code that causes the program to crash.

[0041] In a third aspect, the present application also provides a computer device. The computer device includes a memory and a processor. The memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:

[0042] Acquire the target program code to be tested;

[0043] Perform risk code detection processing on the target program code to obtain a risk code detection result of the target program code; the risk code detection result is used to characterize whether the target program code contains a code block that meets the risk structure; the risk structure is used to represent a code structure that is called and not implemented;

[0044] Perform null check protection detection processing on the target program code to obtain a null check protection detection result of the target program code; the null check protection detection result is used to characterize whether the target program code contains a code block that meets the null check protection structure;

[0045] Obtain a code test result of the target program code according to the risk code detection result and the null check protection detection result; the code test result is used to indicate whether there is a code block in the target program code that causes the program to crash.

[0046] In a fourth aspect, the present application also provides a computer-readable storage medium. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the following steps are implemented:

[0047] Obtain the target program code to be tested;

[0048] Perform risk code detection processing on the target program code to obtain a risk code detection result of the target program code; the risk code detection result is used to characterize whether the target program code contains a code block that meets the risk structure; the risk structure is used to characterize a code structure that is called and not implemented;

[0049] Perform null protection detection processing on the target program code to obtain a null protection detection result of the target program code; the null protection detection result is used to characterize whether the target program code contains a code block that meets the null protection structure;

[0050] Obtain a code test result of the target program code according to the risk code detection result and the null protection detection result; the code test result is used to indicate whether there is a code block in the target program code that causes the program to crash.

[0051] In a fifth aspect, the present application also provides a computer program product. The computer program product includes a computer program, and when the computer program is executed by a processor, the following steps are implemented:

[0052] Obtain the target program code to be tested;

[0053] Perform risk code detection processing on the target program code to obtain a risk code detection result of the target program code; the risk code detection result is used to characterize whether the target program code contains a code block that meets the risk structure; the risk structure is used to characterize a code structure that is called and not implemented;

[0054] Perform null protection detection processing on the target program code to obtain a null protection detection result of the target program code; the null protection detection result is used to characterize whether the target program code contains a code block that meets the null protection structure;

[0055] Obtain a code test result of the target program code according to the risk code detection result and the null protection detection result; the code test result is used to indicate whether there is a code block in the target program code that causes the program to crash.

[0056] The above code testing method, device, computer device, storage medium, and computer program product obtain a target program code to be tested; perform risk code detection processing on the target program code to obtain a risk code detection result of the target program code; the risk code detection result is used to characterize whether the target program code contains a code block that meets the risk structure; the risk structure is used to represent a code structure that is called and not implemented; perform null protection detection processing on the target program code to obtain a null protection detection result of the target program code; the null protection detection result is used to characterize whether the target program code contains a code block that meets the null protection structure; obtain a code test result of the target program code according to the risk code detection result and the null protection detection result; the code test result is used to indicate whether there is a code block in the target program code that causes the program to crash.

[0057] Advantages of this application: By detecting whether there are risk code blocks in the target program code to be tested and detecting whether there is a null protection structure in the target program code during the code compilation stage, in order to comprehensively judge whether there is a code block in the target program code that causes the program to crash by combining the risk code detection result and the null protection monitoring result, so as to identify the problem code block that causes the program to crash in the pre-test stage, thereby effectively improving the recognition accuracy of the problem code, increasing the solution time of the problem code block, and further improving the stability and reliability of the target program code. Description of the Drawings

[0058] Figure 1 It is a schematic flowchart of the code testing method in an embodiment;

[0059] Figure 2 It is a schematic flowchart of the steps of performing risk code detection processing on the target program code in an embodiment;

[0060] Figure 3 It is a schematic flowchart of the steps of performing null protection detection processing on the target program code in an embodiment;

[0061] Figure 4 It is a schematic flowchart of submitting, pulling, testing the code, and feedbacking the code test result in an embodiment;

[0062] Figure 5 It is a schematic flowchart of the code testing method in another embodiment;

[0063] Figure 6 It is a schematic flowchart of the code testing method in yet another embodiment;

[0064] Figure 7 It is a structural block diagram of the code testing device in an embodiment;

[0065] Figure 8 It is the internal structure diagram of a computer device in an embodiment. Specific implementation manners

[0066] In order to make the objectives, technical solutions and advantages of the present application clearer and more understandable, the present application will be further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.

[0067] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in the present application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data need to comply with relevant regulations.

[0068] In one embodiment, as Figure 1 shown, a code testing method is provided. In this embodiment, this method is exemplified by being applied to a terminal. It can be understood that this method can also be applied to a server, and can also be applied to a system including a terminal and a server, and is implemented through the interaction between the terminal and the server. In this embodiment, the method includes the following steps:

[0069] Step S101, obtain the target program code to be tested.

[0070] Among them, the target program code refers to the computer program code that needs to be tested for code. For example, the target program code can be the program code newly submitted by a developer for a certain project.

[0071] Specifically, after a developer completes the code development of a project, the code can be submitted to a specified location. After the submission is successful, a code submission message for the code can also be generated, and then the code submission message is sent to relevant devices. When the terminal receives the code submission message, it can automatically pull the code and use it as the target program code to be tested.

[0072] Step S102, perform risk code detection processing on the target program code to obtain the risk code detection result of the target program code; the risk code detection result is used to characterize whether the target program code contains a code block that meets the risk structure; the risk structure is used to characterize a code structure that is called and not implemented.

[0073] Among them, the risk code detection processing is used to detect whether the target program code contains a risk code block that may cause the program to crash (Crash).

[0074] Among them, a code block (Block) is a data type in Objective-C code used to encapsulate code snippets. It should be noted that when a Block is called, if the Block is not implemented, at this time the Block is a null value, which will cause the program to crash.

[0075] Specifically, the terminal can extract the structural characteristics of the historical risk code block that caused the program to crash from the historical risk code blocks. For example, after analysis, the terminal can conclude that the historical risk code blocks are all called and unimplemented code blocks, then the terminal can define the called and unimplemented code structure as the risk structure. Furthermore, the terminal can detect whether the target program code contains code blocks that meet the risk structure. If a code block that meets the risk structure is detected, it is set as a risk code block. After detecting all the target program code, the terminal obtains the risk code detection result of the target program code.

[0076] Step S103, perform a null protection detection process on the target program code to obtain the null protection detection result of the target program code; the null protection detection result is used to indicate whether the target program code contains code blocks that meet the null protection structure.

[0077] Among them, the null protection detection process is used to detect whether the code blocks in the target program code that may cause the risk of program crashing have null protection designed.

[0078] Among them, null protection is used to ensure that before operating on parameters, the data structures of these parameters are not null, so as to avoid the computer causing exceptions or errors when accessing null data structures, thereby improving the stability and security of the program code. For example, the null protection structure can be to use a conditional judgment (such as an if statement) to check the status of the parameters in the code before execution to see if they are null values. If they are null values, default values or fallback mechanisms can be used to handle the null values of the parameters.

[0079] Specifically, the terminal can also detect whether the target protection program code contains code blocks that meet the null protection structure. For example, the terminal can preset at least one case of the null protection structure in advance, and then match the target program code with the preset null protection structure to obtain the code blocks in the target program code that match the null protection structure, and use this code block as the null protection detection result of the target program code.

[0080] Step S104, obtain the code test result of the target program code according to the risk code detection result and the null protection detection result; the code test result is used to indicate whether there are code blocks in the target program code that cause the program to crash.

[0081] Specifically, since the risk code detection and the null check protection detection are performed independently, the terminal also needs to perform consistency detection processing to determine whether the risk code block in the risk code detection result and the null check protection code block in the null check protection detection result belong to the same code block. Furthermore, by taking the difference set of the risk code block and the null check protection code block, the code block that does not have a null check protection structure in the risk code block can be determined, and it is used as the code test result of the target program code, which means that the code block that does not have a null check protection structure in the risk code block may cause a program crash exception.

[0082] In the above code test method, the target program code to be tested is obtained; the risk code detection processing is performed on the target program code to obtain the risk code detection result of the target program code; the risk code detection result is used to characterize whether the target program code contains a code block that meets the risk structure; the risk structure is used to characterize a code structure that is called and not implemented; the null check protection detection processing is performed on the target program code to obtain the null check protection detection result of the target program code; the null check protection detection result is used to characterize whether the target program code contains a code block that meets the null check protection structure; according to the risk code detection result and the null check protection detection result, the code test result of the target program code is obtained; the code test result is used to indicate whether there is a code block in the target program code that causes the program to crash. By using this method, during the code compilation stage, it is detected whether there is a risk code block in the target program code to be tested, and whether there is a null check protection structure in the target program code, so as to comprehensively judge whether there is a code block in the target program code that causes the program to crash by combining the risk code detection result and the null check protection detection result, thereby identifying the problem code block that causes the program to crash in the pre-test stage, effectively improving the identification accuracy of the problem code, and also increasing the solution time of the problem code block, and further improving the stability and reliability of the target program code.

[0083] In one embodiment, as Figure 2 shown, the above step S102, performing the risk code detection processing on the target program code to obtain the risk code detection result of the target program code, specifically includes the following contents:

[0084] Step S201, determine the first code block, the second code block, the third code block, and the fourth code block from the target program code.

[0085] Among them, the first code block is the called code block; the second code block is the code block belonging to the class attribute declaration; the third code block is the code block containing the code representing the method definition; the fourth code block is the code block with parameters passed in when it is called.

[0086] Specifically, by analyzing historical risk code blocks, the terminal can conclude that the code blocks that cause program crashes usually have the following structural characteristics: the code blocks are declared as properties of a class, and there is no method definition for the code blocks (the method has a return value and will not cause the code block to be null), and no parameters for the code blocks are passed in when they are called.

[0087] Based on this, the terminal can match the above structural characteristics to the abstract syntax tree of the target program code, and obtain the first code block, the second code block, the third code block, and the fourth code block that respectively match the above structural characteristics. That is, the terminal can match from the target program code the first code block that is called, and the second code block that has a property declaration and the property declaration belongs to a class, and the third code block that contains code representing method definitions, and the fourth code block that has parameters passed in when it is called. Among them, the abstract syntax tree (Abstract Syntax Tree, AST) is a data structure that represents the syntax structure of program source code (such as the target program code) in a tree structure. The framework of the abstract syntax tree can be WCClangMatcher (a framework based on Clang AST), or OCLint (a static analysis framework based on Clang AST), or Infer (which has a set of custom ASTs and uses the functional language OCaml).

[0088] Step S202, obtain a risk code detection result according to the first code block, the second code block, the third code block, and the fourth code block.

[0089] Specifically, the terminal first finds the code blocks common to the first code block and the second code block, then excludes the third code block and the fourth code block from the common code blocks, and finally determines the processed code block as the risk code block that meets the risk structure. Furthermore, the terminal can set the risk code block as the risk code detection result of the target program code.

[0090] In this embodiment, code blocks that match the structural characteristics of historical risk codes are first obtained from the abstract syntax tree of the target program code. Furthermore, by comprehensively analyzing the first code block, the second code block, the third code block, and the fourth code block, the risk code blocks in the target program code can be more accurately identified, improving the accuracy of risk code detection. Moreover, the program exception problem can be located at the code block level, so that developers can more quickly locate the problem in the program code, accelerate the code repair and optimization process, and reduce the code debugging time.

[0091] In one embodiment, the second code block in the above step S201 can be obtained in the following manner: determine a first abstract syntax tree from the abstract syntax tree of the target program code; determine an abstract syntax tree that meets a preset condition from the abstract syntax tree of the target program code and set it as the second abstract syntax tree; the preset condition indicates that the code block of the abstract syntax tree has a method definition after a class attribute declaration; obtain the second code block according to the difference structure between the first abstract syntax tree and the second abstract syntax tree.

[0092] It should be noted that when searching for the second code block of the class attribute declaration, due to the property mechanism characteristics of the Objective-C language, if a property is declared in the interface of the class (i.e., the.h file) but the corresponding getter and setter methods are not explicitly written in the implementation file (i.e., the.m file), the compiler will automatically generate these methods. This will cause the structural overlap of the abstract syntax trees of the second code block and the third code block, so the getter / setter methods of the class attribute declaration cannot be directly used as the structural features of the abstract syntax tree.

[0093] Specifically, the terminal can determine, from the abstract syntax tree of the target program code, an abstract syntax tree that has only a property declaration, and can set this abstract syntax tree as the first abstract syntax tree; the terminal can also determine, from the abstract syntax tree of the target program code, an abstract syntax tree that has a method definition after a class attribute declaration, and can set this abstract syntax tree as the second abstract syntax tree. Then the terminal can set the code block corresponding to the difference structure between the first abstract syntax tree and the second abstract syntax tree as the second code block. Among them, the difference structure is used to represent the difference between the abstract syntax tree structures.

[0094] In practical applications, the difference structure can be obtained in the ParmVarDecl node of the abstract syntax tree. Among them, ParmVarDecl is a node in the Clang abstract syntax tree, which is used to represent parameter variable declarations. In the Clang abstract syntax tree, the parameters of each function or method are represented as a ParmVarDecl node. Clang is the front-end part of the compiler framework.

[0095] In this embodiment, by determining, from the abstract syntax tree of the target program code, a first abstract syntax tree that only makes an attribute declaration, and by determining, from the abstract syntax tree of the target program code, a second abstract syntax tree that makes a method definition after making an attribute declaration as a class, and then by comparing and analyzing the different structures between the first abstract syntax tree and the second abstract syntax tree, the second code block is quickly and accurately identified, solving the problem that the abstract syntax tree of the second code block overlaps with the abstract syntax tree of the third code block due to directly selecting the code block of the class's attribute declaration, thereby improving the accuracy of risk code detection.

[0096] In one embodiment, for step S202 above, obtaining a risk code detection result based on the first code block, the second code block, the third code block, and the fourth code block specifically includes the following: determining an intersection code block between the first code block and the second code block; removing the code blocks in the intersection code block that match the third code block and / or the fourth code block to obtain a risk code detection result.

[0097] Specifically, the terminal first takes the intersection between the first code block and the second code block to obtain an intersection code block. If the third code block and / or the fourth code block exist in the intersection code block, then the third code block and / or the fourth code block in the intersection code block are removed to obtain a code block after removal. Finally, the code block after removal is used as the risk code detection result of the target program code.

[0098] In this embodiment, by determining the intersection code block between the first code block and the second code block, the code block that causes the program to crash can be initially determined. Then, by removing the third code block and / or the fourth code block in the intersection code block, the code blocks that do not cause the code block to be null can be excluded, thereby excluding the normal code in the intersection code block, and thus accurately identifying the risk code block in the target program code, effectively improving the accuracy of risk code detection.

[0099] In one embodiment, as Figure 3 shown, for step S103 above, performing a null protection detection process on the target program code to obtain a null protection detection result of the target program code specifically includes the following:

[0100] Step S301, determining, from the target program code, the code blocks that meet the null protection structure to obtain the null protection code blocks in the target program code; the null protection structure is processed based on conditional null information, arithmetic null information, and pre-assignment information.

[0101] Among them, the conditional null information is used to represent that before performing any operation, a conditional judgment statement is first used to check whether the status of the parameters in the code is null.

[0102] Among them, the operation null-checking information is used to characterize the state of checking whether the parameters in the code are null values using a ternary operator. The ternary operator can implement logical null-checking in one line of code.

[0103] Among them, the pre-assignment information is used to characterize that before calling a code block (or function), it is assigned a default value to ensure that a null reference does not occur during execution.

[0104] Specifically, the terminal can set the data structures corresponding to the conditional null-checking information, operation null-checking information, and pre-assignment information as null-check protection structures. Then, the terminal matches the abstract syntax tree corresponding to the target program code with the data structures corresponding to the conditional null-checking information, operation null-checking information, and pre-assignment information, and the terminal obtains the abstract syntax tree that meets the null-check protection structure.

[0105] In practical applications, the null-check protection structure may include the following: 1) if-class conditional null-check; 2) ternary operation null-check; 3) assignment before Block call.

[0106] Step S302, obtain a null-check protection detection result according to the null-check protection code block.

[0107] Specifically, the terminal can set the code block corresponding to the abstract syntax tree that meets any null-check protection structure as the null-check protection detection result of the target program code. For example, if an abstract syntax tree meets the if-class conditional null-check, the code block corresponding to the abstract syntax tree is set as the null-check protection code block.

[0108] In this embodiment, using the conditional null-checking information, operation null-checking information, and pre-assignment information to accurately analyze the target program code can more comprehensively identify the null-check protection measures in the target program code, test the robustness and stability of the target program code, significantly reduce runtime errors caused by null references, improve the overall quality of the code, enhance the security and reliability of the program, and ensure the smooth progress of the project during development and operation and maintenance.

[0109] In one embodiment, the above step S104, obtaining the code test result of the target program code according to the risk code detection result and the null-check protection detection result, specifically includes the following: performing a consistency detection process on the risk code block in the risk code detection result and the null-check protection code block in the null-check protection detection result to obtain a consistency detection result; determining the difference code block between the risk code block and the null-check protection code block according to the consistency detection result; and obtaining the code test result according to the difference code block.

[0110] Among them, the risk code block refers to the code block that meets the risk structure detected from the target program code through risk code detection and processing. The null check protection code block refers to the code block that meets the null check protection structure detected from the target program code through null check protection detection and processing.

[0111] It should be noted that the risk code detection in step S102 and the null check protection detection in step S103 are independent of each other. Therefore, before determining the final code test result, it is necessary to determine whether the risk code block in the risk code detection is consistent with the null check protection code block in the null check protection detection result. For example, assume that there are three risk code blocks A, B, and C detected, and four null check protection code blocks a, b, c, and d detected. Then it is necessary to determine which risk code blocks and null check protection code blocks belong to the same code block, that is, perform consistency detection.

[0112] Specifically, the terminal can obtain the code blocks in the risk code block that are consistent with the null check protection code block by performing consistency detection processing on the risk code block in the risk code detection result and the null check protection code block in the null check protection detection result. Furthermore, the code blocks in the risk code block that are inconsistent with the null check protection code block are determined as difference code blocks. That is to say, the difference code blocks are code blocks that meet the risk structure but have not been null check protected. Therefore, the terminal can use the difference code blocks as the code test result. Error reports can also be generated for the difference code blocks to notify relevant personnel to repair them.

[0113] In this embodiment, by detecting whether the risk code block is consistent with the null check protection code block, it is possible to distinguish the code blocks in the risk code block that have null check protection and the difference code blocks in the risk code block that have not been null check protected. Thus, the difference code blocks are used as the code test result, ensuring that the risk code block contains the necessary null check protection logic, which helps to reduce runtime errors caused by referencing null values, effectively improves the overall stability of the program code. At the same time, the difference code blocks are used as the code test result for key feedback, which helps to focus on maintaining the difference code blocks and improves the maintainability and security of the code.

[0114] In one embodiment, consistency detection processing is performed on the risk code block in the risk code detection result and the null check protection code block in the null check protection detection result to obtain a consistency detection result, which specifically includes the following: consistency detection processing is performed on the method information corresponding to the risk code block and the method information corresponding to the null check protection code block to obtain a method detection result; consistency detection processing is performed on the name information of the risk code block and the name information of the null check protection code block to obtain a name detection result; the line number information where the risk code block is located is compared with the line number information where the null check protection code block is located to obtain a line number comparison result; and a consistency detection result is obtained based on the method detection result, the name detection result, and the line number comparison result.

[0115] Among them, the line number information is used to represent the line number of the code block in the compilation file. For example, the line number information can be the 2nd line, the 6th - 10th lines, etc.

[0116] The terminal can comprehensively detect the consistency between the risk code block and the null check protection code block from multiple aspects. Specifically, the terminal determines whether the risk code block and the null check protection code block are inside the implementation of the same method by detecting the method information corresponding to the risk code block and the method information corresponding to the null check protection code block. If so, it is confirmed that the method detection result indicates that the method implementations are consistent; otherwise, it is confirmed that the method detection result indicates that the method implementations are inconsistent. The terminal also determines whether the name information of the risk code block and the name information of the null check protection code block are consistent by detecting the name information of the risk code block and the name information of the null check protection code block. If so, it is confirmed that the name detection result indicates that the names are consistent; otherwise, it is confirmed that the name detection result indicates that the names are inconsistent. The terminal also determines the magnitude relationship between the line number information where the risk code block is located and the line number information where the null check protection code block is located by comparing the line number information where the risk code block is located with the line number information where the null check protection code block is located. If the line number information where the null check protection code block is located is less than or equal to the line number information where the risk code block is located, it is confirmed that the line number comparison result is passed; otherwise, it is confirmed that the line number comparison result is not passed.

[0117] Furthermore, if the method detection result indicates that the method implementations are consistent, the name detection result indicates that the names are consistent, and the line number comparison result is passed, that is, all consistency detection processing is passed, it is confirmed that the consistency detection result of the risk code block and the null check protection code block indicates consistency, that is, the risk code block and the null check protection code block refer to the same code block; otherwise, it is confirmed that the consistency detection result of the risk code block and the null check protection code block indicates inconsistency, that is, the risk code block and the null check protection code block refer to different code blocks.

[0118] In this embodiment, through method information, name information, and line number information, consistency detection is performed on the risk code block and the null-check protection code block, realizing the detection of whether the risk code block and the null-check protection code block belong to the same code block from three perspectives: function, name, and code location, effectively improving the comprehensiveness and accuracy of the code block consistency detection, and laying a foundation for the subsequent steps to judge the differential code block.

[0119] In one embodiment, the above step S101 of obtaining the target program code to be tested specifically includes the following content: uploading the project to the project management library; if a code update message triggered for the project in the project management library is detected, obtaining the updated code corresponding to the code update message; and setting the updated code as the target program code to be tested.

[0120] Among them, the project management library can be the code repository of various projects. The project management library can store and manage the historical records and version information of the project, and can also perform code branch management and version control of the project. The project management library allows multi-user collaboration. In practical applications, the project management library can be a Git repository (an open-source distributed version control system).

[0121] Specifically, the terminal can perform static scanning processing on the project to upload the project to the project management library in a static scanning manner. The terminal can also configure continuous integration and continuous delivery tools in the project management library, and can also configure plugins for automatic code pulling and submission notifications in the project management library; among them, the continuous integration and continuous delivery tools can be Blue Shield pipelines; the Blue Shield pipelines are used to implement continuous integration and continuous delivery (CI / CD); the Blue Shield pipelines can connect multiple links such as development, testing, and deployment, improving the speed and quality of software delivery. If a developer triggers a code submission operation, the updated code corresponding to the code submission operation can be uploaded to the project management library in a static scanning manner. Then, the continuous integration and continuous delivery tools configured in the project management library will automatically trigger the build and test processes to detect the reliability of the newly submitted updated code. At the same time, the plugins configured in the project management library will also automatically perform code pulling processing and code submission message generation processing; the terminal sets the pulled updated code as the target program code to be tested for subsequent steps to perform code testing processing on it, and sends the code submission message generated for the target program code to relevant devices and displays it.

[0122] Illustrate by way of example, Figure 4It is a schematic diagram of the process for submitting, pulling, testing code, and providing feedback on code test results. The terminal can upload the submitted program code to the Git repository through static scanning. Then, the Blue Shield pipeline pre-configured in the Git repository will automatically trigger the build and test processes. At the same time, the plug-in pre-configured in the Git repository will pull the submitted program code and generate a code submission message. In addition, the static scanning rules apply to all iOS projects developed in Objective-C. One only needs to add the compilation information of the target Xcode project to the configuration file and run the script. After the static scanning script is written and connected to the Blue Shield pipeline, the scanning script can be triggered to run through code submission. After the task is completed, a bill of lading will be automatically generated and the executor will be notified.

[0123] Further, after the terminal performs code test processing on the target program code and obtains the code test result of the target program code, the terminal can also generate an error bill of lading based on the code test result. That is, if the code test result indicates whether there is a target code block in the target program code that causes the program to crash, an error bill of lading for the target code block can be generated. Then, the terminal sends the notification message of the error bill of lading to the relevant device to notify the relevant personnel to repair the target code block in the target program code that may cause the risk of program crashing.

[0124] In this embodiment, uploading the project to the project management library through static scanning can facilitate the collaboration of project team members on the project and ensure that all members have a consistent understanding of the project status. By monitoring the code update messages in the project management library, one can timely obtain the updated code corresponding to the code update message and automatically perform code testing, effectively improving the code testing efficiency and saving the manual testing cost. Through continuous integration, potential problems in the updated code can be detected in a timely manner, which can improve the overall code quality and reduce the subsequent costs caused by delayed problem discovery. This not only improves the development and testing efficiency of the project code but also helps to maintain the stability and high quality of the code and promotes the efficient collaboration of team members.

[0125] In one embodiment, as Figure 5 shown, another code testing method is provided. Taking this method applied to the terminal as an example for illustration, it includes the following steps:

[0126] Step S501, upload the project to the project management library.

[0127] Step S502, if a code update message triggered for the project in the project management library is detected, obtain the updated code corresponding to the code update message; set the updated code as the target program code to be tested.

[0128] Step S503: Determine the first code block, the second code block, the third code block, and the fourth code block from the target program code; the first code block is the called code block; the second code block is the code block belonging to the class attribute declaration; the third code block is the code block containing the code representing the method definition; the fourth code block is the code block with parameters passed in when called.

[0129] Step S504: Determine the intersection code block between the first code block and the second code block.

[0130] Step S505: Remove the code blocks in the intersection code block that match the third code block and / or the fourth code block to obtain the risk code detection result; the risk code detection result is used to indicate whether the target program code contains code blocks that meet the risk structure.

[0131] Step S506: Determine the code blocks that meet the null check protection structure from the target program code to obtain the null check protection code blocks in the target program code; the null check protection structure is processed based on the conditional null check information, the arithmetic null check information, and the pre-assignment information.

[0132] Step S507: Obtain the null check protection detection result based on the null check protection code blocks; the null check protection detection result is used to indicate whether the target program code contains code blocks that meet the null check protection structure.

[0133] Step S508: Perform a consistency detection process on the risk code blocks in the risk code detection result and the null check protection code blocks in the null check protection detection result to obtain the consistency detection result.

[0134] Step S509: Determine the difference code blocks between the risk code blocks and the null check protection code blocks based on the consistency detection result.

[0135] Step S510: Obtain the code test result based on the difference code blocks; the code test result is used to indicate whether there are code blocks in the target program code that cause the program to crash.

[0136] The above code test method can achieve the following beneficial effects: By detecting whether there are risk code blocks in the target program code to be tested and detecting whether there is a null check protection structure in the target program code during the code compilation stage, so as to comprehensively judge whether there are code blocks in the target program code that cause the program to crash by combining the risk code detection result and the null check protection detection result, thereby identifying the problem code blocks that cause the program to crash in the pre-test stage, effectively improving the recognition accuracy of the problem code, and also increasing the solution time of the problem code blocks, thus improving the stability and reliability of the target program code.

[0137] To more clearly illustrate the code testing method provided by the embodiments of the present disclosure, the above code testing method will be specifically described below with a specific embodiment. As Figure 6 shown, another code testing method is provided, which can be applied to a terminal and specifically includes the following contents:

[0138] (1) Obtain test code through static scanning.

[0139] (2) Obtain the first code block called, the second code block declared as a class attribute, the third code block representing the method definition code, and the fourth code block with parameters passed in when called from the test code. Take the intersection of the first code block and the second code block, and then remove the third code block and the fourth code block from the intersection to obtain the code block belonging to the risk structure, that is, the code block that may be a null value when called.

[0140] (3) Obtain the code block that meets any one of the null-check protection structures from the test code; among them, the null-check protection structures are as follows: 1) if class condition null-check; 2) ternary operation null-check; 3) assignment before code block call.

[0141] (4) Data comparison:

[0142] Since the matching processes of the risk structure and the null-check protection structure are independent of each other, when performing data comparison, the consistency judgment of the code block objects in the risk structure and the null-check protection structure also needs to simultaneously meet the following conditions:

[0143] 1) Inside the same method implementation;

[0144] 2) The name of the called code block is the same as the name of the code block in the null-check protection;

[0145] 3) The line number where the code block in the null-check protection structure is located is less than or equal to the line number where the code block in the risk structure is located.

[0146] Finally, take the difference set of the code blocks in the null-check protection structure and the code blocks in the risk structure to obtain the problem structure with Crash risk.

[0147] In this embodiment, the following beneficial effects can be achieved: 1) Match the called and uninitialized Block in the code as the risk structure, and through the null-check protection check before Block call, the Crash risk can be accurately discovered; 2) Based on static code analysis, it takes a short time and is convenient for checking incremental submissions at any time; 3) Potential problems can be discovered in advance.

[0148] It should be understood that although the steps in the flowcharts involved in the above-described embodiments are sequentially shown according to the indications of the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless there is a clear indication in this article, the execution of these steps has no strict order limit, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above-described embodiments may include multiple steps or multiple stages. These steps or stages are not necessarily executed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be executed alternately or in turn with at least a part of other steps or steps or stages in other steps.

[0149] Based on the same inventive concept, an embodiment of the present application also provides a code testing device for implementing the above-mentioned code testing method. The solution provided by this device for solving problems is similar to the solution described in the above method. Therefore, the specific limitations in one or more embodiments of the following code testing devices can refer to the limitations on the code testing method in the above text, and will not be repeated here.

[0150] In one embodiment, as Figure 7 shown, a code testing device 700 is provided, including: a test code acquisition module 701, a risk code detection module 702, a null pointer protection detection module 703, and a test result acquisition module 704, where:

[0151] The test code acquisition module 701 is configured to acquire the target program code to be tested.

[0152] The risk code detection module 702 is configured to perform risk code detection processing on the target program code to obtain a risk code detection result of the target program code; the risk code detection result is used to indicate whether the target program code contains a code block that satisfies the risk structure; the risk structure is used to represent a code structure that is called and not implemented.

[0153] The null pointer protection detection module 703 is configured to perform null pointer protection detection processing on the target program code to obtain a null pointer protection detection result of the target program code; the null pointer protection detection result is used to indicate whether the target program code contains a code block that satisfies the null pointer protection structure.

[0154] The test result acquisition module 704 is configured to obtain a code test result of the target program code according to the risk code detection result and the null pointer protection detection result; the code test result is used to indicate whether there is a code block in the target program code that causes the program to crash.

[0155] In one embodiment, the risk code detection module 702 is further configured to determine, from the target program code, a first code block, a second code block, a third code block, and a fourth code block; the first code block is the called code block; the second code block is the code block belonging to the attribute declaration of the class; the third code block is the code block including the code representing the method definition; the fourth code block is the code block with parameters passed in when called; and obtain a risk code detection result according to the first code block, the second code block, the third code block, and the fourth code block.

[0156] In one embodiment, the code testing device 700 further includes a code block determination module, configured to determine, from the abstract syntax tree of the target program code, a first abstract syntax tree; determine, from the abstract syntax tree of the target program code, the abstract syntax tree that meets the preset conditions and set it as the second abstract syntax tree; the preset conditions represent that the code block of the abstract syntax tree has performed method definition after the attribute declaration of the class; and obtain the second code block according to the difference structure between the first abstract syntax tree and the second abstract syntax tree.

[0157] In one embodiment, the code testing device 700 further includes a detection result determination module, configured to determine the intersection code block between the first code block and the second code block; remove the code blocks in the intersection code block that match the third code block and / or the fourth code block to obtain a risk code detection result.

[0158] In one embodiment, the null check protection detection module 703 is further configured to determine, from the target program code, the code blocks that meet the null check protection structure to obtain the null check protection code blocks in the target program code; the null check protection structure is processed according to the conditional null check information, the arithmetic null check information, and the pre-assignment information; and obtain a null check protection detection result according to the null check protection code blocks.

[0159] In one embodiment, the test result acquisition module 704 is further configured to perform consistency detection processing on the risk code blocks in the risk code detection result and the null check protection code blocks in the null check protection detection result to obtain a consistency detection result; determine the difference code blocks between the risk code blocks and the null check protection code blocks according to the consistency detection result; and obtain a code testing result according to the difference code blocks.

[0160] In one embodiment, the code testing device 700 further includes a consistency detection module, which is configured to perform consistency detection processing on the method information corresponding to the risk code block and the method information corresponding to the null-check protection code block to obtain a method detection result; perform consistency detection processing on the name information of the risk code block and the name information of the null-check protection code block to obtain a name detection result; compare the line number information where the risk code block is located with the line number information where the null-check protection code block is located to obtain a line number comparison result; and obtain a consistency detection result according to the method detection result, the name detection result, and the line number comparison result.

[0161] In one embodiment, the test code acquisition module 701 is further configured to upload the project to the project management library; if a code update message triggered for the project in the project management library is detected, obtain the updated code corresponding to the code update message; and set the updated code as the target program code to be tested.

[0162] Each module in the above code testing device can be implemented in whole or in part by software, hardware, and their combination. The above modules can be embedded in the processor in the computer device in hardware form or independent of it, or stored in the memory in the computer device in software form, so that the processor can call and execute the operations corresponding to the above modules.

[0163] In one embodiment, a computer device is provided. The computer device can be a terminal, and its internal structure diagram can be as Figure 8 shown. The computer device includes a processor, a memory, an input / output interface, a communication interface, a display unit, and an input device. Among them, the processor, the memory, and the input / output interface are connected through a system bus, and the communication interface, the display unit, and the input device are connected to the system bus through the input / output interface. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The input / output interface of the computer device is used to exchange information between the processor and external devices. The communication interface of the computer device is used to communicate with an external terminal in a wired or wireless manner, and the wireless manner can be implemented through WIFI, a mobile cellular network, NFC (Near Field Communication), or other technologies. The computer program, when executed by the processor, is used to implement a code testing method. The display unit of the computer device is used to form a visually visible picture, which can be a display screen, a projection device, or a virtual reality imaging device. The display screen can be a liquid crystal display screen or an electronic ink display screen. The input device of the computer device can be a touch layer covered on the display screen, or a button, a trackball, or a touchpad provided on the housing of the computer device, or an external keyboard, a touchpad, or a mouse, etc.

[0164] Those skilled in the art can understand that Figure 8 the structure shown in [the figure] is only a block diagram of some structures related to the solution of this application, and does not constitute a limitation on the computer device to which the solution of this application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine some components, or have different component arrangements.

[0165] In one embodiment, a computer device is further provided, including a memory and a processor. A computer program is stored in the memory, and when the processor executes the computer program, the steps in the above method embodiments are implemented.

[0166] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored, and when the computer program is executed by a processor, the steps in the above method embodiments are implemented.

[0167] In one embodiment, a computer program product is provided, including a computer program, and when the computer program is executed by a processor, the steps in the above method embodiments are implemented.

[0168] Those of ordinary skill in the art can understand that all or part of the processes in the above-described embodiment methods can be completed by instructing relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above methods. Among them, any reference to a memory, database, or other medium used in the embodiments provided in the present application can include at least one of non-volatile and volatile memories. Non-volatile memories can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memories can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc. The databases involved in the embodiments provided in the present application can include at least one of relational databases and non-relational databases. Non-relational databases can include distributed databases based on blockchain, etc., without limitation. The processors involved in the embodiments provided in the present application can be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, data processing logics based on quantum computing, etc., without limitation.

[0169] The technical features of the above embodiments can be combined arbitrarily. For the sake of concise description, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, it should be considered as the scope described in this specification.

[0170] The above-described embodiments only represent several implementation manners of the present application. The description is relatively specific and detailed, but it should not be construed as a limitation on the patent scope of the present application. It should be noted that for those of ordinary skill in the art, without departing from the concept of the present application, several modifications and improvements can still be made, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application should be subject to the appended claims.

Claims

1. A code testing method, characterized in that, The method includes: Obtaining target program code to be tested; Performing risk code detection processing on the target program code to obtain a risk code detection result of the target program code; the risk code detection result is used to characterize whether the target program code contains a code block that meets a risk structure; the risk structure is used to characterize a code structure that is called and not implemented; Performing null protection detection processing on the target program code to obtain a null protection detection result of the target program code; the null protection detection result is used to characterize whether the target program code contains a code block that meets a null protection structure; Obtaining a code test result of the target program code according to the risk code detection result and the null protection detection result; the code test result is used to indicate whether there is a code block in the target program code that causes the program to crash.

2. The method according to claim 1, wherein The performing risk code detection processing on the target program code to obtain a risk code detection result of the target program code includes: Determining a first code block, a second code block, a third code block, and a fourth code block from the target program code; the first code block is a called code block; the second code block is a code block belonging to a class attribute declaration; the third code block is a code block containing a method definition code; the fourth code block is a code block with parameters passed in when called; Obtaining the risk code detection result according to the first code block, the second code block, the third code block, and the fourth code block.

3. The method according to claim 2, wherein The second code block is obtained by the following method: Determining a first abstract syntax tree from the abstract syntax tree of the target program code; Determining an abstract syntax tree that meets a preset condition from the abstract syntax tree of the target program code and setting it as a second abstract syntax tree; The preset condition characterizes that the code block of the abstract syntax tree has a method definition after being used as a class attribute declaration; Obtaining the second code block according to the difference structure between the first abstract syntax tree and the second abstract syntax tree.

4. The method according to claim 2, wherein The obtaining the risk code detection result according to the first code block, the second code block, the third code block, and the fourth code block includes: Determining an intersection code block between the first code block and the second code block; Removing the code blocks in the intersection code block that match the third code block and / or the fourth code block to obtain the risk code detection result.

5. The method according to claim 1, wherein The performing null protection detection processing on the target program code to obtain a null protection detection result of the target program code includes: Determining a code block that meets the null protection structure from the target program code to obtain a null protection code block in the target program code; the null protection structure is processed according to conditional null information, arithmetic null information, and pre-assignment information; Obtaining the null protection detection result according to the null protection code block.

6. The method according to claim 1, wherein The obtaining the code test result of the target program code according to the risk code detection result and the null protection detection result includes: Perform a consistency detection process on the risk code block in the risk code detection result and the null check protection code block in the null check protection detection result to obtain a consistency detection result; Determine the difference code block between the risk code block and the null check protection code block according to the consistency detection result; Obtain the code test result according to the difference code block.

7. The method according to claim 6, wherein The performing a consistency detection process on the risk code block in the risk code detection result and the null check protection code block in the null check protection detection result to obtain a consistency detection result includes: Perform a consistency detection process on the method information corresponding to the risk code block and the method information corresponding to the null check protection code block to obtain a method detection result; Perform a consistency detection process on the name information of the risk code block and the name information of the null check protection code block to obtain a name detection result; Compare the line number information where the risk code block is located with the line number information where the null check protection code block is located to obtain a line number comparison result; Obtain the consistency detection result according to the method detection result, the name detection result, and the line number comparison result.

8. The method according to claim 1, wherein The obtaining the target program code to be tested includes: Upload the project to the project management library; If a code update message triggered for the project in the project management library is detected, obtain the updated code corresponding to the code update message; Set the updated code as the target program code to be tested.

9. A computer device, comprising a memory and a processor, the memory storing a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 8.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the steps of the method according to any one of claims 1 to 8.

11. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by the processor, it implements the steps of the method according to any one of claims 1 to 8.