JUnit 框架是 Java 语言单元测试当前的一站式解决方案。这个框架值得称赞,因为它把测试驱动的开发思想介绍给 Java 开发人员并教给他们如何有效地编写单元测试。但是,在过去的几年中,JUnit 的改进不大;所以,为当今复杂的环境编写测试已经变成一个越来越困难的任务,即 JUnit 必须与其他一些补充性测试框架集成起来。而 TestNG 是一个测试 Java 应用程序的新框架。TestNG 不仅确实强大、创新、可扩展、灵活,它还展示了 Java Annotations(JDK 5.0 中的重大新特性)的有趣应用。
TestNG 的创造者是 Cedric Beust,他在 Java 编程领域非常出名,是 EJB 3 专家组的成员,也是其他一些流行的开源项目(例如 EJBGen 和 Doclipse)的创造者。
示例测试代码:
package example1;build.xml代码:
import org.testng.annotations.*;
public class SimpleTest {
@BeforeClass
public void setUp() {
// code that will be invoked when this test is instantiated
}
@Test(groups = { "fast" })
public void aFastTest() {
System.out.println("Fast test");
}
@Test(groups = { "slow" })
public void aSlowTest() {
System.out.println("Slow test");
}
}
<project name="sample" default="test" basedir=".">
<!-- COMPILE TESTS-->
<path id="cpath">
<pathelement location="lib/testng-6.5.1.jar"/>
<pathelement location="build"/>
</path>
<target name="compile">
<echo message="compiling tests"/>
<mkdir dir="classes"/>
<javac debug="true" source="1.5" classpathref="cpath" srcdir="src" destdir="classes"/>
</target>
<!-- RUN TESTS-->
<taskdef name="testng" classname="com.beust.testng.TestNGAntTask" classpathref="cpath"/>
<path id="runpath">
<path refid="cpath"/>
<pathelement location="classes"/>
</path>
<target name="test" depends="compile">
<echo message="running tests"/>
<testng classpathref="runpath" outputDir="test-output">
<xmlfileset dir="./" includes="testng.xml"/>
<jvmarg value="-ea" />
</testng>
</target>
</project>
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" parallel="tests" thread-count="1">
<test name="API Test" preserve-order="false">
<classes>
<!-- <class name="com.api.testcase.PerfByProvinceApiTest"/>-->
<class name="example1.SimpleTest"/>
</classes>
</test>
</suite>
本文介绍了TestNG框架,它是Java应用程序的一种新型测试框架。该框架由知名Java专家Cedric Beust创建,以其强大的功能、创新性和灵活性著称。文章通过示例展示了如何使用TestNG进行单元测试,并提供了从编译到运行测试的全过程配置。
3364

被折叠的 条评论
为什么被折叠?



