第一:注解

(1)@test

(2)@BeforeMethod,@AfterMethod

(3)@BeforeClass,@AfterClass

(4)@BeforeSuite,@AfterSuite

第二:套件测试

(1) SuiteConfig:公共方法

public class SuiteConfig{

@BeforeSuite

public void beforesuit(){

  system.out.println("beforesuit 运行啦");

}

@AfterSuite

public void aftersuit(){

  system.out.println("aftersuit 运行啦");

}

}

(2) LoginTest:

public class LoginTest{

@Test

public void logintaobao(){

  system.out.println("login taobao");

}

}

(3) PayTest

 

public class PayTest{

@Test

public void paysuccess(){

  system.out.println("zhifubao success");

}

}

(4) resource:Testng.xml(名字随意)

<?xml version="1.0" encoding="UTF-8" ?>

<suite name="test">

  <test name ="login">

    <classes>

      <class name="com.curse.testng.suite.suiteconfig"/>

      <class name="com.curse.testng.suite.LoginTest"/>

    </classes>

  </test>

  <test name ="pay">

    <classes>

      <class name="com.curse.testng.suite.suiteconfig"/>

      <class name="com.curse.testng.suite.PayTest"/>

    </classes>

  </test>

</suite>

 

右键执行testng.xml,结果如下:

beforesuit 运行啦

login taobao

zhifubao success

aftersuit 运行啦

 

如果在suiteconfig文件中增加beforetest和aftertest,执行结果为:

beforesuit 运行啦

beforetest

login taobao

aftertest

beforetest

zhifubao success

aftertest

aftersuit 运行啦

 

第三:忽略测试

@test(enabled=false)

第四:分组测试

(1)group在方法上

public class groupsonMethod{

@test(groups="server")

public void test1(){

system.out.println("test1");

}

@test(groups="server")

public void test2(){

system.out.println("test2");

}

@test(groups="client")

public void test3(){

system.out.println("test3");

}

@beforegroups("server")

public void beforegroupsonserver(){

system.out.println("beforeservergroups");

}

@aftergroups("server")

public void aftergroupsonserver(){

system.out.println("afterservergroups");

}

}

运行结果:

beforeservergroups

test1

test2

afterservergroups

test3

(2)groups在类上

@test(groups="stu")

public class groupsonclass1{

}

testng.xml:

<test name="onlyrunstu">

  <groups>

    <run>

      <include name="stu"/>

    </run>

  </groups>

 

</test>

第五:异常测试

 

 

第六:依赖测试

public class DpendTest{

  @test

  public void test1(){

    system.out.println("test1");

    throw new RuntimeException();//打印要写在异常前面,因为抛出异常后,后边的代码都不执行

}

  @test(dependsOnMethods={"test1"})

  public void test2(){

    system.out.println("test2");

}

}

 

结果:test1

 

test1执行,test2被忽略了,因为test2的依赖测试test1抛出了异常,执行失败了

 第七: 超时测试

 

public class TimeOutTest{

  @test(timeout=3000)//单位为ms

  public void testsuccess(){

    Thread.sleep(millis:2000);

    system.out.println("test1");

}

@test(timeout=2000)//单位为ms

  public void testfailed(){

    Thread.sleep(millis:3000);

    system.out.println("test1");

}

}