Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude some JUnit tests from automated test suite

When writing code that interacts with external resources (such as using a web service or other network operation), I often structure the classes so that it can also be "stubbed" using a file or some other input method. So then I end up using the stubbed implementation to test other parts of the system and then one or two tests that specifically test calling the web service.

The problem is I don't want to be calling these external services either from Jenkins or when I run all of the tests for my project (e.g. "gradle test"). Some of the services have side effects, or may not be accessible to all developers.

Right now I just uncomment and then re-comment the @Test annotation on these particular test methods to enable and disable them. Enable it, run it manually to check it, then remember to comment it out again.

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

Is there is a better way of doing this?

EDIT: For manual unit testing, I use Eclipse and am able to just right-click on the test method and do Run As -> JUnit test. But that doesn't work without the (uncommented) annotation.

like image 384
Brad Peabody Avatar asked Sep 06 '13 08:09

Brad Peabody


People also ask

How do you ignore a test in JUnit 4?

JUnit 4 – @Ignore Annotation The JUnit 4 @Ignore annotation could be applied for a test method, to skip its execution. In this case, you need to use @Ignore with the @Test annotation for a test method you wish to skip. The annotation could also be applied to the test class, to skip all the test cases under a class.

Are JUnit tests automated?

JUnit is best at creating repeatable test cases. If there is a need to run a test in JUnit 100 times, one doesn't need to write it 100 times or not even execute it manually 100 times. This process is automated. Automation testing is usually preferred over manual testing because of faster and reliable results.


1 Answers

I recommend using 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 set up a two test suites : one that runs the tests of that category and one that ignores tests in that category (but runs everything else)

@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 I see something manually getting turned on or off I cringe.

like image 113
dkatzel Avatar answered Sep 22 '22 18:09

dkatzel