1、TestNG介绍
是java的一个测试框架,类似于JUnit,java中已经有一个JUnit的测试框架,测试人员用TestNG来写自动化测试,开发人员一般用JUnit写单元测试,其实TestNG也可以理解为Eclipse的一个插件。
TestNG的官方网站:http://testng.org/doc/index.html
2、TestNG的安装
两种方式可以安装TestNG 在Eclipse中
第一种:离线安装
TestNG 下载地址:http://testng.org/doc/download.html
从官网上下载的安装时老报错,所以从其他地方得到一个离线的安装包
分别将【features】文件夹下的文件放到eclipse的【features】目录下,将【org.testng.eclipse_6.8.6.20130607_0745】文件夹放在Eclipse的【plugins】目录下
然后从新启动Eclipse即可
验证安装是否成功
打开Eclipse工具,在“文件—新建—其它”结构下有【testNG】目录,如下图所示:
第二种:在线安装
选择菜单:help—install new software,然后在弹出的窗口中点击Add进行添加,如下图所示:
版本不同下载的网址不同,http://beust.com/eclipse1
3、使用testNG新建一个测试案例
3.1、新建一个testNG类,同时生成一个testNG.xml,如下图所示:
3.2、testNG类中测试代码如下:
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Test;
public class testNG {
@Test
public void f() {
File file =new File("C:/Program Files/Internet Explorer/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver",file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();
driver.get("http://www.baidu.com");
driver.quit();
}
}
3.3、 testNG.xml 代码如下(会自动生成)
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="Test">
<classes>
<class name="selenium2.testNG"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
3.4、testNG中如何运行测试
第一种:直接执行
第二种,在testNG.xml中执行
运行testNG.xml ,结果如下所示:
信息: I/O exception (java.net.SocketException) caught when processing request: Software caused connection abort: recv failed
2015-7-18 11:57:20 org.apache.http.impl.client.DefaultRequestDirector tryExecute
信息: Retrying request
===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
3.5、运行多个案例时,修改testNG.xml,例如像同时运行【TestNGSimpleTest】和【testNG】这两个类,将testNG.xml修改如下图所示:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="Test">
<classes>
<class name="selenium2.testNG"/>
<class name="selenium2.TestNGSimpleTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
3.6、运行结果如下:
Listening on port 25572
2015-7-18 12:01:47 org.apache.http.impl.client.DefaultRequestDirector tryExecute
信息: I/O exception (java.net.SocketException) caught when processing request: Software caused connection abort: recv failed
2015-7-18 12:01:47 org.apache.http.impl.client.DefaultRequestDirector tryExecute
信息: Retrying request
this is testng test case
===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
3.7、报告运行完上面的操作后,在项目下会有个test.output文件夹,打开index.html可以看到一个简单的报告