为 TestCafe 异步创建动态测试

Create dynamic tests for TestCafe asynchronously

我正在 TestCafe 中创建测试。目标是用 Gherkin 编写测试。我查看了一些集成了 Cucumber 和 TestCafe 的 GitHub 存储库,但我正在尝试不同的角度。

我想使用 Gherkin 解析器并跳过 Cucumber。相反,我将创建自己的实现来 运行 测试步骤。但目前我一直在尝试让 TestCafe 进行 运行 测试。

如果我是对的,问题是 TestCafe 运行正在我的测试文件中,然后在任何地方都看不到固定装置或测试。这是正确的,因为 Gherkin 解析器正在使用流 API(它使用单独的 Go 进程来解析特征文件)来传递数据,这意味着在我当前的代码中,当 TestCafe 退出时,Promise 仍然处于挂起状态。或者,如果我删除 end 回调尚未发生。

我的分析正确吗?如果是,我如何从流中获取所有数据并创建我的测试,以便 TestCafe 运行 它?

gherkin_executor.js

var Gherkin = require('gherkin');

console.log('start')

const getParsedGherkin = new Promise((resolve, reject) => {
    let stream = Gherkin.fromPaths(['file.feature'])

    let data = []
    stream.on('data', (chunk) => {
        if(chunk.hasOwnProperty('source')){
            data.push({source: chunk.source, name: null, pickles: []})
        }
        else if (chunk.hasOwnProperty('gherkinDocument')){
            data[data.length-1].name = chunk.gherkinDocument.feature.name
        }
        else {
            data[data.length-1].pickles.push(chunk.pickle)
        }
    })
    stream.on('end', () => {
        resolve(data)
    })
})
let data = getParsedGherkin.then((data) => {return data})
console.log(data)

function createTests(data){
    for(let feature of data){
        fixture(feature.name)
        for(let testcase of feature.pickles){
            test(testcase.name, async t => {
                console.log('test')
            })
        }
    }
}

file.feature

Feature: A test feature

    Scenario: A test case
        Given some data
        When doing some action
        Then there is some result

好倡议!

为了在您的方法中走得更远,方法 createTests 必须在至少一个 JavaScript 或 TypeScript 文件中生成 TestCafe 代码。然后你必须从这些文件启动 TestCafe runner。

所以现在,要在您的方法中走得更远,您必须编写一个 TestCafe 源代码生成器。

在 TestCafe 团队正式支持 Cucumber 之前,GitHub 上的 hdorgeval/testcafe-starter 回购可能是替代方案。