【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,让我们来看下实际的运行结果:

从上图的运行结果,我们可以看出和期望的顺序一直。

 

 

 

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