最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I make tests dependent using nested test execution in mocha - Stack Overflow

programmeradmin0浏览0评论

I have a simple example of two tests (A,B) where B depends on A being run.

If I am using Mocha, I can nest test B within 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 );
            } );
        } );
    } );
} );

But both A and B are run even if A fails.

How is this any different than not using nesting?

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

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

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

Is there any way to skip B if A fails?

I have a simple example of two tests (A,B) where B depends on A being run.

If I am using Mocha, I can nest test B within 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 );
            } );
        } );
    } );
} );

But both A and B are run even if A fails.

How is this any different than not using nesting?

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

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

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

Is there any way to skip B if A fails?

Share Improve this question asked Mar 15, 2016 at 3:22 Alister ScottAlister Scott 3,6851 gold badge26 silver badges41 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 11

The easiest way to achieve this is to use 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() {
  });
 
});

Full blog post here.

Ok, there are two questions, so I'll try to answer both.

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

    Generally you should write tests that are not dependent on each other.

    Sometimes tests depend on having certain setup or state before they're able to be run properly though, in which case it's best to do the setup in a before() or beforeEach() block. If either of these blocks fail, the tests after them are not run. Thus you can throw an error in those blocks, when your build up is in a way that you know none of the tests inside that describe block are not going to work.

    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?

    Nesting B inside a describe block allows you to use a different setup for B1 than for A1, while still inheriting some of A's setup.

    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?

This pattern has been working very fell for me:

var itCanLogin;

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

  itCanLogin = true;
});

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

   ...
})

Could also use assert(itCanLogin) but this.skip() helps to keep output cleaner by not producing stacktraces & errors - easier to notice the source of problem this way.

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...
发布评论

评论列表(0)

  1. 暂无评论