junit4对比testng

4 篇文章 0 订阅
2 篇文章 0 订阅

概述

以最新版的junit和testng进行一些横向对比总结。两种框架在功能上非常相似且使用范围都很广泛,哪一个更好?

功能比较

注解异常测试跳过测试超时测试套件分组参数化单值参数化对象依赖测试
testNGYYYYYYYYY
junit4YYYYYNYNN

注解支持

描述JUnit 4TestNG
测试注解@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)

JUnit 4中,必须声明“@BeforeClass”和“@AfterClass”方法作为静态方法,TestNG没有这个约束

简单示例

TestNG独特的“分组”概念,每种方法都可以与一个组合相结合,可以根据功能对测试进行分类(分组)

public class TestNGTestGroups{
	@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()");
	}
}

使用以下XML文件,可以仅使用组“method1”执行单元测试

<suite name="MySuite">
  <test name="testing">
      <groups>
      <run>
        <include name="method1"/>
      </run>
    </groups>
    <classes>
       <class name="demo.testng.TestNGTestGroups" />
    </classes>
  </test>
</suite>

参数化测试是指参数值的变化。JUnit4 “@RunWith”和“@Parameter”用于提供单元测试的参数值,@Parameters必须返回List [],参数将作为参数传入类构造函数。

@RunWith(value = Parameterized.class)
public class JunitTest{

     private int number;

     public JunitTest(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);
     }
}

testNG实现:

public class TestNGTestParameter {

@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="demo.testng.TestNGTestParameter" />
    </classes>
  </test>
</suite>

参数为复杂的类型,TestNG使用@DataProvider注解来处理这种情况

@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"}}
                  };
    }

依赖性测试表示方法是依赖性测试,它将在所需方法之前执行。 如果依赖方法失败,则所有后续测试将会被跳过,不会被标记为失败。JUnit 4JUnit框架着重于测试隔离; 目前它不支持此功能。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()”将跳过测试

咳咳~ 基本就这些,根据自己的需求选择一个合适的框架吧~

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值