How to skip multiple test cases mvn - java

Need flag to skip multiple test cases and not through pom.
-Dtest=!abc.demo1IT and -Dtest=!*test
Want to skip abc.demoIT and unit test cases using flag. How do I combine these 2?
mvn -Dtest=[!abc.demo1IT |!*test] clean install does not seem to work.
Referece from Skipping tests in some modules in Maven

You can exclude multiple patterns using a comma-separated list. Make sure you enclose it in quotes so that it's treated as a single option:
"-Dtest=!abc.demo1IT, !*test"
Reference: https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#test

Related

Using JUnit Categories runner with matchAny with Maven Surefire from command line

I have 2 filters for my JUnit tests that I want to intersect: priorities(e.g. high, medium, low) and features (e.g. account, shop, checkout).
The JUnit Categories runner seems perfect for this, since it supports a matchAny flag, it could do the intersecting filter I need (e.g. return tests that are flagged as both 'high' priority AND 'checkout' feature, doing an AND not an OR).
I am using Maven Surefire to execute tests, which supports JUnit categories through its corresponding groups parameter. This works:
mvn test -Dgroups="priority.High, feature.Checkout"
Unfortunately it applies the default matchAny value which is true, resulting in an OR combination, not AND.
Question: How can I pass matchAny=false through Surefire command line?
You don't need to pass matchAny=false. Just do as follows:
mvn test -Dgroups="priority.High && feature.Checkout"
This should run those tests having only both categories specified.

maven both include and exclude patterns to run tests from command line

With -Dtest=<include-pattern> I can include tests that match include-pattern, and with -Dtest=!exclude-pattern I can exclude tests that match exclude-pattern.
How can I use both include and exclude patterns in command line option?
Next is not working for me: -Dtest=<include-pattern>,!<exclude-pattern>
If I use -Dincludes=**/<include-pattern> -Dexcludes=**/<exclude-pattern> exclude-pattern seem to be applied only.
Is there any way I can configure both include/exclude patterns from command line?
Try to use quoting while passing comma separated values (e.g. -Dtest="<include-pattern>,!<exclude-pattern>").
This works for me and this is how it's planned to work.

Excluding tests from being run in IntellIJ

Is it no option to exclude some tests in IntelliJ IDEA Ultimate? I want to run unit tests in IntelliJ but exclude the integration tests. I name the integration tests with *IT.java so the Maven failsafe plugin can run them seperatly from the unit tests.
In the JUnit Run configuration set the Test kind to Pattern, specify the following regular expression as the pattern:
^(?!.*IT$).*$
It matches against the class name, so you don't need to match .java extension. Regular expression will not match if the class name ends with IT using the negative lookahead.
With JUnit5, you can now tag your tests, e.g: #Tag("integration-test").
Also, given IntelliJ supports now JUnit5 as well, you can then create a JUnit Test Configuration and select Test Kind: Tags (JUnit5).
To exclude let's say "integration-test", you just need to specify as tags:
!integration-test, and IntelliJ will run all your JUnit5 tests except the ones tagged with integration-test.
I would split them to that they are in different packages. They are doing different things after all. You can then run your tests per package. This link details how to do this.

enabling assertions in ant

I want to enable the assertion facility in ant. In my ant build.xml, I put the follows, trying to enable assertions.
<project> ...
<assertions>
<enable/>
</assertions>
</project>
I put assertion in a junit file, which includes only one function,
testAssertions() {
assert false;
}
when running ant, assertion fails are not thrown.. How to enable assertion in this setting?
It looks like your <assertions> subelement is a child of <project>, is this correct?
I am assuming that you are running the test via the <junit> ant task. If this is correct, making the <assertions><enable/></assertions> subelement a child of <junit> should work.
To enable assertions, I edited nbproject/project.properties and changed
# Space-separated list of JVM arguments used when running the project.
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
# To set system properties for unit tests define test-sys-prop.name=value:
run.jvmargs=
to
run.jvmargs=\
-ea
After doing this, assertions were enabled when I executed ant run.

How to use regular expression with mvn test command

i am trying to use below command. I have
Mytest1_test.java,
Mytest2_test.java,
Mytest3_test.java,
Mytest4_test.java,
Mytest5_test.java,
I want to run first 4 at once.
mvn test -Dtest=Mytest[1-4]_test
but it is giving me no test to run.
Can some one plz advice...?
Use:
mvn -Dtest=Mytest*_test test
For the regex to match, you may need to add the package name - just prefixing with .* might be enough (you can try with and without the .* suffix - I used it to match '.class' under JUnit4):
mvn test "-Dtest=%regex[.*Mytest[1-4]_test.*]"
Note that I've used quotes around the whole -Dtest argument to make it more readable when we add more test combinations.
Adding a Problematic Test
When we have a problematic test that fails only in certain combinations then we can add a comma, and a space and another test inside the quotes (") but after the ].
Let's say Mytest5_test fails with tests 1, 2 and 3. We can use regex for the first 3, followed by a comma and standard matching for Mytest5_test, with a * if we need to catch any packages:
mvn test "-Dtest=%regex[.*Mytest[1-3]_test.*], *Mytest5_test"
Strictly speaking we don't need the quotes, but without them we'd need to remove the space between the different tests which would make the command less readable.
Find a Conflicting Test
When our test fails when run in combination with others (static mocks are often the culprit) we also can use regex to help find conflicting tests.
To run all the tests in packages beginning with my.co.[a-m], and Mytest5_test we can use:
mvn test "-Dtest=%regex[.*my.co.[a-m].*], *Mytest5_test"
If I recall correctly, JUnit 4 needs slash separators, JUnit 5 needs dot separators and with Spock just has class name without the package. Using .* and the wildcard single character . as in the regex above will work whether test names have slashes or dots for package names.
Then change the regex to run other tests (e.g. [n-z]) and keep narrowing down until you have the combination you need.
The tricks is to use * character and to exclude other tests with ! character as below.
mvn test -Dtest=Mytest*_test,!Mytest5_test
According to the official documentation of Maven surefire plugin, the command below should work with a regular expression but it don't seem to work (no test executed).
mvn test -Dtest=%regex[Mytest[1-4]_test]
Tested on Windows 7 with Maven Surefire plugin 3.x.
If You have got tests in directories, you can launch all tests from one of them by writing in regexp, eg. for all tests from subdirectory '/doc' in qa/src/test/java/com/frax/doc/:
mvn -Dtest=*/doc/* test

Categories