我使用Nightwatch和Mocha一起编写一些自动(动态)测试。动态,我想动态加载一些数据,用于循环测试用例。见下面的代码。我正在努力解决三个问题:
请注意,我不需要将Mocha与Nightwatch一起使用,但我开始沿着使用Mocha动态测试功能(新TestCase)的路径开始,但我也无法实现这一功能。
以下是我的代码的缩小版。
var testArray = [];
describe('createArray', function() {
before(function(client, done) {
// do some async operations within a loop and create testArray entries
// loop {
testArray.push(foo); // let's say I end up with 3 items.
// }
done();
});
it('foo', function(client) {
console.log(testCaseArray);
testCaseArray.forEach(function(testCase) {
client.url("http://www.google.com"); // let's say here I would eventually want to have something like client.url("http://....." + testCase.value)
});
});
});
提前致谢。
答案 0 :(得分:2)
你是对的,你需要异步处理测试用例。可以这样做:
it('foo', function(client, done) {
var testsLeft = testCaseArray.length;
function onTestComplete() {
testsLeft--;
if (testsLeft === 0)
done();
}
testCaseArray.forEach(function(testCase) {
client.url("http://" + testCase.value, onTestComplete);
});
});
我不熟悉Nightwatch,所以你可能需要像这样使用onTestComplete
:
client.url("http://" + testCase.value).end(onTestComplete);
另外,我意识到这是处理异步回调的一种非常冗长的方式。通常对于这种情况,使用类似CallbackManager的内容会很有帮助,因此您无需手动跟踪剩余的测试数量。
您还可以根据Mocha documentation动态生成测试。