2018년 8월 27일 월요일

TestNG에서 테스트 순서 제어하기


public class TestNGTest3 {
 
    @BeforeClass
    public void beforeclass(){
        System.out.println("@BeforeClass TestNGTest3");
    }
     
    @Test
    public void test7(){
        System.out.println("test7");
        Assert.assertTrue(true);
    }
     
    @Test
    public void test2() throws Exception{
        System.out.println("test2");
        Assert.assertTrue(true);
    }
     
    @Test
    public void test15() throws Exception{
        System.out.println("test15");
        Assert.assertTrue(true);
    }
     
    @AfterClass
    public void afterclass(){
        System.out.println("@AfterClass TestNGTest3");
    }
}
[결과]

@BeforeClass TestNGTest3
test15
test2
test7
@AfterClass TestNGTest3
PASSED: test15
PASSED: test2
PASSED: test7


public class TestNGTest1 {

    @BeforeClass

    public void beforeclass(){
        System.out.println("@BeforeClass TestNGTest1");
    }

    @Test
    public void test12() throws Exception{
        System.out.println("Test12");
        Assert.assertTrue(true);
    }

    @Test
    public void test5(){
        System.out.println("Test5");
        Assert.assertTrue(true);
    }

    @Test
    public void test3(){
        System.out.println("Test3");
        Assert.assertTrue(true);
    }

    @AfterClass
    public void afterclass(){
        System.out.println("@AfterClass TestNGTest1");
    }
}
[결과]

@BeforeClass TestNGTest1
Test12
Test3
Test5
@AfterClass TestNGTest1
PASSED: test12
PASSED: test3
PASSED: test5

[해결책]

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestNG Suite Name">
    <test name="TestNGTest" preserve-order="true">
        <classes>
            <class name="testng.TestNGTest1">
                <methods>
                    <include name="test3" />
                    <include name="test5" />
                    <include name="test12" />
                </methods>
            </class>

            <class name="testng.TestNGTest3">
                <methods>
                    <include name="test2" />
                    <include name="test7" />
                    <include name="test15" />
                </methods>
            </class>
        </classes>
    </test>
</suite>
[설명]
testng의 suite 파일에 preserve-order='true' 를 설정하고 해당 클래스의 test method의 순서를 지정해주면 된다.

[결과]
@BeforeClass TestNGTest1
Test3
Test5
Test12
@AfterClass TestNGTest1
@BeforeClass TestNGTest3
test2
test7
test15
@AfterClass TestNGTest3

※ 위 순서대로 사용하려면, dependsOnMethod를 사용하시면 안됩니다.

댓글 없음:

댓글 쓰기