[go: up one dir, main page]

CN114610633A - Automated testing tool and its implementation method, device, terminal and storage medium - Google Patents

Automated testing tool and its implementation method, device, terminal and storage medium Download PDF

Info

Publication number
CN114610633A
CN114610633A CN202210283326.4A CN202210283326A CN114610633A CN 114610633 A CN114610633 A CN 114610633A CN 202210283326 A CN202210283326 A CN 202210283326A CN 114610633 A CN114610633 A CN 114610633A
Authority
CN
China
Prior art keywords
hook
code
test
file
automated
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202210283326.4A
Other languages
Chinese (zh)
Inventor
黄康
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Ping An Puhui Enterprise Management Co Ltd
Original Assignee
Ping An Puhui Enterprise Management Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Ping An Puhui Enterprise Management Co Ltd filed Critical Ping An Puhui Enterprise Management Co Ltd
Priority to CN202210283326.4A priority Critical patent/CN114610633A/en
Publication of CN114610633A publication Critical patent/CN114610633A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Prevention of errors by analysis, debugging or testing of software
    • G06F11/3668Testing of software
    • G06F11/3672Test management
    • G06F11/3688Test management for test execution, e.g. scheduling of test suites
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Prevention of errors by analysis, debugging or testing of software
    • G06F11/3668Testing of software
    • G06F11/3696Methods or tools to render software testable
    • GPHYSICS
    • G06COMPUTING OR CALCULATING; COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/44Arrangements for executing specific programs
    • G06F9/445Program loading or initiating
    • G06F9/44505Configuring for program initiating, e.g. using registry, configuration files

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Software Systems (AREA)
  • Computer Hardware Design (AREA)
  • Quality & Reliability (AREA)
  • Debugging And Monitoring (AREA)

Abstract

本发明公开了一种自动化测试工具及其实现方法、装置、终端和存储介质,该方法包括:检查并安装前端react框架hook组件自动化测试所需要的文件;hook组件中有N个hook文件;根据每一个hook文件,生成一个自动化测试案例代码;根据所有hook文件,生成所有hook文件的自动化测试案例代码;确定每个测试案例的动态值;将所有测试案例关联的动态值,进行页面可视化展示。该方案,通过设置前端react框架hook组件自动化测试工具,在开发者在完成代码后,使用该工具,可以生成hook组件的自动化测试代码,可以降低人工成本和维护成本。

Figure 202210283326

The invention discloses an automatic testing tool and its realization method, device, terminal and storage medium. The method includes: checking and installing files required for automatic testing of a hook component of a front-end react framework; there are N hook files in the hook component; For each hook file, an automated test case code is generated; according to all hook files, the automated test case code of all hook files is generated; the dynamic value of each test case is determined; the dynamic value associated with all test cases is displayed on the page visually. In this solution, by setting up an automated testing tool for the hook component of the front-end react framework, after the developer completes the code, the tool can be used to generate the automated test code of the hook component, which can reduce labor costs and maintenance costs.

Figure 202210283326

Description

自动化测试工具及其实现方法、装置、终端和存储介质Automated test tool and its implementation method, device, terminal and storage medium

技术领域technical field

本发明属于计算机技术领域,具体涉及一种自动化测试工具及其实现方法、装置、终端和存储介质,尤其涉及一种前端react框架hook组件自动化测试工具及其实现方法、装置、终端和存储介质。The invention belongs to the field of computer technology, in particular to an automated testing tool and its implementation method, device, terminal and storage medium, and in particular to an automated testing tool for a front-end react framework hook component and its implementation method, device, terminal and storage medium.

背景技术Background technique

一个前端项目,随着产品需求的不断迭代,前端的代码量变得越来越庞大。为了减少代码量以及将相同或类似的功能的代码尽可能进行复用,在当前主流框架react中,是将其做成hook组件形式,提供给不同的场景、不同的开发者进行调用。For a front-end project, with the continuous iteration of product requirements, the amount of front-end code becomes larger and larger. In order to reduce the amount of code and reuse the code with the same or similar functions as much as possible, in the current mainstream framework react, it is made into a hook component, which is provided to different scenarios and different developers to call.

但是,如果一个hook组件是复杂的hook组件,那么这个hook组件在遇到需要针对新的需求进行改造的过程中,会显得捉襟见肘,因为改造的过程中,需要保证原有使用方的使用场景是正常的,不能因为对于A使用场景的改造,影响到B使用场景的功能。这种情况下,引入自动化测试就变得十分重要。However, if a hook component is a complex hook component, the hook component will be stretched when it needs to be transformed for new requirements, because during the transformation process, it is necessary to ensure that the original user's usage scenario is Normally, the function of the B usage scenario cannot be affected by the transformation of the A usage scenario. In this case, the introduction of automated tests becomes very important.

这样,引入并加入自动化测试,在代码改造完成后,通过执行原有所有场景的自动化测试案例,根据得到的结果,就可以知道改造后的代码是否能够完美兼容原有功能逻辑。In this way, automatic tests are introduced and added. After the code transformation is completed, by executing the automatic test cases of all the original scenarios, according to the obtained results, you can know whether the transformed code is perfectly compatible with the original functional logic.

但是,项目引入并加入自动化测试,会存在成本高的问题。However, when the project introduces and adds automated testing, there will be a problem of high cost.

比如,自动化测试代码的编写需要具备相当语法的知识储备,自动化测试案例,如果需要调整,只有开发人员可以修改代码进行调整,存在人工成本高的问题。For example, the writing of automated test code requires a considerable knowledge base of grammar, and if the automated test case needs to be adjusted, only the developer can modify the code to adjust, and there is a problem of high labor cost.

又如,自动化测试代码需要长期维护,当有新的需求新增,或原来的组件需要在某些场景下进行修改时,自动化测试的代码需要进行新增测试案例代码,依然存在人工成本高的问题,维护成本也越来越高。Another example is that the automated test code needs long-term maintenance. When new requirements are added, or the original components need to be modified in some scenarios, the automated test code needs to be added with test case code, which still has high labor costs. The problem, the maintenance cost is also getting higher and higher.

上述内容仅用于辅助理解本发明的技术方案,并不代表承认上述内容是现有技术。The above content is only used to assist the understanding of the technical solutions of the present invention, and does not mean that the above content is the prior art.

发明内容SUMMARY OF THE INVENTION

本发明的目的在于,提供一种自动化测试工具及其实现方法、装置、终端和存储介质,以解决前端react框架hook组件,在代码改造完成后,在测试改造后的代码是否能够完美兼容原有功能逻辑时,引入并加入自动化测试,存在人工成本和维护成本高的问题,达到通过设置前端react框架hook组件自动化测试工具,在开发者在完成代码后,使用该工具,可以生成hook组件的自动化测试代码,可以降低人工成本和维护成本的效果。The purpose of the present invention is to provide an automated testing tool and its implementation method, device, terminal and storage medium, so as to solve the front-end react framework hook component, after the code transformation is completed, test whether the transformed code can be perfectly compatible with the original When introducing and adding automated testing to functional logic, there are problems of high labor cost and maintenance cost. By setting up an automated testing tool for hook components of the front-end react framework, developers can use this tool to generate automation of hook components after completing the code. Testing code can reduce labor costs and maintenance costs.

本发明提供一种自动化测试工具的实现方法中,所述自动化测试工具,包括:前端react框架hook组件自动化测试工具;所述前端react框架hook组件自动化测试工具的实现方法,包括:检查并安装前端react框架hook组件自动化测试所需要的文件,并在执行前端react框架hook组件自动化测试时执行前端react框架hook组件自动化测试代码的存放目录下的测试文件;所述前端react框架hook组件中,具有N个hook文件,N为正整数;根据前端react框架hook组件中每一个hook文件,生成一个自动化测试案例代码;根据前端react框架hook组件中所有hook文件,生成所有hook文件的自动化测试案例代码;针对所述所有自动化测试案例代码所对应的每个测试案例,确定每个测试案例的动态值;将所有测试案例关联的动态值,进行页面可视化展示;所展示的可视化页面,是能够对所有hook文件的自动化测试案例代码中任一测试案例直接编辑修改的页面。The present invention provides an implementation method of an automated testing tool, wherein the automated testing tool includes: an automated testing tool for a front-end react framework hook component; an implementation method for the automated testing tool for the front-end react framework hook component includes: checking and installing the front-end The files required for the automated test of the react framework hook component, and the test files in the storage directory of the automated test code of the front-end react framework hook component are executed when the automated test of the front-end react framework hook component is executed; the front-end react framework hook component has N Hook files, N is a positive integer; according to each hook file in the front-end react framework hook component, an automated test case code is generated; according to all hook files in the front-end react framework hook component, the automated test case code for all hook files is generated; for The dynamic value of each test case is determined for each test case corresponding to all the automated test case codes; the dynamic value associated with all test cases is visually displayed on the page; the displayed visual page is capable of displaying all hook files. Any test case in the automated test case code directly edits the modified page.

在一些实施方式中,检查并安装前端react框架hook组件自动化测试所需要的文件,并在执行前端react框架hook组件自动化测试时执行前端react框架hook组件自动化测试代码的存放目录下的测试文件,包括:检查并安装前端react框架hook组件自动化测试所需要的依赖文件;设置前端react框架hook组件自动化测试代码的存放目录,以存放生成的前端react框架hook组件自动化测试代码;设置配置项,以在执行前端react框架hook组件自动化测试时,索引该配置项中前端react框架hook组件自动化测试代码的存放目录下的测试文件,并执行该测试文件。In some embodiments, files required for automated testing of the hook component of the front-end react framework are checked and installed, and when the automated testing of the hook component of the front-end react framework is executed, the test files in the storage directory of the automated test code of the hook component of the front-end react framework are executed, including : Check and install the dependency files required for automated testing of the hook component of the front-end react framework; set the storage directory of the automated test code of the hook component of the front-end react framework to store the generated automated test code of the hook component of the front-end react framework; When the front-end react framework hook component is automatically tested, index the test file in the storage directory of the front-end react framework hook component automated test code in this configuration item, and execute the test file.

在一些实施方式中,根据前端react框架hook组件中每一个hook文件,生成一个自动化测试案例代码,包括:根据预先设置的配置项中配置的需要进行自动化测试的hook组件地址的数组,遍历数组中的每一个元素,找到与数组中的每一个元素对应的每一个hook文件,作为需要进行自动化测试的每一个hook文件;针对需要进行自动化测试的每一个hook文件,执行生成自动化测试案例代码的操作,生成需要进行自动化测试的每一个hook文件的基础代码;根据需要进行自动化测试的每一个hook文件的基础代码,识别不同场景语法,智能反向推导期望结果,并生成需要进行自动化测试的每一个hook文件的代码,作为需要进行自动化测试的每一个hook文件所对应的测试案例代码。In some embodiments, generating an automated test case code according to each hook file in the hook component of the front-end react framework, including: traversing the array of addresses of hook components configured in preset configuration items that need to perform automated testing, and traversing the array For each element of the array, find each hook file corresponding to each element in the array as each hook file that needs to be automated testing; for each hook file that needs to be automated testing, perform the operation of generating automated test case code , generate the basic code of each hook file that needs to be tested automatically; the basic code of each hook file that needs to be tested automatically, identify the syntax of different scenarios, intelligently reverse the expected results, and generate each hook file that needs to be tested automatically. The code of the hook file is used as the test case code corresponding to each hook file that needs to be tested automatically.

在一些实施方式中,针对需要进行自动化测试的每一个hook文件,执行生成自动化测试案例代码的操作,生成需要进行自动化测试的每一个hook文件的基础代码,包括:创建前端react框架hook组件自动化测试代码的存放目录,在该存放目录所在文件夹中,生成与需要进行自动化测试的每一个hook文件对应的空白文件;通过第一设定语法,将未经模拟的hook组件、模拟hook组件渲染完成的方法renderHook、模拟触发hook组件对外暴露函数的act方法,引入到需要进行自动化测试的每一个hook文件中;通过第二设定语法,将即将生成的需要进行自动化测试的每一个hook文件所对应的测试案例包裹起来,以得到需要进行自动化测试的每一个hook文件的基础代码。In some embodiments, the operation of generating automated test case code is performed for each hook file that needs to be tested automatically, and the basic code of each hook file that needs to be tested automatically is generated, including: creating a front-end react framework hook component for automated testing The code storage directory, in the folder where the storage directory is located, generate a blank file corresponding to each hook file that needs to be automated testing; through the first setting syntax, the unsimulated hook components and simulated hook components are rendered. The method renderHook, the act method that simulates the external exposure function of the trigger hook component, is introduced into each hook file that needs to be tested automatically; through the second setting syntax, the corresponding hook file that needs to be tested automatically will be generated. The test cases are wrapped to get the base code for each hook file that needs to be automated.

在一些实施方式中,所述根据需要进行自动化测试的每一个hook文件的基础代码,识别不同场景语法,智能反向推导期望结果,并生成需要进行自动化测试的每一个hook文件的代码,作为需要进行自动化测试的每一个hook文件所对应的测试案例代码,包括:针对未经模拟的hook组件,在函数最后有通过return关键字对外输出的变量中,通过对变量进行typeof判断,如果得出的结果是'function',则反向查找未经模拟的hook组件的hook代码中,关于所述函数的函数名的完整代码块在未经模拟的hook组件的hook代码的代码块内对于所述变量的操作,以反向推导出变量执行的案例和结果;若在未经模拟的hook组件内出现useEffect函数,则确定未经模拟的hook组件的hook文件内有依赖更新事件,对hook组件内依赖更新事件进行模拟,得到自动化测试代码;并在所述自动化测试代码已被修改的情况下,通过所述自动化测试代码被修改后的依赖变量的值,来模拟触发更新事件的执行。In some embodiments, the basic code of each hook file that needs to perform automated testing, identify different scene syntaxes, intelligently reverse deduce the expected result, and generate the code of each hook file that needs to be automated testing, as required The test case code corresponding to each hook file for automated testing, including: for the unsimulated hook component, in the variable output through the return keyword at the end of the function, by judging the variable by typeof, if the result is obtained The result is 'function', then reverse lookup in the hook code of the unsimulated hook component, the complete code block about the function name of the function is in the code block of the hook code of the unsimulated hook component for the variable The operation to reversely deduce the case and result of variable execution; if the useEffect function appears in the unsimulated hook component, it is determined that there is a dependency update event in the hook file of the unsimulated hook component. The update event is simulated to obtain automated test code; and when the automated test code has been modified, the execution of the update event is simulated and triggered by the value of the modified dependent variable of the automated test code.

在一些实施方式中,将所有测试案例关联的动态值,进行页面可视化展示,包括:当所有测试案例生成完成后,将所有测试案例关联的动态值,生成一个页面展示文件;通过执行设定命令,浏览器访问页面地址,打开该页面;在页面上,展示包含所有hook组件名称、以及每一个组件的所有测试案例描述列表的内容。In some embodiments, the dynamic values associated with all test cases are displayed on the page visually, including: after all test cases are generated, a page display file is generated for the dynamic values associated with all test cases; by executing the setting command , the browser accesses the page address and opens the page; on the page, the content including the names of all hook components and the description list of all test cases for each component is displayed.

