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는 진짜 적극 검토해볼만하다~  좋다~



appium으로 사내 서비스 자동화를 진행하다가, junit report나 surefire report 포맷이 별로라서, 좀 더 새로운 html report를 찾다가 발견한 extent report.


좀 더 이쁘장하고, 결과에 따라 필터링도 된다. 

다만, junit 결과를 자동으로 report로 변환하는게 아니라서, 테스트 이후 결과에 따른 로그를 직접 넣어야 된다. 


selenium이나 appium 자동화에 사용하는 사례도 구글링하면 좀 나오는거 보니, 쓸만한 것 같아 적용하기로 했고, 

실제 ui 자동화랑 api 자동화 업무에 해당 리포트를 사용하고 있다. 


우선, maven 설정을 추가하고,

<dependency>
    <groupId>com.aventstack</groupId>
    <artifactId>extentreports</artifactId>
    <version>3.1.2</version>
</dependency>


  • 테스트 시작 시점에는 createReport
  • 테스트케이스 시작시점에는 createTest 
  • 테스트 종료시점에 pass/fail/skip 의 결과를 extent report에 작성-> test.log(STATUS.PASS...)


우선 report 생성할때, 설정 내용을 반영하도록 해야 한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private static ExtentReports createReport() throws Exception {
    if(extent != nullreturn extent;
 
    String reportFile= new File("target/result.html").getAbsolutePath();
 
    htmlReporter = new ExtentHtmlReporter(reportFile);
    htmlReporter.setAppendExisting(true);
    htmlReporter.config().setChartVisibilityOnOpen(true);
    htmlReporter.config().setDocumentTitle("타이틀적용");
    htmlReporter.config().setEncoding("UTF-8");
    htmlReporter.config().setReportName("리포트제목");
 
    extent = new ExtentReports();
    extent.setSystemInfo("OS""Windows 7");
    extent.attachReporter(htmlReporter);
 
    return extent;
}
cs


테스트 시작시

1
2
3
4
5
@BeforeClass
public static void setUp() throws Exception {
    enableLoggingOfRequestAndResponseIfValidationFails();
    extent = createReport();
}
cs


케이스 실행 시작 시점

1
2
3
4
@Before
public void beforeMethod() throws Exception {
    test = extent.createTest(methodName);
}
cs

케이스 종료 시점

1
2
3
4
@After
public void afterMethod() throws Exception {
    extent.flush();
}
cs

테스트 결과에 따라 리포트 로그를 남겨야 하기에 TestWatcher를 활용.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Rule
public TestWatcher testWatcher = new TestWatcher() {
 
    @Override
    protected void failed(Throwable e, Description description) {
        test.log(Status.FAIL, e.toString());
    }
 
    @Override
    protected void succeeded(Description description) {
        test.log(Status.PASS, "SUCCESS");
    }
 
     @Override
    protected void skipped(AssumptionViolatedException e, Description description) {
        test.log(Status.SKIP, SKIP_MSG);
    }
 
}
cs


대충 이정도 구성해놓으면, 각 테스트케이스 @Test 단위에서는 기존과 같이 자동화 코드를 작성하면 된다.


덧붙여 좀 더 추가해서 신경쓸 부분이 있다면, 

  • test.assignCategory(SuiteName); 등을 추가해서, 카테고리별로 구분해서 보게 할 수 있다.
  • 혹시 retry 를 사용하고 있다면, retry시에는 createTest를 추가할 필요가 없다.
  • report에 screenshot을 추가하고 싶다면, test.log(...).addScreenCaptureFromPath() 를 사용하면 된다.
  • jenkins에 연동할때에는 CSS가 적용될 수 있도록 jenkins에 CSS 관련 설정을 해야 한다.


더 좋거나 쉬운 리포트 라이브러리가 있으면 찾아보겠지만, extent report만으로도 현재까지는 만족.





'QA > Test Automation' 카테고리의 다른 글

SoapUI를 이용한 API 테스트 방법 정리  (0) 2018.01.28
API 테스트 방법  (0) 2018.01.27
Jenkins와 Sonar 연동하기  (0) 2012.07.19
Ant 로 Sonar 수행하기.  (0) 2011.04.12
Sonar 소개  (0) 2011.02.14

+ Recent posts