Home > Net >  Testcafe data driven testing - how to drive tests with data fetched from API
Testcafe data driven testing - how to drive tests with data fetched from API

Time:02-21

I'm having trouble figuring out how to drive tests with data fetched from a request. I've read the documentation here: https://testcafe.io/documentation/402804/recipes/best-practices/create-data-driven-tests, and all examples use static json file data available at compile time.

I can't fetch the data in fixture.before hook, because it will only be available inside of the test context, but I need to access the data outside of the test context for iteration, such that the test is inside of a for loop.

I've tried this solution: https://github.com/DevExpress/testcafe/issues/1948, however this fails with testcafe ERROR No tests found in the specified source files. Ensure the sources contain the 'fixture' and 'test' directives., even when I use the flag disable-test-syntax-validation and .run({ disableTestSyntaxValidation: true }); option.

I am looking for suggestions and workarounds so that I can await some data, then run my tests. Even if Testcafe doesn't explicitly support something like this, I figure there must be some workaround... Thanks in advance.

Edit:

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(...);
    }
);

CodePudding user response:

Could you please provide us with any test code snippet that demonstrates the problem? We need to correctly understand the cause of the problem and reproduce it on our side.

CodePudding user response:

In v1.0.0 and later, TestCafe does not validate test syntax. Please specify the TestCafe version that you use when you see the validation error.

Unfortunately, we cannot use pseudo-code to reproduce the issue you encountered. Please share some code that we could run to see the problematic behavior.

Generally speaking, TestCafe allows you to fetch data asynchronously and then spawn tests based on the received values. For instance, the following code works fine for me with 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];
}

The only caveat is that I have to import fixture and test explicitly because I call them from callbacks.

  • Related