1.在pom.xml中增加testng的依赖,以导入testNG

2.在src-main-resources目录下新建xml文件,比如untitled.xml.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="testSuite1">
<test name="UITest">
<classes>
<class name="testcase.TestNG" />
<class name="testcase.testNG2" />
<class name="testcase.testNG1" />
</classes> </test>
</suite>

3.BeforeTest AfterTest BeforeClass AfterClass的区别

BeforeTest AfterTest

case1:

package testcase;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; public class testNG3 {
@BeforeTest
public void setup(){System.out.println("case3:set up");}
@Test
public void test1(){System.out.println("case3");}
@AfterTest
public void tearup(){System.out.println("case3:tear down");}
}

case2:

package testcase;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; public class testNG4 {
@BeforeTest
public void setup(){System.out.println("case4:set up");}
@Test
public void test1(){System.out.println("case4");}
@AfterTest
public void tearup(){System.out.println("case4:tear down");}
}

xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="testSuite1">
<test name="UITest">
<classes>
<!--<class name="testcase.TestNG" />
<class name="testcase.testNG2" />
<class name="testcase.testNG1" />-->
<class name="testcase.testNG3"/>
<class name="testcase.testNG4"/>
</classes> </test>
</suite>
执行结果:

将BeforeTest替换为BeforeClass,AfterTest替换为AfterClass

case1:

package testcase;

import org.testng.annotations.*;

public class testNG3 {
@BeforeClass
public void setup(){System.out.println("case3:set up");}
@Test
public void test1(){System.out.println("case3");}
@AfterClass
public void tearup(){System.out.println("case3:tear down");}
}

case2:

package testcase;

import org.testng.annotations.*;

public class testNG4 {
@BeforeClass
public void setup(){System.out.println("case4:set up");}
@Test
public void test1(){System.out.println("case4");}
@AfterClass
public void tearup(){System.out.println("case4:tear down");}
}

xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="testSuite1">
<test name="UITest">
<classes>
<!--<class name="testcase.TestNG" />
<class name="testcase.testNG2" />
<class name="testcase.testNG1" />-->
<class name="testcase.testNG3"/>
<class name="testcase.testNG4"/>
</classes> </test>
</suite>

执行结果:

4.使用TestNg增加断言

package testcase;
import org.testng.Assert;
import org.testng.annotations.*;
public class testNG3 {
@BeforeClass
public void setup(){System.out.println("case3:set up");}
@Test
public void test1(){
System.out.println("case3");
String actureMessage="abc";
String expectMessage="efg";
Assert.assertTrue(actureMessage.equalsIgnoreCase(expectMessage),"Expect message is "+expectMessage+".But acture message is "+actureMessage);
}
@AfterClass
public void tearup(){System.out.println("case3:tear down");}
}

将expectMessage修改为abc,结果为


5.编写基于TestNg的测试用例

Baidu.java

 package testcase;

 import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import java.util.concurrent.TimeUnit;
public class Baidu {
private WebDriver driver;
@BeforeTest
public void setUp(){
String chromepath = System.getProperty("user.dir")+"/src/main/java/drivers/chromedriver";
System.setProperty("webdriver.chrome.driver",chromepath);
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void test(){
driver.get("https://www.baidu.com");
driver.findElement(By.xpath("//input[@class='s_ipt' and @id='kw']")).sendKeys("selenium");
driver.findElement(By.xpath("//input[@id='su']")).click();
System.out.println(driver.findElement(By.xpath("//span[@class='nums_text']")).getText()); }
@AfterTest
public void tearDown(){
driver.quit();
}
}

test.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="testSuite1">
<test name="UITest">
<classes>
<class name="testcase.Baidu" />
</classes> </test>
</suite>

测试结果:

												

导入testng管理测试用例的更多相关文章

  1. JAE京东云引擎Git上传管理代码教程和京东云数据库导入导出管理

    文章目录 Git管理准备工作 Git工具上传代码 发布代码装程序 mywebsql管理 京东云引擎小结   JAE京东云引擎是京东推出的支持Java.Ruby.Python.PHP.Node.js多语 ...

  2. Webdriver+Testng实现测试用例失败自动截图功能

    testng执行测试用例的时候,如果用例执行失败会自动截图,方便后续排查问题 1.首先定义一个截图类: package com.rrx.utils; import java.io.File;impor ...

  3. 【框架】用excel管理测试用例需要的参数数据(二)

