testng

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

TestNG

TestNG

TestNG is a testing framework setup inspired by JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use, such as parallel testing and data-driven testing.
TestNG是一款受JUnit和NUnit启发的测试框架,同时引入了一些新功能,使其更强大、更易用,例如并行测试和数据驱动测试。

When to Use

适用场景

  • Complex Suites: You need granular control over which groups of tests run via XML configuration (
    testing.xml
    ).
  • Parallel Execution: Historically better support for multi-threaded test execution than JUnit.
  • Dependencies: Tests that depend on other tests (
    @Test(dependsOnMethods = { "serverStarted" })
    ).
  • 复杂测试套件:你需要通过XML配置文件(
    testing.xml
    )精细控制要运行的测试组。
  • 并行执行:在多线程测试执行方面,TestNG的支持历史上优于JUnit。
  • 依赖测试:依赖其他测试的测试用例(
    @Test(dependsOnMethods = { "serverStarted" })
    )。

Quick Start

快速开始

java
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestNGExample {
    @Test(groups = { "fast" })
    public void testAdd() {
        Assert.assertEquals(1 + 1, 2);
    }
}
xml
<!-- testng.xml -->
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Suite1" parallel="methods" thread-count="5">
  <test name="test1">
    <classes>
       <class name="com.example.TestNGExample"/>
    </classes>
  </test>
</suite>
java
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestNGExample {
    @Test(groups = { "fast" })
    public void testAdd() {
        Assert.assertEquals(1 + 1, 2);
    }
}
xml
<!-- testng.xml -->
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Suite1" parallel="methods" thread-count="5">
  <test name="test1">
    <classes>
       <class name="com.example.TestNGExample"/>
    </classes>
  </test>
</suite>

Core Concepts

核心概念

Groups

测试组

Tagging tests (
@Test(groups = "smoke")
). Allows running specific subsets (Include/Exclude in XML).
为测试打标签(
@Test(groups = "smoke")
),允许通过XML配置运行特定子集的测试(包含/排除)。

Data Providers

数据提供者

Native support for passing complex objects to tests.
java
@DataProvider(name = "test1")
public Object[][] createData1() {
 return new Object[][] {
   { "Cedric", new Integer(36) },
   { "Anne", new Integer(37) },
 };
}

@Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) { ... }
原生支持向测试用例传递复杂对象。
java
@DataProvider(name = "test1")
public Object[][] createData1() {
 return new Object[][] {
   { "Cedric", new Integer(36) },
   { "Anne", new Integer(37) },
 };
}

@Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) { ... }

Best Practices (2025)

2025年最佳实践

Do:
  • Use Soft Assertions:
    SoftAssert
    allows the test to continue even if one check fails, reporting all errors at the end.
  • Parallelize: Leverage
    thread-count
    in CI to speed up execution.
Don't:
  • Don't use
    dependsOnMethods
    for Unit Tests
    : Unit tests should be independent. Use dependencies only for Integration flows.
建议
  • 使用软断言
    SoftAssert
    允许测试在某次检查失败后继续执行,最终报告所有错误。
  • 并行执行:在CI环境中利用
    thread-count
    加速测试执行。
不建议
  • 单元测试不要使用
    dependsOnMethods
    :单元测试应保持独立,仅在集成测试流程中使用依赖配置。

References

参考资料