본문 바로가기

SearchDeveloper/애플리케이션테스트

[Postman] Runner 와 newman 으로 response 파일에 쓰기

POSTMAN

API 테스트 툴인 Postman에서도 Jmeter 처럼 API 반복 호출 기능을 제공하여 퍼포먼스 및 부하 테스트에 활용할 수 있다. UI 로 하거나 커맨드라인으로 하는 2가지 방법이 있는데, UI는 Runner 라는 기능으로, 커맨드라인은 newman 이라는 프로그램으로 실행한다. 이제 보자!

1️⃣ 첫 번째 방법: Runner

Runner는 Postman 상단에 위치해있다.

(2023.07.16) 버전 10에서는 runner 가 우측 하단에 있다. 

version 10  runner 위치

 

 

  • 왼쪽 상단 창: Collection 목록
  • Environment: 포스트맨 환경 변수
  • Iterations: URL 호출 횟수
  • Delay: 한 번 호출 완료 후 기다리는 시간 (ms)
  • Data: 호출하면서 변경될 데이터
  • 오른쪽 창: 호출할 URL 선택

 

Data에 들어가는 csv 내용을 보자

test.csv

seed
쿠팡
신협
카카오
  • 첫째 행에는 환경변수명을 넣어준다.
  • API 는 http://127.0.0.1:8080/esf/test?seed={{seed}} 이고 더블 중괄호로 환경변수를 지정한다. 중괄호 안의 seed 가 환경변수의 이름이다.

그리고 하단에 Run 버튼을 누르면, 테스트가 실행된다.

 

이게 기본적인 Runner 사용법이다.

 

이제, 저 테스트 결과 화면에 Resonse body를 뿌려보자

알트탭을 눌러 포스트맨 컬렉션 창으로 돌아가, Tests 탭을 클릭한다.

그리고 에디터에 다음 코드을 입력 후 ctrl+s 를 눌러 저장해준다.

pm.test(responseBody, true)

 

 

다시 Runner 창으로 돌아가 테스트를 실행해보면,

아주 잘 보이쥬~? 크하하하하

이제 상단 Export Results 버튼을 클릭해 결과를 파일로 export 하면,

결과 파일 중 일부

Runner 를 사용해 응답 메시지를 파일로 내보내기 성공이다.

 

 

2️⃣ 두 번째 방법: newman

newman 이라는 프로그램을 cli에서 실행해 결과를 파일에 쓰는 방식이다. UI 보다 복잡한 건 인정하지만, 결과 내용에 대해 다양하게 커스터마이징 가능한 것이 newman 의 장점이다.

 

newman 설치 부터 하자!

https://learning.postman.com/docs/running-collections/using-newman-cli/installing-running-newman/

 

Installing and running Newman | Postman Learning Center

Installing and running Newman: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

learning.postman.com

npm 깔려있으면 한 방에 설치 가능

npm install -g newman

 

 

이제 newman 실행 + 결과 파일에 쓰기 행동을 수행할 자바스크립트를 하나 만든다.

writeToDisk_test.js

const newman = require('newman');
const fs = require('fs');

printError = function(error) {
    if(error) console.error(error);
}

/* 1. newman 테스트 수행 */
newman.run({
    collection: '../test_postman_runner.postman_collection.json',
    environment: '../127.0.0.1-8080.postman_environment.json',
    reporters: 'cli',
    delayRequest: 1000,
    iterationCount: 3,
    iterationData: '../test.csv'
}).on('request', function(error, args) {
/* 2. 수행 결과를 파일에 쓰기 */
    if(error) console.error(error);
    else {
        fs.appendFile('test_result.json', args.response.stream + '\n\n', printError)
    }
});

 

/* 1. newman 테스트 수행 */ 부분 부터 보자. 테스트를 수행하기 위한 인자를 넣는 곳 이다. 위에서 했던 Runner UI 에서 설정했던 옵션을 여기서 설정해준다.

  • delayRequest: 한 번 호출 완료 후 기다리는 시간 (ms)
  • iterationCount: URL 호출 횟수
  • iterationData: 호출하면서 변경될 데이터
  • collection: 테스트 수행할 컬렉션 export 한 파일

해당 컬렉션에서 … 버튼 클릭 후 Export 를 클릭하면 내려받아진다.

  • environment: 환경 변수 파일
    • 왼쪽 상단에 환경변수 설정 버튼 클릭 후 해당 환경변수의 Download Environment 버튼 클릭

더 다양한 옵션은 여기에서 확인할 수 있다.

Newman command options | Postman Learning Center

 

Newman command options | Postman Learning Center

Newman command options: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

learning.postman.com

 

 

/* 2. 수행 결과를 파일에 쓰기 */ 부분은 요청 후에 response 부분만 'test_result.json' 에 쓴다는 뜻이다.

그리고 커맨드창에서 다음 커맨드로 테스트를 실행한다.

node writeToDisk_test.js newman

 

간단한 통계와 함께 테스트는 끝이 났고,

width="100%","caption":"test\_result.json"}

결과 파일에 응답 메시지가 잘 써졌다.

 

참고

https://medium.com/apis-with-valentine/postman-how-to-write-files-to-disk-5ee398624a42

https://stackoverflow.com/questions/39371424/how-can-i-view-responses-in-postman-collection-runner

 

[Postman] Runner 와 newman 으로  response 파일에 쓰기

 

 

 

글 읽어주셔서 언제나 감사합니다. 좋은 피드백, 개선 피드백 너무나도 환영합니다.

'SearchDeveloper > 애플리케이션테스트' 카테고리의 다른 글

TestContainers 설명  (0) 2022.08.02
Junit5 애노테이션 설명  (0) 2022.07.24
Junit5 Assertions / Assumptions 설명  (0) 2022.07.24
Mockito 설명  (0) 2022.07.24