Skip to content
Permalink
Browse files

initial checkin of mixed mode for TestNG:

-added '-mixed' cmd line switch and mode="TESTNG|JUNIT|MIXED" ant task attribute

-when this mode is selected and some version of JUnit is found on the classpath then TestNG tries to automatically recognize the type of test and run it using appropriate test runner (testng for testng, junitcore for junit4/3 - if junit4 is found or junit 3 runner - if only junit3 is found)
  • Loading branch information...
lukasj committed Feb 1, 2012
1 parent 1522307 commit 71126c80df98e529c5175248137128ace55d2576
@@ -27,7 +27,7 @@ JUNIT.REPORT.DIR =${junit.report.dir}
TESTNG.REPORT.DIR=${testng.report.dir}
</echo>
</target>

<target name="compile" depends="prepare">
<echo message=" -- Compiling tests --"/>

@@ -38,15 +38,15 @@ TESTNG.REPORT.DIR=${testng.report.dir}
srcdir="${test.dir}"
destdir="${test.build.dir}"
/>

</target>

<target name="prepare">
<tstamp/>
<mkdir dir="${test.build.dir}"/>
<mkdir dir="${junit.report.dir}"/>
<mkdir dir="${testng.report.dir}"/>

<taskdef name="testng"
classname="org.testng.TestNGAntTask"
classpath="${build.dir}"/>
@@ -35,6 +35,11 @@
@Parameter(names = JUNIT, description ="JUnit mode")
public Boolean junit = Boolean.FALSE;

public static final String MIXED = "-mixed";
@Parameter(names = MIXED, description ="Mixed mode - autodetect the type of current test" +
" and run it with appropriate runner")
public Boolean mixed = Boolean.FALSE;

public static final String LISTENER = "-listener";
@Parameter(names = LISTENER, description = "List of .class files or list of class names" +
" implementing ITestListener or ISuiteListener")
@@ -64,6 +64,7 @@
import java.util.concurrent.TimeUnit;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.testng.junit.JUnitTestFinder;

/**
* This class is the main entry point for running tests in the TestNG framework.
@@ -129,6 +130,7 @@
private String[] m_excludedGroups;

private Boolean m_isJUnit = XmlSuite.DEFAULT_JUNIT;
private Boolean m_isMixed = XmlSuite.DEFAULT_MIXED;
protected boolean m_useDefaultListeners = true;

private ITestRunnerFactory m_testRunnerFactory;
@@ -497,7 +499,13 @@ public void setTestClasses(Class[] classes) {
//
// Add the method tags
//
List<XmlClass> xmlClasses = result.get(0).getTests().get(0).getXmlClasses();
List<XmlClass> xmlClasses = Lists.newArrayList();
for (XmlSuite s : result) {
for (XmlTest t : s.getTests()) {
xmlClasses.addAll(t.getClasses());
}
}

for (XmlClass xc : xmlClasses) {
for (String m : commandLineMethods) {
String[] split = splitMethod(m);
@@ -527,14 +535,24 @@ public void setTestClasses(Class[] classes) {
ITestAnnotation test = (ITestAnnotation) finder.findAnnotation(c, ITestAnnotation.class);
String suiteName = getDefaultSuiteName();
String testName = getDefaultTestName();
boolean isJUnit = false;
if (test != null) {
suiteName = defaultIfStringEmpty(test.getSuiteName(), suiteName);
testName = defaultIfStringEmpty(test.getTestName(), testName);
} else {
if (m_isMixed && JUnitTestFinder.isJUnitTest(c)) {
isJUnit = true;
suiteName += "-junit";
testName += "-junit";
}
}
XmlSuite xmlSuite = suites.get(suiteName);
if (xmlSuite == null) {
xmlSuite = new XmlSuite();
xmlSuite.setName(suiteName);
if (isJUnit) {
xmlSuite.setJUnit(isJUnit);
}
suites.put(suiteName, xmlSuite);
}

@@ -1343,6 +1361,7 @@ protected void configure(CommandLineArgs cla) {
setTestJar(cla.testJar);
setXmlPathInJar(cla.xmlPathInJar);
setJUnit(cla.junit);
setMixed(cla.mixed);
setMaster(cla.master);
setSlave(cla.slave);
setSkipFailedInvocationCounts(cla.skipFailedInvocationCounts);
@@ -1479,6 +1498,7 @@ public void configure(Map cmdLineArgs) {
result.testJar = (String) cmdLineArgs.get(CommandLineArgs.TEST_JAR);
result.xmlPathInJar = (String) cmdLineArgs.get(CommandLineArgs.XML_PATH_IN_JAR);
result.junit = (Boolean) cmdLineArgs.get(CommandLineArgs.JUNIT);
result.mixed = (Boolean) cmdLineArgs.get(CommandLineArgs.MIXED);
result.master = (String) cmdLineArgs.get(CommandLineArgs.MASTER);
result.slave = (String) cmdLineArgs.get(CommandLineArgs.SLAVE);
result.skipFailedInvocationCounts = (Boolean) cmdLineArgs.get(
@@ -1590,6 +1610,15 @@ public void setJUnit(Boolean isJUnit) {
m_isJUnit = isJUnit;
}

/**
* Specify if this run should be made in mixed mode
*
* @param isJUnit
*/
public void setMixed(Boolean isMixed) {
m_isMixed = isMixed;
}

