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

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.

Related

How to skip multiple test cases mvn

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

Is it possible to use wildcard logic '*' to list a specific directory via Apache Maven Goals & Options?

I need to list a specific directory using wildcard logic within my Maven goals and options command.
Currently the root directory is:
mvn test -Dcucumber.options="C:/Users/Joe_Blogs/Desktop/AutoFramework/src/test/resources/features/Bookings/MakeBooking.feature"
I need to use ** to scan the project folders in order to search for a given feature file, as the folder name (Bookings) may change.
mvn test -Dcucumber.options="C:/Users/Joe_Blogs/Desktop/AutoFramework/src/test/resources/features/**/MakeBooking.feature"
In the example above ** will need to reference a variety of different folders, not just Bookings.
Any ideas?
Thanks for your help.
Maven supports the "any" and "any descendant" wildcard denoted by * and **, respectively. It is a subset of the <fileset> Ant convention.
The support comes from the Apache Maven Shared Utils library. The actual logic of walking directory trees, path normalization, pattern matching, etc. is grouped inside the org.apache.maven.shared.utils.io package. Those classes provides uniform file system handling across plugins supported by the the Maven Project.
As the references should demonstrate, wildcard processing is neither trivial logic, nor magically applied outside of Maven proper. Cucumber has to intentionally support the syntax somehow. Sorry, but it doesn't look like Cucumber supports the "any descendant" wildcard. The closest equivalent is /* at the end of a path.
However, one possible workaround is to use tags. Maybe it will seem silly to tag a single file, but doing so dispenses any concern about pathing. Example:
mvn test -Dcucumber.options="--tags #MakeBooking"

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.

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

What is the format of the Maven Surefire plug-in <include> statement?

I'm trying to run Maven Surefire with an include statement of:
<include>**/[A-R]*SeleniumTest.java</include>
<include>**/[A-R]**SeleniumTest.java</include>
<include>**/[A-R].*SeleniumTest.java</include>
But it does not seem to support any of these notations. What is the syntax of this expression? (Documentation link would be much appreciated)
The Maven Surefire Plugin follows the same semantics for configuring includes and excludes as Maven's FileSets.
You can find some details on how that works via Maven: The Definitive Guide (Section 12.7):
The includes section uses a list of include elements, which contain path patterns. These patterns may contain wildcards such as '**' which matches one or more directories or '*' which matches part of a file name, and '?' which matches a single character in a file name
The patterns used by maven's include tags follow the behavior of Ant's Pattern notation.
The [A-R] notation in your example is not supported, but you could use a combination of includes and excludes to narrow down the set of tests that get run.

Categories