testng和junit有什么优缺点,如何选择?

电脑杂谈  发布时间:2020-07-12 14:21:23  来源:网络整理

junit测试_junit web测试_junit测试题目

JUnit 4与TestNG比较

图标

中文翻译: 傅学良JUnit 4 vs. TestNG

JUnit 4和TestNG都是Java中非常流行的单元测试框架. 这两个框架在功能上看起来非常相似. 哪一个更好?我应该在Java项目中使用哪个单元测试框架?

下表总结了JUnit 4和TestNG之间的功能比较. 如下所示-

JUnit 4和TestNG中的注释/注释支持非常相似.

JUnit4和TestNG之间的主要注释差异为:

在JUnit 4中,我们必须将“ @BeforeClass”和“ @AfterClass”方法声明为静态方法. TestNG在方法声明中更为灵活,它没有此约束. 3个附加的upUp / tearDown级别: 套件和组(@ Before / AfterSuite,@ Before / After测试,@ Before / After组).

JUnit 4

@BeforeClass
public static void oneTimeSetUp() {
    // one-time initialization code
    System.out.println(@BeforeClass - oneTimeSetUp);
}

Java

TestNG

@BeforeClass
public void oneTimeSetUp() {
        // one-time initialization code
        System.out.println(@BeforeClass - oneTimeSetUp);
}

Java

在JUnit 4中,注释命名约定有些混乱,例如“ Before”,“ After”和“ Expected”,我们并不太了解“ Before”和“ After”之前的含义以及“ expected”在测试“方法”中. TestiNG较容易理解,它使用类似于“ BeforeMethod”,“ AfterMethod”和“ ExpectedException”的方法.

“异常测试”是指从单元测试引发的异常. 此功能可以在JUnit 4和TestNG中实现.

JUnit 4

@Test(expected = ArithmeticException.class)
public void divisionWithException() {
  int i = 1/0;
}

junit web测试_junit测试题目_junit测试

Java

TestNG

@Test(expectedExceptions = ArithmeticException.class)
public void divisionWithException() {
  int i = 1/0;
}

Java

“忽略”指示是否应忽略单元测试. 此功能可以在JUnit 4和TestNG中实现.

JUnit 4

@Ignore(Not Ready to Run)
@Test
public void divisionWithException() {
  System.out.println(Method is not ready yet);
}

Java

TestNG

@Test(enabled=false)
public void divisionWithException() {
  System.out.println(Method is not ready yet);
}

Java

“时间测试”表示如果单元测试花费的时间超过指定的毫秒数,则测试将终止并标记为失败. 可以在JUnit 4和TestNG中实现此功能.

JUnit 4

@Test(timeout = 1000)
public void infinity() {
    while (true);
}

Java

TestNG

@Test(timeOut = 1000)
public void infinity() {
    while (true);
}

Java

“套件测试”是指捆绑多个单元测试并将其一起运行. 可以在JUnit 4和TestNG中实现此功能. 但是,两者都使用非常不同的方法来实现它.

JUnit 4

junit测试_junit web测试_junit测试题目

“ @ RunWith”和“ @Suite”用于运行套件测试. 以下类代码指示在执行JUnitTest5之后,单元测试“ JunitTest1”和“ JunitTest2”一起运行. 所有声明都在该类中定义.

@RunWith(Suite.class)
@Suite.SuiteClasses({
        JunitTest1.class,
        JunitTest2.class
})
public class JunitTest5 {
}

Java

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>

XML

TestNG可以用于捆绑测试或方法测试. 利用TestNG独特的“分组”概念,可以将每种方法结合起来使用,并且可以根据功能对测试进行分类(分组). 例如,

以下是具有四个方法,三个组(方法1junit测试,方法2和方法3)的类

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

Java

使用以下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>

XML

通过“分组”测试的概念,集成测试的可能性是无限的. 例如,我们只能测试所有单元测试类中的“ DatabaseFuntion”分组.

“参数测试”是指单元测试参数值的变化. 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);
     }
}

Java

junit测试_junit测试题目_junit web测试

这里有很多限制. 我们必须遵循“ JUnit”方法来声明参数,并且必须将参数传递给构造函数以将类成员初始化为测试参数值. 参数类的返回类型为“ List []”,并且数据已限制为String或用于测试的原始类型值.

TestNG

XML文件或“ @DataProvider”用于提供不同的测试参数.

用于参数化测试的XML文件–

在需要参数测试的方法中仅声明“ @Parameters”,并且将在TestNG的XML配置文件中提供参数化的数据. 通过这样做,我们可以使用来自不同数据集的单个测试用例,甚至获得不同的结果. 此外,即使是最终用户,质量检查或质量检查人员也可以在XML文件中提供自己的数据以进行测试.

public class TestNGTest6_1_0 {
@Test
@Parameters(value=number)
public void parameterIntTest(int number) {
       System.out.println(Parameterized Number is :  + number);
    }
}

Java

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>

XML

@DataProvider用于参数化测试

将数据值提取到XML文件中可能很方便,但是偶尔测试将需要复杂的类型,这些类型不能表示为字符串或原始类型值. TestNG使用@DataProvider批注来处理这种情况,这有助于将复杂的参数类型映射到测试方法.


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/ruanjian/article-277750-1.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...