I have a series of tests I'm trying to run on an Express REST API, emphasis on the word series, because they need to be sequential to some degree. Let me explain:
Tests, in order:
Note that these steps really only make any sense if they happen in order, and yet Mocha doesn't seem to run tests in order. I could make one test that runs Step 1, then another that runs Steps 1-2, then another that runs Steps 1-3, and so on, but that's definitely not DRY. I could also try to set up a chain of before
and after
s, but that doesn't seem up to conventions.
Is there a proper way of running Mocha tests checking sequential steps in an process?
I'm trying to get rid of this pattern of just adding additional steps to repeated tests...
describe('Api', () => {
it('should be accessible', (done) => {
// Try to connect:
Api.connect((error, conn) => {
done(error);
});
});
it('should connect and authenticate properly', (done) => {
// Try to connect:
Api.connect((error, api) => {
if (error) done(error);
// Then try to authenticate:
api.authenticate(TEST_AUTH_CREDENTIALS, (error, conn) => {
done(error);
});
});
});
it('should allow for data to be written to the data store', (done) => {
// Try to connect:
Api.connect((error, api) => {
if (error) done(error);
// Then try to authenticate:
api.authenticate(TEST_AUTH_CREDENTIALS, (error, conn) => {
if (error) done(error);
// Then try to write data:
conn.write(generateTestData(), (error, res) => {
done(err);
});
});
});
});
it('should allow for written data to be read from the data store', (done) => {
// Try to connect:
Api.connect((error, api) => {
if (error) done(error);
// Then try to authenticate:
api.authenticate(TEST_AUTH_CREDENTIALS, (error, conn) => {
if (error) done(error);
let testData = generateTestData();
// Then try to write data:
conn.write(testData, (error, res) => {
if (error) done(error);
// Then try to read data:
conn.readLast((error, res) => {
if (error) done(error);
assert.equal(testData, res);
});
});
});
});
});
});
答案 0 :(得分:0)
Mocha测试是连续运行的,但是,如果出现故障,它会继续进行下一次测试,因此看起来它们并行运行。
Mocha测试连续运行,允许灵活和准确 报告,同时将未捕获的异常映射到正确的测试用例
据说你可以在第一次失败后停止,但我无法让这个标志起作用:
-b, - 首次测试失败后保释 -
测试之间传递状态的示例:
describe('Api', () => {
var api, conn;
it('should be accessible', (done) => {
// Try to connect:
Api.connect((error, new_api) => {
if (error) {done(error);} else {
api = new_api
done();
}
});
});
it('should authenticate properly', (done) => {
// Then try to authenticate:
api.authenticate(TEST_AUTH_CREDENTIALS, (error, new_conn) => {
if (error) {done(error);} else {
conn = new_conn;
done();
}
});
});
it('should allow for data to be written to the data store', (done) => {
// Then try to write data:
conn.write(generateTestData(), (error, res) => {
if (error) {done(error);} else {
done();
}
});
});
});