4

In my project, I have acceptance tests which take a long time to run. When I add new features to the code and write new tests, I want to skip some existing test cases for the sake of time. I am using Spring 3 and junit 4 using SpringJUnit4ClassRunner. My idea is to create an annotation (@Skip or something) for the test class. I am guessing I would have to modify the runner to look for this annotation and determine from system properties if a test class should be included while testing. My question is, is this easily done? Or am I missing an existing functionality somewhere which will help me?

Thanks. Eric

3 Answers 3

16

Annotate your class (or unit test methods) with @Ignore in Junit 4 and @Disabled in Junit 5 to prevent the annotated class or unit test from being executed.

Ignoring a test class:

@Ignore
public class MyTests {
    @Test
    public void test1() {
       assertTrue(true);
    }
}

Ignoring a single unit test;

public class MyTests {
    @Test
    public void test1() {
         assertTrue(true);
    }

    @Ignore("Takes too long...")
    @Test
    public void longRunningTest() {
        ....
    }

    @Test
    public void test2() {
         assertTrue(true);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I don't want to use Ignore because it is not a runtime setting. I am trying to avoid this. I feel @Ignore is a bad annotation becuase one easily can forget to remove it.
What do you mean with "runtime setting"? @Ignore is evaluated at runtime by JUnit...
Same with spock @Ignore
This should be an accepted answer because it's absolutely solves this issue and helped be much.
2

mvn install -Dmaven.test.skip=true
so you can build your project without test,

mvn -Dtest=TestApp1 test
you can just add the name of your application and you can test it.

Comments

1

I use Spring profiles to do this. In your test, autowire in the Spring Environment:

@Autowired
private Environment environment;

In tests you don't want to run by default, check the active profiles and return immediately if the relevant profile isn't active:

@Test
public void whenSomeCondition_somethingHappensButReallySlowly() throws Exception{
    if (Arrays.stream(environment.getActiveProfiles()).noneMatch(name -> name.equalsIgnoreCase("acceptance"))) {
        return;
    }
    // Real body of your test goes here
}

Now you can run your everyday tests with something like:

> SPRING_PROFILES_ACTIVE=default,test gradlew test

And when you want to run your acceptance tests, something like:

> SPRING_PROFILES_ACTIVE=default,test,acceptance gradlew test

Of course that's just an example command line assuming you use Gradle wrapper to run your tests, and the set of active profiles you use may be different, but the point is you enable / disable the acceptance profile. You might do this in your IDE, your CI test launcher, etc...

Caveats:

  1. Your test runner will report the tests as run, instead of ignored, which is misleading.
  2. Rather than hard code profile names in individual tests, you probably want a central place where they're all defined... otherwise it's easy to lose track of all the available profiles.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.