How to use regular expression with mvn test command - java

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

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

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.

Gradle + TestNG only runs certain tests as single tests

I've been using Gradle + TestNG + Java + Selenium for my web UI tests for quite a while now and I've only recently run in to this issue. For some reason when I try to run a single test class using -DtaskName.single=ExampleTestClass where ExampleTestClass would be ExampleTestClass.java it only works on some of my test classes.
I'm getting the error: Could not find a matching test for pattern: ExampleTestClass
I've seen this error in the past due to typos or missing #Test annotations etc, so I'm familiar with the "normal" cause, but this is quite bizarre as it appears to work on some test classes and not others. I've inspected the code and all annotations and groups are in place for the test methods, they run fine from my IDE (IntelliJ), and they are all located in the same directory / package path. Is there something I'm missing here? I don't know if I'm seeing things but I did notice that it didn't work with a test class that did not have Test as the last four characters of the Java class name but upon renaming it, still no dice. I've read the documentation and can't find anything wrong. Is there anything else that may be causing this to fail? It's quite odd since these tests are all so similar in every way. I even checked character encoding etc - no discrepancies between any of them.
Any advice or ideas on where to look next would be great.
Cheers,
Darwin
I ran into the exact same thing with gradle 1.6 (haven't had time to upgrade), and TestNG. A single test in a project with multiple tests get skipped and also gradle complains about not finding it if you try running the single test alone. Debug run shows the missing test .class file being found by gradle.
I worked around it by adding an #Test annotation to the test class in addition to the test method. That seems to make gradle find it.
I recently ran into a similar issue. I tried to run a test with -DTest.single and --tests but it wasn't found. After a bit of frustration I realized that the test was in a test group that was excluded in my test task configuration. I had incorrectly assumed that running with -Dtest.single would overrule exclusions, including that test's group allowed it to run as a single test.

maven not running all subset of tests I ask it to when using #

I am trying to run a couple of tests from different classes, my command line is:
mvn -Dtest=com.MyComp.Selenium.SelTests.SomeTests1#XTest,com.MyComp.Selenium.SelTests.SomeTests1#YTest,com.MyComp.Selenium.SelTests.SomeTests2#ZTest
When I run this I would expect it to run XTest and YTest from the class SomeTests1 and ZTest from SomeTests2 but instead it just runs XTest and ZTest and skips YTest.
If I just tell it to run YTest or if I tell it to run all tests in SomeTests1 it works. Please can somebody see what I'm doing wrong?
I don't want to run all the tests in each class I just want a subset.
If you are using Junit 4.x and surefire 2.12.1 or greater then you can use the following syntax for running multiple tests in a class
mvn -Dtest=com.MyComp.Selenium.SelTests.SomeTests1#XTest+#YTest...
Note the + symbol. Here is the documentation

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.

Categories