【问题标题】:How do I make tests dependent using nested test execution in mocha如何在 mocha 中使用嵌套测试执行使测试依赖
【发布时间】:2016-03-15 03:22:00
【问题描述】:

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

如果我使用的是 Mocha,我可以在 A 中嵌套测试 B:

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?

【问题讨论】:

    标签: javascript mocha.js


    【解决方案1】:

    最简单的方法是使用mocha-steps

    describe('my smoke test', function() {
     
      step('login', function() {
      });
     
      step('buy an item', function() {
        throw new Error('failed');
      });
     
      step('check my balance', function() {
      });
     
      xstep('temporarily ignored', function() {
      });
     
    });
    

    完整的博文here

    【讨论】:

      【解决方案2】:

      好的,有两个问题,所以我会尽量回答。

      1. 如果A失败,有什么方法可以跳过B?

        通常你应该编写不依赖于彼此的测试。

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

        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);
              });
            });
          });
        });
        
      2. 如果我使用的是 Mocha,我可以在 A 中嵌套测试 B
        [...]
        这与不使用嵌套有何不同?

        在描述块中嵌套 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);
            });
          });
        });
        

      【讨论】:

      • 谢谢,我知道测试应该是独立的,这是我们为单元测试所做的,但我们也在 Mocha 中编写端到端测试,这就是为什么我问这个关于依赖的问题步骤。
      • 好吧,据我所知,当一个测试直接失败时,你不能告诉 Mocha 让其他测试失败。但作为一种解决方法,您可以使用在第一次测试期间更新的布尔变量,并在所有引发错误的依赖测试前面添加一个 before 块,如果第一次测试失败。
      • @AlisterScott 我遇到了同样需要的问题,即逐步运行测试以进行端到端测试。你有想过这个吗?
      • @rhlsthrm 我们写了一个允许这样做的分叉,它不会被 Mocha 接受但我们仍然使用它:github.com/Automattic/mocha
      【解决方案3】:

      如果A失败,有什么方法可以跳过B?

      这种模式对我来说非常糟糕:

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

      也可以使用 assert(itCanLogin),但 this.skip() 通过不产生堆栈跟踪和错误来帮助保持输出更清晰 - 这样更容易发现问题的根源。

      【讨论】:

      【解决方案4】:
      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...
      

      【讨论】:

        猜你喜欢
        • 2020-03-23
        • 1970-01-01
        • 2012-04-25
        • 2012-07-13
        • 1970-01-01
        • 1970-01-01
        • 2016-05-18
        • 2018-04-12
        • 1970-01-01
        相关资源
        最近更新 更多