junit을 활용한 테스트 자동화에 익숙하다보니, api테스트 자동화 한다고 하면 request, response 동작을 위한 util 및 json / xml parser 등을 모두 구현해서 사용했었는데, 

그동안 왜 그랬나 싶을 정도로,, Rest-Assured는 잘 만들어진 api 테스트 자동화 도구이다. 

링크 : http://rest-assured.io/


환경 구성이나, 손 쉬운 사용법은 위 링크의 User Guide 문서를 참고하면 사용할 수 있다.

그래도 자동화 하면, 쉬운 반복 수행 및 결과 리포팅이 중요하니, 아래와 같이 구성하자.


  • 빌드 도구 : maven (or gradle)
  • 테스트 도구 : junit + rest-assured
  • 리포트 : extent-report
  • 통합 : Jenkins

환경 구성 (maven)

  <dependencies>
     <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.12</version>
     </dependency>
     <dependency>
         <groupId>org.hamcrest</groupId>
         <artifactId>hamcrest-all</artifactId>
         <version>1.3</version>
     </dependency>
     <dependency>
         <groupId>com.google.code.gson</groupId>
         <artifactId>gson</artifactId>
         <version>2.6.2</version>
     </dependency>
     <dependency>
          <groupId>io.rest-assured</groupId>
          <artifactId>rest-assured</artifactId>
          <version>3.0.2</version>
     </dependency>
     <dependency>
         <groupId>io.rest-assured</groupId>
         <artifactId>json-path</artifactId>
         <version>3.0.2</version>
     </dependency>
     <dependency>
         <groupId>io.rest-assured</groupId>
         <artifactId>xml-path</artifactId>
         <version>3.0.2</version>
     </dependency>        
     <dependency>
         <groupId>com.aventstack</groupId>
         <artifactId>extentreports</artifactId>
         <version>3.0.1</version>
     </dependency>
  </dependencies>
cs

샘플 테스트 코드 예제 

1
2
3
4
5
6
7
8
given()
    .header("Cookie""Cookie-Value")
    .contentType("application/x-www-form-urlencoded")
    .body("Body Message")
.when()
    .post("http://www.naver.com/openapi/apitest?method=abcde")
.then()
    .statusCode(200);
cs

Extent-Report 세팅

Jenkins 연동

  • Maven Project 생성 후  > Build Step에 mvn clean test 정도만 추가하면 완료
  • html publisher plugin설정 후, 생성된 extent report 파일을 설정해주면 끝
  • html report에 CSS가 jenkins 권한 등의 문제로 수행이 안된다면, 
    • 젠킨스 관리 > Script console 에서 아래 명령어를 넣어준다.
  • System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")

api 테스트 자동화를 Java 환경에서 진행한다면, rest-assured는 진짜 적극 검토해볼만하다~  좋다~



+ Recent posts