首页 > 解决方案 > TestCafe:将另一个文件中的测试导入当前的夹具

问题描述

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

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

但是,test is not defined尝试执行时出现错误main.js

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();
*/

我已经尝试过:

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

标签: node.jsautomated-testsintegration-testinge2e-testingtestcafe

解决方案


TestCafe 不允许在测试范围之外调用fixture和函数。test您可以将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();

另请参阅:组织测试


推荐阅读