Test script generation method and device, equipment, medium and program product
By training and filtering sensitive features of the program code under test in the test framework, and generating test scripts with reinforcement learning algorithms and reward functions, the problems of diversity and data security risks of test frameworks are solved, and efficient and secure test script generation is achieved.
Patent Information
- Application Number
- CN202411836468.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-13
- Publication Date
- 2025-03-14
AI Technical Summary
The testing frameworks are diverse and different writing methods are available for different codes, which causes developers to spend a lot of effort to design and construct test scripts, which increases the technical threshold. At the same time, intelligently generating single-testing platforms has data security risks, which may lead to the leakage of sensitive information.
By conducting sensitive feature training based on the sensitive data set, sensitive data features are obtained. When it is determined that the program code to be tested contains sensitive program code, it is filtered to obtain the filtered program code. Then, a reinforcement learning algorithm and reward function are used to generate a target test script to avoid direct exposure of sensitive data to external models.
Improve data security, avoid leakage of sensitive data during the generation of test scripts, reduce data security risks, and improve the generation efficiency and quality of test scripts.
Smart Images

Figure CN119621583A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of script generation, and in particular to a test script generation method, device, equipment, medium and program product. Background Art
[0002] The test framework is diverse and has different writing methods for different codes. It takes a lot of effort for developers to design and construct, which raises the technical threshold for developers to a certain extent. In order to write test scripts with high quality and efficiency, a large number of intelligent question-answering robots are used for intelligent generation of single tests. When enterprises use the intelligent single test generation platform for production, they face some data security risks. For example, the conversation data of the intelligent single test generation platform can be used for retraining, which means that some information may be seen by other users; data sent through the application programming interface (API) of the intelligent single test generation platform may be leaked; data exposed to third-party sub-processors also has the risk of exposure, resulting in low data security. Summary of the invention
[0003] Embodiments of the present application provide a test script generation method, apparatus, device, medium, and program product to solve the problem of low security of related data.
[0004] To solve the above technical problems, this application is implemented as follows:
[0005] In a first aspect, an embodiment of the present application provides a test script generation method, comprising:
[0006] Perform sensitive feature training based on sensitive data sets to obtain sensitive data features;
[0007] In the case where it is determined based on the sensitive data feature that the program code under test includes sensitive program code, performing data filtering processing on the program code under test to obtain filtered program code;
[0008] Based on the filtered program code, a target test script is generated using a reinforcement learning algorithm and a reward function.
[0009] Optionally, performing sensitive feature training based on the sensitive data set to obtain sensitive data features includes:
[0010] Extracting features of program codes in the sensitive data set to obtain feature sequences of the codes;
[0011] Extracting features from the annotation sentences in the sensitive data set to obtain a feature sequence of the annotations;
[0012] Concatenate the feature sequence of the code and the feature sequence of the annotation to obtain an input sequence;
[0013] The input sequence is input into a recurrent neural network (RNN) for training to obtain sensitive data features.
[0014] Optionally, the method further comprises:
[0015] Building a decision tree based on the sensitive data features;
[0016] The program code under test is input into the decision tree to determine whether the program code under test includes sensitive program code.
[0017] Optionally, generating a target test script based on the filtered program code using a reinforcement learning algorithm and a reward function includes:
[0018] Get test code data;
[0019] Using the test code data to train the initial model to obtain a pre-trained model;
[0020] Fine-tune the pre-trained model using a reinforcement learning algorithm and set a reward function to obtain a target model;
[0021] The filtered program code is input into the target model to obtain a target test script.
[0022] Optionally, inputting the filtered program code into the target model to obtain a target test script includes:
[0023] Inputting the filtered program code into the target model to generate multiple test scripts;
[0024] Determine reward function values of the plurality of test scripts respectively based on the reward function in the target model;
[0025] Determining cumulative reward values of the multiple test scripts respectively based on the action value function in the target model and the reward function values of the multiple test scripts;
[0026] A target test script is determined, where the target test script is a test script having the largest cumulative reward value among the multiple test scripts.
[0027] Optionally, the method further comprises:
[0028] Determining parameter values of the action-value function based on the loss function;
[0029] The action-value function is updated based on the parameter value.
[0030] Optionally, the reward function value is the sum of a first value and a second value, the first value is the product of a first preset value and a parameter used to characterize the degree of user recognition, and the second value is the product of a second preset value and a parameter used to characterize the executable degree of the code.
[0031] In a second aspect, an embodiment of the present application provides a test script generating device, including:
[0032] A training module is used to perform sensitive feature training based on sensitive data sets to obtain sensitive data features;
[0033] A filtering module, configured to perform data filtering processing on the program code under test to obtain a filtered program code when it is determined based on the sensitive data feature that the program code under test includes sensitive program code;
[0034] A generation module is used to generate a target test script based on the filtered program code using a reinforcement learning algorithm and a reward function.
[0035] Optionally, the training module includes:
[0036] A first extraction unit is used to extract features from the program code in the sensitive data set to obtain a feature sequence of the code;
[0037] A second extraction unit is used to extract features from the annotation sentences in the sensitive data set to obtain a feature sequence of the annotations;
[0038] A processing unit, used for concatenating the feature sequence of the code and the feature sequence of the annotation to obtain an input sequence;
[0039] The first training unit is used to input the input sequence into a recurrent neural network RNN for training to obtain sensitive data features.
[0040] Optionally, the device further comprises:
[0041] A construction module, used to construct a decision tree based on the sensitive data features;
[0042] The input module is used to input the program code under test into the decision tree to determine whether the program code under test includes sensitive program code.
[0043] Optionally, the generating module includes:
[0044] An acquisition unit, used for acquiring test code data;
[0045] A second training unit is used to train the initial model using the test code data to obtain a pre-trained model;
[0046] A fine-tuning unit, used to fine-tune the pre-trained model using a reinforcement learning algorithm and set a reward function to obtain a target model;
[0047] An input unit is used to input the filtered program code into the target model to obtain a target test script.
[0048] Optionally, the input unit is specifically used for:
[0049] Inputting the filtered program code into the target model to generate multiple test scripts;
[0050] Determine reward function values of the plurality of test scripts respectively based on the reward function in the target model;
[0051] Determining cumulative reward values of the multiple test scripts respectively based on the action value function in the target model and the reward function values of the multiple test scripts;
[0052] A target test script is determined, where the target test script is a test script having the largest cumulative reward value among the multiple test scripts.
[0053] Optionally, the device further comprises:
[0054] A determination module, configured to determine a parameter value of the action-value function based on a loss function;
[0055] An updating module is used to update the action-value function based on the parameter value.
[0056] Optionally, the reward function value is the sum of a first value and a second value, the first value is the product of a first preset value and a parameter used to characterize the degree of user recognition, and the second value is the product of a second preset value and a parameter used to characterize the executable degree of the code.
[0057] In a third aspect, an embodiment of the present application provides an electronic device, including a transceiver and a processor, wherein the processor is configured to:
[0058] Perform sensitive feature training based on sensitive data sets to obtain sensitive data features;
[0059] In the case where it is determined based on the sensitive data feature that the program code under test includes sensitive program code, performing data filtering processing on the program code under test to obtain filtered program code;
[0060] Based on the filtered program code, a target test script is generated using a reinforcement learning algorithm and a reward function.
[0061] Optionally, the processor is specifically configured to:
[0062] Extracting features of program codes in the sensitive data set to obtain feature sequences of the codes;
[0063] Extracting features from the annotation sentences in the sensitive data set to obtain a feature sequence of the annotations;
[0064] Concatenate the feature sequence of the code and the feature sequence of the annotation to obtain an input sequence;
[0065] The input sequence is input into a recurrent neural network (RNN) for training to obtain sensitive data features.
[0066] Optionally, the processor is further configured to:
[0067] Building a decision tree based on the sensitive data features;
[0068] The program code under test is input into the decision tree to determine whether the program code under test includes sensitive program code.
[0069] Optionally, the processor is specifically configured to:
[0070] Get test code data;
[0071] Using the test code data to train the initial model to obtain a pre-trained model;
[0072] Fine-tune the pre-trained model using a reinforcement learning algorithm and set a reward function to obtain a target model;
[0073] The filtered program code is input into the target model to obtain a target test script.
[0074] Optionally, the processor is specifically configured to:
[0075] Inputting the filtered program code into the target model to generate multiple test scripts;
[0076] Determine reward function values of the plurality of test scripts respectively based on the reward function in the target model;
[0077] Determining cumulative reward values of the multiple test scripts respectively based on the action value function in the target model and the reward function values of the multiple test scripts;
[0078] A target test script is determined, where the target test script is a test script having the largest cumulative reward value among the multiple test scripts.
[0079] Optionally, the processor is further configured to:
[0080] Determining parameter values of the action-value function based on the loss function;
[0081] The action-value function is updated based on the parameter value.
[0082] Optionally, the reward function value is the sum of a first value and a second value, the first value is the product of a first preset value and a parameter used to characterize the degree of user recognition, and the second value is the product of a second preset value and a parameter used to characterize the executable degree of the code.
[0083] In a fourth aspect, an embodiment of the present application provides an electronic device, comprising: a processor, a memory, and a program stored in the memory and executable on the processor, wherein the program, when executed by the processor, implements the steps of the test script generation method described in the first aspect above.
[0084] In a fifth aspect, an embodiment of the present application provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the test script generation method described in the first aspect above are implemented.
[0085] In a sixth aspect, an embodiment of the present application provides a computer program product, comprising computer instructions, which, when executed by a processor, implement the steps of the test script generation method described in the first aspect above.
[0086] In an embodiment of the present application, the above-mentioned test script generation method can obtain sensitive data features by performing sensitive feature training based on a sensitive data set, and when it is determined based on the sensitive data features that the program code under test includes sensitive program code, data filtering processing is performed on the program code under test to obtain filtered program code, and based on the filtered program code, a target test script is generated using a reinforcement learning algorithm and a reward function, so that sensitive data filtering can be performed before the target test script is generated using the reinforcement learning algorithm and the reward function, which can avoid direct exposure of sensitive data to the external model used to generate the test script, so as to prevent data leakage, thereby improving data security. BRIEF DESCRIPTION OF THE DRAWINGS
[0087] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the description of the embodiments of the present application will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0088] Figure 1 is a flow chart of a test script generation method provided by an embodiment of the present application;
[0089] Figure 2It is a structural diagram of a decision tree provided in an embodiment of the present application;
[0090] Figure 3 It is an application flow chart of a test script generation method provided by an embodiment of the present application;
[0091] Figure 4 It is a structural schematic diagram of a test script generating device provided in an embodiment of the present application;
[0092] Figure 5 It is a structural schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0093] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0094] For ease of understanding, some contents involved in the embodiments of the present application are described below:
[0095] In order to write test scripts with high quality and efficiency, a large number of intelligent question-and-answer robots are used to intelligently generate unit tests. For example, the more popular ones recently include Tongyi Lingma, Baidu Comate, and iFlyCode. Tongyi Lingma is an intelligent coding assistance tool produced by Alibaba Cloud based on the Tongyi big model, which can generate unit tests in the form of intelligent question-and-answer; iFlyCode is an intelligent programming assistant launched by iFlytek, which uses natural language processing and machine learning technology to make the automatic generation of unit tests easier.
[0096] At present, the common method of automatically generating single tests is generally based on natural language processing (NLP) models. Common NLP models include embeddings from language models (ELMo), generative pre-trained transformer (GPT), bidirectional encoder representations from transformers (BERT), etc.
[0097] Common methods to solve data security problems include data desensitization technology, namely data encryption and anonymization; secondly, regularly review the training data, or add some security filters to the model output to prevent the output of sensitive information.
[0098] The related technology provides an optimization method for using reinforcement learning for transformer models, but its performance is only comparable to that of random strategies, and the reward function is not designed based on the actual situation. Regarding data security issues, the related technical means are all for processing sensitive data after it is input into the model, and there are two disadvantages:
[0099] 1. Even if the data input into the model is not displayed after processing, it will be recorded by the intelligent generation unit test platform, and the data will also face the risk of exposure.
[0100] 2. The intelligent code generation platform is developing rapidly, and the setting of security filters is not yet mature, which means that in the future humans may be able to bypass security filters and obtain sensitive data in some way.
[0101] In the embodiments of the present application, a test script generation method, apparatus, device, medium and program product are proposed to solve the problem of low security of related data.
[0102] See also Figure 1 , Figure 1 is a flow chart of a test script generation method provided by an embodiment of the present application, such as Figure 1 As shown, the method comprises the following steps:
[0103] Step 101: Perform sensitive feature training based on the sensitive data set to obtain sensitive data features.
[0104] Specifically, the above-mentioned sensitive data set may refer to a data set containing sensitive information, and the sensitive information may be data that, once leaked, lost or improperly used, will have a serious negative impact on the company's business, reputation, finance or legal compliance, etc. For example, it may include commercial secrets (such as product development data, marketing strategies and customer data, business partner information), financial data (such as financial statements and budget information, tax and audit information), personnel data (such as employee personal information, employee performance evaluation and training records) and other information, which can be obtained from within the company. The above-mentioned sensitive feature training can be understood as extracting sensitive data features from the sensitive data set.
[0105] Step 102: When it is determined based on the sensitive data feature that the program code under test includes sensitive program code, data filtering is performed on the program code under test to obtain filtered program code.
[0106] Specifically, the above-mentioned program code under test can be a program code for generating a test script. The above-mentioned data filtering of the program code under test can be to intercept several statements representing the code semantics in the sensitive program code, such as sumAdd, setOrderObject, getParam, etc., and the "generate front-end test script" can also be spliced with the statements representing the code semantics in the filtered program code as the input text of the reinforcement learning algorithm.
[0107] Step 103: Based on the filtered program code, a target test script is generated using a reinforcement learning algorithm and a reward function.
[0108] Specifically, the above-mentioned generation of the target test script using the reinforcement learning algorithm and the reward function can be to automatically generate a test script that meets the requirements by applying reinforcement learning and using environmental feedback (reward function). The intelligent agent aims to maximize the reward obtained in the test environment, and generates the target test script by continuously trying different operation sequences, finding and optimizing the test path. The above-mentioned target test script can be understood as a test script with a maximum value corresponding to the representation value of the reward function.
[0109] In an embodiment of the present application, the above-mentioned test script generation method can obtain sensitive data features by performing sensitive feature training based on a sensitive data set, and when it is determined based on the sensitive data features that the program code under test includes sensitive program code, data filtering processing is performed on the program code under test to obtain filtered program code, and based on the filtered program code, a target test script is generated using a reinforcement learning algorithm and a reward function, so that sensitive data filtering can be performed before the target test script is generated using the reinforcement learning algorithm and the reward function, which can avoid direct exposure of sensitive data to the external model used to generate the test script, so as to prevent data leakage, thereby improving data security.
[0110] Optionally, performing sensitive feature training based on the sensitive data set to obtain sensitive data features includes:
[0111] Extracting features of program codes in the sensitive data set to obtain feature sequences of the codes;
[0112] Extracting features from the annotation sentences in the sensitive data set to obtain a feature sequence of the annotations;
[0113] Concatenate the feature sequence of the code and the feature sequence of the annotation to obtain an input sequence;
[0114] The input sequence is input into a recurrent neural network (RNN) for training to obtain sensitive data features.
[0115] Specifically, the feature extraction of the program code in the sensitive data set can be to extract the semantic features corresponding to the program code. Exemplarily, the code can be parsed first, the program is lexically analyzed, the program is read according to preset rules, and a tag classification list is obtained. The tag classification list may include grammatical keywords, operators, statement end positions, code data, etc.; the program is grammatically analyzed to obtain an abstract syntax tree (Abstract Syntax Tree, AST) annotated with sensitive information. In order to alleviate the problem of gradient disappearance that is prone to occur during training, the AST syntax tree can be segmented to obtain several small abstract syntax trees. The nodes in the small abstract syntax tree are encoded, and after obtaining the node vector, the node vector is detected by a specific feature detector through a convolutional layer to obtain the semantic information of the small syntax tree. After passing through the maximum pooling layer, the maximum eigenvalue of the semantic information is obtained to obtain a vector sequence of semantic features, that is, the feature sequence of the code.
[0116] The feature extraction of the comment statements in the sensitive data set can be to extract the semantic features corresponding to the comment statements. For example, the comment statements in the code can be first obtained, and the sequential dependency between the local code and the global code can be obtained by using the self-attention mechanism, and the sequential dependency can be encoded into a vector to obtain the sequence information of the program code. The self-attention mechanism process is expressed as follows:
[0117]
[0118] After the multi-head attention mechanism i (Q, K, V) enters the feedforward neural network for further processing, and after residual connection and normalization, a vector sequence of semantic features is obtained.
[0119] The feature sequence of the code and the feature sequence of the annotation are concatenated to obtain an input sequence. The concatenation process is shown as follows:
[0120]
[0121] Then input H into the RNN network to obtain the final training result, that is, the sensitive data features W1, W2, W3…W n .
[0122] In this embodiment, the test script generation method can extract features from the program code in the sensitive data set to obtain a feature sequence of the code, extract features from the comment statements in the sensitive data set to obtain a feature sequence of the comment, concatenate the feature sequence of the code and the feature sequence of the comment to obtain an input sequence, input the input sequence into a recurrent neural network RNN for training to obtain sensitive data features, so that code semantics and comment semantics are considered when extracting sensitive features, and the two features are fused, and sensitivity judgment is made based on the full features of the two, thereby improving the accuracy of the sensitive data features, thereby improving the accuracy of judging sensitive data.
[0123] Optionally, the method further comprises:
[0124] Building a decision tree based on the sensitive data features;
[0125] The program code under test is input into the decision tree to determine whether the program code under test includes sensitive program code.
[0126] Specifically, the above-mentioned construction of a decision tree based on the sensitive data features can be understood as using the sensitive features as the judgment criteria of the decision tree to construct a decision tree. Exemplarily, the code features of each node are determined using the Classification and Regression Trees (CART) Gini coefficient, and the feature with the smallest Gini coefficient is selected as the initial node. The formula is as follows:
[0127]
[0128] Where D is the code dataset, i is the number of categories, currently divided into sensitive code and non-sensitive code, p i It is the proportion of samples of category i in the divided data set after each feature is divided.
[0129] Assume that after the first division, the sensitive data feature W1 is selected as the initial node, and its Gini coefficient is not 0. Then W1 is the initial node. After the two types of data are classified, they are used as their respective data sets again, and their respective child nodes are selected. The formula is shown above, and the child nodes are calculated cyclically until a node with a Gini coefficient of 0 appears, such as Figure 2 shown.
[0130] In this embodiment, the test script generation method can construct a decision tree based on the sensitive data features, input the program code under test into the decision tree, and determine whether the program code under test includes sensitive program code. After the decision tree model is built, the process of sensitive data judgment on new data is mainly to compare and judge along the nodes of the tree, and the computational complexity is relatively low, so that the data can be classified and judged relatively quickly when processing large-scale data.
[0131] Optionally, generating a target test script based on the filtered program code using a reinforcement learning algorithm and a reward function includes:
[0132] Get test code data;
[0133] Using the test code data to train the initial model to obtain a pre-trained model;
[0134] Fine-tune the pre-trained model using a reinforcement learning algorithm and set a reward function to obtain a target model;
[0135] The filtered program code is input into the target model to obtain a target test script.
[0136] Specifically, the test code data may include code snippets, input and output examples, code structure, function verification methods and other information in various test scenarios, which are used for the initial model to learn the test code writing mode, verification logic and other rules. The initial model may adopt a Bidirectional Encoder Representations from Transformers (BERT) model.
[0137] The above-mentioned pre-trained model can be understood as a model that has learned the general rules of the test code. The above-mentioned use of the test code data to train the initial model to obtain the pre-trained model can be to first serialize and fill the test code data to form a pattern-recognizable token. When the token is too long, it needs to be segmented and the token is input into the model for training. After fine-tuning, the general rules of the test code can be obtained.
[0138] In this embodiment, the above-mentioned test script generation method can obtain test code data, use the test code data to train the initial model to obtain a pre-trained model, use a reinforcement learning algorithm to fine-tune the pre-trained model, and set a reward function to obtain a target model, and input the filtered program code into the target model to obtain a target test script, thereby improving the accuracy of the test script generated by the target model.
[0139] Optionally, inputting the filtered program code into the target model to obtain a target test script includes:
[0140] Inputting the filtered program code into the target model to generate multiple test scripts;
[0141] Determine reward function values of the plurality of test scripts respectively based on the reward function in the target model;
[0142] Determining cumulative reward values of the multiple test scripts respectively based on the action value function in the target model and the reward function values of the multiple test scripts;
[0143] A target test script is determined, where the target test script is a test script having the largest cumulative reward value among the multiple test scripts.
[0144] Specifically, the above cumulative reward value can be used to characterize the probability of the dynamically generated test script successfully adapting the code, which can be determined based on the cumulative reward function. The cumulative reward Q n+1 is the action value function, which represents the trade-off between the long-term reward and the current reward generated by selecting the current test script. For example, Q n+1 The representation of is as follows:
[0145] Q n+1 (s t ,a;w)=R+γ·maxQ′ n (s' t+1 ,a′)
[0146] Among them, γ∈(0,1) represents the discount factor, which is a value between 0 and 1 and is used to measure the importance of future rewards. t represents the test code completion status at the current time t, w is the BERT model parameter; a represents the strategy that can be taken in the current state, n represents the number of iterations, R is the reward function value of the current selection, Q′ n (s' t+1 ,a′) is the long-term reward predicted in the target model.
[0147] When the Q value of an action is selected, Q = argmaxQ'(s, a), it means that the choice is optimal.
[0148] It can be understood that the state space S1 is a number of possible code blocks that are determined after model pre-training to form the test code, and the policy space is the code fragments of all possible state spaces. Since the state space is large and the strategy is uncertain, the BERT model can be used as a policy network to generate the probability distribution of the next code block through the model to define the strategy.
[0149] For example, Figure 3 is an application flow chart of a test script generation method provided in an embodiment of the present application, such as Figure 3 As shown:
[0150] The input of the model includes three parts: the code to be tested S1, the existing test template S2, and the test data S3. The code to be tested S1 is used to determine the subject of the test, the relevant test template S2 can find the fixed usage of the unit test language and several test script templates by traversing the syntax of the official website of the test framework, and the test data S3 is used to generate assertions to judge the correctness of the code.
[0151] In this embodiment, the above-mentioned test script generation method can generate multiple test scripts by inputting the filtered program code into the target model, and respectively determine the reward function values of the multiple test scripts based on the reward function in the target model, and respectively determine the cumulative reward values of the multiple test scripts based on the action value function in the target model and the reward function values of the multiple test scripts, and determine the target test script, wherein the target test script is the test script with the largest cumulative reward value among the multiple test scripts, and then the test script with the highest probability of successfully adapting the code to the dynamically generated test script can be obtained, thereby improving the executable degree of the test script.
[0152] Optionally, the method further comprises:
[0153] Determining parameter values of the action-value function based on the loss function;
[0154] The action-value function is updated based on the parameter value.
[0155] Specifically, the above loss function can be used for
[0156] Exemplarily, the loss function is as follows:
[0157] L(w)=E (s,a)~D [(Q n (s t ,a;w)-Q n+1 (s t ,a;w)) 2 ]
[0158] Where D is the experience replay buffer or dataset containing the state-policy pairs learned in the system.
[0159] It is understandable that the goal of the above loss function is to minimize the change of the cumulative reward Q function between two consecutive iterations to ensure that the update of the Q function is smooth and stable. The loss function is partialed and the updated w value is obtained as follows:
[0160]
[0161] δ=Q n (s t ,a;w)-Q n+1 (s t ,a;w)
[0162] In this embodiment, the above-mentioned test script generation method can help improve the efficiency and accuracy of the algorithm by determining the parameter value of the action value function based on the loss function, and updating the action value function based on the parameter value, thereby improving the efficiency and accuracy of generating the target test script.
[0163] Optionally, the reward function value is the sum of a first value and a second value, the first value is the product of a first preset value and a parameter used to characterize the degree of user recognition, and the second value is the product of a second preset value and a parameter used to characterize the executable degree of the code.
[0164] Specifically, the above reward function value may be a value determined based on a reward function. Exemplarily, the reward function may be expressed as:
[0165] R=α·H a +β·H d
[0166] H a =δ·H p +ε·H l
[0167] Among them, R is the reward function value, α is the first preset value, β is the second preset value, H a H is a parameter that characterizes the degree of user recognition. d H is a parameter that characterizes the executableness of the code. p H is the parameter that characterizes user evaluation. l For back-and-forth conversation turns.
[0168] In this embodiment, the above-mentioned test script generation method can be that the reward function value is the sum of a first value and a second value, wherein the first value is the product of a first preset value and a parameter used to characterize the user's recognition degree, and the second value is the product of a second preset value and a parameter used to characterize the code executable degree, so that the target model takes into account the user's recognition degree and the code executable degree in the process of generating the test script, thereby making the generated test script more in line with the actual needs of the user and being able to improve the executable degree of the test script.
[0169] See also Figure 4 , Figure 4 is a schematic diagram of the structure of a test script generating device provided in an embodiment of the present application, such as Figure 4 As shown, the test script generating device 400 includes:
[0170] A training module 401 is used to perform sensitive feature training based on a sensitive data set to obtain sensitive data features;
[0171] A filtering module 402 is used to perform data filtering processing on the program code under test to obtain a filtered program code when it is determined based on the sensitive data feature that the program code under test includes sensitive program code;
[0172] The generation module 403 is used to generate a target test script based on the filtered program code using a reinforcement learning algorithm and a reward function.
[0173] Optionally, the training module 401 includes:
[0174] A first extraction unit is used to extract features from the program code in the sensitive data set to obtain a feature sequence of the code;
[0175] A second extraction unit is used to extract features from the annotation sentences in the sensitive data set to obtain a feature sequence of the annotations;
[0176] A processing unit, used for concatenating the feature sequence of the code and the feature sequence of the annotation to obtain an input sequence;
[0177] The first training unit is used to input the input sequence into a recurrent neural network RNN for training to obtain sensitive data features.
[0178] Optionally, the device further comprises:
[0179] A construction module, used to construct a decision tree based on the sensitive data features;
[0180] The input module is used to input the program code under test into the decision tree to determine whether the program code under test includes sensitive program code.
[0181] Optionally, the generating module 403 includes:
[0182] An acquisition unit, used for acquiring test code data;
[0183] A second training unit is used to train the initial model using the test code data to obtain a pre-trained model;
[0184] A fine-tuning unit, used to fine-tune the pre-trained model using a reinforcement learning algorithm and set a reward function to obtain a target model;
[0185] An input unit is used to input the filtered program code into the target model to obtain a target test script.
[0186] Optionally, the input unit is specifically used for:
[0187] Inputting the filtered program code into the target model to generate multiple test scripts;
[0188] Determine reward function values of the plurality of test scripts respectively based on the reward function in the target model;
[0189] Determining cumulative reward values of the multiple test scripts respectively based on the action value function in the target model and the reward function values of the multiple test scripts;
[0190] A target test script is determined, where the target test script is a test script having the largest cumulative reward value among the multiple test scripts.
[0191] Optionally, the device further comprises:
[0192] A determination module, configured to determine a parameter value of the action-value function based on a loss function;
[0193] An updating module is used to update the action-value function based on the parameter value.
[0194] Optionally, the reward function value is the sum of a first value and a second value, the first value is the product of a first preset value and a parameter used to characterize the degree of user recognition, and the second value is the product of a second preset value and a parameter used to characterize the executable degree of the code.
[0195] It should be noted that the test script generation device provided in the embodiment of the present application is a device capable of executing the above-mentioned test script generation method, and all implementation methods in the above-mentioned test script generation method embodiment are applicable to the device and can achieve the same or similar beneficial effects. To avoid repeated description, this embodiment will not be repeated.
[0196] For details, see Figure 5 As shown, an embodiment of the present application further provides an electronic device, including a bus 501, a transceiver 502, an antenna 503, a bus interface 504, a processor 505 and a memory 506.
[0197] The processor 505 is configured to:
[0198] Perform sensitive feature training based on sensitive data sets to obtain sensitive data features;
[0199] In the case where it is determined based on the sensitive data feature that the program code under test includes sensitive program code, performing data filtering processing on the program code under test to obtain filtered program code;
[0200] Based on the filtered program code, a target test script is generated using a reinforcement learning algorithm and a reward function.
[0201] exist Figure 5 In the embodiment, the bus architecture (represented by bus 501) is shown, and bus 501 may include any number of interconnected buses and bridges, and bus 501 links various circuits including one or more processors represented by processor 505 and memory represented by memory 506. Bus 501 may also link various other circuits such as peripherals, voltage regulators, and power management circuits, which are well known in the art and are therefore not further described herein. Bus interface 504 provides an interface between bus 501 and transceiver 502. Transceiver 502 may be one element or multiple elements, such as multiple receivers and transmitters, providing a unit for communicating with various other devices on a transmission medium. Data processed by processor 505 is transmitted on a wireless medium via antenna 503, and further, antenna 503 also receives data and transmits the data to processor 505.
[0202] The processor 505 is responsible for managing the bus 501 and general processing, and can also provide various functions, including timing, peripheral interfaces, voltage regulation, power management and other control functions. The memory 506 can be used to store data used by the processor 505 when performing operations.
[0203] Optionally, the processor 505 may be a central processing unit (CPU), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or a complex programmable logic device (CPLD).
[0204] Optionally, the processor 505 is specifically configured to:
[0205] Extracting features of program codes in the sensitive data set to obtain feature sequences of the codes;
[0206] Extracting features from the annotation sentences in the sensitive data set to obtain a feature sequence of the annotations;
[0207] Concatenate the feature sequence of the code and the feature sequence of the annotation to obtain an input sequence;
[0208] The input sequence is input into a recurrent neural network (RNN) for training to obtain sensitive data features.
[0209] Optionally, the processor 505 is further configured to:
[0210] Building a decision tree based on the sensitive data features;
[0211] The program code under test is input into the decision tree to determine whether the program code under test includes sensitive program code.
[0212] Optionally, the processor 505 is specifically configured to:
[0213] Get test code data;
[0214] Using the test code data to train the initial model to obtain a pre-trained model;
[0215] Fine-tune the pre-trained model using a reinforcement learning algorithm and set a reward function to obtain a target model;
[0216] The filtered program code is input into the target model to obtain a target test script.
[0217] Optionally, the processor 505 is specifically configured to:
[0218] Inputting the filtered program code into the target model to generate multiple test scripts;
[0219] Determine reward function values of the plurality of test scripts respectively based on the reward function in the target model;
[0220] Determining cumulative reward values of the multiple test scripts respectively based on the action value function in the target model and the reward function values of the multiple test scripts;
[0221] A target test script is determined, where the target test script is a test script having the largest cumulative reward value among the multiple test scripts.
[0222] Optionally, the processor 505 is further configured to:
[0223] Determining parameter values of the action-value function based on the loss function;
[0224] The action-value function is updated based on the parameter value.
[0225] Optionally, the reward function value is the sum of a first value and a second value, the first value is the product of a first preset value and a parameter used to characterize the degree of user recognition, and the second value is the product of a second preset value and a parameter used to characterize the executable degree of the code.
[0226] It should be noted that the electronic device provided in the embodiment of the present application is a device capable of executing the above-mentioned test script generation method, and all implementation methods in the above-mentioned test script generation method embodiment are applicable to the electronic device and can achieve the same or similar beneficial effects. To avoid repeated description, this embodiment will not be repeated.
[0227] An embodiment of the present application also provides an electronic device, including: a processor, a memory, and a program stored in the memory and executable on the processor. When the program is executed by the processor, the various processes of the above-mentioned test script generation method embodiment are implemented, and the same technical effect can be achieved. To avoid repetition, it will not be repeated here.
[0228] The embodiment of the present application also provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, each process of the test script generation method embodiment described above is implemented, and the same technical effect can be achieved. To avoid repetition, it is not repeated here. The computer-readable storage medium is, for example, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0229] An embodiment of the present application also provides a computer program product, including computer instructions, which, when executed by a processor, implement the various processes of the above-mentioned test script generation method embodiment and can achieve the same technical effect. To avoid repetition, they are not repeated here.
[0230] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.
[0231] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, a magnetic disk, or an optical disk), and includes a number of instructions for a terminal (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) to execute the methods described in each embodiment of the present application.
[0232] The embodiments of the present application are described above in conjunction with the accompanying drawings, but the present application is not limited to the above-mentioned specific implementation methods. The above-mentioned specific implementation methods are merely illustrative and not restrictive. Under the guidance of the present application, ordinary technicians in this field can also make many forms without departing from the purpose of the present application and the scope of protection of the claims, all of which are within the protection of the present application.
Claims
1. A test script generation method, characterized in that: include: Perform sensitive feature training based on sensitive data sets to obtain sensitive data features; In the case where it is determined based on the sensitive data feature that the program code under test includes sensitive program code, performing data filtering processing on the program code under test to obtain filtered program code; Based on the filtered program code, a target test script is generated using a reinforcement learning algorithm and a reward function.
2. The method according to claim 1, characterized in that The sensitive feature training is performed based on the sensitive data set to obtain sensitive data features, including: Extracting features of program codes in the sensitive data set to obtain feature sequences of the codes; Extracting features from the annotation sentences in the sensitive data set to obtain a feature sequence of the annotations; Concatenate the feature sequence of the code and the feature sequence of the annotation to obtain an input sequence; The input sequence is input into a recurrent neural network (RNN) for training to obtain sensitive data features.
3. The method according to claim 1, characterized in that The method further comprises: Building a decision tree based on the sensitive data features; The program code under test is input into the decision tree to determine whether the program code under test includes sensitive program code.
4. The method according to claim 1, characterized in that The method of generating a target test script based on the filtered program code using a reinforcement learning algorithm and a reward function includes: Get test code data; Using the test code data to train the initial model to obtain a pre-trained model; Fine-tune the pre-trained model using a reinforcement learning algorithm and set a reward function to obtain a target model; The filtered program code is input into the target model to obtain a target test script.
5. The method according to claim 4, characterized in that The step of inputting the filtered program code into the target model to obtain a target test script comprises: Inputting the filtered program code into the target model to generate multiple test scripts; Determine reward function values of the plurality of test scripts respectively based on the reward function in the target model; Determining cumulative reward values of the multiple test scripts respectively based on the action value function in the target model and the reward function values of the multiple test scripts; A target test script is determined, where the target test script is a test script having the largest cumulative reward value among the multiple test scripts.
6. The method according to claim 5, characterized in that The method further comprises: Determining parameter values of the action-value function based on the loss function; The action-value function is updated based on the parameter value.
7. The method according to claim 5, characterized in that The reward function value is the sum of a first value and a second value, wherein the first value is the product of a first preset value and a parameter used to characterize the degree of user recognition, and the second value is the product of a second preset value and a parameter used to characterize the executable degree of the code.
8. A test script generating device, characterized in that: include: A training module is used to perform sensitive feature training based on sensitive data sets to obtain sensitive data features; A filtering module, configured to perform data filtering processing on the program code under test to obtain a filtered program code when it is determined based on the sensitive data feature that the program code under test includes sensitive program code; A generation module is used to generate a target test script based on the filtered program code using a reinforcement learning algorithm and a reward function.
9. An electronic device, characterized in that: The invention comprises a transceiver and a processor, wherein the processor is configured to: Perform sensitive feature training based on sensitive data sets to obtain sensitive data features; In the case where it is determined based on the sensitive data feature that the program code under test includes sensitive program code, performing data filtering processing on the program code under test to obtain filtered program code; Based on the filtered program code, a target test script is generated using a reinforcement learning algorithm and a reward function.
10. An electronic device, characterized in that: include: A processor, a memory, and a program stored in the memory and executable on the processor, wherein when the program is executed by the processor, the steps of the test script generating method according to any one of claims 1 to 7 are implemented.
11. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the test script generating method according to any one of claims 1 to 7 are implemented.
12. A computer program product, characterized in that The method comprises computer instructions, which, when executed by a processor, implement the steps of the test script generating method according to any one of claims 1 to 7.