Mark unit test as expected failure in JUnit4

Is there an extension for JUnit4 that allows you to mark some tests as "expected failure"?

I would like to mark a test for the current functions in development using some tag, for example, @wip. For these tests, I would like to make sure that they do not work.

My eligibility criteria:

Scenario: A successful test tagged @wip is recorded as failure Given a successful test marked @wip When the test is executed Then the test is recorded as failure. Scenario: A failing test tagged @wip is recorded as fine Given a failing test tagged @wip When the test is executed Then the test is recorded as fine. Scenario: A successful test not tagged @wip is recorded as fine Given a successful test not tagged @wip When the test is executed Then the test is recorded as successful. Scenario: A failing test not tagged with @wip is recorded as failure Given a failing test not tagged with @wip When the test is executed Then the test is recorded as failure. 
+4
source share
2 answers

The short answer is , no extension will do this, as far as I know, and in my opinion, it will defeat the whole JUnit goal, if it exists.

The longer answer , red / green, is something sacred and circumventing should not become a habit. What if you accidentally forgot to remove the bypass and assume that all tests passed?

You can make it expect an AssertionError or Exception .

 @wip @Test(expected=AssertionError.class) public void wipTest() { fail("work in progress"); } 

Creating a shortcut in your IDE should not be too complicated for this. Of course, I assumed that you mark the test with annotation in the source code.

In my opinion, what you ask is against the purpose of JUnit, but I understand how to use it.

An alternative would be to implement a WIPRunner with WIP annotation and somehow force it to accept test failures with WIP annotation.

If you are integrating with the BDD base, I would suggest that it run the unit tests that you marked @wip separately and decide in your BDD methods if the result is ok.

+8
source

@Ignore annotations say you should not worry about the result.

+4
source

All Articles