TestCafe:将测试从另一个文件导入到当前夹具中
TestCafe: import tests from another file into the current fixture
我有一个文件 tests.js
,其中包含一些 test(...)
定义。我想在多个 fixture 中重用这些测试,最好不要对原始代码进行任何修改。
所以我写了一个 main.js
来定义一个夹具并导入 tests.js
,从而 "assembling" 一个测试套件。 (如果可行,我可以用不同的装置编写不同的驱动程序文件,从每个文件中导入相同的 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();
*/
我已经试过了:
- 删除块评论 hack (
test();
) - 这给出了 ERROR No tests to run. Either the test files contain no tests or the filter function is too restrictive.
- 将
tests.js
导入移到顶部 - 仍然会给出 test is not defined
- 从
main.js
和 tests.js
中导入 testcafe
- 同样的错误
有没有办法让test
函数对testcafe入口点文件导入的其他文件"visible"?或者我真的需要修改我的 tests.js
文件才能让它工作吗?也许通过将测试定义添加到一个方法中,并从 main.js
中调用它——就像在 this issue?
的原始代码示例中一样
尝试在 TestCafe 命令行
上添加选项 --disable-test-syntax-validation
(仅适用于最新的 TestCafe 版本)。
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();
另请参阅:Organize Tests
我有一个文件 tests.js
,其中包含一些 test(...)
定义。我想在多个 fixture 中重用这些测试,最好不要对原始代码进行任何修改。
所以我写了一个 main.js
来定义一个夹具并导入 tests.js
,从而 "assembling" 一个测试套件。 (如果可行,我可以用不同的装置编写不同的驱动程序文件,从每个文件中导入相同的 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();
*/
我已经试过了:
- 删除块评论 hack (
test();
) - 这给出了ERROR No tests to run. Either the test files contain no tests or the filter function is too restrictive.
- 将
tests.js
导入移到顶部 - 仍然会给出test is not defined
- 从
main.js
和tests.js
中导入testcafe
- 同样的错误
有没有办法让test
函数对testcafe入口点文件导入的其他文件"visible"?或者我真的需要修改我的 tests.js
文件才能让它工作吗?也许通过将测试定义添加到一个方法中,并从 main.js
中调用它——就像在 this issue?
尝试在 TestCafe 命令行
上添加选项--disable-test-syntax-validation
(仅适用于最新的 TestCafe 版本)。
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();
另请参阅:Organize Tests