与上述方法相匹配,本发明另一方面提供一种自动化测试工具的实现装置中,所述自动化测试工具,包括:前端react框架hook组件自动化测试工具;所述前端react框架hook组件自动化测试工具的实现装置,包括:设置单元,被配置为检查并安装前端react框架hook组件自动化测试所需要的文件,并在执行前端react框架hook组件自动化测试时执行前端react框架hook组件自动化测试代码的存放目录下的测试文件;所述前端react框架hook组件中,具有N个hook文件,N为正整数;生成单元,被配置为根据前端react框架hook组件中每一个hook文件,生成一个自动化测试案例代码;根据前端react框架hook组件中所有hook文件,生成所有hook文件的自动化测试案例代码;展示单元,被配置为针对所述所有自动化测试案例代码所对应的每个测试案例,确定每个测试案例的动态值;将所有测试案例关联的动态值,进行页面可视化展示;所展示的可视化页面,是能够对所有hook文件的自动化测试案例代码中任一测试案例直接编辑修改的页面。Matching with the above method, another aspect of the present invention provides an implementation device for an automated testing tool, wherein the automated testing tool includes: an automated testing tool for a front-end react framework hook component; The implementation device includes: a setting unit, which is configured to check and install the files required for the automated test of the hook component of the front-end react framework, and execute the automated test code of the hook component of the front-end react framework when the automated test of the hook component of the front-end react framework is executed. The test file; the front-end react framework hook component has N hook files, where N is a positive integer; the generation unit is configured to generate an automated test case code according to each hook file in the front-end react framework hook component; according to All hook files in the hook component of the front-end react framework generate automated test case codes for all hook files; the display unit is configured to determine the dynamic value of each test case for each test case corresponding to all the automated test case codes ; Visually display the dynamic values associated with all test cases on the page; the displayed visual page is a page that can directly edit and modify any test case in the automated test case code of all hook files.

在一些实施方式中,所述设置单元,检查并安装前端react框架hook组件自动化测试所需要的文件,并在执行前端react框架hook组件自动化测试时执行前端react框架hook组件自动化测试代码的存放目录下的测试文件,包括:检查并安装前端react框架hook组件自动化测试所需要的依赖文件;设置前端react框架hook组件自动化测试代码的存放目录,以存放生成的前端react框架hook组件自动化测试代码;设置配置项,以在执行前端react框架hook组件自动化测试时,索引该配置项中前端react框架hook组件自动化测试代码的存放目录下的测试文件,并执行该测试文件。In some embodiments, the setting unit checks and installs the files required for the automated test of the front-end react framework hook component, and executes the front-end react framework hook component automated test code storage directory when executing the automated test of the front-end react framework hook component The test files, including: check and install the dependency files required for the automated testing of the hook component of the front-end react framework; set the storage directory of the automated test code of the hook component of the front-end react framework to store the generated automated test code of the hook component of the front-end react framework; set the configuration Item, to index the test file in the storage directory of the automated test code of the front-end react framework hook component in the configuration item, and execute the test file when the automated test of the hook component of the front-end react framework is executed.

在一些实施方式中,所述生成单元,根据前端react框架hook组件中每一个hook文件,生成一个自动化测试案例代码,包括:根据预先设置的配置项中配置的需要进行自动化测试的hook组件地址的数组,遍历数组中的每一个元素,找到与数组中的每一个元素对应的每一个hook文件,作为需要进行自动化测试的每一个hook文件;针对需要进行自动化测试的每一个hook文件,执行生成自动化测试案例代码的操作,生成需要进行自动化测试的每一个hook文件的基础代码;根据需要进行自动化测试的每一个hook文件的基础代码,识别不同场景语法,智能反向推导期望结果,并生成需要进行自动化测试的每一个hook文件的代码,作为需要进行自动化测试的每一个hook文件所对应的测试案例代码。In some embodiments, the generating unit generates an automated test case code according to each hook file in the hook component of the front-end react framework, including: the address of the hook component that needs to perform automated testing according to the configuration in the preset configuration item Array, traverse each element in the array, find each hook file corresponding to each element in the array, as each hook file that needs to be tested automatically; for each hook file that needs to be tested automatically, execute the generation automation The operation of the test case code generates the basic code of each hook file that needs to be tested automatically; the basic code of each hook file that needs to be tested automatically, recognizes the syntax of different scenarios, intelligently reverses the expected results, and generates the required results. The code of each hook file of automated testing is used as the test case code corresponding to each hook file that needs to be automated tested.

在一些实施方式中,所述生成单元,针对需要进行自动化测试的每一个hook文件,执行生成自动化测试案例代码的操作,生成需要进行自动化测试的每一个hook文件的基础代码,包括:创建前端react框架hook组件自动化测试代码的存放目录,在该存放目录所在文件夹中,生成与需要进行自动化测试的每一个hook文件对应的空白文件;通过第一设定语法,将未经模拟的hook组件、模拟hook组件渲染完成的装置renderHook、模拟触发hook组件对外暴露函数的act装置,引入到需要进行自动化测试的每一个hook文件中;通过第二设定语法,将即将生成的需要进行自动化测试的每一个hook文件所对应的测试案例包裹起来,以得到需要进行自动化测试的每一个hook文件的基础代码。In some embodiments, the generating unit performs an operation of generating automated test case codes for each hook file that needs to be automated testing, and generates basic code for each hook file that needs to be automated testing, including: creating a front-end react The storage directory of the automated test code of the framework hook component. In the folder where the storage directory is located, a blank file corresponding to each hook file that needs to be automatically tested is generated; through the first setting syntax, the unsimulated hook components, The renderHook device that simulates the rendering of the hook component, and the act device that simulates the external exposure function of the hook component are introduced into each hook file that needs to be automated. The test case corresponding to a hook file is wrapped to get the basic code of each hook file that needs to be automated.

在一些实施方式中,所述生成单元,所述根据需要进行自动化测试的每一个hook文件的基础代码,识别不同场景语法,智能反向推导期望结果,并生成需要进行自动化测试的每一个hook文件的代码,作为需要进行自动化测试的每一个hook文件所对应的测试案例代码,包括:针对未经模拟的hook组件,在函数最后有通过return关键字对外输出的变量中,通过对变量进行typeof判断,如果得出的结果是'function',则反向查找未经模拟的hook组件的hook代码中,关于所述函数的函数名的完整代码块在未经模拟的hook组件的hook代码的代码块内对于所述变量的操作,以反向推导出变量执行的案例和结果;若在未经模拟的hook组件内出现useEffect函数,则确定未经模拟的hook组件的hook文件内有依赖更新事件,对hook组件内依赖更新事件进行模拟,得到自动化测试代码;并在所述自动化测试代码已被修改的情况下,通过所述自动化测试代码被修改后的依赖变量的值,来模拟触发更新事件的执行。In some embodiments, the generating unit, the basic code of each hook file for automated testing as required, identifies different scene syntaxes, intelligently deduces expected results in reverse, and generates each hook file that requires automated testing The code, as the test case code corresponding to each hook file that needs to be automated, includes: For unsimulated hook components, in the variables that are output externally through the return keyword at the end of the function, the variables are judged by typeof , if the result is 'function', then reverse the lookup in the hook code of the unsimulated hook component, the complete code block of the function name of the function in the code block of the hook code of the unsimulated hook component For the operation of the variable, the case and result of the variable execution are deduced in reverse; if the useEffect function appears in the unsimulated hook component, it is determined that there is a dependency update event in the hook file of the unsimulated hook component, Simulate the dependency update event in the hook component to obtain the automated test code; and when the automated test code has been modified, simulate the triggering of the update event by the value of the modified dependency variable of the automated test code. implement.

在一些实施方式中,所述展示单元,将所有测试案例关联的动态值,进行页面可视化展示,包括:当所有测试案例生成完成后,将所有测试案例关联的动态值,生成一个页面展示文件;通过执行设定命令,浏览器访问页面地址,打开该页面;在页面上,展示包含所有hook组件名称、以及每一个组件的所有测试案例描述列表的内容。In some embodiments, the display unit displays the dynamic values associated with all test cases on a page, including: after all test cases are generated, generating a page display file with the dynamic values associated with all test cases; By executing the setting command, the browser accesses the page address and opens the page; on the page, the content including the names of all hook components and the description list of all test cases for each component is displayed.

与上述装置相匹配,本发明再一方面提供一种终端,包括:以上所述的自动化测试工具的实现装置。Matching with the above-mentioned device, another aspect of the present invention provides a terminal, including: the above-mentioned implementation device of the automatic test tool.

与上述方法相匹配,本发明再一方面提供一种自动化测试工具,所述自动化测试工具是由以上所述的自动化测试工具的实现方法得到的。Matching with the above method, another aspect of the present invention provides an automated test tool, the automated test tool is obtained by the above-mentioned implementation method of the automated test tool.

与上述方法相匹配,本发明再一方面提供一种存储介质,所述存储介质包括存储的程序,其中,在所述程序运行时控制所述存储介质所在设备执行以上所述的自动化测试工具的实现方法。Matching with the above method, another aspect of the present invention provides a storage medium, the storage medium includes a stored program, wherein when the program runs, the device where the storage medium is located is controlled to execute the automatic test tool described above. Implementation.

由此,本发明的方案,通过设置一种前端react框架hook组件自动化测试工具,该前端react框架hook组件自动化测试工具的实现方式,主要包括:检查并安装前端react框架hook组件自动化测试所需要的文件,并在执行前端react框架hook组件自动化测试时执行前端react框架hook组件自动化测试代码的存放目录下的测试文件;根据前端react框架hook组件中每一个hook文件,生成自动化测试案例代码;将所有测试案关联的动态值,进行页面可视化展示,并支持在页面上直接编辑修改,实现可视化界面修改测试案例的目的;从而,通过设置前端react框架hook组件自动化测试工具,在开发者在完成代码后,使用该工具,可以生成hook组件的自动化测试代码,可以降低人工成本和维护成本。Therefore, the solution of the present invention, by setting up a front-end react framework hook component automated testing tool, the implementation method of the front-end react framework hook component automated testing tool mainly includes: checking and installing the front-end react framework hook component automated testing. file, and execute the test file in the storage directory of the automated test code of the front-end react framework hook component when executing the automated test of the front-end react framework hook component; according to each hook file in the front-end react framework hook component, generate the automated test case code; The dynamic value associated with the test case can be displayed visually on the page, and it supports direct editing and modification on the page, so as to realize the purpose of modifying the test case on the visual interface; thus, by setting the front-end react framework hook component automation test tool, after the developer completes the code , Using this tool, you can generate automated test code for hook components, which can reduce labor costs and maintenance costs.

本发明的其它特征和优点将在随后的说明书中阐述,并且,部分地从说明书中变得显而易见,或者通过实施本发明而了解。Other features and advantages of the present invention will be set forth in the description which follows, and in part will be apparent from the description, or may be learned by practice of the invention.

下面通过附图和实施例,对本发明的技术方案做进一步的详细描述。The technical solutions of the present invention will be further described in detail below through the accompanying drawings and embodiments.

附图说明Description of drawings

图1为本发明的自动化测试工具的实现方法的一实施例的流程示意图;1 is a schematic flowchart of an embodiment of an implementation method of an automated testing tool of the present invention;

图2为本发明的方法中设置前端react框架hook组件自动化测试所需要的文件的一实施例的流程示意图;2 is a schematic flowchart of an embodiment of setting files required for automated testing of front-end react framework hook components in the method of the present invention;

图3为本发明的方法中针对每一个hook文件生成一个自动化测试案例代码的一实施例的流程示意图;3 is a schematic flowchart of an embodiment of generating an automated test case code for each hook file in the method of the present invention;

图4为本发明的方法中生成每一个hook文件的基础代码的一实施例的流程示意图;4 is a schematic flowchart of an embodiment of generating the basic code of each hook file in the method of the present invention;

图5为本发明的方法中生成每一个hook文件所对应的测试案例代码的一实施例的流程示意图;5 is a schematic flowchart of an embodiment of generating a test case code corresponding to each hook file in the method of the present invention;

图6为本发明的方法中将所有测试案例进行页面可视化展示的一实施例的流程示意图;6 is a schematic flowchart of an embodiment of visually displaying all test cases on a page in the method of the present invention;

图7为本发明的自动化测试工具的实现装置的一实施例的结构示意图。FIG. 7 is a schematic structural diagram of an embodiment of an apparatus for implementing an automated testing tool of the present invention.

结合附图,本发明实施例中附图标记如下:With reference to the accompanying drawings, the reference numerals in the embodiments of the present invention are as follows:

102-设置单元;104-生成单元;106-展示单元。102-setting unit; 104-generating unit; 106-displaying unit.

具体实施方式Detailed ways

为使本发明的目的、技术方案和优点更加清楚,下面将结合本发明具体实施例及相应的附图对本发明技术方案进行清楚、完整地描述。显然,所描述的实施例仅是本发明一部分实施例,而不是全部的实施例。基于本发明中的实施例,本领域普通技术人员在没有做出创造性劳动前提下所获得的所有其他实施例,都属于本发明保护的范围。In order to make the objectives, technical solutions and advantages of the present invention clearer, the technical solutions of the present invention will be clearly and completely described below with reference to the specific embodiments of the present invention and the corresponding drawings. Obviously, the described embodiments are only some, but not all, embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.

考虑到,前端react框架hook组件,在代码改造完成后,在测试改造后的代码是否能够完美兼容原有功能逻辑时,项目引入并加入自动化测试,会存在一些门槛:Considering that the hook component of the front-end react framework, after the code transformation is completed, when testing whether the transformed code can be perfectly compatible with the original functional logic, the project introduces and adds automated testing, there will be some thresholds:

(1)自动化测试代码的编写需要具备相当语法的知识储备,对于开发者来说,相当于需要额外学习业务逻辑以外的语言。目前自动化测试大部分还是在大型公司的大型项目中采用,市面上的这部分人才不容易招到。(1) The writing of automated test code requires a considerable knowledge reserve of grammar. For developers, it is equivalent to additionally learning languages other than business logic. At present, most of the automated tests are still used in large-scale projects of large companies, and this part of the talent on the market is not easy to recruit.

(2)自动化测试代码需要长期维护,当有新的需求新增,或原来的组件需要在某些场景下进行修改时,自动化测试的代码需要进行新增测试案例代码。随着组件的越来越丰富,加上不同自动化代码开发人员的编码风格不一样,维护成本也越来越高。(2) The automated test code requires long-term maintenance. When new requirements are added, or the original components need to be modified in some scenarios, the automated test code needs to be added with test case code. As the components become more and more abundant, and the coding styles of different automation code developers are different, the maintenance cost is also getting higher and higher.

(3)自动化测试案例,如果需要调整,只有开发人员可以修改代码进行调整,其他团队成员如测试人员、产品经理等想要自行针对不同场景,调整测试案例,就只能请求开发同事帮忙额外添加测试案例。(3) Automated test cases, if adjustments are needed, only developers can modify the code to make adjustments. Other team members such as testers, product managers, etc. want to adjust test cases for different scenarios by themselves, they can only ask development colleagues to help add additional Test Case.

而目前业内尚没有一款工具可以很好的解决这个问题。At present, there is no tool in the industry that can solve this problem well.

