【TestNG快板說二】TestNG常見Annotation的使用

 

哪些常見Annotation

@Test: 標記需要運行的測試方法;

@BeforeMethod: 測試方法運行之前執行;

@AfterMethod: 測試方法完成之後執行;

@BeforeClass: 在類中第一個測試方法被執行之前執行;

@AfterClass: 在類中所有方法運行完成後執行;

@BeforeTest: 在testng.xml中<test>標誌代表的測試事務開始之前執行;

@AfterTest: 在testng.xml中<test>標誌代表的測試事務完成後執行;

@BeforeSuite: 在testng.xml中<suite>標誌代表的測試集合開始前執行;

@AfterSuite: 在testng.xml中<suite>標誌代表的測試集合完成後執行;

@BeforeGroup: 在相同組的第一個測試用例開始前執行;

@AfterGroup: 在相同組的所有測試用例完成後執行;

Test禁止運行的方法

  1. 使用@Test(enabled = false)
  2. testng.xml中使用<exclude>
  3. 使用throw new SkipException()的方式也可以提過,這裏不介紹
  •  先看@Test(enabled = false) ,下面代碼禁用testDemo2 
public class Demo1 {
    private static WebDriver driver;
    private static String executePath;

    @BeforeClass
    public void init() {
        executePath = System.getProperty("user.dir") + File.separator + "bin" + File.separator + "chromedriver_mac";
        System.setProperty("webdriver.chrome.driver", executePath);
        driver = new ChromeDriver();
    }

    @Test(enabled = true)
    public void testDemo1() throws InterruptedException{
        System.out.print("testDemo1");
    }

    @Test(enabled = false)
    public void testDemo2() throws InterruptedException{
        System.out.print("testDemo2");
    }

    @AfterClass
    public void quit() {
        driver.quit();
    }
}

運行結果:

  • 使用xml運行的方式

xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1" >
    <test name="Regression1">
        <classes>
            <class name="com.nitb.demo.Demo1">
                <methods>
                    <include name="testDemo2" />
                    <exclude name="testDemo1" />
                </methods>
            </class>
        </classes>
    </test>
</suite>

java代碼恢復:使能testDemo2

    @Test(enabled = true)
    public void testDemo2() throws InterruptedException{
        System.out.print("testDemo2");
    }

運行結果:只運行testDemo2

測試用例優先級的設置

  1. 默認情況下所有@Test標記的測試用例priority=0,執行的時候是根據英文字母的順序執行;
  2. 可以改變priority的值進行測試用的優先級調整。

案例:

    @Test(priority = 2)
    public void testDemo1() throws InterruptedException{
        System.out.print("testDemo1");
    }

    @Test(priority = 1)
    public void testDemo2() throws InterruptedException{
        System.out.print("testDemo2");
    }

按照上面的設置,預想的執行順序應該是testDemo2-->testDemo1,讓我們來看下實際的運行結果:

從上圖的運行結果,我們可以看出和期望的順序一直。

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章