`before ()`와`beforeEach ()`의 차이점은 무엇입니까?
특히 사이의 차이 무엇입니까 모카 의 before()
와는 beforeEach()
? ( after()
및에 대한 동일한 질문 afterEach()
.)
나는 블록 before()
당 한 번 실행 describe()
되고 beforeEach()
테스트 당 한 번 실행 된다고 가정 합니다 ( it()
블록). 사실인가요?
그리고 언제 다른 것을 사용하기로 선택합니까?
before()
전에 한 번 실행되는 모든 A의 테스트를 describe
after()
한 번 실행 한 후 모든 A의 테스트 describe
beforeEach()
전에 실행되는 각 A의 테스트가 describe
afterEach()
실행 된 이후 각 A의 시험describe
사용하려는 것은 실제 테스트에 따라 다릅니다.
자, 긴 설명을 위해. mocha -R min
이것을 실행 하면 :
describe("top", function () {
before(function () {
console.log("top before");
});
after(function () {
console.log("top after");
});
beforeEach(function () {
console.log("top beforeEach");
});
afterEach(function () {
console.log("top afterEach");
});
it("test1", function () {
console.log("top test1");
});
describe("sublevel", function() {
before(function () {
console.log("sublevel before");
});
after(function () {
console.log("sublevel after");
});
beforeEach(function () {
console.log("sublevel beforeEach");
});
afterEach(function () {
console.log("sublevel afterEach");
});
it("test1", function () {
console.log("sublevel test1");
});
it("test2", function () {
console.log("sublevel test2");
});
});
it("test2", function () {
console.log("top test2");
});
});
다음과 같은 내용이 표시됩니다 (관련없는 출력은 생략했습니다).
top before
top beforeEach
top test1
top afterEach
top beforeEach
top test2
top afterEach
sublevel before
top beforeEach
sublevel beforeEach
sublevel test1
sublevel afterEach
top afterEach
top beforeEach
sublevel beforeEach
sublevel test2
sublevel afterEach
top afterEach
sublevel after
top after
하위 수준에서 각 테스트 전후에 실행되는 작업을 살펴보면 놀라운 것은 최상위 수준과 하위 수준 의 콜백 이 모두beforeEach
호출 된다는 것 입니다. 에 대한 똑같은 afterEach
.
Some are also surprised by the sequence sublevel before
, top beforeEach
, sublevel beforeEach
. They think that all the hooks in an outer scope should execute before all the hooks in an inner scope, so they expect the sequence: top beforeEach
, sublevel before
, sublevel beforeEach
. However, the order in which Mocha executes the hooks makes complete sense: a before
hook is meant to set the stage for a group of tests, whereas a beforeEach
test is for each individual tests. When Mocha executes a test, all the before
and the beforeEach
hooks that were set in the describe
that contains it, and all the ancestors of that describe
apply to the test. Mocha will execute each before
hook from the outermost scope to the innermost, and all beforeEach
hook from the outermost scope to the innermost. However, all before
hooks that apply are executed before any beforeEach
hook. This explains the order above: sublevel before
executes before top beforeEach
because it is a before
hook. And with after
and afterEach
, the same logic applies but the the order is reversed: all afterEach
hooks that apply are executed before any after
hook.
Also notice that Mocha does not care about how I ordered my it
calls relative to the describe
call in the top level describe
. It executes top test1
, top test2
and then the sublevel tests, even though the order I gave was top test1
, then the sublevel tests and then top test2
.
What you want to use among before
, beforeEach
, etc. really depends on the specifics of your tests. If you need to setup a mock object or data structure and this object or structure can be reused by all the tests in a single describe
, you can use before
to set it up, and after
to tear it down. This could be the case if you are doing read-only tests on the structure. If all your tests only read it, then there is no need to create it over and over. If each test in your describe
needs a new copy of the structure because each test is modifying the structure then you should use beforeEach
to create the structure anew for each test and then afterEach
if you need to tear it down cleanly. Doing this ensures test isolation: each test starts from a known state and does not depend on the presence or absence of a previous test to succeed.
참고URL : https://stackoverflow.com/questions/21418580/what-is-the-difference-between-before-and-beforeeach
'developer tip' 카테고리의 다른 글
플러그인을 사용하지 않고 jQuery 여유 기능 (0) | 2020.10.21 |
---|---|
Python : 파일을 stdout으로 인쇄 (0) | 2020.10.21 |
ImageMagick : 변환 실행 중 오류 : 변환 : 글꼴을 읽을 수 없음 (0) | 2020.10.21 |
클래스 멤버를 사용한 C ++ 콜백 (0) | 2020.10.21 |
drop table과 truncate table의 차이점은 무엇입니까? (0) | 2020.10.20 |