Background 시나리오

Seunghoon Yoo·2024년 3월 31일
post-thumbnail

개요

  • behave 프레임워크를 이용한 테스트 자동화를 설계하다 보면, 1개의 시나리오를 수행할 때마다 브라우저가 자동 실행 및 종료된다.
  • 다음 시나리오를 수행할 때 자연스럽게 캐시가 지워진 새로운 브라우저를 실행하는 데, 이는 이전 시나리오에 사전 조건이 있다면 불편함을 초래할 수 있다.
  • 가령, 아래와 같은 상황을 말한다.
    • 상황 1 : 로그인 이후에 상황을 테스트해야 한다.
    • 상황 2 : 브라우저가 켜질 때마다 창을 최대화하여 엘리먼트가 로컬 화면에 보여져야 한다.
  • 이같은 불편함을 줄이고자 behave 프레임워크에는 Background 시나리오가 존재한다.

Background 시나리오를 사용하기 전의 시나리오 기술

feature: NAVER 내 계정 관리
	
	Scenario Outline: 로그인 하기
		Given launch the NAVER homepage with chromebrowser
		When input the "<id>" and "<password>"
		And click the login button
		Then navigate to the NAVER main page
		Examples:
			|id|password|
			|test@naver.com|Password123!|

	Scenario Outline: 내 프로필 확인하기
		Given launch the NAVER homepage with chromebrowser
		When input the "<id>" and "<password>"
		And click the login button
		Then navigate to the NAVER main page
		And check the my profile info
		Then my profile info is displayed
		Examples:
			|id|password|
			|test@naver.com|Password123!|

	Scenario Outline: 메일함 이동하기
		Given launch the NAVER homepage with chromebrowser
		When input the "<id>" and "<password>"
		And click the login button
		Then navigate to the NAVER main page
		And click the my mail inbox tab
		Then navigate to the NAVER mail inbox page
		Examples:
			|id|password|
			|test@naver.com|Password123!|
		
	...
  • Background 시나리오를 사용하지 않고 위와 같은 시나리오를 기술한다면, 1개의 시나리오를 수행할 때마다 로그인을 해줘야 하므로, 중복 스텝이 계속해서 누적되게 된다.
  • 이로 인해 시나리오 가독성이 떨어지고, 관리도 어려워지게 된다.
  • 따라서 Background 시나리오를 별도로 구성하고, 시나리오 수행할 때마다 Background에 있는 스텝을 수행할 수 있도록 한다.

Background 시나리오를 적용한 후의 시나리오 기술

feature: NAVER 내 계정 관리

	Background: 사전 조건 시나리오 수행
		Given launch the NAVER site with chromebrowser
		And input the "id" and "pasword"
		And click the login button

	Scenario: 로그인 하기
		Then navigate to the NAVER main page

	Scenario: 내 프로필 확인하기
		When check the my profile info
		Then my profile info is displayed
	
	Scenario: 메일함 이동하기
		When click the my mail inbox tab
		Then navigate to the NAVER mail inbox page
  • 모든 시나리오의 사전 조건은 “로그인” 이므로, Background 시나리오에 로그인 스텝을 정의한다.
  • 이로 인해 시나리오를 수행할 때마다 Background 내의 스텝을 실행하고, 이후에 실제 정의된 시나리오들을 순차적으로 수행한다.
  • 중복된 시나리오를 제거하였기 때문에 가독성이 좋아졌다.

Background 스텝 작성

  • Background 시나리오에 들어가는 모든 스텝들은 given 으로 정의된다. 보기에는 And 로 작성되어 있어도 실제 step 파일에서는 Given 데코레이터를 정의해 주어야 한다. 위 시나리오를 정의한 commonSteps.py 를 예로 들면 :
from behave import given

@given('launch the NAVER site with chromebrowser')
def step_impl(context):
	context.mainpage.get_homepage()

@given('input the "{id}" and "{password}"')
def step_impl(context):
	context.mainpage.login_homepage()

@given('click the login button')
def step_impl(context):
	context.mainpage.login_button_click()
  • 위와 같이 Background 시나리오는 보통 commonSteps.py 모듈에서 given 키워드를 통해 정의한다.
profile
QA Engineer

0개의 댓글