至少针对前端react框架hook组件,在代码改造完成后,在测试改造后的代码是否能够完美兼容原有功能逻辑时,引入并加入自动化测试,存在人工成本和维护成本高的问题,本发明的方案,提供了一种自动化测试工具,具体是一种前端react框架hook组件自动化测试工具。下面结合附图,示例性描述该工具的实现方式及使用方式。At least for the hook component of the front-end react framework, after the code transformation is completed, when testing whether the transformed code can be perfectly compatible with the original functional logic, an automated test is introduced and added, which has the problem of high labor cost and maintenance cost. The solution of the present invention , which provides an automated testing tool, specifically a front-end react framework hook component automated testing tool. The implementation and usage of the tool will be exemplarily described below with reference to the accompanying drawings.

根据本发明的实施例,提供了一种自动化测试工具的实现方法,如图1所示本发明的方法的一实施例的流程示意图。所述自动化测试工具,包括:前端react框架hook组件自动化测试工具。所述前端react框架hook组件自动化测试工具的实现方法,包括:步骤S110至步骤S130。According to an embodiment of the present invention, an implementation method of an automated testing tool is provided, as shown in FIG. 1 , a schematic flowchart of an embodiment of the method of the present invention. The automated testing tool includes: a front-end react framework hook component automated testing tool. The implementation method of the front-end react framework hook component automated testing tool includes steps S110 to S130.

在步骤S110处,检查并安装前端react框架hook组件自动化测试所需要的文件,并在执行前端react框架hook组件自动化测试时执行前端react框架hook组件自动化测试代码的存放目录下的测试文件。所述前端react框架hook组件中,具有N个hook文件,N为正整数。At step S110, check and install the files required for the automated testing of the front-end react framework hook component, and execute the test files in the storage directory of the automated testing code of the front-end react framework hook component when executing the automated testing of the front-end react framework hook component. The front-end react framework hook component has N hook files, where N is a positive integer.

在一些实施方式中,步骤S110中检查并安装前端react框架hook组件自动化测试所需要的文件,并在执行前端react框架hook组件自动化测试时执行前端react框架hook组件自动化测试代码的存放目录下的测试文件的具体过程,可以参见以下示例性说明。In some embodiments, in step S110, the files required for the automated test of the hook component of the front-end react framework are checked and installed, and the test in the storage directory of the automated test code of the hook component of the front-end react framework is executed when the automated test of the hook component of the front-end react framework is executed. For the specific process of the file, please refer to the following exemplary description.

下面结合图2所示本发明的方法中设置前端react框架hook组件自动化测试所需要的文件的一实施例流程示意图,进一步说明设置前端react框架hook组件自动化测试所需要的文件的具体过程,包括:步骤S210至步骤S230。Below in conjunction with the method of the present invention shown in Figure 2, an embodiment flow diagram of setting the files required for the automated testing of the front-end react framework hook component will further illustrate the specific process of setting the files required for the automated testing of the front-end react framework hook component, including: Steps S210 to S230.

步骤S210,检查并安装前端react框架hook组件自动化测试所需要的依赖文件。Step S210, check and install the dependency files required for the automated testing of the hook component of the front-end react framework.

例如:当构建命令启动时,会去检索项目中的package.json文件中,devDependencies对象中,是否已包含自动化测试框架库jest、以及针对模拟触发hook组件所使用的@testing-library/react-hooks。如果不包含,则会通过以下语法进行启动安装npm install jest@testing-library/react-hook-D。For example: when the build command is started, it will search the package.json file in the project, the devDependencies object, whether the automated testing framework library jest and @testing-library/react-hooks used for the simulation trigger hook component are included. . If not included, it will be launched with the following syntax: npm install jest@testing-library/react-hook-D.

步骤S220,设置前端react框架hook组件自动化测试代码的存放目录,以存放生成的前端react框架hook组件自动化测试代码。Step S220, setting a storage directory for the automated test code of the hook component of the front-end react framework, so as to store the generated automated test code of the hook component of the front-end react framework.

例如:在项目的根目录,生成一个文件夹名称为autotest的文件夹。目的在于:在下面的步骤2的执行过程中,生成的自动化测试代码,会在此文件夹内,按照以下规则新建关于该hook组件的自动化测试代码。规则为:原组件名称+.test.js。For example: in the root directory of the project, generate a folder named autotest. The purpose is: During the execution of the following step 2, the generated automated test code will be created in this folder according to the following rules for the automated test code of the hook component. The rule is: original component name + .test.js.

步骤S230,设置配置项,以在执行前端react框架hook组件自动化测试时,索引该配置项中前端react框架hook组件自动化测试代码的存放目录下的测试文件,并执行该测试文件。Step S230, setting a configuration item to index the test file in the configuration item in the storage directory of the automated test code of the front-end react framework hook component when executing the automated test of the hook component of the front-end react framework, and execute the test file.

例如:在package.json文件中设置jest基础配置,核心配置项为"testRegex",将其值配置文件(/autotest/)$。For example: set the basic configuration of jest in the package.json file, the core configuration item is "testRegex", and set its value to the configuration file (/autotest/)$.

在步骤S120处,根据前端react框架hook组件中每一个hook文件,生成一个自动化测试案例代码。根据前端react框架hook组件中所有hook文件,生成所有hook文件的自动化测试案例代码。At step S120, an automated test case code is generated according to each hook file in the hook component of the front-end react framework. According to all the hook files in the hook component of the front-end react framework, the automated test case code of all the hook files is generated.

在一些实施方式中,步骤S120中根据前端react框架hook组件中每一个hook文件,生成一个自动化测试案例代码的具体过程,参见以下示例性说明。In some embodiments, in step S120, for the specific process of generating an automated test case code according to each hook file in the hook component of the front-end react framework, see the following exemplary description.

下面结合图3所示本发明的方法中针对每一个hook文件生成一个自动化测试案例代码的一实施例流程示意图,进一步说明针对每一个hook文件生成一个自动化测试案例代码的具体过程,包括:步骤S310至步骤S330。Below in conjunction with the method of the present invention shown in FIG. 3, a schematic flow chart of an embodiment of generating an automated test case code for each hook file will further illustrate the specific process of generating an automated test case code for each hook file, including: Step S310 Go to step S330.

步骤S310,根据预先设置的配置项中配置的需要进行自动化测试的hook组件地址的数组,遍历数组中的每一个元素,找到与数组中的每一个元素对应的每一个hook文件,作为需要进行自动化测试的每一个hook文件。Step S310, traverse each element in the array according to the array of hook component addresses configured in the preset configuration item and need to perform automated testing, and find each hook file corresponding to each element in the array, as the need to automate Every hook file tested.

例如:通过配置文件config.js中,配置的需要进行自动化测试的hook组件地址的数组,通过遍历数组中的每一个元素,找到对应的每一个hook文件,并执行后续生成自动化测试案例代码操作。For example, in the configuration file config.js, configure an array of hook component addresses that need to be automated testing, find each corresponding hook file by traversing each element in the array, and perform subsequent operations to generate automated test case code.

步骤S320,针对需要进行自动化测试的每一个hook文件,执行生成自动化测试案例代码的操作,生成需要进行自动化测试的每一个hook文件的基础代码。Step S320, for each hook file that needs to be automated testing, perform the operation of generating automated test case code, and generate the basic code of each hook file that needs to be automated testing.

在一些实施方式中,步骤S320中针对需要进行自动化测试的每一个hook文件,执行生成自动化测试案例代码的操作,生成需要进行自动化测试的每一个hook文件的基础代码的具体过程,参见以下示例性说明。In some embodiments, in step S320, for each hook file that needs to be automated testing, the operation of generating the automated test case code is performed, and the specific process of generating the basic code of each hook file that needs to be automated testing, see the following exemplary illustrate.

下面结合图4所示本发明的方法中生成每一个hook文件的基础代码的一实施例流程示意图,进一步说明生成每一个hook文件的基础代码的具体过程,包括:步骤S410至步骤S430。The specific process of generating the basic code of each hook file is further described below with reference to the schematic flowchart of an embodiment of generating the basic code of each hook file in the method of the present invention shown in FIG. 4 , including steps S410 to S430.

步骤S410,创建前端react框架hook组件自动化测试代码的存放目录,在该存放目录所在文件夹中,生成与需要进行自动化测试的每一个hook文件对应的空白文件。Step S410, creating a storage directory for the automated test code of the hook component of the front-end react framework, and generating a blank file corresponding to each hook file that needs to be automated testing in the folder where the storage directory is located.

例如:假设当前hook组件名为A,在前期准备工作中创建的autotest文件夹中,生成与hook文件对应的文件名为A.test.js的空白文件。For example: Assuming that the current hook component is named A, in the autotest folder created in the previous preparations, a blank file named A.test.js corresponding to the hook file is generated.

步骤S420,通过第一设定语法,将未经模拟的hook组件、模拟hook组件渲染完成的方法renderHook、模拟触发hook组件对外暴露函数的act方法,引入到需要进行自动化测试的每一个hook文件中。Step S420, through the first setting syntax, introduce the unsimulated hook component, the renderHook method that simulates the rendering of the hook component, and the act method that simulates triggering the external exposure function of the hook component into each hook file that needs to be automated. .

例如:通过import语法,将未经模拟的hook组件、模拟hook组件渲染完成的方法renderHook、模拟触发hook组件对外暴露函数的act方法,引入到测试文件中来。For example, through the import syntax, the unsimulated hook component, the renderHook method that simulates the rendering of the hook component, and the act method that simulates the external exposure function of the hook component are introduced into the test file.

步骤S430,通过第二设定语法,将即将生成的需要进行自动化测试的每一个hook文件所对应的测试案例包裹起来,以得到需要进行自动化测试的每一个hook文件的基础代码,以将需要进行自动化测试的每一个hook文件所对应的测试案例按照设定的文件级别范围进行分组定义,以在后期测试结果输出的时按该分组定义的内容进行区分。Step S430, through the second setting syntax, wrap the test case corresponding to each hook file that needs to be automated testing to be generated to obtain the basic code of each hook file that needs to be automated The test case corresponding to each hook file of the automated test is grouped and defined according to the set file level range, so as to distinguish according to the content of the group definition when the later test result is output.

例如:通过语法,describe('A hook autotest',()=>{测试用例代码}),将即将生成的测试用例包裹起来,用于把该文件的测试用例按照文件级别的范围,进行一个范围的分组定义,后期测试结果输出的时候,会按照分组进行定义的内容(即A hookautotest),进行区分。For example: through the syntax, describe('A hook autotest',()=>{test case code}), wrap the test case to be generated, and use it to carry out a scope of the test case of the file according to the scope of the file level When the test results are output later, they will be distinguished according to the content defined by the group (ie A hookautotest).

步骤S330,根据需要进行自动化测试的每一个hook文件的基础代码,识别不同场景语法,智能反向推导期望结果,并生成需要进行自动化测试的每一个hook文件的代码,作为需要进行自动化测试的每一个hook文件所对应的测试案例代码。Step S330, according to the basic code of each hook file for automated testing, identify different scene grammars, intelligently reverse the expected result, and generate the code of each hook file that needs to be automated testing, as each hook file that needs to be automated testing. The test case code corresponding to a hook file.

在一些实施方式中,步骤S330中根据需要进行自动化测试的每一个hook文件的基础代码,识别不同场景语法,智能反向推导期望结果,并生成需要进行自动化测试的每一个hook文件的代码,作为需要进行自动化测试的每一个hook文件所对应的测试案例代码的具体过程,参见以下示例性说明。In some embodiments, in step S330, according to the basic code of each hook file for automated testing, different scene syntaxes are identified, the expected result is intelligently reversed, and the code of each hook file that needs to be automated tested is generated as For the specific process of the test case code corresponding to each hook file that needs to be automated, see the following exemplary description.

下面结合图5所示本发明的方法中生成每一个hook文件所对应的测试案例代码的一实施例流程示意图,进一步说明生成每一个hook文件所对应的测试案例代码的具体过程,包括:步骤S510和步骤S520。The specific process of generating the test case code corresponding to each hook file is further described below in conjunction with the schematic flow chart of an embodiment of generating the test case code corresponding to each hook file in the method of the present invention shown in FIG. 5, including: Step S510 and step S520.

步骤S510,针对未经模拟的hook组件,在函数最后有通过return关键字对外输出的变量中,通过对变量进行typeof判断,如果得出的结果是'function',则说明hook组件有对外输出函数给外部调用,进而,反向查找未经模拟的hook组件的hook代码中,关于所述函数的函数名的完整代码块在未经模拟的hook组件的hook代码的代码块内对于所述变量的操作,以反向推导出变量执行的案例和结果。Step S510, for the unsimulated hook component, in the variable that is output to the outside through the return keyword at the end of the function, the variable is judged by typeof. If the result obtained is 'function', it means that the hook component has an external output function. For external calls, and further, in the hook code of the unsimulated hook component, the complete code block about the function name of the function is in the code block of the hook code of the unsimulated hook component for the variable Actions to reverse the case and result of the variable execution.

具体地,识别不同场景语法,智能反向推导期望结果,并生成测试案例代码。该步骤进一步可以包括:对于对外输出函数的测试。Specifically, it recognizes the syntax of different scenarios, intelligently reverses the expected results, and generates test case code. The step may further include: testing the external output function.

当未经模拟的hook组件中,在函数最后有通过return关键字对外输出的变量中,通过对变量进行typeof判断,如果得出的结果是'function',则说明hook组件有对外输出函数给外部调用。反向查找hook代码中,关于所述函数的函数名的完整代码块在未经模拟的hook组件的hook代码的代码块内对于所述变量的操作,以反向推导出变量执行的案例和结果。具体规则如下:When the unsimulated hook component has variables that are output to the outside through the return keyword at the end of the function, by judging the variable by typeof, if the result is 'function', it means that the hook component has an external output function to the outside. transfer. In the reverse lookup hook code, the complete code block of the function name of the function is the operation of the variable within the code block of the hook code of the unsimulated hook component, so as to reversely deduce the case and result of the variable execution . The specific rules are as follows:

(11)通过useState关键字,查找到变量的初始化代码,通过useState方法执行时,传入的参数,即为变量的初始化值。对其值进行typeof判断,得到的结果即为初始化值的数据类型。(11) The initialization code of the variable is found through the useState keyword. When the useState method is executed, the incoming parameter is the initialization value of the variable. Perform typeof judgment on its value, and the result obtained is the data type of the initialized value.

(12)通过代码块中,涉及到该变量的操作,如+、-、*、/操作。这里对于+号需要做一下区分。如果变量的初始值的数据类型为数字,则+号表示的是数学运算。如果初始值数据类型为字符串,则+号表示的是字符串的拼接操作。其他的操作符如-、*、/都表示了数学运算。举一个最简单的例子,变量的初始值为数字10,在代码中,执行了对于变量执行了*Math.randon()的操作,由于Math.randon(),,会随机输出0-1之间的一个随机数,则输出的结果可以预测为0-10。同理,如果变量初始值为字符串hello,执行了对于变量的操作+'world',则输出的结果可以预测为helloworld。(12) Through the code block, operations involving the variable, such as +, -, *, / operations. A distinction needs to be made here for the + sign. If the data type of the variable's initial value is number, the + sign indicates a mathematical operation. If the data type of the initial value is a string, the + sign indicates the concatenation operation of the string. Other operators such as -, *, / all represent mathematical operations. For the simplest example, the initial value of the variable is the number 10. In the code, the operation of *Math.randon() is performed on the variable. Because of Math.randon(), it will randomly output between 0-1 is a random number, the output result can be predicted as 0-10. Similarly, if the initial value of the variable is the string hello, and the operation on the variable + 'world' is performed, the output result can be predicted as helloworld.

