🔹PostMan

[Postman] Scripts > Snippets 예제 분석

terranbin 2025. 1. 31. 12:04
728x90
SMALL

다양한 "코드 Snippets" 이 있다.
각각의 의미, 코드 예시를 한번 들어보자

 

Postman 페이지

  • 설명 / 예제 / 전제 조건 작성
  • 변수의 정의나, 각각의 차이가 궁금하면 가장 하단을 참고하자
Snippets 설명 예제 코드 전제 조건
Get an environment variable 환경 변수 값 가져오기 pm.environment.get("variable_key"); variable_key가 환경 변수로 설정되어 있어야 함
Get a global variable 글로벌 변수 값 가져오기 pm.globals.get("variable_key"); variable_key가 글로벌 변수로 설정되어 있어야 함
Get a variable 변수 값 가져오기 pm.variables.get("variable_key"); 변수가 Postman 실행 환경에서 존재해야 함
Get a collection variable 컬렉션 변수 값 가져오기 pm.collectionVariables.get("variable_key"); variable_key가 컬렉션 변수로 설정되어 있어야 함
Set an environment variable 환경 변수 설정 pm.environment.set("variable_key", "value"); Postman 환경 변수를 사용할 수 있어야 함
Set a global variable 글로벌 변수 설정 pm.globals.set("variable_key", "value"); Postman 글로벌 변수를 사용할 수 있어야 함
Set a collection variable 컬렉션 변수 설정 pm.collectionVariables.set("variable_key", "value"); 컬렉션 변수를 사용할 수 있어야 함
Clear an environment variable 환경 변수 삭제 pm.environment.unset("variable_key"); variable_key가 환경 변수로 존재해야 함
Clear a global variable 글로벌 변수 삭제 pm.globals.unset("variable_key"); variable_key가 글로벌 변수로 존재해야 함
Clear a collection variable 컬렉션 변수 삭제 pm.collectionVariables.unset("variable_key"); variable_key가 컬렉션 변수로 존재해야 함
Send a request 추가 API 요청 전송 pm.sendRequest({ url: 'https://postman-echo.com/get?test=data', method: 'GET' }, function (err, res) { console.log(res.json()); }); 네트워크 연결이 가능해야 함
Status code: Code is 200 상태 코드 200 확인 pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); API 응답이 200 상태 코드를 반환해야 함
Response body: Contains string 본문 문자열 포함 확인 pm.test("Body contains string", function () { pm.expect(pm.response.text()).to.include("success"); }); 응답 본문에 특정 문자열이 포함되어 있어야 함
Response body:
JSON value check
JSON 키 확인 pm.test("JSON contains key", function () { pm.expect(pm.response.json()).to.have.property("key"); }); API 응답이 JSON 형식이어야 하며 특정 키가 포함되어 있어야 함
Response body:
Is equal to a string
본문 문자열 일치 확인 pm.test("Body equals string", function () { pm.expect(pm.response.text()).to.equal("expectedValue"); }); API 응답 본문이 특정 값과 일치해야 함
Response headers:
Content-Type header check
헤더 값 확인 pm.test("Content-Type is JSON", function () { pm.response.to.have.header("Content-Type", "application/json"); }); API 응답이 'Content-Type' 헤더를 포함해야 함
Response time is less than 200ms 응답 시간 200ms 확인 pm.test("Response time is below 200ms", function () { pm.expect(pm.response.responseTime).to.be.below(200); }); API 응답 시간이 200ms 이하여야 함
Status code:
Successful POST request
POST 요청 성공 확인 pm.test("Successful POST", function () { pm.response.to.have.status(201); }); API 응답이 201 상태 코드를 반환해야 함
Status code:
Code name has string
상태 코드 문자열 확인 pm.test("Status code name has string", function () { pm.expect(pm.response.status).to.include("OK"); }); API 응답 상태 코드 메시지가 특정 문자열을 포함해야 함
Response body:
Convert XML body to a JSON Object
XML을 JSON 변환 let jsonData = xml2Json(pm.response.text()); console.log(jsonData); API 응답이 XML 형식이어야 함
Use Tiny Validator for JSON data JSON 스키마 검증 pm.test("Validate JSON schema", function () { pm.expect(tv4.validate(pm.response.json(), schema)).to.be.true; }); API 응답이 JSON 형식이어야 하며, 검증할 스키마가 정의되어 있어야 함

 


  • Global
    • Global 변수는 전역 변수로 어디서나 사용 가능한다
      ex. Collection, Envrionment, Request, Test Script
  • Collection
    • Collection 변수는 Collection의 Request 전체에서 사용할 수 있고 Environment과는 무관한다
      환경이 하나인 경우에는 Collection 변수를 사용하는 게 적합하다
  • Environment
    • Environment 변수르르 사용하면 로컬 개발 환경, 테스트, 프로덕션 환경 등 다양한 환경으로 작업 범위를 지정할 수 있다
  • Data
    • Data 변수는 newman 이나 Collection Runner를 실행할 때 사용할 수 있는 데이터 집합을 정의할 기 위해 외부 CSV, JSON 파일에서 가져온다
    • Data 변수는 현재 값을 가지며, 요청 또는 컬렉션 실행 이후에는 지속되지 않는다
  • Local
    • Local 변수는 Test Script에서 생성하는 임시 변수이다

 

LIST