Search in sources :

Example 1 with TestSource

use of org.junit.platform.engine.TestSource in project junit5 by junit-team.

the class SummaryGenerationTests method canGetListOfFailures.

@Test
void canGetListOfFailures() {
    RuntimeException failedException = new RuntimeException("Pow!");
    TestDescriptorStub testDescriptor = new TestDescriptorStub(UniqueId.root("root", "1"), "failingTest") {

        @Override
        public Optional<TestSource> getSource() {
            return Optional.of(ClassSource.from(Object.class));
        }
    };
    TestIdentifier failingTest = TestIdentifier.from(testDescriptor);
    listener.testPlanExecutionStarted(testPlan);
    listener.executionStarted(failingTest);
    listener.executionFinished(failingTest, TestExecutionResult.failed(failedException));
    listener.testPlanExecutionFinished(testPlan);
    final List<TestExecutionSummary.Failure> failures = listener.getSummary().getFailures();
    assertThat(failures).hasSize(1);
    assertThat(failures.get(0).getException()).isEqualTo(failedException);
    assertThat(failures.get(0).getTestIdentifier()).isEqualTo(failingTest);
}
Also used : TestSource(org.junit.platform.engine.TestSource) TestIdentifier(org.junit.platform.launcher.TestIdentifier) TestDescriptorStub(org.junit.platform.engine.test.TestDescriptorStub) Test(org.junit.jupiter.api.Test)

Example 2 with TestSource

use of org.junit.platform.engine.TestSource in project junit5 by junit-team.

the class TestIdentifier method from.

/**
 * Factory for creating a new {@link TestIdentifier} from a {@link TestDescriptor}.
 */
@API(status = INTERNAL, since = "1.0")
public static TestIdentifier from(TestDescriptor testDescriptor) {
    Preconditions.notNull(testDescriptor, "TestDescriptor must not be null");
    String uniqueId = testDescriptor.getUniqueId().toString();
    String displayName = testDescriptor.getDisplayName();
    TestSource source = testDescriptor.getSource().orElse(null);
    Set<TestTag> tags = testDescriptor.getTags();
    Type type = testDescriptor.getType();
    String parentId = testDescriptor.getParent().map(parentDescriptor -> parentDescriptor.getUniqueId().toString()).orElse(null);
    String legacyReportingName = testDescriptor.getLegacyReportingName();
    return new TestIdentifier(uniqueId, displayName, source, tags, type, parentId, legacyReportingName);
}
Also used : Preconditions(org.junit.platform.commons.util.Preconditions) ToStringBuilder(org.junit.platform.commons.util.ToStringBuilder) STABLE(org.apiguardian.api.API.Status.STABLE) TestSource(org.junit.platform.engine.TestSource) Set(java.util.Set) API(org.apiguardian.api.API) INTERNAL(org.apiguardian.api.API.Status.INTERNAL) Serializable(java.io.Serializable) Objects(java.util.Objects) Collections.unmodifiableSet(java.util.Collections.unmodifiableSet) Type(org.junit.platform.engine.TestDescriptor.Type) Optional(java.util.Optional) TestTag(org.junit.platform.engine.TestTag) TestDescriptor(org.junit.platform.engine.TestDescriptor) LinkedHashSet(java.util.LinkedHashSet) Type(org.junit.platform.engine.TestDescriptor.Type) TestSource(org.junit.platform.engine.TestSource) TestTag(org.junit.platform.engine.TestTag) API(org.apiguardian.api.API)

Example 3 with TestSource

use of org.junit.platform.engine.TestSource in project intellij-community by JetBrains.

the class JUnit5TestExecutionListener method getLocationHintValue.