(13)判断得到预测结果后,需要生成自动化测试代码。首先通过语法,it('案例描述',()=>{测试代码}),将测试案例包裹在花括号内,其中,案例描述按照以下规则生成:原hook文件中的方法名+case。(13) After judging and obtaining the prediction result, it is necessary to generate automated test code. First, through the syntax, it('case description', ()=>{test code}), wrap the test case in curly brackets, where the case description is generated according to the following rules: the method name in the original hook file + case.

然后在花括号内,通过renderHook方法模拟hook组件的渲染完成的事件,再通过act方法,模拟触发原hook文件中该对应的方法。通过语法expect(方法中使用的变量).toBe(预测出来的结果),得到的结果可能为true或者false,只有true表示案例通过。Then inside the curly braces, the renderHook method is used to simulate the completion of the rendering of the hook component, and then the act method is used to simulate and trigger the corresponding method in the original hook file. Through the syntax expect(variable used in the method).toBe(the predicted result), the result obtained may be true or false, and only true indicates that the case passed.

步骤S520,若在未经模拟的hook组件内出现useEffect函数,则确定未经模拟的hook组件的hook文件内有依赖更新事件,对hook组件内依赖更新事件进行模拟,得到自动化测试代码;并在所述自动化测试代码已被修改的情况下,通过所述自动化测试代码被修改后的依赖变量的值,来模拟触发更新事件的执行。Step S520, if the useEffect function appears in the unsimulated hook component, then determine that there is a dependency update event in the hook file of the unsimulated hook component, simulate the dependency update event in the hook component, and obtain an automated test code; When the automated test code has been modified, the execution of the update event is simulated and triggered by the value of the modified dependency variable of the automated test code.

具体地,对于hook组件内依赖更新事件的模拟,具体实现方式如下:Specifically, for the simulation of the dependency update event in the hook component, the specific implementation is as follows:

(21)由于hook对于依赖更新是通过react提供的useEffect方法实现的。所以,如果在原hook组件(即未经模拟的hook组件)内,出现了useEffect函数。该函数第一个入参为一个匿名的箭头函数,函数内容是执行对于依赖变量的操作。函数的第二个入参,可以是无、空数组、有元素的数组。当函数的第二个入参有值,且是一个非空数组,数组内包含了hook组件内的变量,则说明hook文件内,有依赖更新事件。(21) Since hooks are used to update dependencies, they are implemented through the useEffect method provided by react. Therefore, if the useEffect function appears in the original hook component (ie, the unsimulated hook component). The first input parameter of this function is an anonymous arrow function, and the content of the function is to perform operations on dependent variables. The second input parameter of the function can be nothing, an empty array, or an array with elements. When the second input parameter of the function has a value and is a non-empty array, and the array contains the variables in the hook component, it means that there are dependent update events in the hook file.

(22)通过useState关键字,查找到useEffect方法中第二个参数的数组中,涉及到的变量初始化定义代码,通过useState方法执行时,传入的参数,即得到了变量的初始化值(假设值为a)。在useEffect方法中的匿名箭头函数内,获取到对于该变量修改后的值,作为依赖更新后需要执行变更变量的值(假设值为b)。则预判的结果为,当依赖变更时,变量的值应该从a变更为b。(22) Through the useState keyword, find the variable initialization definition code involved in the array of the second parameter in the useEffect method. When the useState method is executed, the incoming parameters get the initialized value of the variable (assumed value for a). In the anonymous arrow function in the useEffect method, the modified value of the variable is obtained as the value of the variable that needs to be changed after the dependency update (assuming the value is b). The predicted result is that when the dependency changes, the value of the variable should be changed from a to b.

(23)判断得到预测结果后,需要生成自动化测试代码。首先通过语法,it('案例描述',()=>{测试代码}),将测试案例包裹在花括号内,其中,案例描述按照以下规则生成:原hook文件中的方法名+case。(23) After judging and obtaining the prediction result, it is necessary to generate automated test code. First, through the syntax, it('case description', ()=>{test code}), wrap the test case in curly brackets, where the case description is generated according to the following rules: the method name in the original hook file + case.

然后在花括号内,通过renderHook方法模拟hook组件的渲染完成的事件。并在所述自动化测试代码已被修改的情况下,通过所述自动化测试代码被修改后的依赖变量的值,来模拟触发更新事件的执行。Then inside the curly braces, simulate the rendering completion event of the hook component through the renderHook method. And when the automated test code has been modified, the execution of the update event is simulated and triggered by the value of the modified dependency variable of the automated test code.

之后,通过语法expect(a).toBe(b),得到的结果可能为true或者false,只有true表示案例通过。After that, through the syntax expect(a).toBe(b), the result may be true or false, only true means the case passed.

在步骤S130处,针对所述所有自动化测试案例代码所对应的每个测试案例,确定每个测试案例的动态值。将所有测试案例关联的动态值,进行页面可视化展示;所展示的可视化页面,是能够对所有hook文件的自动化测试案例代码中任一测试案例直接编辑修改的页面。也就是说,所展示的可视化页面,支持在页面上直接编辑修改,实现可视化界面修改所述所有测试案例中任一测试案例。At step S130, for each test case corresponding to all the automated test case codes, a dynamic value of each test case is determined. Visually display the dynamic values associated with all test cases on the page; the displayed visual page is a page that can directly edit and modify any test case in the automated test case code of all hook files. That is to say, the displayed visual page supports editing and modification directly on the page, so as to realize the modification of any test case among all the test cases described in the visual interface.

本发明的方案提供一种前端react框架hook组件自动化测试工具,对于前端大型的项目,通过使用此工具,开发者在完成代码后,可以实现生成hook组件的自动化测试代码,不需要掌握react hook自动化测试代码相关的语法,也可以使得项目具备了自动化测试的功能,可以及时的发现问题、修改问题,避免问题带到生产中去,从而,通过设置前端react框架hook组件自动化测试工具,在开发者在完成代码后,使用该工具,可以生成hook组件的自动化测试代码,可以降低人工成本和维护成本。The solution of the present invention provides an automated testing tool for hook components of a front-end react framework. For large-scale front-end projects, by using this tool, developers can generate automated testing codes for hook components after completing the code, without mastering the automation of react hooks The syntax related to the test code can also make the project have the function of automated testing, which can detect and modify problems in time, and avoid problems from being brought into production. Therefore, by setting up the front-end react framework hook component automated testing tools, developers can After completing the code, using this tool, the automated test code of the hook component can be generated, which can reduce labor costs and maintenance costs.

在一些实施方式中,步骤S130中将所有测试案例关联的动态值,进行页面可视化展示的具体过程,参见以下示例性说明。In some embodiments, for the specific process of visually displaying the dynamic values associated with all test cases in step S130, please refer to the following exemplary description.

下面结合图6所示本发明的方法中将所有测试案例进行页面可视化展示的一实施例流程示意图,进一步说明将所有测试案例进行页面可视化展示的具体过程,包括:步骤S610至步骤S630。The specific process of visually displaying all test cases on the page is further described below with reference to the schematic flowchart of an embodiment of the method of the present invention shown in FIG. 6 , including steps S610 to S630.

步骤S610,当所有测试案例生成完成后,将所有测试案例关联的动态值,生成一个页面展示文件。Step S610, after all test cases are generated, generate a page display file with dynamic values associated with all test cases.

步骤S620,通过执行设定命令,浏览器访问页面地址,打开该页面。Step S620, by executing the setting command, the browser accesses the page address and opens the page.

步骤S630,在页面上,展示包含所有hook组件名称、以及每一个组件的所有测试案例描述列表的内容。Step S630, on the page, display the content including the names of all hook components and the description list of all test cases for each component.

具体地,当所有测试案例生成完成后,会生成一个html页面展示文件。进而,通过执行命令npm run show-test-case,在项目本地,会通过nodejs启动一个本机启用了的服务器,通过浏览器访问http://192.168.3.2:8080/case.html地址,可以打开这个页面。Specifically, when all test cases are generated, an html page display file will be generated. Furthermore, by executing the command npm run show-test-case, locally in the project, a locally enabled server will be started through nodejs, and the address of http://192.168.3.2:8080/case.html can be opened through a browser. this page.

进而,在页面上,展示包含所有hook组件名称、以及每一个组件的所有测试案例描述列表的内容。对于测试代码中的expect(a).toBe(b)中的a和b,提供两个input输入框,可以给使用者进行编辑的权利。当编辑完成后,点击保存,会调用本机启用的服务器提供的修改本地文件的接口,接口会修改本地的自动化测试代码文件。由此实现可视化界面修改测试案例。Furthermore, on the page, the contents of the list including all hook component names and all test case descriptions for each component are displayed. For a and b in expect(a).toBe(b) in the test code, two input boxes are provided, which can give the user the right to edit. When the editing is completed, click Save, and the interface provided by the locally enabled server to modify the local file will be called, and the interface will modify the local automated test code file. This realizes the visual interface modification test case.

使用前端react框架hook组件自动化测试工具时,可以进行工具引入,即先引入工具。具体地,可以在命令行(mac系统为终端),定位当前项目根目录,执行命令,npm ireact-hook-autotest-tool-D,就可以添加该工具到当前框架项目中。When using the front-end react framework hook component automated testing tool, you can import the tool, that is, introduce the tool first. Specifically, you can locate the root directory of the current project on the command line (the mac system is the terminal), execute the command, npm ireact-hook-autotest-tool-D, and you can add the tool to the current framework project.

进而,设置配置文件config.js,配置文件配置内容为:需要进行自动化测试hook组件的地址,以数组的形式编写,如[./component/A组件,./component/B组件,./component/C组件]。Then, set the configuration file config.js. The configuration file configuration content is: the address of the hook component that needs to be tested automatically, which is written in the form of an array, such as [./component/A component, ./component/B component, ./component/ C component].

进而,项目构建。在命令行(mac系统为终端),定位当前项目根目录,执行命令构建命令,npm run create-hook-autotest,执行过程中,会涉及到以下核心原理的调用顺序执行。执行完成后,会生成相应的自动化测试代码,以及可视化编辑测试案例入参和结果的html文件。Further, the project is built. On the command line (the mac system is the terminal), locate the root directory of the current project, execute the command build command, npm run create-hook-autotest, during the execution process, the following core principles will be involved in the calling sequence execution. After the execution is completed, the corresponding automated test code will be generated, as well as the html file for visual editing of test case input parameters and results.

本发明的方案提供了前端react框架hook组件自动化测试工具,对于前端大型的项目,通过使用此工具,可以实现生成hook组件的自动化测试代码,而且人工成本和维护成本低。The solution of the present invention provides an automated testing tool for the hook component of the front-end react framework. For large-scale front-end projects, by using this tool, the automated testing code of the hook component can be generated, and the labor cost and maintenance cost are low.

例如:开发者在完成代码后,可以实现生成hook组件的自动化测试代码,不需要掌握react hook自动化测试代码相关的语法,也可以使得项目具备了自动化测试的功能,可以及时的发现问题,修改问题,避免问题带到生产中去。For example, after completing the code, the developer can realize the automated test code for generating the hook component without mastering the syntax related to the react hook automated test code, and can also make the project have the function of automated testing, so that problems can be found and corrected in time. , to prevent problems from being brought into production.

又如:对于公司的人员结构上,也得到了优化。首先,对于测试人员来说,自动化测试案例已经可以覆盖50%的手工测试场景,减轻了测试人员的测试工作量,节约了公司测试人员的投入。再者,在招聘开发人员过程中,对于自动化测试代码编写的要求可以放宽,减轻了招聘压力。Another example: the personnel structure of the company has also been optimized. First of all, for testers, automated test cases can already cover 50% of manual test scenarios, which reduces the testing workload of testers and saves the investment of company testers. Furthermore, in the process of recruiting developers, the requirements for writing automated test codes can be relaxed, reducing the pressure on recruitment.

又如:自动化测试代码由工具按照既定的规则生成,具有统一的编码风格,后期如果需要人为阅读和维护,也可以减轻相应的人力投入成本。Another example: automated test codes are generated by tools according to established rules and have a unified coding style. If manual reading and maintenance are required later, the corresponding labor input costs can also be reduced.

又如:提供测试案例的可视化编辑修改界面,当开发人员需要主动修改测试案例时,可以不用查找到对应的源码,也可以实现测试案例的调整,也给非开发人员需要修改测试案例时,提供了修改的通道。Another example: Provides a visual editing and modification interface for test cases. When developers need to actively modify test cases, they can adjust test cases without finding the corresponding source code, and also provide non-developers when they need to modify test cases. Modified channel.

采用本实施例的技术方案,通过设置一种前端react框架hook组件自动化测试工具,该前端react框架hook组件自动化测试工具的实现方式,主要包括:检查并安装前端react框架hook组件自动化测试所需要的文件,并在执行前端react框架hook组件自动化测试时执行前端react框架hook组件自动化测试代码的存放目录下的测试文件。根据前端react框架hook组件中每一个hook文件,生成自动化测试案例代码。将所有测试案关联的动态值,进行页面可视化展示,并支持在页面上直接编辑修改,实现可视化界面修改测试案例的目的。从而,通过设置前端react框架hook组件自动化测试工具,在开发者在完成代码后,使用该工具,可以生成hook组件的自动化测试代码,可以降低人工成本和维护成本。Using the technical solution of this embodiment, by setting up a front-end react framework hook component automated testing tool, the implementation method of the front-end react framework hook component automated testing tool mainly includes: checking and installing the front-end react framework hook components required for automated testing file, and execute the test file in the storage directory of the automated test code of the hook component of the front-end react framework when executing the automated test of the hook component of the front-end react framework. Generate automated test case code according to each hook file in the hook component of the front-end react framework. Visually display the dynamic values associated with all test cases on the page, and support direct editing and modification on the page to achieve the purpose of modifying test cases on the visual interface. Therefore, by setting up an automated testing tool for the hook component of the front-end react framework, after the developer completes the code, the tool can be used to generate the automated test code of the hook component, which can reduce labor costs and maintenance costs.

根据本发明的实施例,还提供了对应于自动化测试工具的实现方法的一种自动化测试工具的实现装置。参见图7所示本发明的装置的一实施例的结构示意图。所述自动化测试工具,包括:前端react框架hook组件自动化测试工具。所述前端react框架hook组件自动化测试工具的实现装置,包括:设置单元102、生成单元104和展示单元106。According to an embodiment of the present invention, an implementation device of an automated test tool corresponding to the implementation method of an automated test tool is also provided. Referring to FIG. 7, a schematic structural diagram of an embodiment of the device of the present invention is shown. The automated testing tool includes: a front-end react framework hook component automated testing tool. The implementation device of the front-end react framework hook component automated testing tool includes: a setting unit 102 , a generating unit 104 and a display unit 106 .

