Test code generation method and low-code test platform
By generating methods and low-code testing platforms, replacing custom functions as library functions, building basic projects, solving the problem of insufficient automation of low-code platforms in complex process testing, and achieving efficient test code generation and maintenance.
Patent Information
- Application Number
- CN202510336389.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-20
- Publication Date
- 2025-07-08
AI Technical Summary
Existing low-code platforms cannot automate when handling product testing of complex processes, resulting in high cost of testing code development and maintenance, and low scalability and packaging.
Through the generation method and low-code test platform, test description files, configuration files and use case processes are used to generate executable test code, replace custom functions with library functions, build basic projects, and ensure dependency integrity and executability of the operating environment.
降低了测试代码的开发和维护成本,提高了测试代码的可执行性和扩展性,支持复杂流程的产品测试,减少了代码体积和后续维护工作。
Smart Images

Figure CN120276985A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technologies, and in particular, to the fields of software engineering and software testing technologies. Background Art
[0002] Product testing is generally divided into two major categories: front-end testing and back-end testing. Back-end testing mainly tests the underlying capabilities of the product server side. The main testing method is to simulate the business processes of the product for testing, and improve the testing efficiency through automation, thereby reducing the human and time costs. Summary of the Invention
[0003] The present disclosure provides a method, apparatus, device, storage medium for generating test code, and a low-code testing platform.
[0004] According to a first aspect of the present disclosure, there is provided a method for generating test code, including:
[0005] Obtaining test data, a configuration file, a use case process, and an original code snippet including custom functions according to a test description file;
[0006] Constructing a basic project according to the test data and the configuration file;
[0007] Generating a plurality of preliminary code snippets according to the step information in the use case process and a preset code template file;
[0008] Replacing the custom functions included in the preliminary code snippets with references to corresponding library functions to obtain executable code snippets;
[0009] Generating an executable code project file in the directory of the basic project according to the step sequence in the use case process and the executable code snippets.
[0010] According to a second aspect of the present disclosure, there is provided a low-code testing platform, including:
[0011] A code generation engine SDK for executing the method for generating test code provided in the first aspect to generate an executable code project file.
[0012] According to a third aspect of the present disclosure, there is provided a device for generating test code, including:
[0013] An obtaining module for obtaining test data, a configuration file, a use case process, and an original code snippet including custom functions according to a test description file;
[0014] A constructing module for constructing a basic project according to the test data and the configuration file;
[0015] A generation module, configured to generate a plurality of preliminary code snippets according to the step information in the use case process and a preset code template file;
[0016] A replacement module, configured to replace the custom functions included in the preliminary code snippets with calls to corresponding library functions to obtain executable code snippets;
[0017] An assembly module, configured to generate an executable code engineering file in the directory of the basic project according to the step sequence in the use case process and the executable code snippets.
[0018] According to another aspect of the present disclosure, there is provided an electronic device, including:
[0019] At least one processor; and
[0020] A memory communicatively connected to the at least one processor; wherein,
[0021] The memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute any method in the embodiments of the present disclosure.
[0022] According to another aspect of the present disclosure, there is provided a non-transitory computer-readable storage medium storing computer instructions, wherein the computer instructions are used to cause the computer to execute any method in the embodiments of the present disclosure.
[0023] According to another aspect of the present disclosure, there is provided a computer program product, including a computer program, and the computer program implements any method in the embodiments of the present disclosure when executed by a processor.
[0024] According to the solution of the embodiments of the present disclosure, completely executable test code products can be output, reducing the development and maintenance costs of test code.
[0025] It should be understood that the content described in this part is not intended to identify the key or important features of the embodiments of the present disclosure, nor is it used to limit the scope of the present disclosure. Other features of the present disclosure will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0026] The drawings are used to better understand the solution and do not constitute a limitation to the present disclosure. Among them:
[0027] Figure 1 is a flowchart of a method for generating test code according to an embodiment of the present disclosure;
[0028] Figure 2 is a structural schematic diagram of a low-code test platform according to an embodiment of the present disclosure;
[0029] Figure 3 is a schematic flowchart of a method for generating test code according to another embodiment of the present disclosure;
[0030] Figure 4 is a schematic structural diagram of a device for generating test code according to an embodiment of the present disclosure;
[0031] Figure 5 is a block diagram of an electronic device for implementing the method for generating test code according to an embodiment of the present disclosure. Detailed implementation manners
[0032] The following describes exemplary embodiments of the present disclosure with reference to the accompanying drawings. Various details of the embodiments of the present disclosure are included to facilitate understanding and should be considered merely exemplary. Therefore, those of ordinary skill in the art should recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope of the present disclosure. Similarly, descriptions of well-known functions and structures are omitted below for clarity and conciseness.
[0033] In related technologies, low-code platform technology avoids code development through visual configuration operations. Users can edit, debug, and execute automated test cases on the page, providing a non-code automated construction approach and solving the problem of relatively high technical thresholds for automation. However, low-code platform technology can only solve conventional product test requirements, and for the product tests involving many complex processes, automation cannot be achieved through low-code technology. For some test requirements that cannot be achieved through automated technology for product testing, code writing is generally used to implement them, which will introduce code development and maintenance costs. Overall, the test efficiency has not been significantly improved. Especially for the code generated by the low-code platform, the scalability and encapsulation are relatively low, and it is impossible to further write code on this basis to implement product tests with complex processes and improve the construction efficiency of automated testing.
[0034] To at least partially solve one or more of the above problems and other potential problems, embodiments of the present disclosure provide a method for generating test code and a low-code test platform. Using the technical solutions of the embodiments of the present disclosure, completely executable test code products can be output, greatly reducing the development and maintenance costs of test code.
[0035] Figure 1 is a schematic flowchart of a method for generating test code according to an embodiment of the present disclosure. As Figure 1 shown, the method at least includes the following steps:
[0036] S101. Obtain test data, a configuration file, a use case process, and an original code segment including custom functions according to a test description file.
[0037] In the embodiments of the present disclosure, the test description file may be a structured document in the format of JavaScript Object Notation (JSON) or YAML markup language, which is used to define the core elements required for test scenarios such as test objectives, input parameters, expected results, and custom function code blocks. The test description file may also include test data, test environment information, code snippets, product information to be tested (such as addresses, general test data, interface information, etc.), and use case flows.
[0038] In one example, the test description file includes:
[0039] The product information to be tested includes the product version and interface documentation.
[0040] The configuration file includes global test configurations and test environment information.
[0041] The use case flow includes a sequence of test steps, pre - / post - operations, and assertion logic.
[0042] The test data includes, but is not limited to: input parameters, such as Application Programming Interface (API) request bodies, dynamic data drivers, expected results, assertion rules, and data pools.
[0043] S102. Build a basic project according to the test data and the configuration file.
[0044] In the embodiments of the present disclosure, the basic project can be understood as a predefined code framework, which includes a project structure, dependency declarations, and basic function modules. Building the basic project generally includes: creating a code project directory according to preset rules (for example, src stores source code, lib stores dependency libraries, and config stores configuration files); generating a dependency declaration file according to the dependencies in the configuration file (such as Python libraries requests, pymysql); calling the environment initialization API to create a virtual environment and install dependencies.
[0045] S103. Generate a plurality of preliminary code snippets according to the step information in the use case flow and a preset code template file.
[0046] In the embodiments of the present disclosure, the preset code template can be understood as a predefined code skeleton file, which includes general logic (such as request sending, result assertion) and replaceable placeholders. The sequence of test steps included in the use case flow can be understood as a sequence formed by the execution order of several test cases. Each test case usually includes specific step information such as a name, input parameters, operations, and outputs.
[0047] Determine the use case type of each step (such as data assembly, sending requests, custom function) according to the step information in the use case process, select the corresponding preset code template, and inject the input parameters and expected results in the test case into the template placeholder. Output a code file containing the original custom function calls.
[0048] In one example, the use case process includes: user login → search for products → add to cart → submit order → payment verification.
[0049] The use case types and included step information for each step in this process may include:
[0050] 1. Data assembly
[0051] - Name: Generate virtual user information
[0052] - Input: None
[0053] - Operation: Call the generate_user() function to generate username, password, and address
[0054] - Output: User data {name: "test_user_01", password: "Aa123456", address: "Shanghai"}
[0055] 2. Interface request
[0056] - Name: Call the login interface
[0057] - Input: User data generated in the previous step
[0058] - Operation: Send a POST / api / login, with the Body containing username and password
[0059] - Verification: Response status code = 200, return token
[0060] 3. UI operation
[0061] - Name: Search for products
[0062] - Input: Product keyword "mobile phone"
[0063] - Operation: Enter the keyword in the search box and click the "Search" button
[0064] - Verification: The page displays at least 10 product results
[0065] 4. Custom function
[0066] - Name: Calculate order signature
[0067] - Input: Order ID, amount, timestamp
[0068] - Operation: Call sign_order(params) to generate an MD5 signature
[0069] - Output: Signature string
[0070] 5. Conditional control class
[0071] - Name: Process payment result
[0072] - Input: Payment interface response code
[0073] - Branch:
[0074] - If code = 200: Execute the "Generate delivery note" step
[0075] - If code = 400: Execute the "Re - pay" step
[0076] S104. Replace the custom functions included in the preliminary code snippet with calls to the corresponding library functions to obtain an executable code snippet.
[0077] In the embodiments of the present disclosure, the calls to custom functions are identified in the preliminary code snippet, and according to the function registry, the custom functions are replaced with calls to the corresponding library functions, thereby outputting a code file without redundancy and with complete dependencies.
[0078] Library functions are reusable function modules that have been standardized and are stored in the project's common directory for other code to reference. It can be understood as refactoring custom functions into callable code modules.
[0079] S105. Generate an executable code project file in the directory of the basic project according to the step sequence of the use - case process and the executable code snippet.
[0080] In the embodiments of the present disclosure, according to the step sequence of the use - case process (such as first executing login and then executing query), the executable code snippets are sorted and assembled to obtain test code. The code files, configuration files, and dependency declarations are packaged into a compressed package, such as a ZIP - format compressed package.
[0081] According to the solution of the embodiments of the present disclosure, by clearly specifying the third - party components required by the project through library references, it is possible to avoid runtime environment problems caused by implicit dependencies. After replacing custom functions with library functions, the dependency relationships can be automatically identified and a configuration file can be generated to ensure that the required libraries can be completely installed in the runtime environment, so that a completely executable test - code product can be finally output; at the same time, replacing custom functions with standardized library functions can reduce the code volume and lower the subsequent maintenance cost; and developers can easily write further code on this basis to implement product testing for complex processes, greatly reducing the development and maintenance costs of test code.
[0082] In a possible implementation, S104 replaces the custom functions included in the preliminary code snippet with calls to the corresponding library functions to obtain an executable code snippet, which further includes the steps of:
[0083] Render the custom function as a repeatedly callable library function.
[0084] Add a declaration of the library function to the preliminary code snippet.
[0085] Replace the custom function included in the preliminary code snippet with a call to the corresponding library function to obtain an executable code snippet.
[0086] In the embodiments of the present disclosure, a custom function can be encapsulated into an independent module that conforms to the specifications of Python Enhancement Proposal 8 (PEP8). A dependency declaration is automatically inserted at the head of the module. The path of the encapsulated module is written into the function registry (such as registry.json) in the project, so as to serve as a library function and support cross-case calls.
[0087] According to the solution of the embodiments of the present disclosure, replacing the custom function with a standardized library function can reduce the code size and lower the subsequent maintenance cost.
[0088] In a possible implementation, S102 constructs a basic project according to the test data and the configuration file, which further includes the steps of:
[0089] Generate a project framework according to the project type in the configuration file.
[0090] Create a project directory according to the project framework.
[0091] Generate a dependency list according to the environment configuration information in the configuration file.
[0092] Build a basic project by incorporating at least one of file parsing, logging, test reports, and general test functions into the project directory.
[0093] In the embodiments of the present disclosure, building the basic project includes building the project directory and injecting core files. A standard directory structure is created according to the project type in the configuration (such as Web UI testing, API testing). requirements.txt is automatically generated according to the environment configuration information, including a test framework with version locking (such as pytest==7.1.2), driver packages (such as selenium==4.1.0). After the directory is created, core files such as file parsing, logging, test reports, and general test functions can be built-in to ensure the executability of the test code after generation, and it is also convenient to record the execution situation in real time. For example, inject configuration-based log level and output path initialization code, bind the HTML report template, configure the screenshot saving rule, and insert unified timeout retry and error screenshot capture logic.
[0094] According to the solution of the embodiments of the present disclosure, by creating a complete project directory containing all necessary configurations and infrastructure files, the dependencies and execution problems of the test code are solved, and the executability of the test code after generation is ensured.
[0095] In a possible implementation, S104 generates multiple preliminary code segments according to the step information in the use case process and a preset code template file, which further includes the steps of:
[0096] According to the step information in the use case process, multiple structured use case objects and the step lists, parameter dependency relationships, and context marking information corresponding to the multiple structured use case objects are obtained.
[0097] According to the step type of the structured use case object, the corresponding code template is matched from the preset code template file.
[0098] The code template is dynamically replaced to obtain multiple preliminary code segments.
[0099] In the embodiments of the present disclosure, the step list, step type (such as HTTP request, database operation), input parameters (URL, request header), and dependency relationship are extracted from the original test case data, and global variables (such as ${config.host}) and cross-step variable references are identified. The corresponding preset code template is matched according to the use case type, such as matching the send request template for HTTP requests. The code generated at this time is a placeholder without associated specific implementation, and direct invocation will cause a runtime error. Dynamic replacement can inject the input parameters and expected results in the test data into the template placeholder. It also includes converting cross-step variables (such as step1_response) into variable references in the code. For example, for request-type steps, use the send request template to fill in parameters such as URL, request method, and Headers. For data assembly-type steps, use the data construction template to generate parameterized data construction code.
[0100] It should be noted that in the field of code generation, those skilled in the art can write preset code templates based on known template engines. For example, the following methods can be used to build preset code templates based on Jinja2:
[0101] Template structure definition: According to the syntax structure and business logic requirements of the target code, use Jinja2 syntax rules (such as variable placeholders {{}} and control logic markers {%%}) to design the template file, and retain the variable parts as parameterized interfaces.
[0102] Data model binding: Establish a data structure corresponding to the template parameters, clarify the parameter types and constraint conditions, and ensure the mapping relationship between the data source and the template variables.
[0103] Template inheritance mechanism: Through the template inheritance feature of Jinja2, build the hierarchical relationship between the base template and the sub-template to achieve the reuse of common code segments and the modular extension of specific functions.
[0104] Rendering engine configuration: Call the Jinja2 template engine to load the template file, inject the data model into the template variables, perform logical judgments and loop controls, and generate the final code that conforms to the target programming language specification.
[0105] The above methods can be implemented through a standard development toolchain (such as Python environment configuration and IDE plugins). Based on the requirements document and the official specification of the template engine, technicians can reasonably design parameterized templates and complete dynamic code generation. Jinja2 is a Python-based template engine used to embed dynamic data into static template files to generate text content (such as HTML, configuration files, code, test data, etc.).
[0106] According to the solution of the embodiment of the present disclosure, mainstream test scenarios are covered by preset templates, reducing the manual coding workload. By identifying the dependencies between steps, temporary variables are automatically generated to ensure logical coherence. The code template follows the Python coding specification, ensuring a unified code style and the standardization and high encapsulation of the generated code.
[0107] In a possible implementation, the method further includes the steps of:
[0108] When global parameters are included in the function call of the preliminary code snippet, replace the global parameters with reference code.
[0109] In the embodiment of the present disclosure, if global parameters are identified in the function call, the global parameters are replaced with reference code instead of fixed hardcode. For example, replace ${config.host} with the code referenced from the configuration file (such as config.get("host")) to avoid hardcoding.
[0110] According to the solution of the embodiments of the present disclosure, the flexibility of code parameters can be improved.
[0111] In a possible implementation, testing the test description file to obtain test data, a configuration file, a use case process, and an original code snippet containing custom functions further includes the steps of:
[0112] Performing lexical analysis on the test description file to obtain a parsing result of a multi-level relationship.
[0113] Classifying the parsing result into one of test data, a configuration file, a use case process, or an original code snippet containing custom functions.
[0114] In the embodiments of the present disclosure, a syntax analyzer, such as ANother Tool for Language Recognition (ANTLR), can be used to construct a syntax tree for the test description file, and the following key information can be identified through predefined syntax rules (such as test steps, data-driven parameters, environment variable declarations, etc.):
[0115] Test data: parameterized input values (such as boundary values, outlier values), expected results, assertion rules;
[0116] Global configuration: execution environment information, timeout, concurrency settings;
[0117] Custom functions: user-defined assertion methods, data preprocessing logic, reusable operation fragments.
[0118] By identifying the conditional judgment in the loop and the combination of data-driven and API calls, a multi-level logical relationship is obtained, which is transformed into a tree structure representation to obtain the parsing result.
[0119] Classify the parsing result into one of test data, configuration parameters, use case process, or an original code snippet containing custom functions. Generate a configuration file containing configuration parameters and a data-driven file containing test data according to the configuration parameters.
[0120] The ANTLR parser can also be used in the embodiments of the present disclosure to parse the original code containing custom functions, extract information such as function names, parameters, return values, etc. Parse the use case process information to obtain context information, global reference information, etc. between multiple steps.
[0121] According to the solution of the embodiments of the present disclosure, the ANTLR parser converts non-standard user inputs (such as test description files) into machine-processable logical units, and through precise syntax tree analysis, ensures that the generated code has no logical errors.
[0122] In a possible implementation, S105 generates an executable code project file in the directory of the basic project according to the step sequence and executable code snippets in the use case process, and further includes the steps of:
[0123] Parse the context information and / or variable reference information in the code through a syntax parser to construct a variable dependency graph between steps.
[0124] Generate variable passing code in the executable code snippets according to the variable dependency graph to obtain a context-related code block.
[0125] Insert code connectors into the context-related code blocks according to the step sequence of the use case process to obtain an executable code logic block.
[0126] Perform code assembly according to the executable code logic block and dependency library references, and generate an executable code project file in the directory of the basic project.
[0127] In the embodiments of the present disclosure, a context-related code block containing variable passing logic is obtained according to the executable code snippets and the step sequence (such as step 1 → step 2 → step 3).
[0128] Parse the variable references in the code through a syntax parser (such as ANTLR) for variable dependency analysis, construct a variable dependency graph between steps. For example, step 2 depends on the output variable of step 1. Generate variable passing code according to the dependency graph (such as step2_input = step1_response). According to the context-related code blocks and the use case logic description, insert code connectors in the step sequence to achieve logical connection and ensure the correct execution order, thereby obtaining an executable code logic block containing a code segment for sequential execution or exception handling. Merge the code snippets and dependency imports according to the executable code logic block and dependency library references to generate a complete code project file. Thus, a standardized test code file that can be directly executed is obtained.
[0129] According to the solution of the embodiments of the present disclosure, by clarifying the variable passing path between steps, it is possible to avoid undefined variables or missing variable passing, ensure that the code execution order is strictly consistent with the defined process. Support the insertion of complex logics such as branches and loops, improve the scenario coverage ability of the generated code, and facilitate code debugging and function reuse.
[0130] In a possible implementation, the method further includes the steps of:
[0131] Add identification labels to the executable code snippets.
[0132] Execute the corresponding executable code snippets according to the identification labels and the specified execution labels.
[0133] In the embodiments of the present disclosure, specific format comments or metadata (such as #@label:regression_test) can be inserted at the beginning and end of code snippets. The labels can describe the purpose of the code, mark the applicable scenarios, mark the pre- and post-operations, or characterize its functional attributes (such as test type, business module, priority). After the executable code snippets have labels, the matching code snippets can be filtered out according to the execution labels specified by the user and executed as needed. Specifically, by reading the execution labels in the user input or configuration file, traversing the identification labels of all code snippets, filtering out the snippets that meet the logical conditions, and loading and executing the matching code snippets in a preset order (such as dependency relationship, priority).
[0134] The pre- and post-operations can be obtained according to the pre- and post-condition descriptions in the test cases. Identification labels specifically used to identify the pre- and post-operations are added to the executable code snippets. For example, the pre-operation (such as environment initialization) is marked as @setup, and the post-operation (such as resource release) is marked as @teardown, thus obtaining a code block with identification labels.
[0135] According to the solution of the embodiments of the present disclosure, through tagged marking and dynamic selection execution, flexible combination and on-demand invocation of code snippets are realized, solving the problems of resource waste and poor scenario adaptability caused by full-scale execution in traditional code generation. Through the labels, the scope of the problem code can also be quickly located, improving the troubleshooting efficiency.
[0136] In a possible implementation manner, the method further includes the steps of:
[0137] Generate the pytest.ini configuration local browser driver path and the number of test threads according to the generated configuration object and the runtime parameters specified by the user.
[0138] Create a run.sh / run.bat startup script and dynamically splice the execution command according to the parameters input by the user.
[0139] According to the solution of the embodiments of the present disclosure, the startup commands of different execution modes are supported through executable script files.
[0140] Figure 2 It is a structural schematic diagram of a low-code test platform provided by an embodiment of the present disclosure. As Figure 2 shown, the platform includes:
[0141] A code generation engine SDK, which is used to execute the test code generation method provided in any of the above embodiments to generate an executable code project file.
[0142] In the embodiments of the present disclosure, the low-code testing platform is centered around the code generation engine SDK (Software Development Kit), and in combination with the visual interaction module, realizes the full-process automation from test case design to code project generation. The platform architecture is divided into the following modules:
[0143] The visual interaction module is used to provide a drag-and-drop flowchart designer, and users can define test steps, input parameters, and expected results.
[0144] The code generation engine SDK converts the test cases defined by users into executable code projects, and supports variable dependency analysis, dynamic concatenation of code blocks, and engineering assembly.
[0145] The code generation engine includes three parts: the API call layer, the project rendering layer, and the test code rendering layer. It supports the input of structured data, automatically parses information such as use case logical relationships and function calls, identifies automation configuration information and the basic code architecture through the project rendering layer, and then outputs executable code through the test code rendering layer in sequence. The API call layer passes in meta-information such as test steps and data dependencies input by users through standardized interfaces (such as JSON Schema). The project rendering layer assembles general test projects and general functions to build a complete project, and the test code rendering layer fills the executable code logic blocks through mechanisms such as preset code templates, variable dependency graphs, and code connectors.
[0146] In one example, as Figure 3 shown, the specific execution steps of the code generation engine include:
[0147] 1. Input of structured information;
[0148] 2. Call the code generation API;
[0149] 3. Call the project code rendering layer;
[0150] 4. Identify test information and output a test configuration file;
[0151] 5. Assemble general test projects, general functions, and build a complete project;
[0152] 6. Identify and render custom functions;
[0153] 7. After the basic project is completed, call the test code rendering layer;
[0154] 8. Identify test case information and render code segments such as context and function calls;
[0155] 9. Identify the logical relationship of use case steps and assemble complete automation code;
[0156] 10. Configure pre- and post-operations and automatic identification markers;
[0157] 11. Save and store the code, supporting local saving and cloud saving. The cloud saving provides a download link.
[0158] According to the solution of the embodiments of the present disclosure, the time-consuming from use case design to generating an executable project is shortened from the hour level to the minute level, reducing the manual coding cost. The variable dependencies are automatically passed, and the project structure is standardized, ensuring that only the flowchart description file needs to be modified for subsequent iterations, without directly modifying the code.
[0159] In a possible implementation manner, the platform further includes an execution environment management module, and the execution environment management module includes:
[0160] An environment configuration unit, configured to create multiple independent custom execution environments, each environment is associated with a unique virtual environment path, and configure environment variable isolation rules.
[0161] A dependency installation unit, configured to parse the dependency description file input by the user, and call the embedded pip tool to install third-party dependency libraries from a preset image source to the corresponding virtual environment path.
[0162] An environment switching unit, configured to dynamically load the dependency libraries and environment variables of the corresponding virtual environment according to the execution environment identifier selected by the user, so as to implement multi-environment isolated execution.
[0163] In the embodiments of the present disclosure, the execution environment management module, based on virtualization environment isolation and embedded dependency management technologies, solves the problems of third-party dependency conflicts, multi-version compatibility, and rapid environment switching faced during the execution of user-defined functions and test cases in a low-code platform. The core of the module is divided into three parts: an environment configuration unit, configured to create an independent virtual environment and define environment variable isolation rules. A dependency installation unit, configured to achieve accurate installation and version control of dependencies through the embedded pip tool. An environment switching unit, configured to dynamically load the dependencies of the target environment to ensure the environmental isolation during the execution process.
[0164] Allocate an independent directory for each custom execution environment based on the Python virtual environment (such as venv) to isolate system-level dependencies. Accurately install the dependency libraries of each custom execution environment by parsing the dependency description file provided by the user. Load the dependency libraries of the target environment during runtime by modifying sys.path and environment variables to achieve seamless multi-environment switching.
[0165] Specifically, the steps for creating a custom execution environment may include: The user inputs an environment name through the platform interface, triggering the backend to call the venv module to create a virtual environment directory. An environment unique identifier is automatically generated and bound to the virtual environment path. The user uploads a requirements.txt file or inputs dependencies through the page form.
[0166] Parse the dependencies and version constraint rules to generate a structured dependency tree. Call the embedded pip tool (integrated with the platform, no need for local installation), specifying the target virtual environment path (such as --target / venvs / env_payment).
[0167] Pull the dependency packages from a preset image source (such as the Alibaba Cloud PyPI mirror) to accelerate the installation process. Record the installation logs and version information to generate a dependency list file.
[0168] The steps for switching the custom execution environment may include: When the user executes test cases or debugs custom functions, select the target environment identifier from the dropdown list. Find the corresponding virtual environment path according to the environment identifier, modify the runtime sys.path, and preferentially load the site-packages under this path. Temporarily override environment variables (such as PATH, PYTHONHOME) to ensure that child processes inherit the isolation configuration.
[0169] According to the solution of the embodiments of the present disclosure, the low-code test platform can customize function writing and debugging, and customize the execution environment, support the development of custom functions in the python language, integrate the custom part with the existing code project, solve the user's automated testing requirements for complex processes, and support visual online debugging of the platform.
[0170] Use vue and a code editor to implement code writing for the page, call the code generation sdk to assemble a fully executable code project, support calling the built-in general libraries in the sdk in the code, which can greatly reduce the amount of code writing for custom functions. The built-in general libraries may include any one of the following:
[0171] The logging module supports logging printing during code debugging, mainly used for quickly debugging functions.
[0172] Multiple database operation functions, facilitating users to directly call for database operations.
[0173] Requests of multiple protocols, eliminating the need for users to study and encapsulate protocol requests.
[0174] Multiple automated assertions, directly calling or combining them can complete personalized automated assertion functions.
[0175] The purpose of customizing the execution environment is mainly to meet the user's third-party dependency reference problem. It is implemented by using Python dependency installation pip, and the independence of each dependent lib environment is achieved through the platform's custom installation module. This achieves isolation between multiple execution environments and supports users to switch freely.
[0176] It should be noted that, usually when writing code, if the required dependency packages are not available in the environment, code execution will result in an error. The low-code platforms in related technologies generally do not support the function of the custom execution environment of the embodiment of the present disclosure.
[0177] In a possible implementation, the platform further includes a code export module, which is used to:
[0178] Convert the code project file into a code project compressed package that can be run independently.
[0179] In the disclosed embodiment, the code export module takes engineering encapsulation and dependency integrity management as the core, realizing seamless conversion from low-code use cases within the platform to independently run code projects. The module parses the use cases and associated resources selected by the user through the code generation engine SDK, generates a standardized directory structure, dependency list and executable script, and finally packages it into a lightweight compressed package to ensure that it can still run directly without the platform environment.
[0180] In one example, the execution steps of the low-code platform include:
[0181] Step 1. Use case screening and data extraction: Based on the user selecting the target use case through the multiple-selection box on the platform interface, or entering the screening conditions (such as label = smoke_test, priority = P0), a list of matching use cases is queried from the database, and the bound custom functions, execution environment identifiers, and dependency description files (such as requirements.txt) are associated with them.
[0182] Extract use case metadata, including test steps and parameterized data (such as login(username="test_user")), custom function code blocks (such as def verify_payment():...), and execution environment configuration (such as the virtual environment path and dependent library version corresponding to the environment identifier ENV_001).
[0183] Step 2: Generate a structured test description file: Convert the test steps into an ordered JSON object, record the step name, input parameters, assertion logic, and variable transfer relationship between steps. Merge all dependencies associated with the selected use case, remove duplicates, and generate a global dependency declaration file, annotating the library name and version constraints. According to the execution environment identifier, read the corresponding virtual environment metadata (such as Python version, installed dependency list) from the environment management module to generate an environment description file.
[0184] Step 3: Invoke the code generation engine SDK to generate a code project.
[0185] For the implementation process of this step, please refer to the relevant descriptions of the corresponding steps in the above method embodiments, and details will not be elaborated herein.
[0186] Step 4: Compression package encapsulation and download: Store the generated code project files in the project directory, use the ZIP compression algorithm to generate an independently executable code project compression package. Generate a download link for the compression package. Display a download button on the front-end page of the platform, and trigger browser download after the user clicks it.
[0187] According to the solution of the embodiments of the present disclosure, the exported compression package contains a complete dependency description and environment configuration. After the user unzips it, it can be run without manually completing files or configuring paths.
[0188] Figure 4 It is a schematic structural diagram of a test code generation device provided according to an embodiment of the present disclosure. As Figure 4 shown, the device includes:
[0189] An acquisition module 401, configured to obtain test data, a configuration file, a use case process, and an original code snippet containing custom functions according to a test description file;
[0190] A construction module 402, configured to construct a basic project according to the test data and the configuration file;
[0191] A generation module 403, configured to generate a plurality of preliminary code snippets according to the step information in the use case process and a preset code template file;
[0192] A replacement module 404, configured to replace the custom functions included in the preliminary code snippets with calls to corresponding library functions to obtain executable code snippets;
[0193] An assembly module 405, configured to generate an executable code project file in the directory of the basic project according to the step sequence in the use case process and the executable code snippets.
[0194] In a possible implementation manner, the construction module 402 is configured to:
[0195] Generate an engineering framework according to the project type in the configuration file;
[0196] Create an engineering directory according to the engineering framework;
[0197] Generate a dependency list according to the environment configuration information in the configuration file;
[0198] Build a basic project by including at least one of file parsing, logging, test reports, and general test functions in the project directory.
[0199] In a possible implementation, the generation module 403 is used for:
[0200] Obtain multiple structured use case objects, as well as the step lists, parameter dependency relationships, and context marker information corresponding to the multiple structured use case objects, based on the step information in the use case process;
[0201] Match the corresponding code template from the preset code template file according to the step type of the structured use case object;
[0202] Perform dynamic replacement on the code template to obtain multiple preliminary code snippets.
[0203] In a possible implementation, the device further includes:
[0204] A global parameter replacement module, which is used to replace the global parameter with a reference code when the function call in the preliminary code snippet contains a global parameter.
[0205] In a possible implementation, the acquisition module 401 is used for:
[0206] Perform lexical analysis on the test description file to obtain the parsing result of the multi-level relationship;
[0207] Classify the parsing result into one of test data, configuration file, use case process, or raw code snippet containing custom functions.
[0208] In a possible implementation, the assembly module 405 is used for:
[0209] Parse the context information and / or variable reference information in the code through a syntax analyzer to construct a variable dependency graph between steps;
[0210] Generate variable transfer code in the executable code snippet according to the variable dependency graph to obtain a context-related code block;
[0211] Insert code connectors into the context-related code block according to the step sequence of the use case process to obtain an executable code logic block;
[0212] Perform code assembly according to the executable code logic block and the dependency library reference, and generate an executable code project file in the directory of the basic project.
[0213] In a possible implementation, the device further includes a label module, which is used for:
[0214] Add identification labels to the executable code snippets;
[0215] Execute the corresponding executable code segment according to the identification tag and the specified execution tag.
[0216] For the specific functions and example descriptions of each module and sub-module of the device according to the embodiments of the present disclosure, reference may be made to the relevant descriptions of the corresponding steps in the foregoing method embodiments, which will not be elaborated herein.
[0217] In the technical solution of the present disclosure, the acquisition, storage, and application of the user's personal information involved all comply with the provisions of relevant laws and regulations and do not violate public order and good customs.
[0218] According to the embodiments of the present disclosure, the present disclosure also provides an electronic device, a readable storage medium, and a computer program product.
[0219] Figure 5 FIG. shows a schematic block diagram of an exemplary electronic device 500 that can be used to implement the embodiments of the present disclosure. The electronic device is intended to represent various forms of digital computers, such as, for example, a laptop computer, a desktop computer, a workbench, a personal digital assistant, a server, a blade server, a mainframe computer, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as, for example, a personal digital assistant, a cellular phone, a smart phone, a wearable device, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely illustrative and are not intended to limit the implementation of the present disclosure described and / or claimed herein.
[0220] As Figure 5 shown, the device 500 includes a computing unit 501 that can perform various appropriate actions and processes according to a computer program stored in a read-only memory (ROM) 502 or a computer program loaded from a storage unit 508 into a random access memory (RAM) 503. In the RAM 503, various programs and data required for the operation of the device 500 can also be stored. The computing unit 501, the ROM 502, and the RAM 503 are connected to each other via a bus 504. An input / output (I / O) interface 505 is also connected to the bus 504.
[0221] A plurality of components in the device 500 are connected to the I / O interface 505, including: an input unit 506, such as a keyboard, a mouse, etc.; an output unit 507, such as various types of displays, speakers, etc.; a storage unit 508, such as a magnetic disk, an optical disk, etc.; and a communication unit 509, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 509 allows the device 500 to exchange information / data with other devices via a computer network such as the Internet and / or various telecommunication networks.
[0222] The computing unit 501 can be various general-purpose and / or special-purpose processing components with processing and computing capabilities. Some examples of the computing unit 501 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various dedicated artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, a digital signal processor (DSP), and any suitable processor, controller, microcontroller, etc. The computing unit 501 executes the various methods and processes described above, such as the method for generating test code or the low-code test platform. For example, in some embodiments, the method for generating test code or the low-code test platform can be implemented as a computer software program, which is tangibly contained in a machine-readable medium, such as the storage unit 508. In some embodiments, part or all of the computer program can be loaded and / or installed onto the device 500 via the ROM 502 and / or the communication unit 509. When the computer program is loaded into the RAM 503 and executed by the computing unit 501, one or more steps of the method for generating test code or the low-code test platform described above can be executed. Alternatively, in other embodiments, the computing unit 501 can be configured to execute the method for generating test code or the low-code test platform in any other suitable manner (e.g., by means of firmware).
[0223] Various embodiments of the systems and techniques described above in this document can be implemented in digital electronic circuit systems, integrated circuit systems, field-programmable gate arrays (FPGA), application-specific integrated circuits (ASIC), application-specific standard products (ASSP), systems-on-a-chip (SOC), complex programmable logic devices (CPLD), computer hardware, firmware, software, and / or combinations thereof. These various embodiments can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a dedicated or general-purpose programmable processor, receiving data and instructions from a storage system, at least one input device, and at least one output device, and transmitting the data and instructions to the storage system, the at least one input device, and the at least one output device.
[0224] The program code for implementing the methods of the present disclosure can be written in any combination of one or more programming languages. These program codes can be provided to a processor or controller of a general-purpose computer, a special-purpose computer, or other programmable data processing devices, such that when the program codes are executed by the processor or controller, the functions / operations specified in the flowcharts and / or block diagrams are implemented. The program codes can be executed entirely on the machine, partially on the machine, executed partially on the machine as an independent software package and partially on a remote machine, or executed entirely on a remote machine or server.
[0225] In the context of this disclosure, a machine-readable medium can be a tangible medium that can contain or store a program for use by or in connection with an instruction execution system, apparatus, or device. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination of the foregoing. More specific examples of a machine-readable storage medium would include an electrical connection based on one or more wires, a portable computer diskette, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or Flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0226] In order to provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and a pointing device (e.g., a mouse or a trackball) by which the user can provide input to the computer. Other kinds of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).
[0227] The systems and techniques described herein can be implemented in a computing system that includes back-end components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes front-end components (e.g., a user computer having a graphical user interface or a web browser through which the user can interact with an implementation of the systems and techniques described herein), or a computing system that includes any combination of such back-end components, middleware components, or front-end components. The components of the system can be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), and the Internet.
[0228] A computer system may include a client and a server. The client and the server are generally far from each other and usually interact via a communication network. The client-server relationship is generated by computer programs running on respective computers and having a client-server relationship with each other. The server may be a cloud server, a server of a distributed system, or a server incorporating a blockchain.
[0229] It should be understood that various forms of the processes shown above can be used, with steps reordered, added, or deleted. For example, the steps recited in this disclosure can be executed in parallel, sequentially, or in a different order, as long as the desired results of the technical solutions disclosed in this disclosure can be achieved, and no limitation is imposed herein.
[0230] The above specific embodiments do not constitute a limitation on the protection scope of this disclosure. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the principles of this disclosure shall be included within the protection scope of this disclosure.
Claims
1. A method for generating test code, comprising: Obtaining test data, a configuration file, a use case process, and an original code snippet containing custom functions according to a test description file; Constructing a basic project according to the test data and the configuration file; Generating a plurality of preliminary code snippets according to the step information in the use case process and a preset code template file; Replacing the custom functions included in the preliminary code snippets with calls to corresponding library functions to obtain executable code snippets; Generating an executable code project file in the directory of the basic project according to the step sequence in the use case process and the executable code snippets.
2. The method according to claim 1, wherein, The replacing the custom functions included in the preliminary code snippets with calls to corresponding library functions to obtain executable code snippets includes: Rendering the custom functions as repeatedly callable library functions; Adding declarations of the library functions to the preliminary code snippets; Replacing the custom functions included in the preliminary code snippets with calls to corresponding library functions to obtain executable code snippets.
3. The method according to claim 1, wherein, The constructing a basic project according to the test data and the configuration file includes: Generating a project framework according to the project type in the configuration file; Creating a project directory according to the project framework; Generating a dependency list according to the environment configuration information in the configuration file; Building at least one of file parsing, log recording, test report, and general test function files in the project directory to obtain a basic project.
4. The method according to claim 1, wherein Generating a plurality of preliminary code snippets according to the step information in the use case process and a preset code template file includes: Obtaining a plurality of structured use case objects and step lists, parameter dependency relationships, and context marker information corresponding to the plurality of structured use case objects according to the step information in the use case process; Matching corresponding code templates from a preset code template file according to the step types of the structured use case objects; Performing dynamic replacement on the code templates to obtain a plurality of preliminary code snippets.
5. The method according to claim 1 or 4, further comprising: When global parameters are included in the function calls of the preliminary code snippets, replacing the global parameters with reference codes.
6. The method according to claim 1, wherein The obtaining test data, a configuration file, a use case process, and an original code snippet containing custom functions according to a test description file includes: Performing lexical analysis on the test description file to obtain a parsing result with a multi-level relationship; Classifying the parsing result into one of test data, a configuration file, a use case process, or an original code snippet containing custom functions.
7. The method according to claim 1, wherein The generating an executable code project file in the directory of the basic project according to the step sequence in the use case process and the executable code snippets includes: Parsing context information and / or variable reference information in the code through a syntax analyzer to construct a variable dependency graph between steps; Generating variable passing code in the executable code snippets according to the variable dependency graph to obtain context-related code blocks; Inserting code connectors into the context-related code blocks according to the step sequence of the use case process to obtain executable code logic blocks; According to the executable code logic blocks and dependency library references, perform code assembly to generate an executable code project file in the directory of the base project.
8. The method according to claim 1, further comprising: Adding identification tags to the executable code snippets; Executing the corresponding executable code snippets according to the identification tags and the specified execution tags.
9. A low-code test platform, comprising: A code generation engine SDK for executing the test code generation method according to any one of claims 1 to 7 to generate an executable code project file.
10. The low-code test platform according to claim 9, further comprising an execution environment management module, and the execution environment management module includes: An environment configuration unit for creating multiple independent custom execution environments, each environment associated with a unique virtual environment path and configuring environment variable isolation rules; A dependency installation unit for parsing the input dependency description file and calling the embedded pip tool to install third-party dependency libraries from a preset image source to the corresponding virtual environment path; An environment switching unit for dynamically loading the dependency libraries and environment variables of the corresponding virtual environment according to the execution environment identifier selected by the user to achieve multi-environment isolated execution.
11. The low-code test platform according to claim 9, further comprising a code export module for: Converting the code project file into an independently runnable code project compressed package.
12. A test code generation device, comprising: An acquisition module for obtaining test data, a configuration file, a use case process, and an original code snippet containing custom functions according to a test description file; A construction module for constructing a base project according to the test data and the configuration file; A generation module for generating a plurality of preliminary code snippets according to the step information in the use case process and a preset code template file; A replacement module for replacing the custom functions included in the preliminary code snippets with calls to the corresponding library functions to obtain executable code snippets; An assembly module for generating an executable code project file in the directory of the base project according to the step sequence in the use case process and the executable code snippets.
13. An electronic device, comprising: At least one processor; And A memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method according to any one of claims 1-8.
14. A non-transitory computer-readable storage medium storing computer instructions, wherein, The computer instructions are used to cause the computer to execute the method according to any one of claims 1-8.
15. A computer program product, comprising a computer program which, when executed by a processor, implements the method according to any one of claims 1-8.