Javascript 如何使用mocha中的嵌套测试执行使测试相互依赖

Javascript 如何使用mocha中的嵌套测试执行使测试相互依赖,javascript,mocha.js,Javascript,Mocha.js,我有两个测试(a,B)的简单示例,其中B依赖于正在运行的a 如果我正在使用摩卡,我可以将测试B嵌套在A中: describe.only( 'AB:', function() { describe( 'A', function() { it( 'A1', function() { assert.equal( 1, 2 ); } ); describe( 'B', function() { i

我有两个测试(a,B)的简单示例,其中B依赖于正在运行的a

如果我正在使用摩卡,我可以将测试B嵌套在A中:

describe.only( 'AB:', function() {

    describe( 'A', function() {
        it( 'A1', function() {
            assert.equal( 1, 2 );
        } );

        describe( 'B', function() {
            it( 'B1', function() {
                assert.equal( 1, 1 );
            } );
        } );
    } );
} );
但即使A失败,A和B都会运行

这与不使用嵌套有什么不同

describe.only( 'AB:', function() {

    describe( 'A&B', function() {
        it( 'A1', function() {
            assert.equal( 1, 2 );
        } );

        it( 'B1', function() {
            assert.equal( 1, 1 );
        } );
    } );
} );

如果A失败了,有没有办法跳过B?

好的,有两个问题,所以我将尝试回答这两个问题

  • 如果A失败,有没有办法跳过B

    通常,您应该编写互不依赖的测试

    有时测试依赖于在能够正常运行之前具有特定的设置或状态,在这种情况下,最好在
    before()
    beforeach()
    块中进行设置。如果这些块中的任何一个失败,则不会运行它们之后的测试。因此,当您的构建方式使您知道描述块中的任何测试都不会工作时,您可以在这些块中抛出错误

    describe.only('AB:', function() {
      var numberUnderTest = 0;
    
      describe('A&B', function() {
        it('A1', function() {
          assert.equal(1, 1 * numberUnderTest);
        });
    
        describe('B', function() {
          before(function() {
            if (numberUnderTest === 0) {
              throw 'cannot divide by zero';
            }
          });
    
          it('B1', function() {
            assert.equal(1, 1 / numberUnderTest);
          });
        });
      });
    });
    
  • 如果我正在使用摩卡咖啡,我可以将测试B嵌套在A中
    […]
    这与不使用嵌套有什么不同

    describe.only( 'AB:', function() {
    
        describe( 'A&B', function() {
            it( 'A1', function() {
                assert.equal( 1, 2 );
            } );
    
            it( 'B1', function() {
                assert.equal( 1, 1 );
            } );
        } );
    } );
    
    在描述块中嵌套B允许B1使用与A1不同的设置,同时仍然继承a的一些设置

    describe('A', function() {
      var numberUnderTest;
      var anotherNumber;
    
      beforeEach(function() {
        numberUnderTest = 1;
        anotherNumber = 0;
      });
    
      it('A1'), function() {
        assert.equal(0, anotherNumber * numberUnderTest);
      });
    
      describe('B', function() {
        before(function() {
          anotherNumber = 1;
        });
    
        it('B1', function() {
          assert.equal(1, anotherNumber / numberUnderTest);
        });
      });
    });
    
  • 如果A失败,有没有办法跳过B

    这种模式对我来说非常有效:

    var itCanLogin;
    
    it('Can login', function() {
      ...
    
      itCanLogin = true;
    });
    
    it('Can Logout', function(){
       if(!itCanLogin) this.skip()
    
       ...
    })
    

    也可以使用
    assert(itCanLogin)
    但是
    this.skip()
    有助于通过不产生堆栈跟踪和错误来保持输出的整洁-通过这种方式更容易发现问题的根源。

    实现这一点的最简单方法是使用:

    完整的博客帖子:

    使用try-Catch!-设置一个标志(数组)并检查任何后续测试(如果
    设置为失败,然后跳过测试。
    试一试{
    var pageTitle=await ndp.currtTitle();
    等待pageTitle.should.equal('Google');
    }
    捕获(e){
    testStatus[0]=“失败”
    assert.fail('由于错误-->'+e,第一次测试失败);
    }
    如果(testStatus[0]=“失败”)
    {
    这个。skip();
    }
    您也可以在同一模块中的任何其他测试中检查此标志,
    testStatus[0],testStatus[1]等具有不同的状态。使用
    柜台等。。。
    
    谢谢,我理解测试应该是独立的,这是我们为单元测试所做的,但我们也在用Mocha编写端到端测试,这就是为什么我问这个关于依赖步骤的问题。好吧,据我所知,当一个测试直接失败时,你不能告诉Mocha让其他测试失败。但作为一种解决方法,您可以使用在第一次测试期间更新的布尔变量,如果第一次测试失败,您可以在所有引发错误的相关测试之前添加一个
    块。@AlisterScott我遇到这个问题时,也有同样的需要,需要为端到端测试一步一步地运行测试。你发现了吗?@rhlsthrm我们写了一个叉子,允许这样做,摩卡咖啡不会接受,但我们还是用它:我现在用的是摩卡咖啡步骤()-这里的完整解释:太棒了!正是我需要的+1.
    Use try Catch! - Set a flag (array) and check in any subsequent tests if it 
    is set to Fail then skip the test.
    
    try{
    var pageTitle = await ndp.currTitle();
    await pageTitle.should.equal('Google');  
    }
    catch (e) {
    testStatus[0] === 'Fail'
    assert.fail('1st Test Failed due to error --> ' +e);
    }
    <<Subsequent Test>>
    
    if (testStatus[0] === 'Fail')
    {
        this.skip();
    }
    
    you can also check this flag for any other test too in the same module, 
    testStatus[0], testStatus[1] etc wt different status. Make it dynamic using 
    a counter etc...