📅  最后修改于: 2023-12-03 14:47:57.237000             🧑  作者: Mango
TestNG是一款基于Java的测试框架,被设计得比JUnit更强大,并具有更好的灵活性、更多的功能和可重复利用性。TestNG可以帮助程序员轻松地编写并执行基本到复杂的自动化测试。
TestNG支持使用maven和gradle构建项目。以下是TestNG的gradle和maven依赖:
dependencies {
testCompile 'org.testng:testng:7.4.0'
}
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
在TestNG中编写测试用例非常简单,只需添加注解即可。下面是一个简单的测试用例:
import org.testng.annotations.Test;
public class TestExample {
@Test
public void test() {
// 测试内容
}
}
TestNG使用@Test注解来标识测试用例,编写测试用例的方式与JUnit类似。
TestNG支持按照顺序执行测试用例。下面是一个简单的测试用例并指定执行顺序:
import org.testng.annotations.Test;
public class TestExample {
@Test(priority=1)
public void testOne() {
// 测试内容
}
@Test(priority=2)
public void testTwo() {
// 测试内容
}
@Test(priority=3)
public void testThree() {
// 测试内容
}
}
在上面的例子中,testOne()测试用例有最高优先级,testThree()测试用例有最低优先级。
TestNG支持数据驱动测试,也就是允许同一组测试方法针对不同的数据进行多次测试。下面是一个简单的数据驱动测试用例:
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestExample {
@DataProvider(name = "testData")
public Object[][] getData() {
return new Object[][] { { 2, 4, 6 }, { 3, 6, 9 }, { 4, 8, 12 } };
}
@Test(dataProvider = "testData")
public void test(int a, int b, int c) {
int sum = a + b;
Assert.assertEquals(sum, c);
}
}
在上面的例子中,使用@DataProvider注解来提供测试数据,并在@Test注解中指定使用的数据提供程序。数据驱动测试可以让程序员轻松地测试同一组测试用例针对不同的数据。这大大减少了繁琐的测试工作量。
TestNG支持在一个测试套件中包含多个测试类,以便更好地组织测试用例。下面是一个简单的测试套件:
import org.testng.annotations.Test;
public class TestOne {
@Test
public void testCase1() {
// 测试用例1
}
@Test
public void testCase2() {
// 测试用例2
}
}
import org.testng.annotations.Test;
public class TestTwo {
@Test
public void testCase3() {
// 测试用例3
}
@Test
public void testCase4() {
// 测试用例4
}
}
import org.testng.annotations.Test;
import org.testng.suites.XmlSuite;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import java.util.ArrayList;
import java.util.List;
public class TestSuite {
@Test
public void test() {
XmlSuite suite = new XmlSuite();
suite.setName("My test suite");
List<XmlClass> classes = new ArrayList<>();
classes.add(new XmlClass("TestOne"));
classes.add(new XmlClass("TestTwo"));
suite.setXmlClasses(classes);
TestNG testNG = new TestNG();
List<XmlSuite> suites = new ArrayList<>();
suites.add(suite);
testNG.setXmlSuites(suites);
testNG.run();
}
}
在上面的例子中,创建了两个测试类TestOne和TestTwo,并将它们添加到测试套件中。然后,创建了一个包含这个测试套件的测试套件类TestSuite,最后执行这个测试套件。
TestNG支持生成测试报告,可以帮助程序员了解测试结果并发现问题。下面是如何配置和生成测试报告:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<reportsDirectory>${project.basedir}/target/surefire-reports</reportsDirectory>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
<configuration>
<reportPlugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
<reports>
<report>index</report>
<report>dependency-info</report>
<report>team</report>
<report>license</report>
<report>dependency-convergence</report>
<report>plugin-management</report>
</reports>
</plugin>
<plugin>
<groupId>org.testng</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</reportPlugins>
</configuration>
</plugin>
</plugins>
</build>
$ ./mvnw site
TestNG是一款基于Java的测试框架,具有许多功能和可定制的功能。它支持多线程测试,数据驱动测试,测试套件和测试报告生成。 更重要的是,它是开源的,并且有一个很强大的社区。TestNG可以帮助程序员编写和执行基本到复杂的测试用例。