如何在 mocha 中使用嵌套测试执行使测试依赖

How do I make tests dependent using nested test execution in mocha

我有两个测试 (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和B都是运行即使A失败了。

这与不使用嵌套有何不同?

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

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

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

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

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

  1. Is there any way to skip B if A fails?

    通常你应该编写相互不依赖的测试。

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

    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. If I am using Mocha, I can nest test B within A
    [...]
    How is this any different than not using nesting?

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

Is there any way to skip B if A fails?

这个模式对我来说一直很有效:

var itCanLogin;

it('Can login', function() {
  ...

  itCanLogin = true;
});

it('Can Logout', function(){
   if(!itCanLogin) this.skip()

   ...
})

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

最简单的方法是使用 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() {
  });
 
});

完整博客 post here.

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...