Browse Source

@BeforeAll failing should break build. Pr 63479

master
jkf 6 years ago
parent
commit
35d3b9ab11
6 changed files with 125 additions and 12 deletions
  1. +0
    -0
      src/etc/testcases/taskdefs/optional/TEST-org.example.junitlauncher.jupiter.JupiterSampleTestFailingBeforeAll.txt
  2. +18
    -0
      src/etc/testcases/taskdefs/optional/junitlauncher.xml
  3. +1
    -1
      src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java
  4. +46
    -11
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/junitlauncher/JUnitLauncherTaskTest.java
  5. +5
    -0
      src/tests/junit/org/example/junitlauncher/Tracker.java
  6. +55
    -0
      src/tests/junit/org/example/junitlauncher/jupiter/JupiterSampleTestFailingBeforeAll.java

+ 0
- 0
src/etc/testcases/taskdefs/optional/TEST-org.example.junitlauncher.jupiter.JupiterSampleTestFailingBeforeAll.txt View File


+ 18
- 0
src/etc/testcases/taskdefs/optional/junitlauncher.xml View File

@@ -300,6 +300,24 @@
</test>
</junitlauncher>
</target>
<target name="test-beforeall-failure-stops-build" depends="init">
<property name="junitlauncher.test.tracker.append.file" value="${output.dir}/${test-beforeall-failure-stops-build.tracker}"/>
<junitlauncher>
<listener classname="org.example.junitlauncher.Tracker"
if="test-beforeall-failure-stops-build.tracker"/>
<test name="org.example.junitlauncher.jupiter.JupiterSampleTestFailingBeforeAll" haltOnFailure="true"/>
</junitlauncher>
</target>

<target name="test-beforeall-failure-continues-build" depends="init">
<property name="junitlauncher.test.tracker.append.file" value="${output.dir}/${test-beforeall-failure-continues-build.tracker}"/>
<junitlauncher>
<listener classname="org.example.junitlauncher.Tracker"
if="test-beforeall-failure-continues-build.tracker"/>
<test name="org.example.junitlauncher.jupiter.JupiterSampleTestFailingBeforeAll"/>
</junitlauncher>
</target>

<target name="test-method-with-tag-fileset" depends="init">
<property name="junitlauncher.test.tracker.append.file" value="${output.dir}/${test-method-with-tag-fileset.tracker}"/>


+ 1
- 1
src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java View File

@@ -304,7 +304,7 @@ public class LauncherSupport {
// print the summary to System.out
summary.printTo(new PrintWriter(System.out, true));
}
final boolean hasTestFailures = summary.getTestsFailedCount() != 0;
final boolean hasTestFailures = summary.getTotalFailureCount() != 0;
if (hasTestFailures) {
// keep track of the test failure(s) for the entire launched instance
this.testsFailed = true;


+ 46
- 11
src/tests/junit/org/apache/tools/ant/taskdefs/optional/junitlauncher/JUnitLauncherTaskTest.java View File

@@ -17,12 +17,24 @@
*/
package org.apache.tools.ant.taskdefs.optional.junitlauncher;

import static org.example.junitlauncher.Tracker.verifyFailed;
import static org.example.junitlauncher.Tracker.verifySetupFailed;
import static org.example.junitlauncher.Tracker.verifySkipped;
import static org.example.junitlauncher.Tracker.verifySuccess;
import static org.example.junitlauncher.Tracker.wasTestRun;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileRule;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.JUnitLauncherTask;
import org.apache.tools.ant.util.LoaderUtils;
import org.example.junitlauncher.jupiter.JupiterSampleTest;
import org.example.junitlauncher.jupiter.JupiterSampleTestFailingBeforeAll;
import org.example.junitlauncher.jupiter.JupiterTagSampleTest;
import org.example.junitlauncher.vintage.AlwaysFailingJUnit4Test;
import org.example.junitlauncher.vintage.ForkedTest;
@@ -32,16 +44,6 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.example.junitlauncher.Tracker.verifyFailed;
import static org.example.junitlauncher.Tracker.verifySkipped;
import static org.example.junitlauncher.Tracker.verifySuccess;
import static org.example.junitlauncher.Tracker.wasTestRun;

/**
* Tests the {@link JUnitLauncherTask}
*/
@@ -78,7 +80,7 @@ public class JUnitLauncherTaskTest {
}
}
}
/**
* Tests that when a test, that's isn't configured with {@code haltOnFailure=true}, continues the
* build even when there are test failures
@@ -368,6 +370,39 @@ public class JUnitLauncherTaskTest {
Assert.assertFalse("testMethodIncludeTagisNotExecutedTagSampleTest2 was expected NOT to be run", wasTestRun(tracker2, JupiterTagSampleTest.class.getName(),
"testMethodIncludeTagisNotExecutedTagSampleTest2"));
}

/**
* Tests that failure at with beforeall stops the build
*/
@Test
public void testBeforeAllFailureStopsBuild() throws Exception {
final String targetName = "test-beforeall-failure-stops-build";
final Path trackerFile = setupTrackerProperty(targetName);
try {
buildRule.executeTarget(targetName);
Assert.fail(targetName + " was expected to fail");
} catch (BuildException e) {
// expected, but do further tests to make sure the build failed for expected reason
if (!verifySetupFailed(trackerFile, JupiterSampleTestFailingBeforeAll.class.getName())) {
// throw back the original cause
throw e;
}
}
}
/**
* Tests that when a test, that's isn't configured with {@code haltOnFailure=true}, continues the
* build even when there are test failures
*/
@Test
public void testBeforeAllFailureContinuesBuild() throws Exception {
final String targetName = "test-beforeall-failure-continues-build";
final Path trackerFile = setupTrackerProperty(targetName);
buildRule.executeTarget(targetName);
Assert.assertTrue("Expected @BeforeAll failure to lead to failing testcase", verifySetupFailed(trackerFile, JupiterSampleTestFailingBeforeAll.class.getName()));
}


/**
* Tests execution of a test which is configured to execute only methods with special tags, two classes specified


+ 5
- 0
src/tests/junit/org/example/junitlauncher/Tracker.java View File

@@ -143,6 +143,11 @@ public class Tracker implements TestResultFormatter {
final List<String> lines = readTrackerFile(trackerFile);
return lines.contains(TestExecutionResult.Status.FAILED + ":test-method:" + className + "#" + methodName);
}
public static boolean verifySetupFailed(final Path trackerFile, final String className) throws IOException {
final List<String> lines = readTrackerFile(trackerFile);
return lines.contains(TestExecutionResult.Status.FAILED + ":test-class:" + className);
}

public static boolean verifySuccess(final Path trackerFile, final String className, final String methodName) throws IOException {
final List<String> lines = readTrackerFile(trackerFile);


+ 55
- 0
src/tests/junit/org/example/junitlauncher/jupiter/JupiterSampleTestFailingBeforeAll.java View File

@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.example.junitlauncher.jupiter;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
*
*/
public class JupiterSampleTestFailingBeforeAll {

private static final String message = "The quick brown fox jumps over the lazy dog";

@BeforeAll
static void beforeAll() {
throw new RuntimeException("Intentional failure");
}

@BeforeEach
void beforeEach() {
}

@Test
void testSucceeds() {
System.out.println(message);
System.out.print("<some-other-message>Hello world! <!-- some comment --></some-other-message>");
}

@AfterEach
void afterEach() {
}

@AfterAll
static void afterAll() {
}
}

Loading…
Cancel
Save