Mocha 在块未执行之前生成动态测试 [英] Mocha Dynamic test generation in before block not getting executed

查看:64
本文介绍了Mocha 在块未执行之前生成动态测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如在这个 post 中所建议的,我尝试了创建动态测试的步骤,但我看到了实际的测试(测试.getMochaTest() 在我下面的实现中)没有被执行.我在这里遗漏了什么,对 test.getMochaTest() 的调用没有在 before 块中执行.

As suggested in this post , I tried the steps to create dynamic tests , but I see the actual test(test.getMochaTest()in my below implementation) not getting executed. What's that I'm missing here, the call on test.getMochaTest() does not get executed in the before block.

describe('Dynamic Tests for Mocha', async () => {
    let outcome ;
    before(async() => {
        await init().catch(() => console.error('Puppeteer environment initialization failed'));
        return collectTests().then(async(collectTests) => {
            console.info('4.Executing tests :');
            describe('Dynamic test execution', async() => {
                collectTests.forEach(async(test) => {
                    console.info(`\tModule under test : ${test.name}`);

        // impl. of test.getMochaTest() DOES NOT get executed. 

                    it(test.name, async() => outcome = await test.getMochaTest().catch(async () => {
                        console.error(`error while executing test:\t${test.name}`);
                    }));
                });
            }) ;
        });
    });

    after(async () => {
        console.info('5. Exiting tests...');
        await HelperUtils.delay(10000).then(async () => { await browser.close(); });
        console.log('executing after block');
    });

    it('placeholder',  async() =>  {
        await
        console.log('place holder hack - skip it');
    });

});

这里返回测试数组:

async function collectTests():Promise<Array<ITest>> {
    console.info('3.Collecting tests to execute ...');
    testArray = new Array<ITest>();
    const login:ITest = new SLogin('Login Check');
    testArray.push(login);
    
    return testArray;
}

SLogin 中 getMochaTest 的以下实现 ->不会被执行.

The below implementation of getMochaTest in SLogin -> does not get executed .

export default class SLogin extends BTest implements ITest {
    constructor(name: string) {
        super(name);
    }
    async getMochaTest():Promise<Mocha.Func> {
        return async () => {
            console.log('Running Login check');
            expect(true).to.equal(true);
        };
    }
}

推荐答案

看起来您实际上并未调用测试.

It doesn't look like you're actually invoking the test.

调用 test.getMochaTest() 只会返回 Promise 中的异步测试函数,它不会执行它.因此,您的 catch 块是在 获取 函数时捕获错误,而不是在 执行 函数时捕获错误.

Calling test.getMochaTest() only returns the async test function in a Promise, it doesn't execute it. So your catch block is catching errors while obtaining the function, not while executing it.

将其分成多行将有望使事情更清晰.

Breaking it out across multiple lines will hopefully make things clearer.

这是您的代码示例的作用.注意它从不执行返回的测试函数:

Here's what your code sample does. Notice it never executes the returned test function:

it(test.name, async () => {
    const testFn = await test.getMochaTest().catch(() => 
        console.error(`error while ***obtaining*** test: \t${test.name}`));

    // oops - testFn never gets called!
});

这是一个更正的版本,其中实际调用了测试:

And here's a corrected version where the test actually gets called:

it(test.name, async () => {
    const testFn = await test.getMochaTest().catch(() => 
        console.error(`error while ***obtaining*** test: \t${test.name}`));

    const outcome = await testFn().catch(() => 
        console.error(`error while ***executing*** test: \t${test.name}`));
});

注意:我用 awaitcatch() 以这种方式编写它以更好地匹配您的代码示例的格式.但是,值得指出的是,它混合了 async/awaitPromise 语法.更惯用的方法是在使用 async/await 时使用 try/catch 块捕获错误.

Note: I wrote it that way with await and catch() to better match the format of your code sample. However, it's worth pointing out that it mixes async/await and Promise syntax. More idiomatic would be to catch errors with a try/catch block when using async/await.

这篇关于Mocha 在块未执行之前生成动态测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