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 项失败,很可能就会迫使您重新运行整个测试套件(修改错误以后)。不用说,这样的工作可能会耗费几个小时。
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 项失败,很可能就会迫使您重新运行整个测试套件(修改错误以后)。不用说,这样的工作可能会耗费几个小时。

