Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mark unit test as an expected failure in JUnit4

Is there an extension for JUnit4 which allows for marking some tests as "expected to fail"?

I would like to mark the test for current features under development with some tag, for instance @wip. For these tests I would like to ensure that they are failing.

My acceptance 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.
like image 379
Alex Schwartz Avatar asked May 04 '11 21:05

Alex Schwartz


1 Answers

Short answer, no extension will do that as far as I know and in my opinion it would defeat the whole purpose of JUnit if it would exist.

Longer answer, red/green is kind of sacred and circumventing it shouldn't become a habit. What if you accidentally forgot to remove the circumvention and assume that all tests passed?

You could make it expect an AssertionError or Exception.

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

Making a shortcut in your IDE for that shouldn't be too hard. Of course I was assuming you tag the test with an annotation in the source code.

In my opinion what you are asking is against JUnit's purpose, but I do understand the use for it.

An alternative would be to implement a WIPRunner with the WIP annotation and somehow make it accept failures of tests with the WIP annotation.

If you are integrating with a BDD framework I would suggest a way to let it run the unit tests you marked @wip seperately and decide within your BDD methods if the result is ok.

like image 183
Gressie Avatar answered Sep 28 '22 02:09

Gressie