Cypress의 Before, BeforeEach

murkgom·2021년 9월 23일
1

Describe, Context, It

  • Describe : 대상 선언(누가)
  • Context : Case 언급(어떤 상황일 때)
  • It : 결과 예상(어떻게 될 것이다)
describe('A함수는', () => {
    context('b가 c일 때', () => {
        it('x는 홀수값이다', () => {
      		//...
        });
    
      	it('x는 3의 배수이다', () => {
      		//...
      	});
    });
});

Before와 BeforeEach(After와 After Each)

코드

describe('describe1', () => {
    before('before', () => console.log('describe1, before'));
    beforeEach('beforeEach', () => console.log('describe1, beforeEach'));

    context('context1', () => {
        before('before', () => console.log('context1, before'));
        beforeEach('beforeEach', () => console.log('context1, beforeEach'));

        it('it1', () => console.log('it11'));
        it('it2', () => console.log('it12'));
    });

    context('context2', () => {
        before('before', () => console.log('context2, before'));
        beforeEach('beforeEach', () => console.log('context2, beforeEach'));

        it('it1', () => console.log('it21'));
        it('it2', () => console.log('it22'));
    });
});

describe('describe2', () => {
    before('before', () => console.log('describe2, before'));
    beforeEach('beforeEach', () => console.log('describe2, beforeEach'));

    context('context21', () => {
        before('before', () => console.log('context21, before'));
        beforeEach('beforeEach', () => console.log('context21, beforeEach'));

        it('it21', () => console.log('it21'));
    });
});

결과

//before는 예상한 타이밍에 호출됨
describe1, before		
context1, before
//beforeEach는 자기 하위 it 블럭이 실행될 때마다 순차적으로 반복
describe1, beforeEach
context1, beforeEach
it11
describe1, beforeEach
context1, beforeEach
it12

context2, before
describe1, beforeEach
context2, beforeEach
it21
describe1, beforeEach
context2, beforeEach
it22

describe2, before
context21, before

describe2, beforeEach
context21, beforeEach
it21

0개의 댓글