简体   繁体   中英

Junit throw warning while still passing test

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.

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.

org.junit.Assume会做你需要的吗?

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.

I know i'm about 2 years out, but I would comment/ignore the tests you don't need, and as a process make sure public code reviews also cover unit tests along with the submitted code. Which should improve your testing methodologies & coverage overall.

You could throw a NotImplementedException in your new methods:

Apache Commons NotImplementedException

eg

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.

Unit testing works best when the tests are full ie 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.

IMO, @lumpynose is the better answer.

If for some reason, you can't execute your test according some missing requirement but it doesn't mean that the code is broken, you can use Assume to skip it.

Here is a piece of code :

Assume.assumeTrue("The test is not meaningful because...", <condition_for_test_meaningful>);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM