如何正确地将测试套件放入 mocha 的函数中?
How to correctly put test suites in functions in mocha?
我是 mocha 的新手,整个夏天我一直在使用它为 Web 应用程序中的功能编写动态测试用例。基本上我可以发送一个请求并获取我可以发送的所有可能的值。所以我一直在使用它来循环并测试所有内容。为了使这更容易,我创建了一个带有测试套件的函数和一些应该让它 运行 所有测试的变量。它有点工作,我的意思是它正确地 运行s 所有测试。但是,它会等到其他所有内容由于某种原因 运行 之后,这会导致我正在尝试做的事情出现一些问题。
我已经将我正在尝试做的事情简化为一些非常基本的代码:
function test(){
//Testing suite to test through a certain functionality using different variables
describe('inner',function(){
it('test3',function(){
console.log('\ninner test');
});
});
}
function entityLoop() {
describe('outer',function(){
//Get field values from an http request
it('test1',function(){
console.log('\ntest1');
});
it('test2',function(){
console.log('\ntest2');
});
//This MUST run after the other two tests, as those tests get the values needed to run the following tests
//I know it's typically bad practice to make dependent tests, but that's how we're dynamically creating the tests
after(function(){
//Use field values in a testing suite
console.log('after test');
test();
})
})
}
describe("Overall loop",function(){
//Need to loop through a testing suite, changing some variables each time
for(var i = 0; i < 5;i++){
entityLoop();
}
});
这是我从中得到的输出:
test1
test2
after test
test1
test2
after test
test1
test2
after test
test1
test2
after test
test1
test2
after test
inner test
inner test
inner test
inner test
inner test
Process finished with exit code 0
不明白为什么最后连续输出5次'inner test',而不是每次都在'after test'之后输出。非常感谢任何输入。
您的代码从 after
挂钩内部调用 describe
和 it
。它不会使测试套件崩溃,但它不是 Mocha 明确支持的使用模式。因此它不会执行您期望的操作。
你得到你得到的结果是因为 describe(name, fn)
这样做:
Create a test suite named name
. Add it to the suite that is currently being defined. Push the new suite on the suite stack. Call fn
. Pop the suite stack.
"suite that is currently being defined" 是套件堆栈顶部的那个。 Mocha 维护着一堆套件。它最初包含 Mocha 创建的用于包含所有测试的顶级隐式套件。 (所以你可以有一个根本不使用 describe
的测试文件,它仍然可以工作,因为所有测试都将在这个隐式顶级套件上定义。)
并且 it(name, fn)
这样做:
Create a test named name
and with callback fn
. Add it to the suite currently being defined.
如果你在脑子里追踪你的代码在 运行s 时发生了什么,你会发现当 after
运行s 中的代码时,"suite currently being defined" 是 Mocha 默认创建的顶级隐式套件,用于包含所有测试。 (它没有名称。如果将 after(function () { console.log("after overall loop"); })
添加到顶层 describe
,您会看到所有 inner test
输出都出现在 after overall loop
输出之后。)
因此您的 after
挂钩正在向顶级隐式套件添加新测试。它们必然 运行 在你所有的测试之后,因为它们是在套件的末尾添加的。
我是 mocha 的新手,整个夏天我一直在使用它为 Web 应用程序中的功能编写动态测试用例。基本上我可以发送一个请求并获取我可以发送的所有可能的值。所以我一直在使用它来循环并测试所有内容。为了使这更容易,我创建了一个带有测试套件的函数和一些应该让它 运行 所有测试的变量。它有点工作,我的意思是它正确地 运行s 所有测试。但是,它会等到其他所有内容由于某种原因 运行 之后,这会导致我正在尝试做的事情出现一些问题。 我已经将我正在尝试做的事情简化为一些非常基本的代码:
function test(){
//Testing suite to test through a certain functionality using different variables
describe('inner',function(){
it('test3',function(){
console.log('\ninner test');
});
});
}
function entityLoop() {
describe('outer',function(){
//Get field values from an http request
it('test1',function(){
console.log('\ntest1');
});
it('test2',function(){
console.log('\ntest2');
});
//This MUST run after the other two tests, as those tests get the values needed to run the following tests
//I know it's typically bad practice to make dependent tests, but that's how we're dynamically creating the tests
after(function(){
//Use field values in a testing suite
console.log('after test');
test();
})
})
}
describe("Overall loop",function(){
//Need to loop through a testing suite, changing some variables each time
for(var i = 0; i < 5;i++){
entityLoop();
}
});
这是我从中得到的输出:
test1
test2
after test
test1
test2
after test
test1
test2
after test
test1
test2
after test
test1
test2
after test
inner test
inner test
inner test
inner test
inner test
Process finished with exit code 0
不明白为什么最后连续输出5次'inner test',而不是每次都在'after test'之后输出。非常感谢任何输入。
您的代码从 after
挂钩内部调用 describe
和 it
。它不会使测试套件崩溃,但它不是 Mocha 明确支持的使用模式。因此它不会执行您期望的操作。
你得到你得到的结果是因为 describe(name, fn)
这样做:
Create a test suite named
name
. Add it to the suite that is currently being defined. Push the new suite on the suite stack. Callfn
. Pop the suite stack.
"suite that is currently being defined" 是套件堆栈顶部的那个。 Mocha 维护着一堆套件。它最初包含 Mocha 创建的用于包含所有测试的顶级隐式套件。 (所以你可以有一个根本不使用 describe
的测试文件,它仍然可以工作,因为所有测试都将在这个隐式顶级套件上定义。)
并且 it(name, fn)
这样做:
Create a test named
name
and with callbackfn
. Add it to the suite currently being defined.
如果你在脑子里追踪你的代码在 运行s 时发生了什么,你会发现当 after
运行s 中的代码时,"suite currently being defined" 是 Mocha 默认创建的顶级隐式套件,用于包含所有测试。 (它没有名称。如果将 after(function () { console.log("after overall loop"); })
添加到顶层 describe
,您会看到所有 inner test
输出都出现在 after overall loop
输出之后。)
因此您的 after
挂钩正在向顶级隐式套件添加新测试。它们必然 运行 在你所有的测试之后,因为它们是在套件的末尾添加的。