views:

366

answers:

5

I'm working on a project at the moment that we're using junit to test, but as its still fairly early stages a lot of features aren't yet implemented, though they already have tests written for them

this means these tests (obviously) always fail

I was wondering if anyone knew a way to get JUnit to pass the test while displaying a warning. preferably with a customizable message so we can note that the feature is not yet implemented.

The point of this being that we want to code to compile only if all tests pass, and at the moment this simply isn't possible.

I realise we can just comment out or remove the problem tests, but we then run the risk of forgetting to add them back in later.

A: 

You can @Ignore in front of the test method. It then depends on the test runner what kind of output you get. You can get something like successfully run 25 tests and 6 tests are ignored...

All further things depend on how you want to run the unit tests.

Janusz
This doesn't seem to generate any kind of indication of ignored tests for me, but thanks all the same, definitely something to keep in mind
Josh Bones
seems that this is te closest I could get, thanks mate
Josh Bones
A: 

You could throw a NotImplementedException in your new methods:

Apache Commons NotImplementedException

e.g.

public String getName() {
    throw new NotImplementedException("Oh so soon...");
}

and then specify in your JUnit test that it should expect this exception to be thrown:

@Test (expected=NotImplementedException.class) 
public void testGetName()  {
    myKingdom.getName();
}

That serves as a reminder to everyone to implement that functionality, of course when they do add it (or remove it by mistake) the unit test will notice that the exception is no longer thrown and throw a big wobbly.

Jon
This is what I'm currently doing, but since it just happily passes the test with no indication that anything is out of the normal (because nothing is) it can be fairly easy to miss the fact that the feature is still not implemented. But thanks anyway
Josh Bones
+5  A: 

Most JUnit runners I've seen will give you one of four statuses for a test case: passed, failed, had errors or ignored.

As had errors (threw an Exception) is reported in a similar fashion as failed ("red result") there isn't a good way to generate a warning and still have a "green result" - I assume that is what you are looking for?

As mentioned by Janusz, you can use the @Ignore attribute to ignore a test case which can contain a message as well:

@Ignore("disabled until implementation is finished")
public void testMe() {
   //do something
}

Most runners will not list them explicitely in the results, but you are able to look for any ignored test cases automatically using tools, by looking for the @Ignore attribute and generate a report afterwards, which lists all test cases which have been skipped and the reason (given in the attribute) why.

Christian Hang
+1  A: 

Would org.junit.Assume do what you need?

lumpynose
A: 

Unit testing works best when the tests are full i.e. they do something and when the fail something is wrong. they are thus not designed to "half fail" or "pass with a warning".

This is because unit tests are designed to test. Thus when they fail, something is wrong.

Wouldn't it thus be better to not use tests to track what is not implemented yet, but rather use tests to test the functionality that is there.

So those tests that are currently testing functionality that is not there should probably not be run until that functionality is there.

Michael Wiles