node.js - TestCafe:将测试从另一个文件导入到当前夹具中

标签 node.js automated-tests integration-testing e2e-testing testcafe

我有一个 tests.js 文件,其中包含一些 test(...) 定义。我想在多个装置中重复使用这些测试,最好不要对原始代码进行任何修改。

所以我写了一个 main.js 定义了一个夹具并导入了 tests.js,从而“组装”了一个测试套件。 (如果可行,我可以用不同的装置编写不同的驱动程序文件,从每个文件中导入相同的 tests.js。)

但是,我在尝试执行 main.js 时遇到了一个test is not defined 错误:

C:\Windows\Temp\dummy>testcafe chrome main.js --debug-on-fail
ERROR Cannot prepare tests due to an error.

ReferenceError: test is not defined
    at Object.<anonymous> (C:\Windows\Temp\dummy\tests.js:1:1)
    at Object.<anonymous> (C:\Windows\Temp\dummy\main.js:7:1)

Type "testcafe -h" for help.

最小样本:

// tests.js

test('wait', async t => {
    await t.wait(1);
});


// main.js

fixture `here goes the name`
    .page("http://localhost:3000")
    .beforeEach(async t => {
        // do stuff
    });

import "./tests";

/*
trick testcafe to scan the file;
based on https://github.com/DevExpress/testcafe/issues/2889#issuecomment-423859785

test();
*/

我已经试过了:

  • 删除 block 注释 hack (test();) - 这会导致 ERROR No tests to run。要么是测试文件不包含测试,要么是过滤函数限制太多。
  • tests.js 导入移动到顶部 - 仍然给出 test is not defined
  • main.jstests.js 中导入 testcafe - 同样的错误

有没有办法让 test 函数对 testcafe 入口点文件导入的其他文件“可见”?或者我真的需要修改我的 tests.js 文件才能让它工作吗?也许通过将测试定义添加到一个方法中,并从 main.js 中调用它——就像在 this issue 的原始代码示例中一样?

最佳答案

TestCafe 不允许在测试范围之外调用fixturetest 函数。您可以将来自 tests.js 文件的测试包装在一个函数中,并在 main.js 文件中调用此函数:

// tests.js
export default function () {
   test('Test 1', () => {});
   test('Test 2', () => {});
   test('Test 3', () => {});
}
// main.js
import defineTests from './tests';

defineTests();

另请参阅:Organize Tests

关于node.js - TestCafe:将测试从另一个文件导入到当前夹具中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55389523/

相关文章:

testing - 使用 multiple-cucumber-html-reporter 时如何从报告或屏幕截图中的变量添加测试数据

laravel - 测试 Laravel Controller 的 try catch

javascript - 组件/集成测试主干 View 等

java.lang.LinkageError : loader constraint violation: loader (instance of org/apache/catalina/loader/WebappClassLoader) 错误

database - 将 Effort(EF 测试工具)与计算列结合使用

node.js - 如何访问在docker容器内运行的kurento媒体服务器

javascript - Node.js - 公开吗?

node.js - 要求 ts-node 和 ts-node/register 有什么区别

javascript - Passport.js:使用 Github 登录并返回电子邮件地址

unit-testing - VS 2013 MSTest 与 nUnit 与 xUnit