TestNG和junit的整合
A.junit4采用JunitCore调用执行类。
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class JunitTestSuiteRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(JunitTestSuite.class);
for (Failure fail : result.getFailures()) {
System.out.println(fail.toString());
}
if (result.wasSuccessful()) {
System.out.println("All tests finished successfully...");
}
}
}
详细代码如下:
org.testng.junit.Junit4TestRunner
/**
* Starts a test run. Analyzes the command line arguments and runs the given
* test suite.
*/
public Result start(final Class testCase, final String... methods) {
try {
JUnitCore core = new JUnitCore();
core.addListener(new RL());
Request r = Request.aClass(testCase);
return core.run(r.filterWith(new Filter() {
@Override
public boolean shouldRun(Description description) {
if (description == null) {
return false;
}
if (methods.length == 0) {
//run everything
return true;
}
for (String m: methods) {
Pattern p = Pattern.compile(m);
if (p.matcher(description.getMethodName()).matches()) {
return true;
}
}
return false;
}
@Override
public String describe() {
return "TestNG method filter";
}
}));
} catch (Throwable t) {
throw new TestNGException("Failure in JUnit mode for class " + testCase.getName(), t);
}
}
B.junit3 采用反射实现调用和执行
org.testng.junit.JUnit3TestRecognizer
public boolean isTest(Class c) {
//class implementing junit.framework.Test with at least one test* method
if (Test.class.isAssignableFrom(c)) {
boolean haveTest = false;
for (Method m : c.getMethods()) {
if (m.getName().startsWith("test")) {
haveTest = true;
break;
}
}
if (haveTest) {
return true;
}
}
try {
//or a class with public static Test suite() method
Method m = c.getDeclaredMethod("suite");
if (Modifier.isPublic(m.getModifiers()) && Modifier.isStatic(m.getModifiers())) {
return m.getReturnType().isAssignableFrom(Test.class);
}
} catch (Throwable t) {
return false;
}
return false;
}
官方Junit转为TestNG文档:
Using Eclipse
The easiest way to convert your JUnit tests to TestNG is to use the Eclipse TestNG plug-in refactoring support. You will find a full description of its features in the Eclipse section.
Asserts
Note that the class org.testng.Assert uses a different argument ordering than the ones used by JUnit. If you are porting code that uses JUnit's asserts, you might want to us a static import of that class:
import static org.testng.AssertJUnit.*;
|
Running JUnit Tests
TestNG can automatically recognize and run JUnit tests, so you can use TestNG as a runner for all your existing tests and write new tests using TestNG.
All you have to do is to put JUnit library on the TestNG classpath, so it can find and use JUnit classes, change your test runner from JUnit to TestNG in Ant and then run TestNG in "mixed" mode. This way you can have all your tests in the same project, even in the same package, and start using TestNG. This approach also allows you to convert your existing JUnit tests to TestNG incrementally.
Example - replacing JUnit Ant task with TestNG one
JUnit version:
< junit dir = "${work.dir}" errorproperty = "tests.failed" failureproperty = "tests.failed" fork = "true" >
|
< batchtest todir = "${build.test.results.dir}" >
|
< fileset dir = "${test.src.dir}" >
|
< include name = "**/*Test.*" />
|
< path path = "${run.test.classpath}" />
|
< propertyref prefix = "test-sys-prop." />
|
< mapper from = "test-sys-prop.*" to = "*" type = "glob" />
|
< jvmarg line = "${run.jvmargs}" />
|
TestNG version:
< taskdef name = "testng" classname = "org.testng.TestNGAntTask" classpath = "${run.test.classpath}" />
|
< fileset id = "mixed.tests" dir = "${test.src.dir}" >
|
< include name = "**/*Test.*" />
|
< testng mode = "mixed" classfilesetref = "mixed.tests" workingDir = "${work.dir}" failureProperty = "tests.failed" outputdir = "${build.test.results.dir}" >
|
< pathelement path = "${build.test.classes.dir}" />
|
< pathelement path = "${run.test.classpath}" />
|
< pathelement path = "${junit.lib}" />
|
< propertyref prefix = "test-sys-prop." />
|
< mapper from = "test-sys-prop.*" to = "*" type = "glob" />
|
< jvmarg line = "${run.jvmargs}" />
|
Related reading
分享到:
相关推荐
事实上,是TestNG在Java编程中率先 实现了利用注释进行测试,这使它成为JUnit的有力竞争对手。本文还讨论TestNG不同于JUnit 4的一些特性,并提议采用一些方法,使得这两个框架能继续互相补充,而不是互相竞争。
testng下一代测试框架
Selenium+Eclipse+Junit+TestNG自动化学习笔记
终极自动化测试环境搭建:Selenium+Eclipse+Junit+TestNG+Python。 使用最新版本软件,所有步骤均有截图,5个代码实例。
Abbot是一种JUnit扩展框架,主要目的是使GUI组件实现编程隔离,它还提供了一种验证GUI行为的简易方法。TestNG-Abbot是一种测试框架,它为GUI组件的测试带来了新的活力。本文介绍使用TestNG-Abbot测试GUI过程中难度...
testing-junit-testng
NULL 博文链接:https://darkranger.iteye.com/blog/814494
TestNG是一个测试框架,其灵感来自JUnit和NUnit的,但引入了一些新的功能,使其功能更强大,使用更方便。TestNG是一个开源自动化测试框架;TestNG表示下一代。 TestNG是类似于JUnit(特别是JUnit 4),但它不是一个...
junit-dataprovider:为JUnit提供一个类似于TestNG的dataprovider runner
终极自动化测试环境搭建:Selenium+Eclipse+Junit+TestNG+Python
maven3-junit-spock-testng-mixin-master.rar
TestNG类似于JUnit(特别是JUnit 4),但它不是JUnit框架的扩展。它的灵感来源于JUnit。它的目的是优于JUnit,尤其是在用于测试集成多类时。 TestNG的创始人是Cedric Beust(塞德里克·博伊斯特)。TestNG消除了大部分...
本文介绍和对比了TestNG和junit框架,并举出实例说明。 同时讲解了Mockito和Dbunit框架的使用
终极自动化测试环境搭建:Selenium+Eclipse+Junit+TestNG+Python
TestNG 不仅确实强大、创新、可扩展、灵活,它还展示了 Java Annotations(JDK 5.0 中的重大新特性)的有趣应用。 TestNG 的创造者是 Cedric Beust,他在 Java 编程领域非常出名,是 EJB 3 专家组的成员,也是其他...
testng1.5.8, 类似于junit的一个测试方法,但是比junit更强大
Selenium+Eclipse+Junit+TestNG,Selenium自动化测试框架以及使用步骤_详细备注
testng java junit 单元测试 测试驱动
junit-vintage-engine-5.6.2.jarjunit-vintage-engine-5.6.2.jarjunit-vintage-engine-5.6.2.jar