我很难弄清楚如何用从请求中获取的数据来驱动测试。我在这里阅读了文档:https://testcafe.io/documentation/402804/recipes/best-practices/create-data-driven-tests,所有示例都使用编译时可用的静态json文件数据。
我无法在fixture.before钩子中获取数据,因为它只能在测试上下文的中可用,但我需要在测试上下文的之外访问数据以进行迭代,这样测试就在for循环中。
我尝试过这个解决方案:https://github.com/DevExpress/testcafe/issues/1948,但是这在testcafe ERROR No tests found in the specified source files. Ensure the sources contain the 'fixture' and 'test' directives.中失败了,即使我使用标志disable-test-syntax-validation和.run({ disableTestSyntaxValidation: true });选项。
我正在寻找建议和解决办法,以便我可以等待一些数据,然后运行我的测试。即使Test咖啡馆不明确地支持这样的东西,我想一定有一些解决办法.提前谢谢。
编辑:
file-a.ts
export function tSteps(...args) {
// some setup
const testcase = args[args.length - 1];
const testCtx = test(name, async t => {
...
});
return testCtx;
}
----
file-b.ts
export const parameterizedTest = <T>(..., testcase: (scenario: T) => TestFn) => {
// some setup...
// I have also tried awaiting rows data here, which does not work
// because tests are not discoverable at compile time
...
const scenarios: T[] = rows.map(row => {
...
});
scenarios.forEach((scenario, idx) => {
return testcase(scenario).meta({
some metadata
});
});
};
----
tests.ts
fixture(...).before(async () => {
// can't get the data i need here because it needs to be available outside of the fixture context
})
parameterizedTest<MyInterface>(some params, (scenario: MyInterface) => {
return tSteps('my test',
async f => {
// some setup
// test code goes here which uses scenario.attributex, scenario.attributey, etc.
}
).meta(...);
}
);发布于 2022-02-21 09:57:16
在v1.0.0及更高版本中,TestCafe 不验证测试语法。。请指定当您看到验证错误时使用的TestCafe版本。
不幸的是,我们不能使用伪代码来再现您遇到的问题。请分享一些代码,我们可以运行,以查看问题的行为。
一般来说,TestCafe允许您异步获取数据,然后根据接收到的值生成测试。例如,在TestCafe 1.18.3中,下面的代码对我来说很好:
import { fixture, test } from 'testcafe';
import fetch from './node-fetch-mock';
(async () => {
const testData = await fetch();
testData
.map(n => () => {
fixture `Fixture ${n}`
.page `https://google.com`;
test(`Test ${n}`, async t => {
await t.expect(true).ok();
});
})
.map(async test => { await test(); });
})();node-fetch-mock.js
export default async function fetch() {
return [1, 2, 3, 4, 5];
}唯一的警告是,我必须显式导入fixture和test,因为我从回调中调用了它们。
发布于 2022-02-18 07:34:47
你能给我们提供任何测试代码片段来演示这个问题吗?我们需要正确地理解问题的原因,并在我们这一边再现它。
https://stackoverflow.com/questions/71148556
复制相似问题