I am using Selenium Junit Maven and Jenkins, what is the best way to specify which tests to run?
I tried Categories but found it too complicated. Is there an easy way to specify which test methods/class to run?
Although I believe Categories is the way to go, you could alternatively include/exclude test classes in the surefire plugin configuration.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>Sample.java</include>
</includes>
</configuration>
</plugin>
If you want to execute a single test method, you can specify the test property
mvn -Dtest=TestCircle#mytest test
You could set the test property in your pom.xml as well and set it differently in different profiles, but in the end, categories is superior to that and a good test suite design practice.
Related
I want to run selenium tests only if a special flag is provided e.g.
mvn -DrunUiTests=true test
I am using Junit5. I tried already:
An env varible. But the tests will be instantieted anyway.
An home made annotation
#Target({ ElementType.TYPE, ElementType.METHOD })
#Retention(RetentionPolicy.RUNTIME)
#ExtendWith(DisableUITests.class)
public #interface UITest { }
Where DisableUITests returns ConditionEvaluationResult.enabled if a env variable is set. But maven run on console ignores that annotationn.
Furthermore it tryied the surefire plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<excludes>
<exclude>**/*UiTest.java</exclude>
</excludes>
</configuration>
</plugin>
But then I am not able to start the tests my own :-)
I'm afraid the only condition that surefire plugin can consider is file/test name patterns. However you can use the approach described in Conditionally ignoring tests in JUnit 4 where you would test required system property as Assumption
In my maven project I have a number of modules. Is it possible to turn off running unit test for some modules via command line options?
My project takes about 15 mins to run through all unit tests. I would like to speed up the overall build by running just the unit tests in the module I am working on. I do not want to go in and edit each individual pom.xml to achieve this.
I have tried a solution outlined here: Can I run a specific testng test group via maven? However the result is a lot of test failures in modules that I want to skip. I suppose 'group' is not the same concept of module?
To toggle unit tests on and off for an entire project use Maven Surefire Plugin's capability of skipping tests. There is a drawback with using skipTests from the command line. In a multi-module build scenario, this would disable all tests across all modules.
If you need more fine grain control of running a subset of tests for a module, look into using the Maven Surefire Plugin's test inclusion and exclusion capabilities.
To allow for command-line overrides, make use of POM properties when configuring the Surefire Plugin. Take for example the following POM segment:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<excludes>
<exclude>${someModule.test.excludes}</exclude>
</excludes>
<includes>
<include>${someModule.test.includes}</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<someModule.skip.tests>false</someModule.skip.tests>
<skipTests>${someModule.skip.tests}</skipTests>
<someModule.test.includes>**/*Test.java</someModule.test.includes>
<someModule.test.excludes>**/*Test.java.bogus</someModule.test.excludes>
</properties>
With a POM like the above you can execute tests in a variety of ways.
Run all tests (the above configuration includes all **/*Test.java test source files)
mvn test
Skip all tests across all modules
mvn -DskipTests=true test
Skip all tests for a particular module
mvn -DsomeModule.skip.tests=true test
Only run certain tests for a particular module (this example includes all **/*IncludeTest.java test source files)
mvn -DsomeModule.test.includes="**/*IncludeTest.java" test
Exclude certain tests for a particular module (this example excludes all **/*ExcludeTest.java source files)
mvn -DsomeModule.test.excludes="**/*ExcludeTest.java" test
Found a way to exclude on command line:
# Exclude one test class, by using the explanation mark (!)
mvn test -Dtest=!LegacyTest
# Exclude one test method
mvn verify -Dtest=!LegacyTest#testFoo
# Exclude two test methods
mvn verify -Dtest=!LegacyTest#testFoo+testBar
# Exclude a package with a wildcard (*)
mvn test -Dtest=!com.mycompany.app.Legacy*
This is from: https://blog.jdriven.com/2017/10/run-one-or-exclude-one-test-with-maven/
…and if you like to pass the parameter to maven release plugin in Hudson/Jenkins you have to use
-Darguments=-DskipTests
to get it work.
If you want to use Maven profiles:
http://maven.apache.org/guides/introduction/introduction-to-profiles.html
you might want to make it work doing something like this:
Skipping tests in some modules in Maven
I don't know if there is a supported command line option that does the same.
You also might try using environment properties directly, something as per this doc page:
http://maven.apache.org/plugins/maven-surefire-plugin/examples/skipping-test.html
i.e. something like:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<skipTests>${moduleA.skipTests}</skipTests>
</configuration>
</plugin>
then using mvn -DmoduleA.skipTests=false test to test that one module.
I am facing an issue with Maven and the Surefire plugin.
I have two tests: testDatePos.java and testDateNeg.java and for each test an environment variable must be set. It is the same environment variable (DATE_SHIFT) but not the same value (-1 and 1).
Is it possible to configure the section surefire-plugin in the pom.xml of maven to have those tests running?
Here is my pom.xml that exclude the testDatePos.java to have the mvn test running OK (I know this is NOT a solution):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/testDatePos.java</exclude>
<!-- this test needs the env variable DATE_SHIFT=1 but
the test testDateNeg.java needs it at -1 -->
</excludes>
<environmentVariables>
<DATE_SHIFT>-1</DATE_SHIFT>
</environmentVariables>
</configuration>
</plugin>
You can do this by specifying two different executions of surefire in your pom, and forking each execution.
However, this means that these tests will only work when you're running them from maven, or at least you have to change the configuration everywhere you run them from. So, for the tests which require a environment variable, I would add this to the #Before/#After (or #BeforeClass/#AfterClass) of those specific tests.
#Before public void before() {
System.setProperty("DATE_SHIFT", "-1");
}
This way, you don't need to execute the tests in maven for them to work. You'll probably want to store the original value of DATE_SHIFT and restore it at the end of the test.
I'd like my console to be as quiet as possible when I run my tests with mvn test, unless something goes wrong. Then again, when I'm writing tests in Eclipse (in other words, when I run single junit tests inside Eclipse), it's ok for them to be pretty verbose.
So I would need a way to have different log4j/logback settings when running all my tests with surefire than when I run them one by one in Eclipse. Is there a way to accomplish this?
You can configure surefire to use a different logger settings file. That will be ignored by Eclipse.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dlogback.configuration=[wherever]</argLine>
</configuration>
</plugin>
I use maven-failsafe-plugin to run my integration tests. When it fails, I see full stack trace. For each failed test case it may has about ~50 lines, most of which are related to Junit. I found that Junit has filtertrace option which allows to strip part of stack trace, but I cannot figure out how to pass this option to Junit?
Also I tried trimStackTrace option of maven-failsafe-plugin but it doesn't changed output of failed tests.
Is it possible to pass filtertrace option from plugin to Junit? (without using maven-antrun-plugin or similar plugins.)
The first thing is which version of the failsafe-plugin and JUnit do you use? How did you try to configure the trimStackTrace Option, because the trimStackTrace option is true by default.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.9</version>
<configuration>
<trimStackTrace>true</trimStackTrace>
</configuration>
</plugin>
</plugins>
Have you tried to configure the systemPropertiesFile to pass the filtertrace to JUnit.
maven-failsafe-plugin (and maven-surefire-plugin) contains bug when trimStackTrace has no effect on JUnit 4.x It fixed in version 2.13, so in my case i should update plugin to newer version.