【问题标题】:Testcafe - beforeAll and afterAll tests in a file or all filesTestcafe - 一个文件或所有文件中的 beforeAll 和 afterAll 测试
【发布时间】:2022-12-09 16:53:15
【问题描述】:

如果我有 3 个测试文件,每个文件都有几个固定装置和测试,就像这样

file1.js
    fixture1
        test1
        test2
    fixture2
        test3
        test4
file2.js
    fiture3
        test5
        test6
    fixture4
        test7
        test8
file3.js
    fixture5
        test9
        test10
        test11
    fixture6
        test12
        test13

我发现我可以将fixture.after()fixture.before() 用于某个固定装置。我可以使用 test.after()test.before() 进行特定测试。

如果我想给file2.js设置环境,并在file2.js中的所有测试(test5、test6、test7、test8)执行完后clean,testcafe有这样的功能吗?

如果我想为所有文件设置环境,并在所有测试执行时清理它,testcafe有没有这样的功能?

谢谢


更新 1

在阅读了vasily.strelyaev提供的Hooks before and after test runs后,我创建了.testcaferc_seq.js并在package.json中添加了"test": "./node_modules/.bin/testcafe chrome --sf ./integration_tests/file2.js --live --config-file .testcaferc_seq.js"

.testcaferc_seq.js

const utils = require ("integration_tests/utils/afterAllTestRunner.js");

module.exports = {
  hostname: "localhost",
  ...

  // before all and after all tests
  hooks: {
    testRun: {
      before: async ctx => {
        console.log("---------------- \n before all tests \n ---------------------");
        utils.cleanUsers();
      },
      after: async ctx => {
        console.log("---------------- \n after all tests \n ---------------------");
        utils.cleanUsers();
      },
    },
  },
}

然而hook并没有起作用,看不到两个console.log(...)的两句话。我错过了什么?

还有一个问题,如果我想控制浏览器(比如chrome)做一些pre/post testRun的步骤,怎么办?

【问题讨论】:

    标签: javascript testing automated-tests e2e-testing testcafe


    【解决方案1】:

    目前没有内置方法可以在特定文件中的第一个 fixture 开始之前以及该文件中的最后一个 fixture 结束之后运行挂钩。作为解决方法,您可以将此文件中的所有测试放在一个夹具中并指定 its hooks。或者,如果可以在您的场景中多次运行挂钩代码,您可以使用设置和拆卸代码创建辅助函数,并从each fixture's individual hooks 调用这些函数。

    至于你的第二个问题,是的,你可以使用全局测试运行挂钩在第一个测试开始之前设置环境并在所有测试完成后清理它:https://testcafe.io/documentation/403435/guides/intermediate-guides/hooks#hooks-before-and-after-test-runs

    您可以在我们的 Hooks 指南中找到更多信息:https://testcafe.io/documentation/403435/guides/intermediate-guides/hooks

    【讨论】:

    • 嗨 vasily.strelyaev,我在帖子中添加了一些问题和步骤。谢谢回答。
    【解决方案2】:

    读取配置文件可能存在一些问题。查看控制台,您将看到:

    An error has occurred while reading the "....testcaferc.js" configuration file.
    

    这就是全局挂钩不起作用的原因。这个错误可能是由integration_tests/utils/afterAllTestRunner.js模块引起的。尝试在不使用它的情况下运行它。

    关于 testRun 挂钩之前/之后的浏览器操作。这是不支持的。

    【讨论】:

      猜你喜欢
      • 2019-09-09
      • 2014-03-10
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      相关资源
      最近更新 更多