Skip to content
Permalink
Browse files

mixed mode for TestNG - 2:

-better separation of older JUnit3 and JUnit4 runners
-improved test results reporting for invoked JUnit tests
  • Loading branch information...
lukasj committed Feb 4, 2012
1 parent 71126c8 commit f25820911efbd242cb0284133912a2bd178df8fb
@@ -540,19 +540,15 @@ public void setTestClasses(Class[] classes) {
suiteName = defaultIfStringEmpty(test.getSuiteName(), suiteName);
testName = defaultIfStringEmpty(test.getTestName(), testName);
} else {
if (m_isMixed && JUnitTestFinder.isJUnitTest(c)) {
isJUnit = true;
suiteName += "-junit";
testName += "-junit";
}
if (m_isMixed && JUnitTestFinder.isJUnitTest(c)) {
isJUnit = true;
testName = c.getName();
}
}
XmlSuite xmlSuite = suites.get(suiteName);
if (xmlSuite == null) {
xmlSuite = new XmlSuite();
xmlSuite.setName(suiteName);
if (isJUnit) {
xmlSuite.setJUnit(isJUnit);
}
suites.put(suiteName, xmlSuite);
}

@@ -570,6 +566,7 @@ public void setTestClasses(Class[] classes) {
if (xmlTest == null) {
xmlTest = new XmlTest(xmlSuite);
xmlTest.setName(testName);
xmlTest.setJUnit(isJUnit);
}

xmlTest.getXmlClasses().add(xmlClasses[i]);
@@ -81,6 +81,8 @@
transient private IConfigurationListener m_confListener= new ConfigurationListener();
transient private boolean m_skipFailedInvocationCounts;

transient private List<IInvokedMethodListener> m_invokedMethodListeners = Lists.newArrayList();

/**
* All the test methods we found, associated with their respective classes.
* Note that these test methods might belong to different classes.
@@ -189,6 +191,7 @@ private void init(IConfiguration configuration,
}

m_annotationFinder= annotationFinder;
m_invokedMethodListeners = invokedMethodListeners;
m_invoker = new Invoker(m_configuration, this, this, m_suite.getSuiteState(),
m_skipFailedInvocationCounts, invokedMethodListeners);

@@ -645,7 +648,7 @@ private void beforeRun() {
}

private void privateRunJUnit(XmlTest xmlTest) {
final ClassInfoMap cim = new ClassInfoMap(m_testClassesFromXml);
final ClassInfoMap cim = new ClassInfoMap(m_testClassesFromXml, false);
final Set<Class<?>> classes = cim.getClasses();
final List<ITestNGMethod> runMethods = Lists.newArrayList();
List<IWorker<ITestNGMethod>> workers = Lists.newArrayList();
@@ -674,6 +677,7 @@ public void run() {
methods.add(inc.getName());
}
IJUnitTestRunner tr= ClassHelper.createTestRunner(TestRunner.this);
tr.setInvokedMethodListeners(m_invokedMethodListeners);
try {
tr.run(tc, methods.toArray(new String[methods.size()]));
}
@@ -9,11 +9,17 @@

public class ClassInfoMap {
private Map<Class<?>, XmlClass> m_map = Maps.newHashMap();
private boolean includeNestedClasses;

public ClassInfoMap() {
}

public ClassInfoMap(List<XmlClass> classes) {
this(classes, true);
}

public ClassInfoMap(List<XmlClass> classes, boolean includeNested) {
includeNestedClasses = includeNested;
for (XmlClass xmlClass : classes) {
try {
Class c = xmlClass.getSupportClass();
@@ -29,7 +35,6 @@ public ClassInfoMap(List<XmlClass> classes) {
}

private void registerClass(Class cl, XmlClass xmlClass) {
boolean includeNestedClasses = true;
m_map.put(cl, xmlClass);
if (includeNestedClasses) {
for (Class c : cl.getClasses()) {
@@ -1,17 +1,20 @@
package org.testng.junit;

import java.util.List;
import org.testng.IInvokedMethodListener;
import org.testng.ITestNGMethod;
import org.testng.internal.ITestResultNotifier;

import java.util.List;


/**
* An abstraction interface over JUnit test runners.
*
* @author <a href='mailto:the_mindstorm@evolva.ro'>Alexandru Popescu</a>
*/
public interface IJUnitTestRunner {

void setInvokedMethodListeners(List<IInvokedMethodListener> listener);

void setTestResultNotifier(ITestResultNotifier notifier);

void run(Class junitTestClass, String... methods);
@@ -0,0 +1,14 @@
package org.testng.junit;

import junit.framework.Test;

/**
*
* @author lukas
*/
public class JUnit3TestClass extends JUnitTestClass {

public JUnit3TestClass(Test test) {
super(test.getClass());
}
}
@@ -0,0 +1,29 @@
package org.testng.junit;

import java.lang.reflect.Method;
import junit.framework.Test;
import org.testng.internal.Utils;

/**
*
* @author lukas
*/
public class JUnit3TestMethod extends JUnitTestMethod {

public JUnit3TestMethod(JUnitTestClass owner, Test test) {
super(owner, getMethod(test), test);
}

private static Method getMethod(Test t) {
String name = null;
try {
Method nameMethod = t.getClass().getMethod("getName");
name = (String) nameMethod.invoke(t);
return t.getClass().getMethod(name);
} catch (Throwable th) {
Utils.log("JUnit4TestMethod", 2,
"Method '" + name + "' not found in class '" + t + "': " + th.getMessage());
return null;
}
}
}
@@ -0,0 +1,14 @@
package org.testng.junit;

import org.junit.runner.Description;

/**
*
* @author lukas
*/
public class JUnit4TestClass extends JUnitTestClass {

public JUnit4TestClass(Description test) {
super(test.getTestClass());
}
}
@@ -0,0 +1,33 @@
package org.testng.junit;

import java.lang.reflect.Method;
import org.junit.runner.Description;
import org.testng.internal.Utils;

/**
*
* @author lukas
*/
public class JUnit4TestMethod extends JUnitTestMethod {

public JUnit4TestMethod(JUnitTestClass owner, Description desc) {
super(owner, getMethod(desc), desc);
}

@Override
public Object[] getInstances() {
return new Object[0];
}

private static Method getMethod(Description desc) {
Class<?> c = desc.getTestClass();
String method = desc.getMethodName();
try {
return c.getMethod(method);
} catch (Throwable t) {
Utils.log("JUnit4TestMethod", 2,
"Method '" + method + "' not found in class '" + c.getName() + "': " + t.getMessage());
return null;
}
}
}
@@ -8,10 +8,7 @@
import org.junit.runner.manipulation.Filter;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.testng.ITestListener;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.TestNGException;
import org.testng.*;
import org.testng.collections.Lists;
import org.testng.internal.ITestResultNotifier;
import org.testng.internal.InvokedMethod;
@@ -28,6 +25,7 @@
private ITestResultNotifier m_parentRunner;
private List<ITestNGMethod> m_methods = Lists.newArrayList();
private List<ITestListener> m_listeners = Lists.newArrayList();
private List<IInvokedMethodListener> m_invokeListeners = Lists.newArrayList();

public JUnit4TestRunner() {
}
@@ -54,6 +52,10 @@ public void setTestResultNotifier(ITestResultNotifier notifier) {
m_listeners = m_parentRunner.getTestListeners();
}

public void setInvokedMethodListeners(List<IInvokedMethodListener> listeners) {
m_invokeListeners = listeners;
}

/**
* A
* <code>start</code> implementation that ignores the
@@ -79,6 +81,9 @@ public Result start(final Class testCase, final String... methods) {

@Override
public boolean shouldRun(Description description) {
if (description == null) {
return false;
}
if (methods.length == 0) {
//run everything
return true;
@@ -113,6 +118,7 @@ public void testAssumptionFailure(Failure failure) {
tr.setStatus(TestResult.FAILURE);
tr.setEndMillis(Calendar.getInstance().getTimeInMillis());
tr.setThrowable(failure.getException());
m_parentRunner.addFailedTest(tr.getMethod(), tr);
for (ITestListener l : m_listeners) {
l.onTestFailure(tr);
}
@@ -126,6 +132,7 @@ public void testFailure(Failure failure) throws Exception {
tr.setStatus(TestResult.FAILURE);
tr.setEndMillis(Calendar.getInstance().getTimeInMillis());
tr.setThrowable(failure.getException());
m_parentRunner.addFailedTest(tr.getMethod(), tr);
for (ITestListener l : m_listeners) {
l.onTestFailure(tr);
}
@@ -138,11 +145,11 @@ public void testFinished(Description description) throws Exception {
if (!failures.contains(description)) {
tr.setStatus(TestResult.SUCCESS);
tr.setEndMillis(Calendar.getInstance().getTimeInMillis());
m_parentRunner.addPassedTest(tr.getMethod(), tr);
for (ITestListener l : m_listeners) {
l.onTestSuccess(tr);
}
}
m_parentRunner.addInvokedMethod(new InvokedMethod(tr.getTestClass(), tr.getMethod(), new Object[0], true, false, tr.getStartMillis(), tr));
m_methods.add(tr.getMethod());
}

@@ -152,23 +159,21 @@ public void testIgnored(Description description) throws Exception {
ITestResult tr = createTestResult(description);
tr.setStatus(TestResult.SKIP);
tr.setEndMillis(tr.getStartMillis());
m_parentRunner.addSkippedTest(tr.getMethod(), tr);
m_methods.add(tr.getMethod());
for (ITestListener l : m_listeners) {
l.onTestSkipped(tr);
}
m_parentRunner.addInvokedMethod(new InvokedMethod(tr.getTestClass(), tr.getMethod(), new Object[0], true, false, tr.getStartMillis(), tr));
m_methods.add(tr.getMethod());
}

@Override
public void testRunFinished(Result result) throws Exception {
super.testRunFinished(result);
//TODO: ITestContext to be implemented by JUnitTestRunner
}

@Override
public void testRunStarted(Description description) throws Exception {
super.testRunStarted(description);
//TODO: ITestContext to be implemented by JUnitTestRunner
}

@Override
@@ -182,8 +187,8 @@ public void testStarted(Description description) throws Exception {
}

private ITestResult createTestResult(Description test) {
JUnitUtils.JUnitTestClass tc = new JUnitUtils.JUnitTestClass(test);
JUnitUtils.JUnitTestMethod tm = new JUnitUtils.JUnitTestMethod(test, tc);
JUnit4TestClass tc = new JUnit4TestClass(test);
JUnitTestMethod tm = new JUnit4TestMethod(tc, test);

TestResult tr = new TestResult(tc,
test,
@@ -192,6 +197,11 @@ private ITestResult createTestResult(Description test) {
Calendar.getInstance().getTimeInMillis(),
0);

InvokedMethod im = new InvokedMethod(tr.getTestClass(), tr.getMethod(), new Object[0], true, false, tr.getStartMillis(), tr);
m_parentRunner.addInvokedMethod(im);
for (IInvokedMethodListener l: m_invokeListeners) {
l.beforeInvocation(im, tr);
}
return tr;
}
}

0 comments on commit f258209

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