其中,设置单元102,被配置为检查并安装前端react框架hook组件自动化测试所需要的文件,并在执行前端react框架hook组件自动化测试时执行前端react框架hook组件自动化测试代码的存放目录下的测试文件。所述前端react框架hook组件中,具有N个hook文件,N为正整数。该设置单元102的具体功能及处理参见步骤S110。Wherein, the setting unit 102 is configured to check and install the files required for the automatic test of the front-end react framework hook component, and execute the test under the storage directory of the automatic test code of the front-end react framework hook component when executing the automatic test of the front-end react framework hook component document. The front-end react framework hook component has N hook files, where N is a positive integer. For the specific function and processing of the setting unit 102, refer to step S110.

在一些实施方式中,所述设置单元102,检查并安装前端react框架hook组件自动化测试所需要的文件,并在执行前端react框架hook组件自动化测试时执行前端react框架hook组件自动化测试代码的存放目录下的测试文件,包括:In some embodiments, the setting unit 102 checks and installs the files required for the automated test of the hook component of the front-end react framework, and executes the storage directory of the automated test code of the hook component of the front-end react framework when executing the automated test of the hook component of the front-end react framework The test files below include:

所述设置单元102,具体还被配置为检查并安装前端react框架hook组件自动化测试所需要的依赖文件。该设置单元102的具体功能及处理还参见步骤S210。The setting unit 102 is further configured to check and install the dependency files required for automated testing of the hook component of the front-end react framework. For specific functions and processing of the setting unit 102, refer to step S210.

例如:当构建命令启动时,会去检索项目中的package.json文件中,devDependencies对象中,是否已包含自动化测试框架库jest、以及针对模拟触发hook组件所使用的@testing-library/react-hooks。如果不包含,则会通过以下语法进行启动安装npm install jest@testing-library/react-hook-D。For example: when the build command is started, it will search the package.json file in the project, the devDependencies object, whether the automated testing framework library jest and @testing-library/react-hooks used for the simulation trigger hook component are included. . If not included, it will be launched with the following syntax: npm install jest@testing-library/react-hook-D.

所述设置单元102,具体还被配置为设置前端react框架hook组件自动化测试代码的存放目录,以存放生成的前端react框架hook组件自动化测试代码。该设置单元102的具体功能及处理还参见步骤S220。The setting unit 102 is further configured to set a storage directory for the automated test code of the hook component of the front-end react framework, so as to store the generated automated test code of the hook component of the front-end react framework. For specific functions and processing of the setting unit 102, refer to step S220.

例如:在项目的根目录,生成一个文件夹名称为autotest的文件夹。目的在于:在下面的步骤2的执行过程中,生成的自动化测试代码,会在此文件夹内,按照以下规则新建关于该hook组件的自动化测试代码。规则为:原组件名称+.test.js。For example: in the root directory of the project, generate a folder named autotest. The purpose is: During the execution of the following step 2, the generated automated test code will be created in this folder according to the following rules for the automated test code of the hook component. The rule is: original component name + .test.js.

所述设置单元102,具体还被配置为设置配置项,以在执行前端react框架hook组件自动化测试时,索引该配置项中前端react框架hook组件自动化测试代码的存放目录下的测试文件,并执行该测试文件。该设置单元102的具体功能及处理还参见步骤S230。The setting unit 102 is also specifically configured to set a configuration item to index the test file in the storage directory of the automated test code of the front-end react framework hook component in the configuration item when performing the automated test of the hook component of the front-end react framework, and execute the test file. the test file. For specific functions and processing of the setting unit 102, refer to step S230.

例如:在package.json文件中设置jest基础配置,核心配置项为"testRegex",将其值配置文件(/autotest/)$。For example: set the basic configuration of jest in the package.json file, the core configuration item is "testRegex", and set its value to the configuration file (/autotest/)$.

生成单元104,被配置为根据前端react框架hook组件中每一个hook文件,生成一个自动化测试案例代码。根据前端react框架hook组件中所有hook文件,生成所有hook文件的自动化测试案例代码。该生成单元104的具体功能及处理参见步骤S120。The generating unit 104 is configured to generate an automated test case code according to each hook file in the hook component of the front-end react framework. According to all the hook files in the hook component of the front-end react framework, the automated test case code of all the hook files is generated. Refer to step S120 for the specific function and processing of the generating unit 104 .

在一些实施方式中,所述生成单元104,根据前端react框架hook组件中每一个hook文件,生成一个自动化测试案例代码,包括:In some embodiments, the generating unit 104 generates an automated test case code according to each hook file in the hook component of the front-end react framework, including:

所述生成单元104,具体还被配置为根据预先设置的配置项中配置的需要进行自动化测试的hook组件地址的数组,遍历数组中的每一个元素,找到与数组中的每一个元素对应的每一个hook文件,作为需要进行自动化测试的每一个hook文件。该生成单元104的具体功能及处理还参见步骤S310。The generating unit 104 is also specifically configured to traverse each element in the array according to the array of hook component addresses configured in the preset configuration items and need to perform automated testing, and find each element corresponding to each element in the array. A hook file for each hook file that needs to be automated. For the specific function and processing of the generating unit 104, refer to step S310.

例如:通过配置文件config.js中,配置的需要进行自动化测试的hook组件地址的数组,通过遍历数组中的每一个元素,找到对应的每一个hook文件,并执行后续生成自动化测试案例代码操作。For example, in the configuration file config.js, configure an array of hook component addresses that need to be automated testing, find each corresponding hook file by traversing each element in the array, and perform subsequent operations to generate automated test case code.

所述生成单元104,具体还被配置为针对需要进行自动化测试的每一个hook文件,执行生成自动化测试案例代码的操作,生成需要进行自动化测试的每一个hook文件的基础代码。该生成单元104的具体功能及处理还参见步骤S320。The generating unit 104 is further configured to perform an operation of generating automated test case codes for each hook file that needs to be automated testing, and generate basic code for each hook file that needs to be automated testing. For the specific function and processing of the generating unit 104, refer to step S320.

在一些实施方式中,所述生成单元104,针对需要进行自动化测试的每一个hook文件,执行生成自动化测试案例代码的操作,生成需要进行自动化测试的每一个hook文件的基础代码,包括:In some embodiments, the generating unit 104 performs an operation of generating automated test case codes for each hook file that needs to be automated testing, and generates the basic code of each hook file that needs to be automated testing, including:

所述生成单元104,具体还被配置为创建前端react框架hook组件自动化测试代码的存放目录,在该存放目录所在文件夹中,生成与需要进行自动化测试的每一个hook文件对应的空白文件。该生成单元104的具体功能及处理还参见步骤S410。The generating unit 104 is further configured to create a storage directory for the automated test code of the hook component of the front-end react framework, and in the folder where the storage directory is located, generate a blank file corresponding to each hook file that needs to be automated tested. For the specific function and processing of the generating unit 104, refer to step S410.

例如:假设当前hook组件名为A,在前期准备工作中创建的autotest文件夹中,生成与hook文件对应的文件名为A.test.js的空白文件。For example: Assuming that the current hook component is named A, in the autotest folder created in the previous preparation, a blank file named A.test.js corresponding to the hook file is generated.

所述生成单元104,具体还被配置为通过第一设定语法,将未经模拟的hook组件、模拟hook组件渲染完成的装置renderHook、模拟触发hook组件对外暴露函数的act装置,引入到需要进行自动化测试的每一个hook文件中。该生成单元104的具体功能及处理还参见步骤S420。The generating unit 104 is also specifically configured to introduce the unsimulated hook component, the renderHook device that simulates the rendering of the hook component, and the act device that simulates triggering the external exposure function of the hook component into the need to perform the first setting syntax. in each hook file of automated tests. For the specific function and processing of the generating unit 104, refer to step S420.

例如:通过import语法,将未经模拟的hook组件、模拟hook组件渲染完成的装置renderHook、模拟触发hook组件对外暴露函数的act装置,引入到测试文件中来。For example, through the import syntax, the unsimulated hook component, the renderHook device that simulates the rendering of the hook component, and the act device that simulates the external exposure function of the hook component are introduced into the test file.

所述生成单元104,具体还被配置为通过第二设定语法,将即将生成的需要进行自动化测试的每一个hook文件所对应的测试案例包裹起来,以得到需要进行自动化测试的每一个hook文件的基础代码,以将需要进行自动化测试的每一个hook文件所对应的测试案例按照设定的文件级别范围进行分组定义,以在后期测试结果输出的时按该分组定义的内容进行区分。该生成单元104的具体功能及处理还参见步骤S430。The generating unit 104 is also specifically configured to wrap the test case corresponding to each hook file that needs to be automatically tested to be generated to obtain each hook file that needs to be automatically tested through the second setting syntax. The basic code of , to group and define the test cases corresponding to each hook file that needs to be automated testing according to the set file level range, so as to distinguish according to the content of the group definition when the later test results are output. For the specific function and processing of the generating unit 104, refer to step S430.

例如:通过语法,describe('A hook autotest',()=>{测试用例代码}),将即将生成的测试用例包裹起来,用于把该文件的测试用例按照文件级别的范围,进行一个范围的分组定义,后期测试结果输出的时候,会按照分组进行定义的内容(即A hookautotest),进行区分。For example: through the syntax, describe('A hook autotest',()=>{test case code}), wrap the test case to be generated, and use it to carry out a scope of the test case of the file according to the scope of the file level When the test results are output later, they will be distinguished according to the content defined by the group (ie A hookautotest).

所述生成单元104,具体还被配置为根据需要进行自动化测试的每一个hook文件的基础代码,识别不同场景语法,智能反向推导期望结果,并生成需要进行自动化测试的每一个hook文件的代码,作为需要进行自动化测试的每一个hook文件所对应的测试案例代码。该生成单元104的具体功能及处理还参见步骤S330。The generating unit 104 is specifically configured to perform the basic code of each hook file for automated testing as required, identify different scene syntaxes, intelligently reverse deduce the expected result, and generate the code for each hook file that needs to perform automated testing. , as the test case code corresponding to each hook file that needs to be automated. For the specific function and processing of the generating unit 104, refer to step S330.

在一些实施方式中,所述生成单元104,所述根据需要进行自动化测试的每一个hook文件的基础代码,识别不同场景语法,智能反向推导期望结果,并生成需要进行自动化测试的每一个hook文件的代码,作为需要进行自动化测试的每一个hook文件所对应的测试案例代码,包括:In some embodiments, the generating unit 104, the basic code of each hook file for automated testing according to needs, identifies different scene syntaxes, intelligently reverses the expected results, and generates each hook that needs to be automated testing. The code of the file, as the test case code corresponding to each hook file that needs to be automated, includes:

所述生成单元104,具体还被配置为针对未经模拟的hook组件,在函数最后有通过return关键字对外输出的变量中,通过对变量进行typeof判断,如果得出的结果是'function',则反向查找未经模拟的hook组件的hook代码中,关于所述函数的函数名的完整代码块在未经模拟的hook组件的hook代码的代码块内对于所述变量的操作,以反向推导出变量执行的案例和结果。该生成单元104的具体功能及处理还参见步骤S510。The generating unit 104 is also specifically configured to, for the unsimulated hook component, at the end of the function, there is a variable that is output externally through the return keyword, by judging the variable by typeof, if the result obtained is 'function', Then, in the hook code of the unsimulated hook component, the complete code block of the function name of the function is reversed. The operation of the variable in the code block of the hook code of the unsimulated hook component is reversed. Derive cases and outcomes of variable execution. For the specific function and processing of the generating unit 104, refer to step S510.

具体地,识别不同场景语法,智能反向推导期望结果,并生成测试案例代码。该步骤进一步可以包括:对于对外输出函数的测试。Specifically, it recognizes the syntax of different scenarios, intelligently reverses the expected results, and generates test case code. The step may further include: testing the external output function.

当未经模拟的hook组件中,在函数最后有通过return关键字对外输出的变量中,通过对变量进行typeof判断,如果得出的结果是'function',则说明hook组件有对外输出函数给外部调用。反向查找hook代码中,关于所述函数的函数名的完整代码块在未经模拟的hook组件的hook代码的代码块内对于所述变量的操作,以反向推导出变量执行的案例和结果。具体规则如下:When the unsimulated hook component has variables that are output to the outside through the return keyword at the end of the function, by judging the variable by typeof, if the result is 'function', it means that the hook component has an external output function to the outside. transfer. In the reverse lookup hook code, the complete code block of the function name of the function is the operation of the variable within the code block of the hook code of the unsimulated hook component, so as to reversely deduce the case and result of the variable execution . The specific rules are as follows:

(11)通过useState关键字,查找到变量的初始化代码,通过useState装置执行时,传入的参数,即为变量的初始化值。对其值进行typeof判断,得到的结果即为初始化值的数据类型。(11) Through the useState keyword, the initialization code of the variable is found. When executing through the useState device, the incoming parameter is the initialization value of the variable. Perform typeof judgment on its value, and the result obtained is the data type of the initialized value.

(12)通过代码块中,涉及到该变量的操作,如+、-、*、/操作。这里对于+号需要做一下区分。如果变量的初始值的数据类型为数字,则+号表示的是数学运算。如果初始值数据类型为字符串,则+号表示的是字符串的拼接操作。其他的操作符如-、*、/都表示了数学运算。举一个最简单的例子,变量的初始值为数字10,在代码中,执行了对于变量执行了*Math.randon()的操作,由于Math.randon(),,会随机输出0-1之间的一个随机数,则输出的结果可以预测为0-10。同理,如果变量初始值为字符串hello,执行了对于变量的操作+'world',则输出的结果可以预测为helloworld。(12) Through the code block, operations involving the variable, such as +, -, *, / operations. A distinction needs to be made here for the + sign. If the data type of the variable's initial value is number, the + sign indicates a mathematical operation. If the data type of the initial value is a string, the + sign indicates the concatenation operation of the string. Other operators such as -, *, / all represent mathematical operations. For the simplest example, the initial value of the variable is the number 10. In the code, the operation of *Math.randon() is performed on the variable. Because of Math.randon(), it will randomly output between 0-1 is a random number, the output result can be predicted as 0-10. Similarly, if the initial value of the variable is the string hello, and the operation on the variable + 'world' is performed, the output result can be predicted as helloworld.

(13)判断得到预测结果后,需要生成自动化测试代码。首先通过语法,it('案例描述',()=>{测试代码}),将测试案例包裹在花括号内,其中,案例描述按照以下规则生成:原hook文件中的装置名+case。(13) After judging and obtaining the prediction result, it is necessary to generate automated test code. First, through the syntax, it('case description',()=>{test code}), wrap the test case in curly brackets, where the case description is generated according to the following rules: the device name in the original hook file + case.

然后在花括号内,通过renderHook装置模拟hook组件的渲染完成的事件,再通过act装置,模拟触发原hook文件中该对应的装置。通过语法expect(装置中使用的变量).toBe(预测出来的结果),得到的结果可能为true或者false,只有true表示案例通过。Then, within the curly brackets, the rendering completion event of the hook component is simulated by the renderHook device, and then the corresponding device in the original hook file is simulated and triggered by the act device. Through the syntax expect(variable used in the device).toBe(predicted result), the result may be true or false, only true means the case passed.

