testng与junit的区别
selenium吧
全部回复
仅看楼主
level 5
hebxtgsl 楼主
Testng与junit对比
1.
总体概念
TestNG,即Testing, Next
Generation,下一代测试技术,是一套根据JUnit 和 NUnit思想而构建的利用注释来强化测试功能的一个测试框架,即可以用来做单元测试,也可以用来做集成测试。
2.
testNGxml
testNG的运行需要一个配置文件,默认为testng.xml,其描述了要运行哪些测试等配置。
下面来看一下,如何来实现测试的,与JUnit4 差不多。
声明测试方法如下:
@Test
public void testMethod1() {
System.out.println("in
testMethod1");
}
@Test
public void testMethod2() {
System.out.println("in
testMethod2");
}
基本上都是采用java的注释实现的。
与JUnit4 不同在于,测试方法可以分组,它可以根据诸如运行时间这样的特征来对测试分类。
@Test(groups={"fun1","fun2"})
public void testMethod1() {
System.out.println("in
testMethod1");
}
@Test(groups={"fun1"})
public void testMethod2() {
System.out.println("in
testMethod2");
}
同JUnit4 一样,同样支持Before,After方法,如同setUp 和tearDown,不过TestNG更为灵活,支持各种签名方式,如private,protected。
@BeforeMethod
protected void beforeMethod() {
System.out.println("in
beforeMethod");
}
@AfterMethod
protected void afterMethod() {
System.out.println("in
afterMethod");
}
同样也支持BeforeClass 和AfterClass,只执行一次的方法,但是可以不需要使用static签名
@BeforeClass
protected void beforeClassMethod() {
System.out.println("in
beforeClassMethod");
}
@AfterClass
protected void afterClassMethod() {
System.out.println("in
afterClassMethod");}
3.
失败和重运行
在大型测试套件中,这种重新运行失败测试的能力显得尤为方便。这是 TestNG 独有的一个特性。在 JUnit 4 中,如果测试套件包括 1000 项测试,其中 3 项失败,很可能就会迫使您重新运行整个测试套件(修改错误以后)。不用说,这样的工作可能会耗费几个小时。
2013年02月21日 01点02分 1
level 5
hebxtgsl 楼主

一旦 TestNG 中出现失败,它就会创建一个 XML 配置文件,对失败的测试加以说明。如果利用这个文件执行 TestNG 运行程序,TestNG 就只 运行失败的测试。所以,在前面的例子里,您只需重新运行那三个失败的测试,而不是整个测试套件。
4.
testng的参数化
第一种是使用testNG.xml
第二种是使用Data
Providers.
1.
用testNG.xml传参
这个技巧可以使我们很方便的在源代码中引用testNG.xml里面配置的参数。
首先,参数在testNG.xml定义,示例如下:
<suite name="Parameters">
<test
name="ParameterTest">
<parameter name="xml-file" value="accounts.xml"/>
<parameter name="hostname"
value="arkonis.example.com"/>
</test>
</suite>
Test method调用的方式如下:
@Test
@Parameters({ "hostname",
"xml-file"})
public void fileShouldExistOnFtpServer(String
hostName,
String xmlFile)
{
// xmlFile has the value
"xml-file" and
// hostname is
"terra.example.com"
}
TestNG会在testNG.xml中找到对应的parameter,按照参数的要求进行格式转换(比如string 转换为int),然后将其赋值给对应的函数,运行。
2. 利用DataProvider传参
我们可以很方便的利用DataProvider来将外部数据传递给指定的test method,并循环起来,即是一个很标准的数据驱动的方法
下面是一个demo ,testng读取excel:
项目结构:
Feed4TestExcel.java源码:
Test.xls截图:
运行结果片段:
5.
并发测试
TestNG天生是支持并发测试的,这个包含两层含义:
1. Concurrent
testing:
也就是说,你可以很方便的创建一个并发的测试用例,来测试我们的代码是否是thread safe的,性能是否是符合预期的等等……。
@Test(invocationCount = 5, threadPoolSize =
3)
public void smallThreadPool() {
System.out.println("[Small] Thread#:
" +
Thread.currentThread().getId());
}
threadPoolSize用来指明线程池的大小,也就是并发的线程数目是多少,对于这个特定的例子,其输出可能如下:
[Small] Thread#: 14
[Small] Thread#: 13
[Small] Thread#: 15
[Small] Thread#: 14
[Small] Thread#: 13
2. Concurrent
running of tests:
TestNG可以以多线程的模式运行所有的test,这样可以获得最大的运行速度,最大限度的节约执行时间。当然,并发运行也是有代价的,就是需要我们的代码是线程安全的。
并发运行测试的话,需要我们指定运行的配置文件,一个示例如下:
<suite name="Concurrent Suite"
parallel="methods" thread-count="2" verbose="1"
>
<>……<>
</suite>
Parallel=”methods”的意思是指TestNG会将method作为并发的元子单位,即每个method运行在自己的thread中。
如果parallel=”tests”,则指
Thread-count=”2”是指,运行的时候,并发度为2,同时会有两个线程在运行。
6.
依赖测试
各个test之间有依赖关系是一个常态,而testNG对依赖有很好的支持,主要有两个@Test 的属性标签:dependsOnGroups以及dependsOnMethods。
testNG会确保在每个声明dependsOn*的method运行之前,被依赖的method都会先被运行,如果被依赖的method fail了的话,那么这个method也就不会执行,而会被标示为SKIP状态。
一个示例如下:
@Test(groups = "init")
public void launchServer() {}
@Test(dependsOnGroups = "init", groups =
"deploy-apps")
public void deploy() {}
@Test(dependsOnGroups = "init", groups =
"deploy-apps")
public void deployAuthenticationServer() {}
@Test(dependsOnGroups = "deploy-apps")
public void test1() {}
@Test(dependsOnGroups = "deploy-apps")
public void test2() {}
上述代码将会建立如下依赖关系:
7.
总结
Junit4和testng各有优点,我们可以结合这两种框架,使我们的工作更加的便利。合理
使用这两种框架,让他们相互促进和补充,而不是非得放弃其中之一!
2013年02月21日 01点02分 2
1