Why is a junit test that is skipped because of an assumption failure is not reported as skipped?
I use junit assumptions to decide whether to run a test or not.
Tests where the assumption fails are ignored/skipped by the junit framework.I wonder why skipped tests are not reported as 'skipped'?
Please have a look at the example:
import static org.junit.Assert.fail;
import org.junit.Assume;
import org.junit.Test;
public class Assum开发者_StackOverflowptionTest {
@Test
public void skipAssumptionFailureTest() {
Assume.assumeTrue("foo".equals("bar"));
fail("This should not be reached!");
}
}
Running this test in a maven project results in:
Running AssumptionTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.015 sec
I would prefer to have this test reported as 'skipped'. Is there any chance to achieve this?
(junit 4.8.1; maven 2.2.1; java 1.6.0_21)
While previous answers are good on their own, I'd like to address the "WHY" question.
The problem stemmed from the fact how tests failed / ignored were counted. When assumptions were added, usual counts were kinda skewered (assumption can IGNORE the tests half-way through it's run).
Tools runners had problems with that as well, causing messy reporting. See here for Surefire dev conversation with JUnit people, and here for related Eclipse bug.
Right now this ain't a problem in Maven, and it should not be in newer Eclipses. I use STS based on eclipse.buildId = 2.9.2.201205071000-RELEASE
and I still experience this behaviour.
EDIT: reference type links did not work.
Spring brings another variant of an Ignore annotation, one that is able to decide at runtime whether to ignore a test: @IfProfileValue
. In combination with a user supplied ProfileValueSource
in a @ProfileValueSourceConfiguration
, you can do some limited tricks.
See Spring Test Documentation.
But it is not the same as being able to perform some test code and in the middle of the test method decide to ignore the test, as you can with assumptions.
You can't do this out-of-the-box as the only way to skip tests is with the @Ignore
annotation. However, I found a blog post which might just be what you are looking for:
on maven the solution is quite simple, just add this on plugin section in your pom.xml !
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
精彩评论