The proper use of @Ignore in unit testing

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 responses on “The proper use of @Ignore in unit testing

  1. 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…

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.