我正在尝试在mocha中编写一些测试,我将通过BrowserStack在多个浏览器中实现自动化。我没有为每个浏览器重复test.it
代码,而是希望保持代码DRY并在函数中包含几个test.it
块,并在test.describe
内调用该函数,就像这样(实际的测试细节并不重要);
function runTests(driver) {
test.it('form works', function() {
var result = testForm(driver, '#formId', 'test@test.com');
return 'Expected Result' === result;
});
}
test.describe('Tests for IE 9', function() {
var driver;
test.before(function() {
var capabilities = {
'browser' : 'IE',
'browser_version' : '9.0'
};
driver = setupDriver(capabilities);
});
runTests(driver);
test.after(function() { driver.quit(); });
});
test.describe('Tests for IE 10', function() {
var driver;
test.before(function() {
var capabilities = {
'browser' : 'IE',
'browser_version' : '10.0'
};
driver = setupDriver(capabilities);
});
runTests(driver);
test.after(function() { driver.quit(); });
});
但由于此代码是异步的,因此在调用driver
时未定义runTests()
,因此失败。我如何构建它以便我不必为每个浏览器重复test.it
块?
答案 0 :(得分:1)
我会像这样构建它:
function makeTests(kind, version) {
describe("Tests for " + kind + " " + version, function () {
var driver;
before(function() {
var capabilities = {
'browser' : kind,
'browser_version' : version
};
driver = setupDriver(capabilities);
});
it('form works', function() {
var result = testForm(driver, '#formId', 'test@test.com');
if ('Expected Result' !== result)
throw new Error("failed!");
});
// Add more tests here...
after(function() { driver.quit(); });
});
}
makeTests('IE', '9');
makeTests('IE', '10');
我已在上面的代码中直接使用describe
和it
而不是test.describe
等。我想您的测试环境包含Mocha调用或您正在使用接口我不熟悉。
答案 1 :(得分:0)
function runTests(driver, done) {
test.it('form works', function(done) {
var result = testForm(driver, '#formId', 'test@test.com');
return 'Expected Result' === result;
done();
});
}
test.describe('Tests for IE 9', function() {
var driver;
test.before(function(done) {
var capabilities = {
'browser' : 'IE',
'browser_version' : '9.0'
};
driver = setupDriver(capabilities);
done();
});
runTests(driver, done);
test.after(function(done) {
driver.quit();
done();
});
});
的更多信息