1

How would I have maven ignore specific test cases based on a variable or an build profile?

I have a few test cases that involve connecting to an LDAP server. How would I get these test cases to be ignored if I am building with a specific profile or a maven variable being passed into the command? I do not wish to ignore all test cases.

monksy
  • 14,156
  • 17
  • 75
  • 124
  • 1
    Would it make sense for you to split tests along the lines of unit versus integration tests? Or do you have other integration tests that don't need LDAP and could potentially always run? – Sander Verhagen Oct 08 '13 at 18:57
  • I can split the tests, are you insinuating splitting by test suite? I'm also looking for a way to have maven to accept some kind of input to say "i can run the tests here" or that I can't – monksy Oct 08 '13 at 19:59

1 Answers1

3

Your solution seems to be two parts: (1) informing the process, through Maven, that it is running in a given configuration, and (2) running a test conditionally based on that configuration.

The first bit is easy: Use the -D switch to pass in a system property, as in this SO answer, acknowledging that you need to pass a system property as an argument within a system property using the surefire argLine property:

mvn -DargLine="-DldapTests=disable" test

For the second, you can use the new assumeTrue and related assumption API in JUnit, as in this other SO answer.

@Before
public void checkLdapTests() {
  assumeThat(System.getProperty("ldapTests"), is(not(equalTo("disable"))));
}
Community
  • 1
  • 1
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251