/**
* @deprecated The TestNG version is now established at load time. This
* method is not required anymore and is now a no-op.
@@ -1639,6 +1668,13 @@ protected static void validateCommandLineParameters(CommandLineArgs args) {
throw new ParameterException(CommandLineArgs.SLAVE + " can't be combined with "
+ CommandLineArgs.MASTER);
}

Boolean junit = args.junit;
Boolean mixed = args.mixed;
if (junit && mixed) {
throw new ParameterException(CommandLineArgs.MIXED + " can't be combined with "
+ CommandLineArgs.JUNIT);
}
}

/**
@@ -113,7 +113,6 @@
protected File m_testjar;
protected File m_workingDir;
private Integer m_timeout;
protected Boolean m_isJUnit;
private List<String> m_listeners= Lists.newArrayList();
private List<String> m_methodselectors= Lists.newArrayList();
private String m_objectFactory;
@@ -151,7 +150,12 @@
private String m_testName="Ant test";
private Boolean m_skipFailedInvocationCounts;
private String m_methods;
private Mode mode = Mode.TESTNG;

public enum Mode {
TESTNG, JUNIT, MIXED;
}

/**
* The list of report listeners added via &lt;reporter&gt; sub-element of the Ant task
*/
@@ -367,7 +371,12 @@ public void setTestName(String s) {

// TestNG settings
public void setJUnit(boolean value) {
m_isJUnit= Boolean.valueOf(value);
mode = value ? Mode.JUNIT : Mode.TESTNG;
}

// TestNG settings
public void setMode(Mode mode) {
this.mode = mode;
}

/**
@@ -528,7 +537,8 @@ public void execute() throws BuildException {

private List<String> createArguments() {
List<String> argv= Lists.newArrayList();
addBooleanIfTrue(argv, CommandLineArgs.JUNIT, m_isJUnit);
addBooleanIfTrue(argv, CommandLineArgs.JUNIT, mode == Mode.JUNIT);
addBooleanIfTrue(argv, CommandLineArgs.MIXED, mode == Mode.MIXED);
addBooleanIfTrue(argv, CommandLineArgs.SKIP_FAILED_INVOCATION_COUNTS, m_skipFailedInvocationCounts);
addIntegerIfNotNull(argv, CommandLineArgs.LOG, m_verbose);
addDefaultListeners(argv);
@@ -52,6 +52,7 @@
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.testng.xml.*;

/**
* This class takes care of running one Test.
@@ -644,7 +645,7 @@ private void beforeRun() {
}

private void privateRunJUnit(XmlTest xmlTest) {
ClassInfoMap cim = new ClassInfoMap(m_testClassesFromXml);
final ClassInfoMap cim = new ClassInfoMap(m_testClassesFromXml);
final Set<Class<?>> classes = cim.getClasses();
final List<ITestNGMethod> runMethods = Lists.newArrayList();
List<IWorker<ITestNGMethod>> workers = Lists.newArrayList();
@@ -667,9 +668,14 @@ public long getTimeOut() {
@Override
public void run() {
for(Class<?> tc: classes) {
List<XmlInclude> includedMethods = cim.getXmlClass(tc).getIncludedMethods();
List<String> methods = Lists.newArrayList();
for (XmlInclude inc: includedMethods) {
methods.add(inc.getName());
}
IJUnitTestRunner tr= ClassHelper.createTestRunner(TestRunner.this);
try {
tr.run(tc);
tr.run(tc, methods.toArray(new String[methods.size()]));
}
catch(Exception ex) {
ex.printStackTrace();
@@ -14,7 +14,7 @@
public interface IJUnitTestRunner {
void setTestResultNotifier(ITestResultNotifier notifier);

void run(Class junitTestClass);
void run(Class junitTestClass, String... methods);

List<ITestNGMethod> getTestMethods();

@@ -0,0 +1,41 @@
package org.testng.junit;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import junit.framework.Test;

/**
*
* @author lukas
*/
public class JUnit3TestRecognizer implements JUnitTestRecognizer {

public 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;
}
}
@@ -0,0 +1,27 @@
package org.testng.junit;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
*
* @author lukas
*/
public final class JUnit4TestRecognizer implements JUnitTestRecognizer {

public JUnit4TestRecognizer() {
}

public boolean isTest(Class c) {
boolean haveTest = false;
for (Method m : c.getMethods()) {
for (Annotation a : m.getDeclaredAnnotations()) {
if (org.junit.Test.class.isAssignableFrom(a.annotationType())) {
haveTest = true;
break;
}
}
}
return haveTest;
}
}
@@ -5,6 +5,7 @@
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.testng.ITestListener;
@@ -61,20 +62,40 @@ public void setTestResultNotifier(ITestResultNotifier notifier) {
* @param testClass the JUnit test class
*/
@Override
public void run(Class testClass) {
start(testClass);
public void run(Class testClass, String... methods) {
start(testClass, methods);
}

/**
* Starts a test run. Analyzes the command line arguments and runs the given
* test suite.
*/
public Result start(Class testCase) {
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);
return core.run(r.filterWith(new Filter() {

@Override
public boolean shouldRun(Description description) {
if (methods.length == 0) {
//run everything
return true;
}
for (String m: methods) {
if (m.equals(description.getMethodName())) {
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);
}

0 comments on commit 71126c8

Please sign in to comment.
You can’t perform that action at this time.