Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Use TestNG API to execute tests
Browse files Browse the repository at this point in the history
Issue #1
  • Loading branch information
sormuras committed Aug 30, 2018
1 parent 78eb11d commit ec20f38
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 21 deletions.
18 changes: 12 additions & 6 deletions src/main/java/org/testng/junit5/NGClassDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.junit.platform.commons.util.ReflectionUtils.isInnerClass;
import static org.junit.platform.commons.util.ReflectionUtils.isPublic;

import org.junit.platform.engine.TestSource;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.support.descriptor.AbstractTestDescriptor;
import org.junit.platform.engine.support.descriptor.ClassSource;
Expand All @@ -24,17 +23,24 @@ static boolean isCandidate(Class<?> candidate) {
return true;
}

static NGClassDescriptor newContainerDescriptor(UniqueId container, Class<?> candidate) {
UniqueId id = container.append("testng-class", candidate.getTypeName());
return new NGClassDescriptor(id, candidate.getSimpleName(), ClassSource.from(candidate));
static NGClassDescriptor newContainerDescriptor(UniqueId container, Class<?> testClass) {
UniqueId id = container.append("testng-class", testClass.getTypeName());
return new NGClassDescriptor(id, testClass);
}

private NGClassDescriptor(UniqueId uniqueId, String displayName, TestSource source) {
super(uniqueId, displayName, source);
private final Class<?> testClass;

private NGClassDescriptor(UniqueId uniqueId, Class<?> testClass) {
super(uniqueId, testClass.getSimpleName(), ClassSource.from(testClass));
this.testClass = testClass;
}

@Override
public Type getType() {
return Type.CONTAINER;
}

public Class<?> getTestClass() {
return testClass;
}
}
88 changes: 73 additions & 15 deletions src/main/java/org/testng/junit5/TestNGine.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.platform.engine.support.filter.ClasspathScanningSupport.buildClassNamePredicate;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Optional;
import org.junit.platform.commons.util.ClassFilter;
Expand All @@ -13,6 +14,11 @@
import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.support.descriptor.EngineDescriptor;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestNGListener;
import org.testng.ITestResult;
import org.testng.TestNG;
import org.testng.annotations.Test;

public class TestNGine implements TestEngine {
Expand Down Expand Up @@ -55,32 +61,27 @@ public void execute(ExecutionRequest request) {
TestDescriptor engine = request.getRootTestDescriptor();
EngineExecutionListener listener = request.getEngineExecutionListener();
listener.executionStarted(engine);

// iterate engine.getChildren() recursively and process each via:
// 1. tell the listener we started
// 2. try to execute the container/test and evaluate its result
// 3. tell the listener about the test execution result
for (TestDescriptor classDescriptor : engine.getChildren()) {
listener.executionStarted(classDescriptor);
for (TestDescriptor methodDescriptor : classDescriptor.getChildren()) {
listener.executionStarted(methodDescriptor);
TestExecutionResult result = executeMethod((NGMethodDescriptor) methodDescriptor);
listener.executionFinished(methodDescriptor, result);
}

Class<?>[] testClasses = {((NGClassDescriptor) classDescriptor).getTestClass()};
TestListener testListener = new TestListener(listener, classDescriptor.getUniqueId());

TestNG testNG = new TestNG(false);
testNG.addListener((ITestNGListener) testListener);
testNG.setTestClasses(testClasses);
testNG.run();

listener.executionFinished(classDescriptor, TestExecutionResult.successful());
}
listener.executionFinished(engine, TestExecutionResult.successful());
}

private TestExecutionResult executeMethod(NGMethodDescriptor descriptor) {
try {
Object target = descriptor.getMethod().getDeclaringClass().getConstructor().newInstance();
descriptor.getMethod().invoke(target);
} catch (ReflectiveOperationException e) {
return TestExecutionResult.failed(e);
}
return TestExecutionResult.successful();
}

public Optional<String> getGroupId() {
return Optional.of("org.testng");
}
Expand All @@ -92,4 +93,61 @@ public Optional<String> getArtifactId() {
public Optional<String> getVersion() {
return Optional.of("DEVELOPMENT");
}

class TestListener implements ITestListener {

final EngineExecutionListener platform;
final UniqueId classDescriptorId;

TestListener(EngineExecutionListener platform, UniqueId classDescriptorId) {
this.platform = platform;
this.classDescriptorId = classDescriptorId;
}

private TestDescriptor toDescriptor(ITestResult result) {
Method method = result.getMethod().getConstructorOrMethod().getMethod();
return NGMethodDescriptor.newMethodDescriptor(classDescriptorId, method);
}

@Override
public void onTestStart(ITestResult result) {
// System.out.println("TestListener.onTestStart" + " " + result);
platform.executionStarted(toDescriptor(result));
}

@Override
public void onTestSuccess(ITestResult result) {
// System.out.println("TestListener.onTestSuccess" + " " + result);
platform.executionFinished(toDescriptor(result), TestExecutionResult.successful());
}

@Override
public void onTestFailure(ITestResult result) {
// System.out.println("TestListener.onTestFailure" + " " + result);
platform.executionFinished(
toDescriptor(result), TestExecutionResult.failed(result.getThrowable()));
}

@Override
public void onTestSkipped(ITestResult result) {
// System.out.println("TestListener.onTestSkipped" + " " + result);
platform.executionSkipped(toDescriptor(result), "because");
}

@Override
public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
// System.out.println("TestListener.onTestFailedButWithinSuccessPercentage" + " " + result);
platform.executionFinished(toDescriptor(result), TestExecutionResult.successful());
}

@Override
public void onStart(ITestContext context) {
// System.out.println("TestListener.onStart" + " " + context);
}

@Override
public void onFinish(ITestContext context) {
// System.out.println("TestListener.onFinish" + " " + context);
}
}
}

0 comments on commit ec20f38

Please sign in to comment.