Annotation on TestNG

Dahun Yoo·2022년 10월 22일
0

Introduce to TestNG

목록 보기
3/5

TestNG에서의 어노테이션에 대해 기재합니다.


Annotation

자바 애너테이션은 자바 소스 코드에 추가하여 사용할 수 있는 메타데이터의 일종이다. 보통 @ 기호를 앞에 붙여서 사용한다. JDK 1.5 버전 이상에서 사용 가능하다. 자바 애너테이션은 클래스 파일에 임베디드되어 컴파일러에 의해 생성된 후 자바 가상머신에 포함되어 작동한다.

https://ko.wikipedia.org/wiki/%EC%9E%90%EB%B0%94_%EC%95%A0%EB%84%88%ED%85%8C%EC%9D%B4%EC%85%98

자바에서의 어노테이션은 기존부터 존재하고 있었습니다.
TestNG는 테스트를 위해 몇가지 어노테이션을 제공합니다.

@BeforeSuite:The annotated method will be run before all tests in this suite have run.
@AfterSuite:The annotated method will be run after all tests in this suite have run.
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the "test" tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the "test" tag have run.
@BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
@AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.
@BeforeMethod: The annotated method will be run before each test method.
@AfterMethod: The annotated method will be run after each test method.

https://testng.org/doc/documentation-main.html

기준은 xml파일에 선언하는 클래스들의 기준입니다.

eduCBA
from eduCBA

위 그림에서의 구조 처럼 실행됩니다.

기본적으로 모든 테스트 메소드에는 @Test 어노테이션을 붙여야하는데, 이 어노테이션을 기준으로 method, class, Test, Suite를 구분하며, 이 구분 기준이 xml 파일 내부에 선언된 태그들을 기준으로 실행됩니다.

실행순서

위 어노테이션은 xml계층구조에 따른 실행순서를 가집니다. (그림처럼) @Test 어노테이션의 경우에는, 이 어노테이션을 사용하는 메소드들을 알파벳 순서로 실행합니다. 따라서 메소드들간의 의존관계가 있다면 테스트가 꼬일 가능성이 있습니다.

Base파일?

이러한 테스트를 위한 어노테이션은, classTest 레벨이면 테스트 실행을 위한 데이터 초기화, 드라이버 생성 등 모튼 테스트에서 공통적으로 수행이 필요한 것들에 대한 내용이 기재되기 마련입니다.

xml파일은 기본적으로는 파일 내에 기재된 파일들만 실행하는데, 이 경우에 일종의 base 역할을 하는 파일을 만든다음, 테스트케이스들을 가진 클래스들에 직접적으로 상속시켜 사용할 수도 있습니다.
이 때, 오버라이드하는 경우는 있겠지만 기본적으로는 모든 테스트케이스에서 공통적인 동작을 기재하는 것을 전제로 해야합니다.

예를 들어,

public class APILoginTest extends AnnotationTest

이러한 파일이 있다고 가정해봅시다. AnnotationTest 에는 아래의 메소드들이 있습니다.

@BeforeClass
public void beforeClass() {
    System.out.println("Start this AnnotationClass");
}

@AfterClass
public void afterClass() {
    System.out.println("End this AnnotationClass");
}

@BeforeTest
public void prerequire() {
    System.out.println("Ready to Test !");

}

@AfterTest
public void endTest() {
    System.out.println("End Test !");
}

@Test()
public void annotationTest1() {
    System.out.println("Annotatiton Test 1");
}

실행하고자 하는 xml파일은 아래와 같습니다.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
    <test verbose="2" preserve-order="true" name="AppWebLogin">
        <classes>
            <class name="priorityExecute.AppLoginTest"/>

            <class name="priorityExecute.WebLoginTest"/>
        </classes>
    </test>
    <test name="APILogin">
        <classes>
            <class name="priorityExecute.APILoginTest">
            </class>
        </classes>
    </test>
</suite>

AnnotationTest 를 상속받지 않은 AppWebLogin 테스트의 하위 클래스 실행 결과

AnnotationTest 를 상속받은 APILoginTest 테스트의 하위 클래스 실행 결과

위 스크린샷에서도 볼 수 있듯, 상속받으면 직접 xml파일에 선언해주지 않아도 잘 실행합니다.
이렇게 공통적으로 사용할 로직들을 담은 파일들에 일부 어노테이션을 선언하여 이용하는 것도 방법입니다.

Annotation helper attribute

