JUnit 4 Vs TestNG比较
JUnit 4和TestNG都是Java中非常受欢迎的单元测试框架。两种框架在功能上看起来非常相似。 哪一个更好? 在Java项目中应该使用哪个单元测试框架?
下面表中概括了JUnit 4和TestNG之间的功能比较。如下图所示 -

1. 注释支持
注释/注解支持在JUnit 4和TestNG中是非常类似的。
| 特点 | JUnit 4 | TestNG |
|---|---|---|
| 测试注释 | @Test | @Test |
| 在套件中的所有测试运行之前运行 | - | @BeforeSuite |
| 在套件中的所有测试运行之后运行 | - | @AfterSuite |
| 测试之前运行 | - | @BeforeTest |
| 测试之后运行 | - | @AfterTest |
| 在调用属于任何这些组的第一个测试方法之前运行 | - | @BeforeGroups |
| 在调用属于任何这些组的第一个测试方法之后运行 | - | @AfterGroups |
| 在调用当前类的第一个测试方法之前运行 | @BeforeClass | @BeforeClass |
| 在调用当前类的第一个测试方法之后运行 | @AfterClass | @AfterClass |
| 在每个测试方法之前运行 | @Before | @BeforeMethod |
| 在每个测试方法之后运行 | @After | @AfterMethod |
| 忽略测试 | @ignore | @Test(enbale=false) |
| 预期的异常 | @Test(expected = ArithmeticException.class) | @Test(expectedExceptions = ArithmeticException.class) |
| 超时测试 | @Test(timeout = 1000) | @Test(timeout = 1000) |
JUnit4和TestNG之间的主要注释差异是:
在JUnit 4中,我们必须声明“
@BeforeClass”和“@AfterClass”方法作为静态方法。 TestNG在方法声明中更灵活,它没有这个约束。3个额外的setUp / tearDown级别:suite和group(@Before / AfterSuite,@Before / After Test,@Before / After Group)。
JUnit 4
@BeforeClass
public static void oneTimeSetUp() {
// one-time initialization code
System.out.println("@BeforeClass - oneTimeSetUp");
}
TestNG
@BeforeClass
public void oneTimeSetUp() {
// one-time initialization code
System.out.println("@BeforeClass - oneTimeSetUp");
}
在JUnit 4中,注释命名约定有点混乱,例如“Before”,“After”和“Expected”,我们并不真正了解“Before”和“After”之前的内容,以及要测试中的“预期” 方法。TestiNG更容易理解,它使用类似“BeforeMethod”,“AfterMethod”和“ExpectedException”就很明了。
2. 异常测试
“异常测试”是指从单元测试中抛出的异常,此功能在JUnit 4和TestNG中都可实现。
JUnit 4
@Test(expected = ArithmeticException.class)
public void divisionWithException() {
int i = 1/0;
}
TestNG
@Test(expectedExceptions = ArithmeticException.class)
public void divisionWithException() {
int i = 1/0;
}
3. 忽略测试
“忽略”表示是否应该忽略单元测试,该功能在JUnit 4和TestNG中均可实现。
JUnit 4
@Ignore("Not Ready to Run")
@Test
public void divisionWithException() {
System.out.println("Method is not ready yet");
}
TestNG
@Test(enabled=false)
public void divisionWithException() {
System.out.println("Method is not ready yet");
}
4. 时间测试
“时间测试”表示如果单元测试所花费的时间超过指定的毫秒数,则测试将会终止,并将其标记为失败,此功能在JUnit 4和TestNG中均可实现。
JUnit 4
@Test(timeout = 1000)
public void infinity() {
while (true);
}
TestNG
@Test(timeOut = 1000)
public void infinity() {
while (true);
}
5. 套件测试
“套件测试”是指捆绑几个单元测试并一起运行。 此功能在JUnit 4和TestNG中都可实现。 然而,两者都使用非常不同的方法来实现它。
JUnit 4
“@RunWith”和“@Suite”用于运行套件测试。下面的类代码表示在JunitTest5执行之后,单元测试“JunitTest1”和“JunitTest2”一起运行。 所有的声明都是在类内定义的。
@RunWith(Suite.class)
@Suite.SuiteClasses({
JunitTest1.class,
JunitTest2.class
})
public class JunitTest5 {
}
TestNG
XML文件用于运行套件测试。以下XML文件表示单元测试“TestNGTest1”和“TestNGTest2”将一起运行。
<suite name="My test suite">
<test name="testing">
<classes>
<class name="com.fsecure.demo.testng.TestNGTest1" />
<class name="com.fsecure.demo.testng.TestNGTest2" />
</classes>
</test>
</suite>
TestNG可以做捆绑类测试,也可以捆绑方法测试。 凭借TestNG独特的“分组”概念,每种方法都可以与一个组合相结合,可以根据功能对测试进行分类(分组)。 例如,
下面是一个有四个方法的类,三个组(method1,method2和method3)
@Test(groups="method1")
public void testingMethod1() {
System.out.println("Method - testingMethod1()");
}
@Test(groups="method2")
public void testingMethod2() {
System.out.println("Method - testingMethod2()");
}
@Test(groups="method1")
public void testingMethod1_1() {
System.out.println("Method - testingMethod1_1()");
}
@Test(groups="method4")
public void testingMethod4() {
System.out.println("Method - testingMethod4()");
}
使用以下XML文件,可以仅使用组“method1”执行单元测试。
<suite name="My test suite">
<test name="testing">
<groups>
<run>
<include name="method1"/>
</run>
</groups>
<classes>
<class name="com.fsecure.demo.testng.TestNGTest5_2_0" />
</classes>
</test>
</suite>
通过“分组”测试概念,集成测试的可能性是无限制的。 例如,我们只能从所有单元测试类中测试“DatabaseFuntion”分组。
6. 参数化测试
“参数化测试”是指单位测试参数值的变化。 此功能在JUnit 4和TestNG中都实现。 然而,两者都使用非常不同的方法来实现它。
JUnit 4
“@RunWith”和“@Parameter”用于提供单元测试的参数值,@Parameters必须返回List [],参数将作为参数传入类构造函数。
@RunWith(value = Parameterized.class)
public class JunitTest6 {
private int number;
public JunitTest6(int number) {
this.number = number;
}
@Parameters
public static Collection<Object[]> data() {
Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
return Arrays.asList(data);
}
@Test
public void pushTest() {
System.out.println("Parameterized Number is : " + number);
}
}
这里有很多限制,我们必须遵循“JUnit”的方式来声明参数,并且必须将参数传递给构造函数才能初始化类成员作为测试的参数值。参数类的返回类型为“List []”,数据已被限制为String或用于测试的原始类型值。
TestNG
XML文件或“@DataProvider”用于提供不同参数进行测试。
用于参数化测试的XML文件 -
只有“@Parameters”在需要参数测试的方法中声明,参数化数据将在TestNG的XML配置文件中提供。 通过这样做,我们可以使用不同数据集的单个测试用例,甚至获得不同的结果。 另外,即使是最终用户,QA还是QE都可以在XML文件中提供自己的数据进行测试。
public class TestNGTest6_1_0 {
@Test
@Parameters(value="number")
public void parameterIntTest(int number) {
System.out.println("Parameterized Number is : " + number);
}
}
XML文件的内容如下 -
<suite name="My test suite">
<test name="testing">
<parameter name="number" value="2"/>
<classes>
<class name="com.fsecure.demo.testng.TestNGTest6_0" />
</classes>
</test>
</suite>
@DataProvider用于参数化测试
将数据值拉入XML文件可能非常方便,但测试偶尔会需要复杂的类型,这些类型不能被表示为一个字符串或一个原始类型值。 TestNG使用@DataProvider注解来处理这种情况,这有助于将复杂参数类型映射到测试方法。
@DataProvider for Vector,String或Integer作为参数,参考如下代码 -
@Test(dataProvider = "Data-Provider-Function")
public void parameterIntTest(Class clzz, String[] number) {
System.out.println("Parameterized Number is : " + number[0]);
System.out.println("Parameterized Number is : " + number[1]);
}
//This function will provide the patameter data
@DataProvider(name = "Data-Provider-Function")
public Object[][] parameterIntTestProvider() {
return new Object[][]{
{Vector.class, new String[] {"java.util.AbstractList",
"java.util.AbstractCollection"}},
{String.class, new String[] {"1", "2"}},
{Integer.class, new String[] {"1", "2"}}
};
}
@DataProvider作为对象的参数
“TestNGTest6_3_0”是一个简单的对象,只需使用get/set方法进行演示。
@Test(dataProvider = "Data-Provider-Function")
public void parameterIntTest(TestNGTest6_3_0 clzz) {
System.out.println("Parameterized Number is : " + clzz.getMsg());
System.out.println("Parameterized Number is : " + clzz.getNumber());
}
//This function will provide the patameter data
@DataProvider(name = "Data-Provider-Function")
public Object[][] parameterIntTestProvider() {
TestNGTest6_3_0 obj = new TestNGTest6_3_0();
obj.setMsg("Hello");
obj.setNumber(123);
return new Object[][]{
{obj}
};
}
TestNG的参数化测试非常用户友好和灵活(在XML文件或类内)。 它可以支持许多复杂的数据类型作为参数值,可能性是无限的。 如上例所示,我们甚至可以传入我们自己的对象(TestNGTest6_3_0)进行参数化测试
7.依赖性测试
“参数化测试”表示方法是依赖性测试,它将在所需方法之前执行。 如果依赖方法失败,则所有后续测试将会被跳过,不会被标记为失败。
JUnit 4
JUnit框架着重于测试隔离; 目前它不支持此功能。
TestNG
它使用“dependOnMethods”来实现依赖测试如下 -
@Test
public void method1() {
System.out.println("This is method 1");
}
@Test(dependsOnMethods={"method1"})
public void method2() {
System.out.println("This is method 2");
}
“method2()”只有在“method1()”运行成功的情况下才会执行,否则“method2()”将跳过测试。
结论
在考虑所有功能比较之后,建议使用TestNG作为Java项目的核心单元测试框架,因为TestNG在参数化测试,依赖测试和套件测试(分组概念)方面更加突出。 TestNG用于高级测试和复杂集成测试。 它的灵活性对于大型测试套件尤其有用。 此外,TestNG还涵盖了整个核心的JUnit4功能。这样说来,好像也没有理由使用JUnit了。
JUnit 4 Vs TestNG比较的更多相关文章
- JUnit 4 与 TestNG 对比
原文出处: 付学良的网志 原文出处2: http://www.importnew.com/16270.html -------------------------------------------- ...
- JUnit 4 和 TestNG
JUnit 4和TestNG都是Java中非常受欢迎的单元测试框架.两种框架在功能上看起来非常相似.哪一个更好?在Java项目中应该使用哪个单元测试框架? 下面表中概括了JUnit 4和TestNG之 ...
- testNg vs junit 4.X @Test
http://www.ibm.com/developerworks/cn/java/j-cq08296/ 一个简单的测试用例 初看起来,JUnit 4 和 TestNG 中实现的测试非常相似.为了更好 ...
- 关于testNG和JUnit的对比
关于选择JUnit还是选testNG,这几篇文章,建议读一读: API参考文档: Junit API文档:http://junit.org/junit4/javadoc/latest/index.ht ...
- testng入门教程11 TestNG运行JUnit测试
现在,您已经了解了TestNG和它的各种测试,如果现在担心如何重构现有的JUnit代码,那就没有必要,使用TestNG提供了一种方法,从JUnit和TestNG按照自己的节奏.也可以使用TestNG执 ...
- 在Eclipse中开发使用Spring IOC的JUnit/TestNG测试用例之详解
转载自:http://blog.csdn.net/radic_feng/article/details/6740438 我们期望能像在产品代码中一样,在测试用例中使用的bean也由Spring Con ...
- Java Unit Testing - JUnit & TestNG
转自https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaUnitTesting.html yet another insignifican ...
- Why TestNG?
最近计划将工程中的单元测试从JUnit迁移到TestNG上面. Why TestNG(http://kaczanowscy.pl/tomek/sites/default/files/testng_vs ...
- 一个简单的TestNG例子
关于TestNG好的资源: 官网文档:http://testng.org/doc/documentation-main.html 一 下载并安装:1. JDK 1.7 $ java -version ...
随机推荐
- Linux基础命令---uniq
uniq 将文件中重复出现的行删除,结果送到标准输出或者指定文件.在使用uniq指令之前,必须使用sort对内容进行排序,否则没有效果.如果没有选项,则将匹配的行合并到第一个匹配项. 此命令的适用范围 ...
- 【前端安全】JavaScript防流量劫持
劫持产生的原因和方式 在网页开发的访问过程中,http是我们主要的访问协议.我们知道http是一种无状态的连接.即没有验证通讯双方的身份,也没有验证信息的完整性,所以很容易受到篡改.运营商就是利用了这 ...
- WEB前端移动开发初始化
meta篇 1.视窗宽度 <meta name="viewport" content="width=device-width,initial-scale=1.0,m ...
- jQuery实现无刷新切换主题皮肤功能
主题皮肤切换功能在很多网站和系统中应用,用户可以根据此功能设置自己喜欢的主题颜色风格,增强了用户体验.本文将围绕如何使用jQuery实现点击无刷新切换主题皮肤功能. 查看演示DEMO:https:// ...
- JustOj 1994: P1001
题目描述 给定一个长度为N(0< n< =10000)的序列,保证每一个序列中的数字a[i]是小于maxlongint的非负整数 ,编程要求求出整个序列中第k大的数字减去 ...
- [转载]时间显示插件 flipclock.js
<html> <head> <link rel="stylesheet" href="/assets/css/flipclock.css&q ...
- ubunta apt install error
ubuntu系统: 用apt-get命令安装一些软件包时,总报错:E:could not get lock /var/lib/dpkg/lock -open等 出现这个问题的原因可能是有另外一个程序正 ...
- 在linux系统中安装redis
使用的工具是 连接上虚拟机,进入安装文件的目录 进入redis压缩包中 将压缩包解压: tar -zxvf redis-2.8.3.tar.gz 进入这个文件中: cd redis-2.8.3 进入 ...
- sql 思路
先 django 定好sql框架 再 sqlalchemy 根据框架写...
- WINDOW 安装ImageMagick服务端和PHP的imagick插件
写在最前: windows下要注意的就是ImageMagick版本要与php扩展的Imagick能对应上,同时Imagick版本的选择也要与服务器环境以及php的情况对应上,不然就容易安装失败,我就是 ...