首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试咖啡馆数据驱动测试-如何使用从API获取的数据驱动测试

测试咖啡馆数据驱动测试-如何使用从API获取的数据驱动测试
EN

Stack Overflow用户
提问于 2022-02-16 20:02:02
回答 2查看 143关注 0票数 0

我很难弄清楚如何用从请求中获取的数据来驱动测试。我在这里阅读了文档: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咖啡馆不明确地支持这样的东西,我想一定有一些解决办法.提前谢谢。

编辑:

代码语言:javascript
复制
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(...);
    }
);
EN

回答 2

Stack Overflow用户

发布于 2022-02-21 09:57:16

在v1.0.0及更高版本中,TestCafe 不验证测试语法。。请指定当您看到验证错误时使用的TestCafe版本。

不幸的是,我们不能使用伪代码来再现您遇到的问题。请分享一些代码,我们可以运行,以查看问题的行为。

一般来说,TestCafe允许您异步获取数据,然后根据接收到的值生成测试。例如,在TestCafe 1.18.3中,下面的代码对我来说很好:

代码语言:javascript
复制
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

代码语言:javascript
复制
export default async function fetch() {
    return [1, 2, 3, 4, 5];
}

唯一的警告是,我必须显式导入fixturetest,因为我从回调中调用了它们。

票数 1
EN

Stack Overflow用户

发布于 2022-02-18 07:34:47

你能给我们提供任何测试代码片段来演示这个问题吗?我们需要正确地理解问题的原因,并在我们这一边再现它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71148556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档