    一.总体思路 以类为excel名,测试方法名为sheet名,建立excel文件.用jxl包里的方法去读取excel文件里的内容,然后用testng里的dataprovider,将数据传递给测试用例 二 ...

  4. [Xcode 实际操作]一、博主领进门-(3)使用资源文件夹(Assets.xcassets)导入并管理图片素材

    目录:[Swift]Xcode实际操作 本文将演示如何使用资源文件夹(Assets.xcassets)导入并管理图片素材. [Assets.xcassets]资源文件夹可以方便的进行图片的管理, 在读 ...

  5. excel+requests管理测试用例接口自动化框架

    背景: 某项目有多个接口,之前使用的unittest框架来管理测试用例,将每个接口的用例封装成一个py文件,接口有数据或者字段变动后,需要去每个py文件中找出变动的接口测试用例,维护起来不方便,为了便 ...

  6. Jenkins+SVN+Maven+testNG管理项目

    1.登录访问:http://localhost:8080/jenkins 2.系统管理 => 全局工具配置 => ADD JDK  AND  Add Maven 3.安装SVN插件:系统管 ...

  7. Robotium测试套管理测试用例

    前提:已写好测试用例 新建个测试套MyTestSuite管理你需要跑的测试用例,或者将相同功能的测试用例归纳到一个测试套中 package com.robotium.test.testsuite; i ...

  8. TestNG执行测试用例的顺序

    import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebEle ...

  9. TestNG设置测试用例执行优先级

    @Test(priority = x)设置测试用例执行优先级.x默认为0,0的优先级最高,0>1>2>3... import org.testng.annotations.Test; ...

随机推荐

  1. 吐血推荐:罗马尼亚无版权VPS服务商HostSolutions评测-绝对真实

    要放心大胆的玩 VPS,要找性价比高.信用好,服务稳定,限制少的服务商是很不容易的.这里我先点几个黑名单:第一类 HiFormance.VirMach 低价流 VPS 服务商,以低从吸引消费者.俗话说 ...

  2. jquery 中事件

    jQuery 事件 - submit() 方法 定义和用法 当提交表单时,会发生 submit 事件. 该事件只适用于表单元素. submit() 方法触发 submit 事件,或规定当发生 subm ...

  3. 中国教授在BlackHat现场演示破解SIM卡AES-128加密

    使用一个PC和示波器克隆3G/4G SIM卡,破解过程只需十分钟.上海交大教授郁昱现场展示了如何成功复制SIM卡,以及一张克隆卡如何变更了支付宝的密码并潜在盗取账户资金. 破解SIM卡加密 今年二月, ...

  4. Ubuntu 12.04硬盘安装教程

    从服务器下载Ubuntu 12.04光盘镜像文件到 C 盘.下载地址:\\192.167.100.225\share\Tool\Ubuntu\ubuntu-12.04.1-desktop-amd64. ...

  5. 显式(静态)调用: LIB + DLL + .H

    1.编程时用ad.h,ad.lib,放在项目当前目录里2.在头文件中加入#include "ad.h"3.在Project Setting–>Link–>Object/ ...

  6. C语言基础总结 分类: iOS学习 c语言基础 2015-06-11 10:08 23人阅读 评论(0) 收藏

    //欲练此功必先自宫!!!     //第一天:C语言的基础     //进制     //2进制, 10进制, 8进制, 16进制     //注:8进制数前加0, 16进制数前加0x        ...

  7. caffe编译问题-src/caffe/net.cpp:8:18: fatal error: hdf5.h: No such file or directory compilation terminated.

    错误描述 src/caffe/net.:: fatal error: hdf5.h: No such : recipe 操作过程 step1: 在Makefile.config文件更改INCLUDE_ ...

  8. STM32中TIMx的映射及其通道

    TIMx,通道x,无映射,部分映射,完全映射    TIM1_CH1, PA8,    PE9,    TIM1_CH2, PA9,    PE11    TIM1_CH3, PA10,    PE1 ...

  9. liunx的磁盘管理的基本命令

    df     查看磁盘占用率 du -sh    查看磁盘多大 sudo fdisk -l   查看硬盘信息 sudo mkfs -t ext3 /dev/sdb1    建立文件系统(相当于格式化) ...

  10. Laravel学习之旅(二)

    控制器 一.怎么编写控制器? 1.控制器文件存放路径:app\Http\Controllers: 2.命名规范如:TestController.php 3.完整的控制器例子如下: <?php n ...