diff --git a/src/main/java/org/testng/junit5/NGClassDescriptor.java b/src/main/java/org/testng/junit5/NGClassDescriptor.java index e2dc72b..06b00e9 100644 --- a/src/main/java/org/testng/junit5/NGClassDescriptor.java +++ b/src/main/java/org/testng/junit5/NGClassDescriptor.java @@ -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; @@ -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; + } } diff --git a/src/main/java/org/testng/junit5/TestNGine.java b/src/main/java/org/testng/junit5/TestNGine.java index 64bdabd..3e14c98 100644 --- a/src/main/java/org/testng/junit5/TestNGine.java +++ b/src/main/java/org/testng/junit5/TestNGine.java @@ -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; @@ -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 { @@ -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 getGroupId() { return Optional.of("org.testng"); } @@ -92,4 +93,61 @@ public Optional getArtifactId() { public Optional 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); + } + } }