注解
@Test
我们在类方法中编写用例代码
选择第一个,会自动导入包
package com.demo.testng;
import org.testng.annotations.Test;
public class TestNGDemo1 {
@Test
public void test01(){
System.out.println("This is test01");
}
@Test
public void test03(){
System.out.println("This is test03");
}
@Test
public void test02(){
System.out.println("This is test02");
}
}
这里我将test02
和test03
位置调整了一下,目的是查看一下TestNG的执行顺序,可以看到执行结果:
仍然是按照01->02->03
的顺序进行执行的。
@BeforeMethod与@AfterMethod
我们有时候会要求每个方法前和每个方法后
需要执行一些操作,我们就可以使用@BeforeMethod
和@AfterMethod
方法
可以看到,@Before***
还有很多类似unittest和Pytest的执行方式
package com.demo.testng;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class TestNGDemo1 {
@BeforeMethod
public void openBrowser(){
System.out.println("Open browser of @BeforeMethod");
}
@AfterMethod
public void closeBrowser(){
System.out.println("Close browser of @AfterMehtod");
}
@Test
public void test01(){
System.out.println("This is test01");
}
@Test
public void test03(){
System.out.println("This is test03");
}
@Test
public void test02(){
System.out.println("This is test02");
}
}
执行代码:
可以看到每个方法执行前后都执行了一次@BeforeMehtod和@AfterMethod。
@BeforeClass与@AfterClass
当我们进行Selenium
自动化测试时,经常会有打开浏览器
和关闭浏览器
的操作,我们不会每个用例方法都执行一次操作,而是在用例开始前和结束后整体执行一次:
package com.demo.testng;
import org.testng.annotations.*;
public class TestNGDemo1 {
@BeforeClass
public void openBrowser(){
System.out.println("Open browser of @BeforeMethod");
}
@AfterClass
public void closeBrowser(){
System.out.println("Close browser of @AfterMehtod");
}
@Test
public void test01(){
System.out.println("This is test01");
}
@Test
public void test03(){
System.out.println("This is test03");
}
@Test
public void test02(){
System.out.println("This is test02");
}
}
执行代码:
@BeforeClass与@AfterClass是在每个类的执行开始和结束执行一次。
方法名 | 解释 |
---|
@BeforeSuite | 被注释的方法在所有测试运行前运行 |
@AfterSuite | 被注释的方法在所有测试运行后运行 |
@BeforeTest | 被注释的方法在测试运行前运行 |
@AfterTest | 被注释的方法在测试运行后运行 |
@BeforeClass | 被注释的方法在当前类的第一个测试方法调用前运行 |
@AfterClass | 被注释的方法在当前类的所有测试方法调用后运行 |
@BeforeMethod | 被注释的方法在每一个测试方法调用前运行 |
@AfterMethod | 被注释的方法在每一个测试方法调用后运行 |
整个流程的控制如图:
@Test(enable=false)跳过用例
我们跳过test03用例,只需要在@Test注解中添加enable=false
package com.demo.testng;
import org.testng.annotations.*;
public class TestNGDemo1 {
@BeforeClass
public void openBrowser(){
System.out.println("Open browser of @BeforeMethod");
}
@AfterClass
public void closeBrowser(){
System.out.println("Close browser of @AfterMehtod");
}
@Test
public void test01(){
System.out.println("This is test01");
}
@Test(enabled = false)
public void test03(){
System.out.println("This is test03");
}
@Test
public void test02(){
System.out.println("This is test02");
}
}
执行用例:
分组执行
package com.demo.testng;
import org.testng.annotations.*;
public class TestNGDemo1 {
@BeforeClass
public void openBrowser(){
System.out.println("Open browser of @BeforeMethod");
}
@AfterClass
public void closeBrowser(){
System.out.println("Close browser of @AfterMehtod");
}
@Test(groups = {"groups01"})
public void test01(){
System.out.println("This is test01");
}
@Test(groups = {"groups02"})
public void test03(){
System.out.println("This is test03");
}
@Test(groups = {"groups01", "groups02"})
public void test02(){
System.out.println("This is test02");
}
}
但是如果以上方法直接执行,分组标签就不会生效,此时我们需要新建一个testng.xml
文件。这个文件不需要我们手动去写,可以直接去下载一个插件:File->Setting->Plugins
,搜索TestNG
下载
下载好以后在项目中右键就会出现生成testng.xml
文件的选项,这个一般在最下面
添加完成以后需要重新加载一下才会显示出来
进入文件后代码都在一行,只需要在选项Code->Reformat Code
格式化一下代码:
然后添加分组执行的代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http:///testng-1.0.dtd">
<suite name="All Test Suite">
<test verbose="2" preserve-order="true" name="C:/Users/UC504933/Desktop/JavaCode/TestNGDemo">
<groups>
<run>
<include name="groups01" />
</run>
</groups>
<classes>
<class name="com.demo.testng.TestNGDemo1"></class>
</classes>
</test>
</suite>
在testng.xml
文件中执行代码
可以看到执行结果:
如果要不执行某个分组,则把
<include name="groups01" />
改为
<exclude name="groups01" />
即可。
依赖控制
在@Test
注解中添加dependsOnMethods = {"test01"}
。
package com.demo.testng;
import org.testng.Assert;
import org.testng.annotations.*;
public class TestNGDemo2 {
@BeforeClass
public void openBrowser(){
System.out.println("Open browser of @BeforeMethod");
}
@AfterClass
public void closeBrowser(){
System.out.println("Close browser of @AfterMehtod");
}
@Test
public void test01(){
System.out.println("This is test01");
Assert.assertEquals(1,3);
}
@Test(dependsOnMethods = {"test01"})
public void test02(){
System.out.println("This is test02");
}
@Test
public void test03(){
System.out.println("This is test03");
}
}
这里我把test01
添加了报错断言,执行时就可以看到用例跳过了test02
。而且这里需要注意,当添加了dependsOnMehtods
以后执行用例的顺序变了。【如果要验证,可以删掉test01中的Assert语句】
可以看到由于test01
报错,test02
被跳过了。
testNG多线程
测试方法通过在@Test
注解中配置threadPoolSize
属性进入多线程模式。参数解释:
属性 | 描述 |
---|
invocationCount | 执行的次数 |
threadPoolSize | 线程池内线程的个数 |
timeOut | 超时时间-毫秒 |
现在我们希望test02
执行五次,而且用多线程以加快执行效率。为了看清,我给线程号打印出来。
package com.demo.testng;
import org.testng.Assert;
import org.testng.annotations.*;
public class TestNGDemo2 {
@BeforeClass
public void openBrowser(){
System.out.println("Open browser of @BeforeMethod");
}
@AfterClass
public void closeBrowser(){
System.out.println("Close browser of @AfterMehtod");
}
@Test
public void test01(){
System.out.println("This is test01");
Assert.assertEquals(1,1);
}
@Test(threadPoolSize = 5, invocationCount = 5, timeOut = 5000)
public void test02(){
long id = Thread.currentThread().getId();
System.out.println("This is test02 --->"+"Thread id: "+id);
}
@Test
public void test03(){
System.out.println("This is test03");
}
}
执行代码:
数据驱动dataProvider
package com.demo.testng;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestNGDemo3 {
@DataProvider(name = "data")
public Object[][] dataList(){
Object[][] object;
return object = new Object[][]{
{"Package One", 1},
{"Package Two", 2},
{"Package Three", 3}
};
}
@Test(dataProvider = "data")
public void identyData(String name, int num){
System.out.println("包名:"+ name + " 编号:"+num);
}
}
执行代码:
可以看到数据以三个用例的方式执行了三次。
以上就是TestNG基础方法了。
如果您觉得对您有帮助,请帮忙点一下公众号底部的广告,点一下就可以,谢谢~