Closed ghilainm closed 8 years ago
Hi ghilainm, I think When you don't call the Assume.assumeTrue in the @After everything is not fine. The test is skipped. It is not successfully run. Could you please check this again?
If you do the following you will get a stack trace and the test is skipped.
... @Before public void init() { try { Assume.assumeTrue(false); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } } ...
+1 on this. I did a little more investigating on this and it appears that the test fails whenever assumptions fail in both the @Before
and @After
blocks. I added some lines to the dummy test method as well with the same result. Here's a further example of a test that should be skipped, but fails:
import org.junit.After;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.hamcrest.CoreMatchers;
public class ExampleTest {
@Before
public void init(){
Assume.assumeTrue(false);
}
//This test fails?
@Test
public void dummyTest(){
Assert.assertEquals(1, 1);
}
@After
public void cleanUp(){
Assume.assumeThat(1, CoreMatchers.equalTo(2));
}
}
Assumptions were never intended to be used in @After
methods. They were originally introduced for the Theories
runner to enable specification of preconditions for a @Theory
. The can be used in regular @Test
methods as well as in @Before
and @BeforeClass
. In the latter case, a special event is sent to RunListener
implementations, namely testAssumptionFailure
. Most IDEs and build tools will report this as "skipped" (like testIgnored
).
IMHO it does not make sense to use assumptions after the test has already been executed, i.e. in methods annotated with @After
or @AfterClass
. Maybe you can shed more light on what you are trying to accomplish?
It makes sense if you want to skip some clean-up which must be done after the test but cannot be done for some conditions.
I'm afraid you'll have to use an if
in this case.
^^, wouldn't it be possible to extends the support to the @After? Because it seems a convenient way of skipping code (and seems logical as it works for @Before)!
The assume feature is not meant to be used for skipping code. The intention of assume is to dynamically ignore tests.
Fine for me but then the test should be skipped as soon as I did the Assume.assumeTrue in the @Before which is even better for me!
It is skipped. The test test()
of the following example is skipped:
public class ExampleTest {
@Before
public void skipTest() {
assumeTrue(false);
fail();
}
@Test
public void test() {
fail();
}
}
See below the test class, with a dummy test doing nothing.
If I don't call the Assume.assumeTrue in the @After everything is fine. But calling the assumeTrue method in @Before and @After the JUnit test fails with the following error.