所述生成单元104,具体还被配置为若在未经模拟的hook组件内出现useEffect函数,则确定未经模拟的hook组件的hook文件内有依赖更新事件,对hook组件内依赖更新事件进行模拟,得到自动化测试代码;并在所述自动化测试代码已被修改的情况下,通过所述自动化测试代码被修改后的依赖变量的值,来模拟触发更新事件的执行。该生成单元104的具体功能及处理还参见步骤S520。The generating unit 104 is also specifically configured to, if the useEffect function appears in the unsimulated hook component, determine that there is a dependency update event in the hook file of the unsimulated hook component, and simulate the dependency update event in the hook component , to obtain the automated test code; and when the automated test code has been modified, the execution of an update event is simulated and triggered by the value of the modified dependent variable of the automated test code. For the specific function and processing of the generating unit 104, refer to step S520.

具体地,对于hook组件内依赖更新事件的模拟,具体实现方式如下:Specifically, for the simulation of the dependency update event in the hook component, the specific implementation is as follows:

(21)由于hook对于依赖更新是通过react提供的useEffect装置实现的。所以,如果在原hook组件(即未经模拟的hook组件)内,出现了useEffect函数。该函数第一个入参为一个匿名的箭头函数,函数内容是执行对于依赖变量的操作。函数的第二个入参,可以是无、空数组、有元素的数组。当函数的第二个入参有值,且是一个非空数组,数组内包含了hook组件内的变量,则说明hook文件内,有依赖更新事件。(21) Since hooks are used to update dependencies, they are implemented through the useEffect device provided by react. Therefore, if the useEffect function appears in the original hook component (ie, the unsimulated hook component). The first input parameter of this function is an anonymous arrow function, and the content of the function is to perform operations on dependent variables. The second input parameter of the function can be nothing, an empty array, or an array with elements. When the second input parameter of the function has a value and is a non-empty array, and the array contains the variables in the hook component, it means that there are dependent update events in the hook file.

(22)通过useState关键字,查找到useEffect装置中第二个参数的数组中,涉及到的变量初始化定义代码,通过useState装置执行时,传入的参数,即得到了变量的初始化值(假设值为a)。在useEffect装置中的匿名箭头函数内,获取到对于该变量修改后的值,作为依赖更新后需要执行变更变量的值(假设值为b)。则预判的结果为,当依赖变更时,变量的值应该从a变更为b。(22) Through the useState keyword, find the variable initialization definition code involved in the array of the second parameter in the useEffect device, and when the useState device is executed, the incoming parameters get the initialized value of the variable (assumed value for a). In the anonymous arrow function in the useEffect device, the modified value of the variable is obtained as the value of the variable that needs to be changed after the dependency update (assuming the value is b). The predicted result is that when the dependency changes, the value of the variable should be changed from a to b.

(23)判断得到预测结果后,需要生成自动化测试代码。首先通过语法,it('案例描述',()=>{测试代码}),将测试案例包裹在花括号内,其中,案例描述按照以下规则生成:原hook文件中的装置名+case。(23) After judging and obtaining the prediction result, it is necessary to generate automated test code. First, through the syntax, it('case description',()=>{test code}), wrap the test case in curly brackets, where the case description is generated according to the following rules: the device name in the original hook file + case.

然后在花括号内,通过renderHook装置模拟hook组件的渲染完成的事件。并在所述自动化测试代码已被修改的情况下,通过所述自动化测试代码被修改后的依赖变量的值,来模拟触发更新事件的执行。Then inside the curly braces, the rendering completion event of the hook component is simulated by the renderHook device. And when the automated test code has been modified, the execution of the update event is simulated and triggered by the value of the modified dependency variable of the automated test code.

之后,通过语法expect(a).toBe(b),得到的结果可能为true或者false,只有true表示案例通过。After that, through the syntax expect(a).toBe(b), the result may be true or false, only true means the case passed.

展示单元106,被配置为针对所述所有自动化测试案例代码所对应的每个测试案例,确定每个测试案例的动态值。将所有测试案例关联的动态值,进行页面可视化展示;所展示的可视化页面,是能够对所有hook文件的自动化测试案例代码中任一测试案例直接编辑修改的页面。也就是说,所展示的可视化页面,支持在页面上直接编辑修改,实现可视化界面修改所述所有测试案例中任一测试案例。该展示单元106的具体功能及处理参见步骤S130。The presentation unit 106 is configured to, for each test case corresponding to all the automated test case codes, determine the dynamic value of each test case. Visually display the dynamic values associated with all test cases on the page; the displayed visual page is a page that can directly edit and modify any test case in the automated test case code of all hook files. That is to say, the displayed visual page supports editing and modification directly on the page, so as to realize the modification of any test case among all the test cases described in the visual interface. Refer to step S130 for specific functions and processing of the display unit 106 .

本发明的方案提供一种前端react框架hook组件自动化测试工具,对于前端大型的项目,通过使用此工具,开发者在完成代码后,可以实现生成hook组件的自动化测试代码,不需要掌握react hook自动化测试代码相关的语法,也可以使得项目具备了自动化测试的功能,可以及时的发现问题、修改问题,避免问题带到生产中去,从而,通过设置前端react框架hook组件自动化测试工具,在开发者在完成代码后,使用该工具,可以生成hook组件的自动化测试代码,可以降低人工成本和维护成本。The solution of the present invention provides an automated testing tool for hook components of a front-end react framework. For large-scale front-end projects, by using this tool, developers can generate automated testing codes for hook components after completing the code, without mastering the automation of react hooks The syntax related to the test code can also make the project have the function of automated testing, which can detect and modify problems in time, and avoid problems from being brought into production. Therefore, by setting up the front-end react framework hook component automated testing tools, developers can After completing the code, using this tool, the automated test code of the hook component can be generated, which can reduce labor costs and maintenance costs.

在一些实施方式中,所述展示单元106,将所有测试案例关联的动态值,进行页面可视化展示,包括:In some embodiments, the display unit 106 visually displays the dynamic values associated with all test cases on a page, including:

所述展示单元106,具体还被配置为当所有测试案例生成完成后,将所有测试案例关联的动态值,生成一个页面展示文件。该展示单元106的具体功能及处理还参见步骤S610。The display unit 106 is specifically configured to generate a page display file with dynamic values associated with all test cases after all test cases are generated. For specific functions and processing of the display unit 106, refer to step S610.

所述展示单元106,具体还被配置为通过执行设定命令,浏览器访问页面地址,打开该页面。该展示单元106的具体功能及处理还参见步骤S620。The display unit 106 is further configured to, by executing the setting command, access the address of the page by the browser, and open the page. For specific functions and processing of the display unit 106, refer to step S620.

所述展示单元106,具体还被配置为在页面上,展示包含所有hook组件名称、以及每一个组件的所有测试案例描述列表的内容。该展示单元106的具体功能及处理还参见步骤S630。The display unit 106 is specifically configured to display, on the page, the content including the names of all hook components and the description list of all test cases of each component. For the specific function and processing of the display unit 106, refer to step S630.

具体地,当所有测试案例生成完成后,会生成一个html页面展示文件。进而,通过执行命令npm run show-test-case,在项目本地,会通过nodejs启动一个本机启用了的服务器,通过浏览器访问http://192.168.3.2:8080/case.html地址,可以打开这个页面。Specifically, when all test cases are generated, an html page display file will be generated. Furthermore, by executing the command npm run show-test-case, locally in the project, a locally enabled server will be started through nodejs, and the address of http://192.168.3.2:8080/case.html can be opened through a browser. this page.

进而,在页面上,展示包含所有hook组件名称、以及每一个组件的所有测试案例描述列表的内容。对于测试代码中的expect(a).toBe(b)中的a和b,提供两个input输入框,可以给使用者进行编辑的权利。当编辑完成后,点击保存,会调用本机启用的服务器提供的修改本地文件的接口,接口会修改本地的自动化测试代码文件。由此实现可视化界面修改测试案例。Furthermore, on the page, the contents of the list including all hook component names and all test case descriptions for each component are displayed. For a and b in expect(a).toBe(b) in the test code, two input boxes are provided, which can give the user the right to edit. When the editing is completed, click Save, and the interface provided by the locally enabled server to modify the local file will be called, and the interface will modify the local automated test code file. This realizes the visual interface modification test case.

使用前端react框架hook组件自动化测试工具时,可以进行工具引入,即先引入工具。具体地,可以在命令行(mac系统为终端),定位当前项目根目录,执行命令,npm ireact-hook-autotest-tool-D,就可以添加该工具到当前框架项目中。When using the front-end react framework hook component automated testing tool, you can import the tool, that is, introduce the tool first. Specifically, you can locate the root directory of the current project on the command line (the mac system is the terminal), execute the command, npm ireact-hook-autotest-tool-D, and you can add the tool to the current framework project.

进而,设置配置文件config.js,配置文件配置内容为:需要进行自动化测试hook组件的地址,以数组的形式编写,如[./component/A组件,./component/B组件,./component/C组件]。Then, set the configuration file config.js. The configuration file configuration content is: the address of the hook component that needs to be tested automatically, which is written in the form of an array, such as [./component/A component, ./component/B component, ./component/ C component].

进而,项目构建。在命令行(mac系统为终端),定位当前项目根目录,执行命令构建命令,npm run create-hook-autotest,执行过程中,会涉及到以下核心原理的调用顺序执行。执行完成后,会生成相应的自动化测试代码,以及可视化编辑测试案例入参和结果的html文件。Further, the project is built. On the command line (the mac system is the terminal), locate the root directory of the current project, execute the command build command, npm run create-hook-autotest, during the execution process, the following core principles will be involved in the calling sequence execution. After the execution is completed, the corresponding automated test code will be generated, as well as the html file for visual editing of test case input parameters and results.

本发明的方案提供了前端react框架hook组件自动化测试工具,对于前端大型的项目,通过使用此工具,可以实现生成hook组件的自动化测试代码,而且人工成本和维护成本低。The solution of the present invention provides an automated testing tool for the hook component of the front-end react framework. For large-scale front-end projects, by using this tool, the automated testing code of the hook component can be generated, and the labor cost and maintenance cost are low.

例如:开发者在完成代码后,可以实现生成hook组件的自动化测试代码,不需要掌握react hook自动化测试代码相关的语法,也可以使得项目具备了自动化测试的功能,可以及时的发现问题,修改问题,避免问题带到生产中去。For example, after completing the code, the developer can realize the automated test code for generating the hook component without mastering the syntax related to the react hook automated test code, and can also make the project have the function of automated testing, so that problems can be found and corrected in time. , to prevent problems from being brought into production.

又如:对于公司的人员结构上,也得到了优化。首先,对于测试人员来说,自动化测试案例已经可以覆盖50%的手工测试场景,减轻了测试人员的测试工作量,节约了公司测试人员的投入。再者,在招聘开发人员过程中,对于自动化测试代码编写的要求可以放宽,减轻了招聘压力。Another example: the personnel structure of the company has also been optimized. First of all, for testers, automated test cases can already cover 50% of manual test scenarios, which reduces the testing workload of testers and saves the investment of company testers. Furthermore, in the process of recruiting developers, the requirements for writing automated test codes can be relaxed, reducing the pressure on recruitment.

又如:自动化测试代码由工具按照既定的规则生成,具有统一的编码风格,后期如果需要人为阅读和维护,也可以减轻相应的人力投入成本。Another example: automated test codes are generated by tools according to established rules and have a unified coding style. If manual reading and maintenance are required later, the corresponding labor input costs can also be reduced.

又如:提供测试案例的可视化编辑修改界面,当开发人员需要主动修改测试案例时,可以不用查找到对应的源码,也可以实现测试案例的调整,也给非开发人员需要修改测试案例时,提供了修改的通道。Another example: Provides a visual editing and modification interface for test cases. When developers need to actively modify test cases, they can adjust test cases without finding the corresponding source code, and also provide non-developers when they need to modify test cases. Modified channel.

由于本实施例的装置所实现的处理及功能基本相应于前述方法的实施例、原理和实例,故本实施例的描述中未详尽之处,可以参见前述实施例中的相关说明,在此不做赘述。Since the processing and functions implemented by the device in this embodiment basically correspond to the embodiments, principles, and examples of the foregoing methods, the description of this embodiment is not detailed, and reference may be made to the relevant descriptions in the foregoing embodiments. Do repeat.

采用本发明的技术方案,通过设置一种前端react框架hook组件自动化测试工具,该前端react框架hook组件自动化测试工具的实现方式,主要包括:检查并安装前端react框架hook组件自动化测试所需要的文件,并在执行前端react框架hook组件自动化测试时执行前端react框架hook组件自动化测试代码的存放目录下的测试文件;根据前端react框架hook组件中每一个hook文件,生成自动化测试案例代码;将所有测试案关联的动态值,进行页面可视化展示,并支持在页面上直接编辑修改,实现可视化界面修改测试案例的目的;对于前端大型的项目,通过使用此工具,开发者在完成代码后,可以实现生成hook组件的自动化测试代码,不需要掌握react hook自动化测试代码相关的语法,也可以使得项目具备了自动化测试的功能,可以及时的发现问题,修改问题,避免问题带到生产中去。By adopting the technical scheme of the present invention, by setting up a front-end react framework hook component automated testing tool, the implementation of the front-end react framework hook component automated testing tool mainly includes: checking and installing the files required for the front-end react framework hook component automated testing , and execute the test file in the storage directory of the automated test code of the front-end react framework hook component when executing the automated test of the front-end react framework hook component; according to each hook file in the front-end react framework hook component, generate the automated test case code; The dynamic value associated with the case can be displayed visually on the page, and it supports direct editing and modification on the page, so as to realize the purpose of modifying the test case in the visual interface; for large-scale front-end projects, by using this tool, the developer can generate the code after completing the code. The automated test code of the hook component does not need to master the syntax related to the react hook automated test code, but also enables the project to have the function of automated testing, which can detect problems in time, modify them, and prevent problems from being brought into production.

根据本发明的实施例,还提供了对应于自动化测试工具的实现装置的一种终端。该终端可以包括:以上所述的自动化测试工具的实现装置。According to an embodiment of the present invention, a terminal corresponding to an implementation device of an automated test tool is also provided. The terminal may include: an implementation device of the above-mentioned automated testing tool.

由于本实施例的终端所实现的处理及功能基本相应于前述装置的实施例、原理和实例,故本实施例的描述中未详尽之处,可以参见前述实施例中的相关说明,在此不做赘述。Since the processing and functions implemented by the terminal in this embodiment basically correspond to the embodiments, principles, and examples of the foregoing apparatus, the description of this embodiment is not detailed, and reference may be made to the relevant descriptions in the foregoing embodiments, which are not described here. Do repeat.

