数据驱动测试一:使用TestNG进行数据驱动
来源:互联网 发布:查看被占用的端口 编辑:程序博客网 时间:2024/06/02 17:13
一、数据驱动测试概念
数据驱动测试是相同的测试脚本使用不同的测试数据执行,测试数据和测试行为完全分离。
二、实施数据驱动测试的步骤:
1、编写测试脚本,脚本需要支持程序对象、文件或者数据库读入测试数据。
2、将测试脚本使用的测试数据存入程序对象、文件或者数据库等外部介质中。
3、运行脚本,循环调用存储在外部介质的测试数据。
4、验证所有的测试结果是否符合期望的结果。
三、使用TestNG进行数据驱动
使用@DataProvider注解定义当前方法中的返回对象作为测试脚本的测试数据集。
用法参考代码:
import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;import org.testng.Assert;import org.testng.annotations.AfterMethod;import org.testng.annotations.BeforeMethod;import org.testng.annotations.DataProvider;import org.testng.annotations.Test;//搜索2个关键词,验证搜索结果页面是否包含期望的关键词public class DataProviderDemo {private static WebDriver driver;@DataProvider(name="searchData")public static Object[][] data(){return new Object[][] {{"老九门","演员","赵丽颖"},{"X站警天启","导演","布莱恩·辛格"},{"诛仙青云志","编剧","张戬"}};} @Test(dataProvider="searchData") public void testSearch(String searchdata1,String searchdata2,String searchResult) { //打开sogou首页 driver.get("http://www.sogou.com/"); //输入搜索条件 driver.findElement(By.id("query")).sendKeys(searchdata1+" "+searchdata2); //单击搜索按钮 driver.findElement(By.id("stb")).click(); //单击搜索按钮后,等待3秒显示搜索结果 try{ Thread.sleep(3000); }catch(InterruptedException e){ e.printStackTrace(); } //判断搜索的结果是否包含期望的关键字 Assert.assertTrue(driver.getPageSource().contains(searchResult)); } @BeforeMethod public void beforeMethod() { //若无法打开Firefox浏览器,可设定Firefox浏览器的安装路径 System.setProperty("webdriver.firefox.bin", "D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); //打开Firefox浏览器 driver=new FirefoxDriver(); //设定等待时间为5秒 driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); } @AfterMethod public void afterMethod() { //关闭打开的浏览器 driver.quit(); }}
运行结果:
PASSED: testSearch("老九门", "演员", "赵丽颖")PASSED: testSearch("X站警天启", "导演", "布莱恩·辛格")PASSED: testSearch("诛仙青云志", "编剧", "张戬")=============================================== Default test Tests run: 3, Failures: 0, Skips: 0===============================================上述代码表示测试方法中3个参数分别使用searchData测试数据集中每个一维数组中的数据进行赋值。此方法会被调用3次,分别使用测试数据集中的三组数据。
0 0
- 数据驱动测试一:使用TestNG进行数据驱动
- TestNG数据驱动测试
- 数据驱动测试二:使用TestNG和CSV文件进行数据驱动
- 数据驱动测试三:使用TestNG、Apache POI和Excel文件进行数据驱动
- 基于testNG数据驱动测试
- 基于testNg的测试框架设计(一)数据驱动
- testng使用excel进行数据驱动代码参照
- 使用MbUnit进行数据驱动测试(单元测试)
- UI自动化测试系列之TestNG-TestNg的数据驱动一
- testng 数据驱动方法
- TestNG数据驱动
- 数据驱动-testng配置
- HttpClient +Testng(2)使用testng的DataProvicer来进行数据驱动
- selenium Excel数据驱动测试(基于 TestNG Apache POI)
- 使用 TestNG 创建一个数据驱动测试过程中遇到的问题
- 在RFT中使用Excel进行数据驱动测试
- UI自动化测试系列之TestNg-TestNg数据驱动二------>以XML外部文件作为数据驱动(1)
- Testng之使用@DataProvider注解做数据驱动
- 使用一个类代替资源文件代码
- MapReduce Main Points
- 学习与记忆
- Discuz! 更改用户名长度(代码和数据库字段)
- 基于python的数字判断程序和字符串判断程序
- 数据驱动测试一:使用TestNG进行数据驱动
- C语言中 fp = fopen(filename, “rb”); 那个rb是什么意思啊
- AC again
- LeetCode-338:Counting Bits
- Swift类型推测在可选调用中的小提示
- C#关键字—yield
- 欢迎界面倒计时
- 144. Binary Tree Preorder Traversal
- GPIO