一、使用testng的大流程
1、按照官网下载
官网地址:
https://testng.org/doc/index.html
参考学习:
documentation下
2、编写用例,执行testng用例
已经配置好maven项目管理,习惯将测试用例放在test文件夹下
执行方式:
点击.java文件,右键:RUN AS—TestNG Test
3、配置xml文件组织用例按套件执行
完成本次迭代测试用例编写,包含冒烟用例、sit用例、回归用例等
选择版本涉及的cases:右键:TestNG—Convert to TestNG
xml文件名可自定义
会在项目尾部生成XML文件
4、TestNG的常用注解
@BeforeClass
一般是写前置条件,可以修改方法名,习惯写为setUp()
@AfterClass
一般用于对测试用例执行后清理工作编写,方法名习惯写为:tearDown()
@Test
编写测试用例。
二、编写testng的用例
注解可以添加参数,来实现一些功能。
1、执行顺序:
指定一个TestNG的测试中多个用例执行顺序
写法:@Test(priority=1/2/3/…/N),就会按照顺序去执行1、2、3…N了
```java
public class TestCaseThree {
@Test(priority=1)
public void one() {
System.out.println("this case1 for ClassThreee");
Assert.assertEquals(false, true);
}
@Test(priority=2)
public void two() {
System.out.println("this case2 for ClassThreee");
}
}
2、忽略测试
当运行测试计划报告运行失败,有BUG,开发需要修复,但不能影响同计划其他测试,也使用参数忽略该条测试的执行
写法 @Test(enabled=false),当改成=true,会重新又运行
```java
@Test(priority=3,enabled=false)
public void three() {
System.out.println("this case3 for ClassThreee");
}
3、测试依赖
用例依赖时:
写法 @Test(dependsOnMethods=“testcasename”)
//dependsOnMethods="one"用例依赖,双引号扩上用例名
@Test(priority=3,dependsOnMethods="one")
4、测试分组
指根据项目类型,敏捷或小瀑布项目,采用对应的V模型和X模型测试,需要有分组测试,做冒烟、SIT、单元、系统等测试
使用参数:groups可实现
写法: @Test(groups=“groupname”)
1、编写用例
@Test(groups="smoking")
public void openBaidu() throws InterruptedException {
//通过对象driver调用具体的get方法来打开网页
driver.get("https://www.baidu.com");
//最大化浏览器窗口
driver.manage().window().maximize();
Thread.sleep(3000);
//检查:1:验证打开页面的标题与预期相符
Assert.assertEquals(driver.getTitle(), "百度一下,你就知道");
}
2、修改xml文件
在test标签下加入groups标签,再加入run标签,
最后写入include name,(要运行的分组名),或者指定不执行的分组,写入exclude name
<suite name="Suite">
<test thread-count="5" name="Test">
<groups>
<run>
<include name="smoking"></include>
</run>
</groups>
<classes>
<class name="modualA.TestCaseOne"/>
<class name="modualA.TestCaseThree"/>
<class name="modualA.TestCaseTwo"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
完成用例注解后参数分组设定,以及xml文件分组指定后就可以运行测试套件,
注意:xml文件是严格区分大小写的,所以在xml中分组名最好复制粘贴过来。
Run As —TestNG Suite