static String getLocationHintValue(TestSource testSource, boolean isTest) {
    if (testSource instanceof CompositeTestSource) {
        CompositeTestSource compositeTestSource = ((CompositeTestSource) testSource);
        for (TestSource sourceFromComposite : compositeTestSource.getSources()) {
            String locationHintValue = getLocationHintValue(sourceFromComposite, isTest);
            if (!NO_LOCATION_HINT_VALUE.equals(locationHintValue)) {
                return locationHintValue;
            }
        }
        return NO_LOCATION_HINT_VALUE;
    }
    if (testSource instanceof FileSource) {
        FileSource fileSource = (FileSource) testSource;
        File file = fileSource.getFile();
        String line = fileSource.getPosition().map(position -> ":" + position.getLine()).orElse("");
        return "file://" + file.getAbsolutePath() + line;
    }
    if (testSource instanceof MethodSource) {
        MethodSource methodSource = (MethodSource) testSource;
        return javaLocation(methodSource.getClassName(), methodSource.getMethodName(), isTest);
    }
    if (testSource instanceof ClassSource) {
        String className = ((ClassSource) testSource).getClassName();
        return javaLocation(className, null, isTest);
    }
    return NO_LOCATION_HINT_VALUE;
}
Also used : ReportEntry(org.junit.platform.engine.reporting.ReportEntry) TestPlan(org.junit.platform.launcher.TestPlan) JUnit4TestListener(com.intellij.junit4.JUnit4TestListener) CompositeTestSource(org.junit.platform.engine.support.descriptor.CompositeTestSource) TestExecutionResult(org.junit.platform.engine.TestExecutionResult) ClassSource(org.junit.platform.engine.support.descriptor.ClassSource) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) AssertionFailedError(org.opentest4j.AssertionFailedError) Map(java.util.Map) ExpectedPatterns(com.intellij.junit4.ExpectedPatterns) MapSerializerUtil(com.intellij.rt.execution.junit.MapSerializerUtil) MethodSource(org.junit.platform.engine.support.descriptor.MethodSource) PrintStream(java.io.PrintStream) PrintWriter(java.io.PrintWriter) ComparisonFailureData(com.intellij.rt.execution.junit.ComparisonFailureData) FileSource(org.junit.platform.engine.support.descriptor.FileSource) MultipleFailuresError(org.opentest4j.MultipleFailuresError) StringWriter(java.io.StringWriter) TestSource(org.junit.platform.engine.TestSource) Set(java.util.Set) File(java.io.File) TestIdentifier(org.junit.platform.launcher.TestIdentifier) ValueWrapper(org.opentest4j.ValueWrapper) TestExecutionListener(org.junit.platform.launcher.TestExecutionListener) CompositeTestSource(org.junit.platform.engine.support.descriptor.CompositeTestSource) CompositeTestSource(org.junit.platform.engine.support.descriptor.CompositeTestSource) TestSource(org.junit.platform.engine.TestSource) FileSource(org.junit.platform.engine.support.descriptor.FileSource) MethodSource(org.junit.platform.engine.support.descriptor.MethodSource) File(java.io.File) ClassSource(org.junit.platform.engine.support.descriptor.ClassSource)

Example 4 with TestSource

use of org.junit.platform.engine.TestSource in project ant by apache.

the class AbstractJUnitResultFormatter method isTestClass.

static Optional<ClassSource> isTestClass(final TestIdentifier testIdentifier) {
    if (testIdentifier == null) {
        return Optional.empty();
    }
    final Optional<TestSource> source = testIdentifier.getSource();
    if (!source.isPresent()) {
        return Optional.empty();
    }
    final TestSource testSource = source.get();
    if (testSource instanceof ClassSource) {
        return Optional.of((ClassSource) testSource);
    }
    return Optional.empty();
}
Also used : TestSource(org.junit.platform.engine.TestSource) ClassSource(org.junit.platform.engine.support.descriptor.ClassSource)

Example 5 with TestSource

use of org.junit.platform.engine.TestSource in project junit5 by junit-team.

the class TestFactoryTestDescriptor method invokeTestMethod.

// --- Node ----------------------------------------------------------------
@Override
protected void invokeTestMethod(JupiterEngineExecutionContext context, DynamicTestExecutor dynamicTestExecutor) {
    ExtensionContext extensionContext = context.getExtensionContext();
    context.getThrowableCollector().execute(() -> {
        Object instance = extensionContext.getRequiredTestInstance();
        Object testFactoryMethodResult = executableInvoker.invoke(getTestMethod(), instance, extensionContext, context.getExtensionRegistry());
        TestSource source = getSource().orElseThrow(() -> new JUnitException("Illegal state: TestSource must be present"));
        try (Stream<DynamicNode> dynamicNodeStream = toDynamicNodeStream(testFactoryMethodResult)) {
            int index = 1;
            Iterator<DynamicNode> iterator = dynamicNodeStream.iterator();
            while (iterator.hasNext()) {
                DynamicNode dynamicNode = iterator.next();
                Optional<JupiterTestDescriptor> descriptor = createDynamicDescriptor(this, dynamicNode, index++, source, getDynamicDescendantFilter());
                descriptor.ifPresent(dynamicTestExecutor::execute);
            }
        } catch (ClassCastException ex) {
            throw invalidReturnTypeException(ex);
        }
    });
}
Also used : TestSource(org.junit.platform.engine.TestSource) JUnitException(org.junit.platform.commons.JUnitException) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) DynamicNode(org.junit.jupiter.api.DynamicNode)

Aggregations

TestSource (org.junit.platform.engine.TestSource)7 Test (org.junit.jupiter.api.Test)3 TestIdentifier (org.junit.platform.launcher.TestIdentifier)3 File (java.io.File)2 Set (java.util.Set)2 ClassSource (org.junit.platform.engine.support.descriptor.ClassSource)2 TestDescriptorStub (org.junit.platform.engine.test.TestDescriptorStub)2 ExpectedPatterns (com.intellij.junit4.ExpectedPatterns)1 JUnit4TestListener (com.intellij.junit4.JUnit4TestListener)1 ComparisonFailureData (com.intellij.rt.execution.junit.ComparisonFailureData)1 MapSerializerUtil (com.intellij.rt.execution.junit.MapSerializerUtil)1 PrintStream (java.io.PrintStream)1 PrintWriter (java.io.PrintWriter)1 Serializable (java.io.Serializable)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Collections.unmodifiableSet (java.util.Collections.unmodifiableSet)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1