어노테이션에는 속성값을 지정해 줄수 있습니다. 여러가지 값들이 있지만 아래에서는 자주 쓰이는 값들 몇가지를 소개합니다. 자세한 것은 공식문서를 확인해주세요.

@Test(groups = {})

@Test 에는 groups 를 추가해줄 수 있습니다. 이것은 특정 테스트들을 특정 그룹으로 관리하고자할 때 좋습니다. 콤마를 이용해서 여러개의 그룹을 지정해줄 수 있습니다.

public class WebLoginTest {

    @Test(groups = {"web"})
    public void pcWebLogin() {
        System.out.println("pc web login");
    }

    @Test(groups = {"Android"})
    public void androidWebLogin() {
        System.out.println("Android web login");
    }

    @Test(groups = {"iOS"})
    public void iosWebLogin() {
        System.out.println("IOS web login");
    }
}
public class AppLoginTest {

    @Test(groups = {"Android"})
    public void androidlogin(){
        System.out.println("androidlogin");

    }

    @Test(groups = {"iOS"})
    public void iosLogin() {
        System.out.println("iosLogin");
    }

}

이렇게 2개의 테스트 파일이 있다고 가정하고, 각각의 플랫폼들로 묶어서 실행하고싶다고 해봅시다. xml파일은 아래와 같이 될 것입니다.

<suite name="All Test Suite">
    <test verbose="2" preserve-order="true" name="Android">
        <groups>
            <run>
                <include name="Android"/>
            </run>
        </groups>
        <classes>
            <class name="priorityExecute.WebLoginTest"/>
            <class name="priorityExecute.AppLoginTest"/>
        </classes>
    </test>
    <test verbose="2" preserve-order="true" name="iOS">
        <groups>
            <run>
                <include name="iOS"/>
            </run>
        </groups>
        <classes>
            <class name="priorityExecute.WebLoginTest"/>
            <class name="priorityExecute.AppLoginTest"/>
        </classes>
    </test>
    <test verbose="2" preserve-order="true" name="web">
        <groups>
            <run>
                <include name="web"/>
            </run>
        </groups>
        <classes>
            <class name="priorityExecute.WebLoginTest"/>
            <class name="priorityExecute.AppLoginTest"/>
        </classes>
    </test>
</suite>

xml 파일에서, test 태그 안에 groups 태그를 선언하고, include 로 명시적으로 그룹이름을 지정해줍니다. xml파일 계층을 보시면 아시겠지만 1개의 테스트 스위트 아래에 각각의 플랫폼으로 test가 나뉘어져 있습니다.

실행결과 입니다. 그룹핑한 메소드들을 묶어서 실행하였습니다.

@Test(dependsOnMethod = {}), @Test(dependsOnGroups={})

특정 메소드나, 그룹에 의존적인 테스트를 실행할 때 지정합니다. 지정한 메소드나 그룹이 실행되지 않으면, 이 어노테이션을 달고 있는 메소드도 실행되지 않고, 지정한 메소드나 그룹의 결과가 Fail이 나면, 이 어노테이션을 달고 있는 메소드도 실행되지 않습니다.

실패해도 무조건 실행하게 하려면 alwaysRun=true 를 지정해주어야 합니다.

이 헬퍼 어트리뷰트는, 지정한 메소드나 그룹에 이어서 바로 다음에 실행된다는 의미는 아닙니다. 그러나 여러 메소드들에 지정을 함으로 인해 순서를 강제할 수도 있습니다.
자세한 것은 아래 링크를 확인해주세요.

https://www.toolsqa.com/testng/testng-dependent-tests/

@Test(Enabled=false)

해당 옵션이 걸린 메소드는 실행하지 않습니다.

@Test(Timeout = x)

해당 옵션이 걸린 메소드는 지정한 시간동안 충분히 실행될 수 있도록 기다립니다.

@Test(priority = x)

해당 옵션을 이용해서 실행되는 메소드들의 순서를 강제할 수 있습니다. 0부터 입력할 수 있습니다. 이 조건을 이용하면 TestNG의 알파벳순서 순의 실행을 무시할 수 있습니다.
단, 우선순위를 지정하지 않은 메소드들과 지정한 메소드들이 같이 있는 상황에서 실행하면, 알파벳 순서가 먼저 입니다.

https://www.guru99.com/test-case-priority-testng.html


다음 포스트에서는 파라미터 전달방법에 대해 기재합니다.

ref

profile
QA Engineer

0개의 댓글