采用本发明的技术方案,通过设置一种前端react框架hook组件自动化测试工具,该前端react框架hook组件自动化测试工具的实现方式,主要包括:检查并安装前端react框架hook组件自动化测试所需要的文件,并在执行前端react框架hook组件自动化测试时执行前端react框架hook组件自动化测试代码的存放目录下的测试文件;根据前端react框架hook组件中每一个hook文件,生成自动化测试案例代码;将所有测试案关联的动态值,进行页面可视化展示,并支持在页面上直接编辑修改,实现可视化界面修改测试案例的目的;对于前端大型的项目,通过使用此工具,对于公司的人员结构上,也得到了优化:首先,对于测试人员来说,自动化测试案例已经可以覆盖50%的手工测试场景,减轻了测试人员的测试工作量,节约了公司测试人员的投入;再者,在招聘开发人员过程中,对于自动化测试代码编写的要求可以放宽,减轻了招聘压力。By adopting the technical scheme of the present invention, by setting up a front-end react framework hook component automated testing tool, the implementation of the front-end react framework hook component automated testing tool mainly includes: checking and installing the files required for the front-end react framework hook component automated testing , and execute the test file in the storage directory of the automated test code of the front-end react framework hook component when executing the automated test of the front-end react framework hook component; according to each hook file in the front-end react framework hook component, generate the automated test case code; The dynamic value associated with the case can be displayed visually on the page, and it supports direct editing and modification on the page, so as to realize the purpose of modifying the test case on the visual interface; for large-scale front-end projects, by using this tool, the company's personnel structure has also been obtained. Optimization: First of all, for testers, automated test cases can already cover 50% of manual test scenarios, which reduces the testing workload of testers and saves the investment of company testers; Requirements for automated test code writing can be relaxed, reducing recruitment pressure.

根据本发明的实施例,还提供了对应于自动化测试工具的实现方法的一种自动化测试工具,所述自动化测试工具是由以上所述的自动化测试工具的实现方法得到的。According to an embodiment of the present invention, an automated test tool corresponding to the implementation method of the automated test tool is also provided, and the automated test tool is obtained by the above-described implementation method of the automated test tool.

本发明的方案提出的一种前端react框架hook组件自动化测试工具的实现方法,主要包括如下步骤:The implementation method of a front-end react framework hook component automated testing tool proposed by the solution of the present invention mainly includes the following steps:

步骤1、检查并安装前端react框架hook组件自动化测试所需要的文件,并在执行前端react框架hook组件自动化测试时执行前端react框架hook组件自动化测试代码的存放目录下的测试文件。Step 1. Check and install the files required for the automated test of the front-end react framework hook component, and execute the test file in the storage directory of the automated test code of the front-end react framework hook component when executing the automated test of the front-end react framework hook component.

本步骤作为前期准备工作,具体可以包括:This step is the preparatory work, which can specifically include:

步骤1-1、检查并安装自动化测试所需要的依赖文件。Step 1-1. Check and install the dependency files required for automated testing.

例如:当构建命令启动时,会去检索项目中的package.json文件中,devDependencies对象中,是否已包含自动化测试框架库jest、以及针对模拟触发hook组件所使用的@testing-library/react-hooks。如果不包含,则会通过以下语法进行启动安装npm install jest@testing-library/react-hook-D。For example: when the build command is started, it will search the package.json file in the project, the devDependencies object, whether the automated testing framework library jest and @testing-library/react-hooks used for the simulation trigger hook component are included. . If not included, it will be launched with the following syntax: npm install jest@testing-library/react-hook-D.

步骤1-2、生成自动化测试代码存放目录,以存放生成的自动化测试代码。Step 1-2, generate an automated test code storage directory to store the generated automated test code.

例如:在项目的根目录,生成一个文件夹名称为autotest的文件夹。目的在于:在下面的步骤2的执行过程中,生成的自动化测试代码,会在此文件夹内,按照以下规则新建关于该hook组件的自动化测试代码。规则为:原组件名称+.test.js。For example: in the root directory of the project, generate a folder named autotest. The purpose is: During the execution of the following step 2, the generated automated test code will be created in this folder according to the following rules for the automated test code of the hook component. The rule is: original component name + .test.js.

步骤1-3、设置配置项,以在在执行自动化测试的时候,去索引这个目录下的测试文件,并执行。Steps 1-3, set configuration items to index the test files in this directory and execute them when executing automated tests.

例如:在package.json文件中设置jest基础配置,核心配置项为"testRegex",将其值配置文件(/autotest/)$。For example: set the basic configuration of jest in the package.json file, the core configuration item is "testRegex", and set its value to the configuration file (/autotest/)$.

步骤2、根据前端react框架hook组件中每一个hook文件,生成自动化测试案例代码。Step 2. Generate automated test case code according to each hook file in the hook component of the front-end react framework.

本步骤具体可以包括:This step can specifically include:

步骤2-1、查找需自动化测试的hook文件。Step 2-1. Find the hook file to be automated test.

例如:通过配置文件config.js中,配置的需要进行自动化测试的hook组件地址的数组,通过遍历数组中的每一个元素,找到对应的每一个hook文件。并执行后续生成自动化测试案例代码操作。For example, in the configuration file config.js, configure the array of hook component addresses that need to be automated testing, and find each corresponding hook file by traversing each element in the array. And perform subsequent operations to generate automated test case code.

步骤2-2、生成测试文件的基础代码,如可以假设当前hook组件名为A。该步骤进一步可以包括:Step 2-2. Generate the basic code of the test file. For example, it can be assumed that the name of the current hook component is A. This step may further include:

步骤2-2.1、在前期准备工作中创建的autotest文件夹中,生成与hook文件对应的文件名为A.test.js的空白文件。Step 2-2.1. In the autotest folder created in the preparatory work, generate a blank file named A.test.js corresponding to the hook file.

步骤2-2.2、通过import语法,将未经模拟的hook组件、模拟hook组件渲染完成的方法renderHook、模拟触发hook组件对外暴露函数的act方法,引入到测试文件中来。Step 2-2.2, through the import syntax, introduce the unsimulated hook component, the renderHook method that simulates the rendering of the hook component, and the act method that simulates triggering the external exposure function of the hook component into the test file.

步骤2-2.3、通过语法,describe('A hook autotest',()=>{测试用例代码}),将即将生成的测试用例包裹起来,用于把该文件的测试用例按照文件级别的范围,进行一个范围的分组定义,后期测试结果输出的时候,会按照分组进行定义的内容(即A hookautotest),进行区分。Step 2-2.3, through the syntax, describe('A hook autotest',()=>{test case code}), wrap the test case to be generated, and use it to make the test case of the file according to the scope of the file level, A range of grouping is defined, and when the later test results are output, they will be distinguished according to the content of the grouping definition (ie A hookautotest).

步骤2-3、识别不同场景语法,智能反向推导期望结果,并生成测试案例代码。该步骤进一步可以包括:Steps 2-3, identify the syntax of different scenarios, intelligently reverse the expected results, and generate test case codes. This step may further include:

步骤2-3.1、对于对外输出函数的测试。Step 2-3.1. Test the external output function.

当未经模拟的hook组件中,在函数最后有通过return关键字对外输出的变量中,通过对变量进行typeof判断,如果得出的结果是'function',则说明hook组件有对外输出函数给外部调用。反向查找hook代码中,关于所述函数的函数名的完整代码块在未经模拟的hook组件的hook代码的代码块内对于所述变量的操作,以反向推导出变量执行的案例和结果。具体规则如下:When the unsimulated hook component has variables that are output to the outside through the return keyword at the end of the function, by judging the variable by typeof, if the result is 'function', it means that the hook component has an external output function to the outside. transfer. In the reverse lookup hook code, the complete code block of the function name of the function is the operation of the variable within the code block of the hook code of the unsimulated hook component, so as to reversely deduce the case and result of the variable execution . The specific rules are as follows:

步骤2-3.1.1、通过useState关键字,查找到变量的初始化代码,通过useState方法执行时,传入的参数,即为变量的初始化值。对其值进行typeof判断,得到的结果即为初始化值的数据类型。Step 2-3.1.1. Use the useState keyword to find the initialization code of the variable. When the useState method is executed, the parameter passed in is the initialization value of the variable. Perform typeof judgment on its value, and the result obtained is the data type of the initialized value.

步骤2-3.1.2、通过代码块中,涉及到该变量的操作,如+、-、*、/操作。这里对于+号需要做一下区分。如果变量的初始值的数据类型为数字,则+号表示的是数学运算。如果初始值数据类型为字符串,则+号表示的是字符串的拼接操作。其他的操作符如-、*、/都表示了数学运算。举一个最简单的例子,变量的初始值为数字10,在代码中,执行了对于变量执行了*Math.randon()的操作,由于Math.randon(),,会随机输出0-1之间的一个随机数,则输出的结果可以预测为0-10。同理,如果变量初始值为字符串hello,执行了对于变量的操作+'world',则输出的结果可以预测为helloworld。Step 2-3.1.2. Through the code block, operations involving the variable, such as +, -, *, / operations. A distinction needs to be made here for the + sign. If the data type of the variable's initial value is number, the + sign indicates a mathematical operation. If the data type of the initial value is a string, the + sign indicates the concatenation operation of the string. Other operators such as -, *, / all represent mathematical operations. For the simplest example, the initial value of the variable is the number 10. In the code, the operation of *Math.randon() is performed on the variable. Because of Math.randon(), it will randomly output between 0-1 is a random number, the output result can be predicted as 0-10. Similarly, if the initial value of the variable is the string hello, and the operation on the variable + 'world' is performed, the output result can be predicted as helloworld.

步骤2-3.1.3、判断得到预测结果后,需要生成自动化测试代码。首先通过语法,it('案例描述',()=>{测试代码}),将测试案例包裹在花括号内,其中,案例描述按照以下规则生成:原hook文件中的方法名+case。Step 2-3.1.3. After judging and obtaining the prediction result, you need to generate automated test code. First, through the syntax, it('case description', ()=>{test code}), wrap the test case in curly brackets, where the case description is generated according to the following rules: the method name in the original hook file + case.

然后在花括号内,通过renderHook方法模拟hook组件的渲染完成的事件,再通过act方法,模拟触发原hook文件中该对应的方法。通过语法expect(方法中使用的变量).toBe(预测出来的结果),得到的结果可能为true或者false,只有true表示案例通过。Then inside the curly braces, the renderHook method is used to simulate the completion of the rendering of the hook component, and then the act method is used to simulate and trigger the corresponding method in the original hook file. Through the syntax expect(variable used in the method).toBe(the predicted result), the result obtained may be true or false, and only true indicates that the case passed.

步骤2-3.2、对于hook组件内依赖更新事件的模拟,具体实现方式如下:Step 2-3.2, for the simulation of the dependency update event in the hook component, the specific implementation method is as follows:

步骤2-3.2.1、由于hook对于依赖更新是通过react提供的useEffect方法实现的。所以,如果在原hook组件(即未经模拟的hook组件)内,出现了useEffect函数。该函数第一个入参为一个匿名的箭头函数,函数内容是执行对于依赖变量的操作。函数的第二个入参,可以是无、空数组、有元素的数组。当函数的第二个入参有值,且是一个非空数组,数组内包含了hook组件内的变量,则说明hook文件内,有依赖更新事件。Step 2-3.2.1. Because hooks are used to update dependencies, they are implemented through the useEffect method provided by react. Therefore, if the useEffect function appears in the original hook component (ie, the unsimulated hook component). The first input parameter of this function is an anonymous arrow function, and the content of the function is to perform operations on dependent variables. The second input parameter of the function can be nothing, an empty array, or an array with elements. When the second input parameter of the function has a value and is a non-empty array, and the array contains the variables in the hook component, it means that there are dependent update events in the hook file.

步骤2-3.2.2、通过useState关键字,查找到useEffect方法中第二个参数的数组中,涉及到的变量初始化定义代码,通过useState方法执行时,传入的参数,即得到了变量的初始化值(假设值为a)。在useEffect方法中的匿名箭头函数内,获取到对于该变量修改后的值,作为依赖更新后需要执行变更变量的值(假设值为b)。则预判的结果为,当依赖变更时,变量的值应该从a变更为b。Step 2-3.2.2. Through the useState keyword, find the variable initialization definition code involved in the array of the second parameter in the useEffect method. When the useState method is executed, the incoming parameters get the initialization of the variable. value (assuming the value is a). In the anonymous arrow function in the useEffect method, the modified value of the variable is obtained as the value of the variable that needs to be changed after the dependency update (assuming the value is b). The predicted result is that when the dependency changes, the value of the variable should be changed from a to b.

步骤2-3.2.3、判断得到预测结果后,需要生成自动化测试代码。首先通过语法,it('案例描述',()=>{测试代码}),将测试案例包裹在花括号内,其中,案例描述按照以下规则生成:原hook文件中的方法名+case。Step 2-3.2.3. After judging and obtaining the prediction result, you need to generate automated test code. First, through the syntax, it('case description', ()=>{test code}), wrap the test case in curly brackets, where the case description is generated according to the following rules: the method name in the original hook file + case.

然后在花括号内,通过renderHook方法模拟hook组件的渲染完成的事件。并在所述自动化测试代码已被修改的情况下,通过所述自动化测试代码被修改后的依赖变量的值,来模拟触发更新事件的执行。通过语法expect(a).toBe(b),得到的结果可能为true或者false,只有true表示案例通过。Then inside the curly braces, simulate the rendering completion event of the hook component through the renderHook method. And when the automated test code has been modified, the execution of the update event is simulated and triggered by the value of the modified dependency variable of the automated test code. Through the syntax expect(a).toBe(b), the result may be true or false, and only true means the case passes.

步骤3、将所有测试案例关联的动态值,进行页面可视化展示;所展示的可视化页面,是能够对所有hook文件的自动化测试案例代码中任一测试案例直接编辑修改的页面,实现可视化界面修改测试案例。Step 3. Visually display the dynamic values associated with all test cases on the page; the displayed visual page is a page that can directly edit and modify any test case in the automated test case code of all hook files, so as to realize the modification test of the visual interface case.

本步骤具体可以包括:This step can specifically include:

步骤3-1、当所有测试案例生成完成后,会生成一个html页面展示文件。Step 3-1. When all test cases are generated, an html page display file will be generated.

步骤3-2、通过执行命令npm run show-test-case,在项目本地,会通过nodejs启动一个本机启用了的服务器,通过浏览器访问http://192.168.3.2:8080/case.html地址,可以打开这个页面。Step 3-2. By executing the command npm run show-test-case, in the project local, a locally enabled server will be started through nodejs, and the address http://192.168.3.2:8080/case.html will be accessed through the browser , you can open this page.

步骤3-3、在页面上,展示包含所有hook组件名称、以及每一个组件的所有测试案例描述列表的内容。Step 3-3. On the page, display the contents of the list including the names of all hook components and all test case descriptions for each component.

对于测试代码中的expect(a).toBe(b)中的a和b,提供两个input输入框,可以给使用者进行编辑的权利。当编辑完成后,点击保存,会调用本机启用的服务器提供的修改本地文件的接口,接口会修改本地的自动化测试代码文件。由此实现可视化界面修改测试案例。For a and b in expect(a).toBe(b) in the test code, two input boxes are provided, which can give the user the right to edit. When the editing is completed, click Save, and the interface provided by the locally enabled server to modify the local file will be called, and the interface will modify the local automated test code file. This realizes the visual interface modification test case.

该前端react框架hook组件自动化测试工具的使用方法,包括:How to use the automated testing tool for hook components of the front-end react framework, including:

