Exclude some JUnit tests from the automatic test suite

When writing code that interacts with external resources (for example, using a web service or other network operation), I often structure classes so that they can be "truncated" using a file or some other input method. Thus, I end up using the enveloped implementation to test other parts of the system, and then one or two tests that specifically test the web service call.

The problem is that I do not want to access these external services from Jenkins or while running all the tests for my project (for example, "gradle test"). Some of the services have side effects or may not be available to all developers.

For the moment, I just uncomment and then re-comment the @Test annotation on these specific testing methods to enable and disable them. Turn it on, run it manually to check it, then remember to comment it.

// Uncomment to test external service manually //@Test public void testSomethingExternal() { 

Is there a better way to do this?

EDIT: For manual unit testing, I use Eclipse and can just right-click on the test method and run Run As -> JUnit test. But this does not work without annotation (no comment).

+6
source share
3 answers

I recommend using the junit categories. See this blog for details: https://community.oracle.com/blogs/johnsmart/2010/04/25/grouping-tests-using-junit-categories-0 .

Basically, you can annotate some tests as being in a special category, and then you can configure two sets of tests: one that runs tests in this category, and those that ignore tests in this category (but run the rest)

 @Category(IntegrationTests.class) public class AccountIntegrationTest { @Test public void thisTestWillTakeSomeTime() { ... } @Test public void thisTestWillTakeEvenLonger() { .... } 

}

you can even annotate individual tests "

 public class AccountTest { @Test @Category(IntegrationTests.class) public void thisTestWillTakeSomeTime() { ... } 

Anytime, when I see something manually turning on or off, I cringe.

+6
source

If you just want to disable tests for functionality that has not yet been written, or otherwise manually disable some tests temporarily, you can use @Ignore ; tests will be skipped but still marked in the report.

If you need something like Spring Profiles, where you can define a set of rules for which tests run, when you need to split your tests into separate test cases or use Filter .

+3
source

As far as I can see, you are using gradle, and the API for JUnit says that the @Ignore annotation disables the test. I will add a gradle task that will add @Ignore for these tests.

+2
source

Source: https://habr.com/ru/post/953296/


All Articles