1、case suit
在单元测试里面,多个case可以作为一个suit(簇)
2、关于testng的一些注解
@BeforeClass:整个类执行前执行,在同一个类中只执行一次,一般在测试中用于浏览器的初始化。
@AfterClass:整个类执行完毕前执行,在同一个类中只执行一次,一般在测试中用于浏览器的关闭。
@BeforeMethod:在每个测试用例开始前执行一次,有多少个方法就执行多少次,常用于一些表单的默认设置。
@AfterMethod:在每个测试用例结束后执行一次,有多少个方法就执行多少次。
@Test:用于每个测试用例,有多少个用例就有多少个test
3、testng的数据驱动
第一种驱动模式:
@DataProvider
public Object[][] logindata() {
return new Object[][]{{"a"}, {"b"}};
}
@Test(dataProvider = "logindata")
public void test1(String info) {
System.out.println(info);
}
//执行结果
//a
//b
第二种驱动模式:
@DataProvider(name = "logoutdata")
public Object[][] logoutinfo() {
return new Object[][]{{"1"}, {"2"}, {"3"}};
}
@Test(dataProvider = "logoutdata")
public void test2(String info) {
System.out.println(info);
}
//执行结果
//1
//2
//3
4、使用xml来完成testng的调用,在xml文件上右键run ,比较好维护,自由度更高
(1)整个类的调用
<suite name="Suite" verbose="1" parallel="false" thread-count="1">
<test name="Test1" preserve-order="true">
<classes>
<class name="com.example.newTestNG.TestNG"></class>
</classes>
</test>
</suite>
(2)某些方法的调用(只调用类的test2方法)
<suite name="Suite" verbose="1" parallel="false" thread-count="1">
<test name="Test1" preserve-order="true">
<classes>
<class name="com.example.newTestNG.TestNG">
<methods>
<include name="test2"/>
<!--<include name="test1"/>-->
</methods>
</class>
</classes>
</test>
</suite>
5、依赖测试
(1)dependsOnMethods示例
method2()依赖于method1()
如果method1通过将执行method2,否则method2也不会执行
@Test
public void method1(){
...
}
@Test(dependsOnMethods={"method1"})
public void method2(){
...
}
(2)dependsOnGroups
dependsOnMethod 和dependsOnGroup的混合使用
@Test(groups="deploy")
public class TestServer{
@Test
public void deployServer(){
System.out.println("deployServer...");
}
@Test(dependsOnMethods={"deployServer"})
public void deployBackUpServer(){
System.out.println("Deploy Backup Server");
}
}
6、testNG+Selenium负载测试:invocationCount代表执行多少次这个方法
这是只有一个线程,可以使用线程池以通过多个线程运行测试方法。使用线程池会大大降低测试方法的运行时间。
比如刚刚那个方法执行10次耗时117ms,添加了线程池后,耗时38ms。