步骤4-1、工具引入。在命令行(mac系统为终端),定位当前项目根目录,执行命令,npm i react-hook-autotest-tool-D,就可以添加该工具到当前框架项目中。Step 4-1. Tool introduction. On the command line (the mac system is the terminal), locate the root directory of the current project, execute the command, npm i react-hook-autotest-tool-D, you can add the tool to the current framework project.

步骤4-2、设置配置文件config.js,配置文件配置内容为:Step 4-2, set the configuration file config.js, the configuration content of the configuration file is:

(1)需要进行自动化测试hook组件的地址,以数组的形式编写,如[./component/A组件,./component/B组件,./component/C组件]。(1) The address of the hook component that needs to be automatically tested, written in the form of an array, such as [./component/A component, ./component/B component, ./component/C component].

步骤4-3、项目构建。在命令行(mac系统为终端),定位当前项目根目录,执行命令构建命令,npm run create-hook-autotest,执行过程中,会涉及到以下核心原理的调用顺序执行。执行完成后,会生成相应的自动化测试代码,以及可视化编辑测试案例入参和结果的html文件。Step 4-3, project construction. On the command line (the mac system is the terminal), locate the root directory of the current project, execute the command build command, npm run create-hook-autotest, during the execution process, the following core principles will be involved in the calling sequence execution. After the execution is completed, the corresponding automated test code will be generated, as well as the html file for visual editing of test case input parameters and results.

由于本实施例的存储介质所实现的处理及功能基本相应于前述方法的实施例、原理和实例,故本实施例的描述中未详尽之处,可以参见前述实施例中的相关说明,在此不做赘述。Since the processing and functions implemented by the storage medium in this embodiment basically correspond to the embodiments, principles, and examples of the foregoing methods, the descriptions of this embodiment are not detailed, and reference may be made to the relevant descriptions in the foregoing embodiments. I won't go into details.

采用本发明的技术方案,通过设置一种前端react框架hook组件自动化测试工具,该前端react框架hook组件自动化测试工具的实现方式,主要包括:检查并安装前端react框架hook组件自动化测试所需要的文件,并在执行前端react框架hook组件自动化测试时执行前端react框架hook组件自动化测试代码的存放目录下的测试文件;根据前端react框架hook组件中每一个hook文件,生成自动化测试案例代码;将所有测试案关联的动态值,进行页面可视化展示,实现可视化界面修改测试案例的目的;对于前端大型的项目,通过使用此工具,自动化测试代码由工具按照既定的规则生成,具有统一的编码风格,后期如果需要人为阅读和维护,也可以减轻相应的人力投入成本。By adopting the technical scheme of the present invention, by setting up a front-end react framework hook component automated testing tool, the implementation of the front-end react framework hook component automated testing tool mainly includes: checking and installing the files required for the front-end react framework hook component automated testing , and execute the test file in the storage directory of the automated test code of the front-end react framework hook component when executing the automated test of the front-end react framework hook component; according to each hook file in the front-end react framework hook component, generate the automated test case code; The dynamic value associated with the case is displayed on the page visually, so as to realize the purpose of modifying the test case on the visual interface; for large-scale front-end projects, by using this tool, the automated test code is generated by the tool according to the established rules, and has a unified coding style. It requires manual reading and maintenance, which can also reduce the corresponding human input cost.

根据本发明的实施例,还提供了对应于自动化测试工具的实现方法的一种存储介质,所述存储介质包括存储的程序,其中,在所述程序运行时控制所述存储介质所在设备执行以上所述的自动化测试工具的实现方法。According to an embodiment of the present invention, a storage medium corresponding to an implementation method of an automated testing tool is also provided, wherein the storage medium includes a stored program, wherein when the program runs, the device where the storage medium is located is controlled to execute the above The implementation method of the automatic test tool.

由于本实施例的存储介质所实现的处理及功能基本相应于前述方法的实施例、原理和实例,故本实施例的描述中未详尽之处,可以参见前述实施例中的相关说明,在此不做赘述。Since the processing and functions implemented by the storage medium in this embodiment basically correspond to the embodiments, principles, and examples of the foregoing methods, the descriptions of this embodiment are not detailed, and reference may be made to the relevant descriptions in the foregoing embodiments. I won't go into details.

采用本发明的技术方案,通过设置一种前端react框架hook组件自动化测试工具,该前端react框架hook组件自动化测试工具的实现方式,主要包括:检查并安装前端react框架hook组件自动化测试所需要的文件,并在执行前端react框架hook组件自动化测试时执行前端react框架hook组件自动化测试代码的存放目录下的测试文件;根据前端react框架hook组件中每一个hook文件,生成自动化测试案例代码;将所有测试案关联的动态值,进行页面可视化展示,实现可视化界面修改测试案例的目的;此工具提供测试案例的可视化编辑修改界面,当开发人员需要主动修改测试案例时,可以不用查找到对应的源码,也可以实现测试案例的调整,也给非开发人员需要修改测试案例时,提供了修改的通道。By adopting the technical scheme of the present invention, by setting up a front-end react framework hook component automated testing tool, the implementation of the front-end react framework hook component automated testing tool mainly includes: checking and installing the files required for the front-end react framework hook component automated testing , and execute the test file in the storage directory of the automated test code of the front-end react framework hook component when executing the automated test of the front-end react framework hook component; according to each hook file in the front-end react framework hook component, generate the automated test case code; This tool provides a visual editing and modification interface for test cases. When developers need to actively modify test cases, they do not need to find the corresponding source code. The adjustment of the test case can be realized, and it also provides a modification channel for non-developers when they need to modify the test case.

综上,本领域技术人员容易理解的是,在不冲突的前提下,上述各有利方式可以自由地组合、叠加。To sum up, those skilled in the art can easily understand that, on the premise that there is no conflict, the above advantageous manners can be freely combined and superimposed.

以上所述仅为本发明的实施例而已,并不用于限制本发明,对于本领域的技术人员来说,本发明可以有各种更改和变化。凡在本发明的精神和原则之内,所作的任何修改、等同替换、改进等,均应包含在本发明的权利要求范围之内。The above description is only an embodiment of the present invention, and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and changes. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included within the scope of the claims of the present invention.

Claims (10)

1. An implementation method of an automated testing tool, the automated testing tool comprising: the front-end act frame hook component automatic testing tool; the implementation method of the front-end exact frame hook component automatic testing tool comprises the following steps:
checking and installing files required by the front-end exact frame hook component automatic test, and executing test files under a storage directory of front-end exact frame hook component automatic test codes when the front-end exact frame hook component automatic test is executed; the front-end exact frame hook assembly is provided with N hook files, wherein N is a positive integer;
generating an automatic test case code according to each hook file in the front-end act frame hook assembly; generating automatic test case codes of all hook files according to all hook files in a front-end act framework hook assembly;
determining a dynamic value of each test case according to each test case corresponding to all the automatic test case codes; performing page visual display on the dynamic values associated with all the test cases; the displayed visual page is a page capable of directly editing and modifying any test case in the automatic test case codes of all hook files.
2. The method for implementing the automated testing tool according to claim 1, wherein the checking and installing of the files required for the automated testing of the front-end exact frame hook component and the executing of the test files under the storage directory of the automated testing code of the front-end exact frame hook component when the automated testing of the front-end exact frame hook component is executed comprise:
checking and installing a dependency file required by the front-end exact frame hook component automatic test;
setting a storage catalog of the front-end exact frame hook assembly automatic test codes to store the generated front-end exact frame hook assembly automatic test codes;
and setting a configuration item, so that when the front-end exact frame hook component automatic test is executed, a test file in a storage directory of the front-end exact frame hook component automatic test code in the configuration item is indexed, and the test file is executed.
3. The method of claim 2, wherein generating an automated test case code from each hook file in the front-end exact framework hook component comprises:
traversing each element in the array according to the array of hook component addresses which are configured in the preset configuration items and need to be automatically tested, and finding each hook file corresponding to each element in the array as each hook file which needs to be automatically tested;
aiming at each hook file needing to be automatically tested, executing the operation of generating an automatic test case code, and generating a basic code of each hook file needing to be automatically tested;
and identifying different scene grammars according to the basic codes of each hook file needing to be automatically tested, intelligently and reversely deducing expected results, and generating codes of each hook file needing to be automatically tested as test case codes corresponding to each hook file needing to be automatically tested.
4. The method for implementing the automated testing tool according to claim 3, wherein the executing the operation of generating the automated testing case code for each hook file to be automatically tested to generate the base code for each hook file to be automatically tested comprises:
creating a storage directory of automatic test codes of a front-end exact frame hook component, and generating blank files corresponding to each hook file needing to be subjected to automatic test in a folder where the storage directory is located;
introducing an unmarked hook component, a method renderHook for finishing the rendering of the simulated hook component and an act method for simulating and triggering the hook component to expose a function outwards into each hook file needing to be subjected to automatic testing through a first set grammar;
and packaging the test cases corresponding to each hook file to be generated and needing automatic testing through a second set grammar so as to obtain the basic code of each hook file to be automatically tested.
5. The method as claimed in claim 3, wherein the step of identifying different scenario grammars according to the basic code of each hook file requiring the automatic testing, intelligently and reversely deriving the expected result, and generating the code of each hook file requiring the automatic testing as the test case code corresponding to each hook file requiring the automatic testing comprises:
for the non-simulated hook component, in the variable which is output externally through the return keyword at the end of the function, by carrying out typeof judgment on the variable, if the obtained result is 'function', reversely searching the hook code of the non-simulated hook component, and reversely deducing the case and the result of the execution of the variable by operating the complete code block of the function name of the function on the variable in the code block of the hook code of the non-simulated hook component;
if a useEffect function appears in the hook assembly which is not simulated, determining that a dependent updating event exists in a hook file of the hook assembly which is not simulated, and simulating the dependent updating event in the hook assembly to obtain an automatic test code; and simulating execution of a trigger update event by the value of the modified dependent variable of the automated test code if the automated test code has been modified.
6. The method for implementing the automated testing tool according to any one of claims 1 to 5, wherein the page visualization presentation of the dynamic values associated with all the test cases comprises:
after all the test cases are generated, generating a page display file according to the dynamic values associated with all the test cases;
by executing a set command, the browser accesses a page address and opens the page;
on the page, the content is shown containing all hook component names, and a list of all test case descriptions for each component.
7. An apparatus for implementing an automated testing tool, the automated testing tool comprising: the front end act frame hook component automatic testing tool; the device for realizing the front end exact frame hook component automatic testing tool comprises:
the device comprises a setting unit, a storage unit and a processing unit, wherein the setting unit is configured to check and install files required by the front-end exact frame hook component automatic test, and execute test files under a storage catalog of front-end exact frame hook component automatic test codes when the front-end exact frame hook component automatic test is executed; the front-end exact frame hook assembly is provided with N hook files, wherein N is a positive integer;
the generating unit is configured to generate an automatic test case code according to each hook file in the front-end act framework hook assembly; generating automatic test case codes of all hook files according to all hook files in a front-end act framework hook assembly;
the display unit is configured to determine a dynamic value of each test case for each test case corresponding to all the automated test case codes; performing page visual display on the dynamic values associated with all the test cases; the displayed visual page is a page capable of directly editing and modifying any test case in the automatic test case codes of all hook files.
8. A terminal, comprising: the apparatus of claim 7.
9. An automated testing tool, characterized in that it is obtained by the implementation method of an automated testing tool according to any one of claims 1 to 6.
10. A storage medium comprising a stored program, wherein the program, when executed, controls a device on which the storage medium is located to perform a method of implementing an automated testing tool according to any one of claims 1 to 6.
CN202210283326.4A 2022-03-22 2022-03-22 Automated testing tool and its implementation method, device, terminal and storage medium Pending CN114610633A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210283326.4A CN114610633A (en) 2022-03-22 2022-03-22 Automated testing tool and its implementation method, device, terminal and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210283326.4A CN114610633A (en) 2022-03-22 2022-03-22 Automated testing tool and its implementation method, device, terminal and storage medium

Publications (1)

Publication Number Publication Date
CN114610633A true CN114610633A (en) 2022-06-10

Family

ID=81865384

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210283326.4A Pending CN114610633A (en) 2022-03-22 2022-03-22 Automated testing tool and its implementation method, device, terminal and storage medium

Country Status (1)

Country Link
CN (1) CN114610633A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115237690A (en) * 2022-07-14 2022-10-25 京东科技信息技术有限公司 Test tool generation method, device, electronic device and storage medium

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US5600789A (en) * 1992-11-19 1997-02-04 Segue Software, Inc. Automated GUI interface testing
US20080250051A1 (en) * 2007-04-05 2008-10-09 Accenture Global Services Gmbh Automatic test generation for reference testing
CN111679977A (en) * 2020-05-29 2020-09-18 杭州趣链科技有限公司 Jest-based React project unit testing method, equipment and storage medium

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US5600789A (en) * 1992-11-19 1997-02-04 Segue Software, Inc. Automated GUI interface testing
US20080250051A1 (en) * 2007-04-05 2008-10-09 Accenture Global Services Gmbh Automatic test generation for reference testing
CN111679977A (en) * 2020-05-29 2020-09-18 杭州趣链科技有限公司 Jest-based React project unit testing method, equipment and storage medium

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN115237690A (en) * 2022-07-14 2022-10-25 京东科技信息技术有限公司 Test tool generation method, device, electronic device and storage medium

Similar Documents

Publication Publication Date Title
CN110008113B (en) Test method and device and electronic equipment
US7530056B1 (en) Method and system for detecting runtime defects in a program by comparing correct and incorrect runs
CN109446059A (en) The generation method and device of test template script
CN107729228A (en) Interface test method, device, storage medium and processor
CN112148278A (en) Visual block chain intelligent contract framework and intelligent contract development and deployment method
US8745602B2 (en) Automatic configuration of project system from project capabilities
CN109656813A (en) A kind of test case operation method and device
KR20130017337A (en) Hybrid app development platform architecture and test method of the same, and computer-readable recording medium with program for the same
CN108304164B (en) Business logic development method and development system
CN117055858A (en) Method for constructing front-end scaffold
US11442845B2 (en) Systems and methods for automatic test generation
CN114489861A (en) Target component conversion method and device, electronic equipment and readable storage medium
CN116679912A (en) Code generation method, apparatus, device, storage medium, and computer program
CN114610633A (en) Automated testing tool and its implementation method, device, terminal and storage medium
CN115756490A (en) Method, device, equipment, storage medium and product for improving android compiling efficiency
CN119047384A (en) Compiling method and device for chip verification and computing equipment
CN114490337A (en) Commissioning method, commissioning platform, equipment and storage medium
CN112052039A (en) Method and device for synchronously constructing multiple projects and electronic equipment
CN117271368A (en) Page feasibility test method and device, computer equipment and storage medium
CN111142861A (en) Method and device for integrating structured comprehensive control system
CN112799942B (en) Keil C51-based software source code and target code coverage rate analysis method and system
CN115269386A (en) A kind of interface automatic testing method, system, device and readable storage medium
CN113590225A (en) Map detection method and device, electronic equipment and storage medium
CN115934500A (en) Pipeline construction method and device, computer equipment and storage medium
CN113971028A (en) Data processing method, device, equipment, storage medium and computer program product

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination