【问题标题】:Mocha Dynamic test generation in before block not getting executed在块未执行之前生成 Mocha 动态测试
【发布时间】:2021-02-21 13:08:09
【问题描述】:

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

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 的以下实现 -> 没有被执行。

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

【问题讨论】:

    标签: javascript typescript mocha.js


    【解决方案1】:

    看起来您实际上并没有调用测试。

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

    将其拆分为多行有望使事情变得更清晰。

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

    it(test.name, async () => {
        const testFn = await test.getMochaTest().catch(() => 
            console.error(`error while ***obtaining*** test: \t${test.name}`));
    
        // oops - testFn never 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 块捕获错误。

    【讨论】:

    • @thanks 指出这一点。但是它抱怨node:4164) UnhandledPromiseRejectionWarning: TypeError: testFn is not a function
    • => 谢谢!这行得通,通过在描述之前将声明const testFn 更改为let testFn 来解决这个问题。在按顺序执行测试时,使用 for..of 而不是 forEach 循环也很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多