Assertion in Postman

Dahun Yoo·2020년 11월 28일
0

Introduce to Postman

목록 보기
8/16
post-thumbnail

앞서 간단한 테스트 기능을 소개해드렸는데요, Postman에서는 여러가지 response body에 대한 assertion기능을 제공하고 있습니다.

Response Type

API는 설계 서비스의 방식에 따라, 여러가지 형태를 response body로 제공할 수도 있습니다.

  • json : pm.response.json()
  • XML : xml2Json(responseBody)
  • HTML : cheerio(pm.response.text())
  • Plain-text : pm.response.text()
  • CSV : csv-parse/lib/sync

보통은 json format으로 return합니다만 오래된 서비스이면 보통 XML이 많을 것 입니다.
XML형식을 Postman에서는 json형으로 변환하여 테스트할 수 있도록 해줍니다.

Chai Library

Postman의 javascript에서 테스팅기능은, Chai Library 기반으로 제공됩니다.
자세한 내용은 아래 링크를 확인해주세요!
https://www.chaijs.com/api/bdd/

expect([]).to.be.an('array').that.is.empty;
chai library는 읽기 쉬운 코드를 제공합니다.

Assertion

기본적으로는

pm.test("Your test name", fucntion() {
	pm.expect(100).to.eql(101, 'must equal 101');
});

와 같은, javascript의 함수형태를 띄는, 테스트코드를 작성하게 됩니다.
안에 내부에서 직접 검증하는 코드를 작성할 수 있습니다.
위 코드처럼, eql()의 두번째 인자처럼 커스텀 에러메세지도 지원합니다.

pm.expect(true).to.be.true;
pm.expect(null).to.be.null;
pm.expect(undefined).to.be.undefined;
pm.expect([]).to.be.empty;
pm.expect([].length).to.eql(0);
pm.expect([1,2,3]).to.include(2);
pm.expect(2).to.be.oneOf([1,2,3]) // 1,2,3중에 2가 들어있기 때문에 true
pm.expect(‘John Doe’).to.match(/^John/) //regx를 사용하여 확인.

//-----------------------------------------------------------------------
let manufacturer;

for(let filter of jsonData.filters) { //특정값들을 반복해나가며 확인
	console.log(filter);
	if(filter.name == "Manufacturer") {
		console.log(filter);
	}
}

다양한 패턴으로 값들을 검증해볼 수 있습니다.

Header and cookies

response 의 body뿐만 아니라, 헤더와 쿠키도 테스트해볼 수 있습니다.

pm.response.headers.get(‘X-Cache’)
이렇게 response의 headers값들 중, getter를 이용하여 값을 조회해볼 수도 있고,

pm.response.to.have.header(X-Cache’);
특정 헤더가 존재하는지.

pm.expect(pm.response.headers.get(‘X-Cache’)).to.eql(‘HIT’);
또는 특정 헤더의 값이 예상값과 일치하는지(assertion)도 확인할 수 있습니다.

pm.expect(pm.cookies.has(‘sessionId’)).to.be.true;
pm.expect(pm.cookies.get(‘sessionId’)).to.eql(’ad3se3ss8sg7sg3’);

쿠키 역시 마찬가지입니다.

profile
QA Engineer

0개의 댓글