Hard Assertion과 Soft Assertion

정태경·2022년 9월 26일
0
post-thumbnail

개요

Java 개발자에게는 테스팅 프레임워크로 Junit과 TestNG가 가장 널리 사용되고 있다.
Java와 Selenium을 이용한 웹 테스트 자동화에도 위 두 가지 테스트 프레임워크가 가장 많이 사용될 것이다. 이번엔 테스팅 프레임워크 중 TestNG의 Hard Assertion과 Soft Assertion에 대해 알아보려고 한다.

Hard Assertion과 Soft Assertion은 어떤 차이가 있을까?

Hard Assertion은 TestNG의 Assert 클래스를 통해 동작하게 되는데, 만약 Assertion Fail이 발생하면 그 아래 코드들은 더 이상 동작하지 않는다. 아래 예제 코드의 주석을 보면 명확하게 이해할 수 있을 것이다.

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;


import java.time.Duration;

public class SoftAssertionDemo {

    public static void main(String[] args) {
        WebDriverManager.chromiumdriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));

        driver.get("https://www.myrealtrip.com");
        String titleName = driver.getTitle();
        
        Assert.assertEquals(titleName, "Fail 발생 시키기"); // Fail 발생 후 결과 보고.
        System.out.println("Hard Assertion Testing"); // 이 코드는 실행되지 않음.

Soft Assertion은 SoftAssert 객체를 생성하는 것만 다르고 나머지는 Hard Assertion과 거의 동일하게 사용한다. 다만 실제 동작에는 차이점이 존재하는데 Hard Assertion은 Assertion Fail 발생 즉시 코드 실행을 중지하고 결과를 보고하는 반면, Soft Assertion은 Assertion Fail이 발생하더라도 나머지 코드를 모두 실행한 후 결과를 보고하게 된다.

따라서 실패하는 케이스가 존재하더라도 나머지 코드가 실행(혹은 테스트 케이스 실행)되는 것을 원한다면 Soft Assertion을 활용할 수 있다.

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.asserts.SoftAssert;


import java.time.Duration;

public class SoftAssertionDemo {

    public static void main(String[] args) {
        WebDriverManager.chromiumdriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));

        driver.get("https://www.myrealtrip.com");
        String titleName = driver.getTitle();
        System.out.println(titleName);

        SoftAssert a = new SoftAssert();              // SoftAssert 객체 생성
        a.assertEquals(titleName, "Fail 발생 시키기");   // Fail 발생 후 결과 저장.
        System.out.println("Soft Assertion Testing");  // 이 코드도 실행됨.

        a.assertAll(); // assertion 실패 케이스를 보고함.

    }
}


profile
現 두나무 업비트 QA 엔지니어, 前 마이리얼트립 TQA 엔지니어

0개의 댓글