如何在 Mocha 的测试用例中调用另一个测试用例

How to call another TestCase within a TestCase in Mocha

我将 webdriver-io 与 Mocha(和 JavaScript)结合使用。我想在另一个测试用例中调用一个特定的测试用例。

假设我们有以下代码:

describe('TestSuite', function(){

    it('TestCase A', function(){
        return browser
            .getTitle()
            .then( function(title) {
                (title).should.equal('title');
            });
    });

    it('TestCase B', function() {
        // call 'TestCase A'
    });
});

是否可以在 'TestCase B' 中调用 'TestCase A'? 感谢您的帮助。

Mocha 没有 "calling test cases" 的概念。但是您正在使用 JavaScript 并且可以利用它。将公共代码做成一个函数并从多个测试中调用它:

describe('TestSuite', function(){

    function checkTitle() {
        return browser
            .getTitle()
            .then( function(title) {
                (title).should.equal('title');
            });
    }

    it('TestCase A', function() {
        return checkTitle();
    });

    it('TestCase B', function() {
        return checkTitle().then(...);
    });
});