`
longgangbai
  • 浏览: 7250267 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

TestNG简单的学习(十三)TestNG中Junit的实现

阅读更多

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.*"/>
    </batchtest>
    <classpath>
        <path path="${run.test.classpath}"/>
    </classpath>
    <syspropertyset>
        <propertyref prefix="test-sys-prop."/>
        <mapper from="test-sys-prop.*" to="*" type="glob"/>
    </syspropertyset>
    <formatter type="xml"/>
    <jvmarg value="-ea"/>
    <jvmarg line="${run.jvmargs}"/>
</junit>

TestNG version:

 
<taskdef name="testng" classname="org.testng.TestNGAntTask" classpath="${run.test.classpath}"/>
 
<fileset id="mixed.tests" dir="${test.src.dir}">
    <include name="**/*Test.*"/>
</fileset>
 
<testng mode="mixed" classfilesetref="mixed.tests" workingDir="${work.dir}" failureProperty="tests.failed" outputdir="${build.test.results.dir}">
    <classpath>
        <pathelement path="${build.test.classes.dir}"/>
        <pathelement path="${run.test.classpath}"/>
        <pathelement path="${junit.lib}"/>
    </classpath>
    <propertyset>
        <propertyref prefix="test-sys-prop."/>
        <mapper from="test-sys-prop.*" to="*" type="glob"/>
    </propertyset>
    <jvmarg line="${run.jvmargs}"/>
</testng>

 

Related reading

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics