The proper use of @Ignore in unit testing

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

What is the purpose of the @Ignore annotation in unit testing?

Either a test will be green and everything is fine or it is red so you fix it before commit. So if there is an ignored test, it is the same thing as dead code and we know what to do with that: delete it! Keep the code clean!

Now, there is actually a proper use of the @Ignore annotation, that is when you are refactoring the design.

You still can’t commit code with ignored tests as this will be confusing if spotted by a fellow programmers. But for a while, in your isolation you can find it useful to live with.

The scenario I’m thinking of is when you happily do your TDD test-implement-refactor cycle and run into a dead end in your design. You have written a failing test and discovered that, to create an implementation for the test, you must change the design. But when refactoring, you need to been in the green state, i e no failing tests. So what do you do?

The failing test must go away and you have two options, hide it with comments or use the @Ignore annotation. The latter is the better since while doing the redesign, you will have the test come up in your IDE as ignored during each run.

That way, you will never loose track of what got you started with the refactoring while you still can look for failing tests during the refactoring.

Once the redesign is done, you remove the ignore annotation and implement to pass the test. You have taken a big step forward without loosing control for a single moment.

Then you commit.

This is the single case I can think of that justifies the use of the @Ignore annotation.

Update: I know of a second case now, see comments. 🙂

2 Comments

  • 1
    2013-07-24 - 12:15 | Permalink

    I can see another case:

    If you have are in a team that depends on an API (or something similar) from another team, you can write a test that shows the API team the kind of functionality you want.
    And then you give them the test.
    If you keep your test in your testsuite, that would fail.
    Yet if you throw the test away, you lost a great way to verify if the API team has delivered what you asked. (Yes when you work with feature teams you don’t need this, yet not everyone is working in this situation, + maybe the API is from another company)

    When you work with the ignore tag, you keep track of what you asked, and when you got the new version you can remove the tag and see if the API does what you have asked for…

    • 2
      2013-07-24 - 17:32 | Permalink

      Thanks for that! Very cool to specify by tests what you want from an API team. Now I know two ways of using @Ignore. 🙂

  • Leave a Reply

    Your email address will not be published. Required fields are marked *