iOS/Test

테스트 응용 - ScreenShot (XCUIScreen, XCTAttechment)

@서비 2022. 8. 8. 21:56

 

테스트 함수에서 제공하는 스크린샷 기능을 사용해 본다. 

최종 목적지는 버튼을 눌렀을 때, 나오는 화면을 캡쳐해서 폴더에 저장 하는 것이다.

(나중에 slack 같은 곳에 업로드도 가능할 거 같다.)

 

1. 먼저 버튼 을 클릭하면 나오는 화면을 캡쳐하는 테스트 코드를 작성한다.

코드는 앱을 런칭하고 버튼을 누르면 약간의 딜레이 후에 스크린샷을 찍고, 어테치먼트에 추가하는 코드이다.

    func testButtonClickEventResult() throws {
        
        //given
        app.launch()
        
        //when
        app.buttons["Button"].tap()
        
        //then
        sleep(10)
        let screenshot = XCUIScreen.main.screenshot()
        let attachment = XCTAttachment(screenshot: screenshot)
        
        attachment.lifetime = .keepAlways
        add(attachment)
        
        
     
    }

2. 테스트 결과를 열어보면 스크린샷이 첨부된 것을 볼 수 있다.

테스트 결과는 레포트 네비게이터에 테스트 부분을 선택하면 테스트 목록이 나오고 열면 그 안에 파일로 스크린 샷이 보여진다.

 

 

3. 이제 테스트 결과 스크린샷만 모아보자.

shell script 로 작성해서 터미널에서 실행 한다.

 

rm -rf resultBundle				//테스트 결과 삭제
rm -rf resultBundle.xcresult	//테스트 결과 삭제

xcodebuild \
  -workspace ActiveLabelTest.xcworkspace \
  -scheme ActiveLabelTest \
  -sdk iphonesimulator \
  -destination "platform=iOS Simulator,name=iPhone SE (1st generation)" \ #작은폰
  -destination 'platform=iOS Simulator,name=iPhone 13 Pro Max' \   #큰폰
  -derivedDataPath build/ \
  -resultBundlePath resultBundle \
  test

xcparse screenshots resultBundle.xcresult screenshotOutput	#결과 파일에서 스크린샷 만 폴더로 저장
#brew install chargepoint/xcparse/xcparse

 

4. 결과파일

 

 

 

참고 : 뱅크샐러드 기술 블로그

2022.08.09 - [iOS/Test] - 테스트 응용 - 비 동기 실행

 

테스트 응용 - 비 동기 실행

두 가지 방법이 있는데, 첫 번째는 swift 에서 제공하는 await, async 를 사용한다. 아래의 예시를 보면 함수에 async 로 작성하고 함수에 await로 작성하였다. func testDownloadWebDataWithConcurrency() async..

iosdevhistory.tistory.com