Ignore Test Cases Based on Environment

2019-08-15 06:12发布

问题:

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.

回答1:

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"))));
}