views:

172

answers:

3

We have a number of integration tests that fail when our staging server goes down for weekly maintenance. When the staging server is down we send a specific response that I could detect in my integration tests. When I get this response instead of failing the tests I'm wondering if it is possible to skip/ignore that test even though it has started running. This would keep our test reports a bit cleaner.

Does anybody have suggestions?

A: 

In your test you could test for this condition before performing the Assert.whatever. If the test fails just return from the method without running the rest of the code/asserts.

John Weldon
+5  A: 

It's been awhile since I used JUnit but isn't there a way to Assume a condition is true? I think that has a different meaning than a pass/fail of the test. Your report should indicate that the test was not run.

Edited to add: Assume class

Mike Daniels
A: 

The simplest solution would be to split your tests up into two suites: integration tests and pure unit tests. Then develop a script or some other automated means to determine if the server is up, and simply skip the integration test suite if the server is down. But if grouping the tests into suites like this is not practical for some reason, here's an alternative:

You could create a custom Runner that skips tests if the server is unavailable. You could either program the runner to determine server availability on its own or determine it through some external process such as a script that executes before the test phase and sets a system property on the JVM that the runner can check (e.g. pass -Dcom.company.testrun.integration=false as a command line argument).

You could enable your custom runner using the @RunWith annotation on your integration test classes and use the built-in runner for all other tests so they are not impacted. Alternatively, you could use your runner for all tests and invent a new annotation (e.g. @IntegrationTest) that you use to decorate your integration test methods. Using the latter approach, the runner would apply its skip logic only if the server is unavailable and the test method has the special annotation.

Rob H