Disable selenium tests in "mvn test" runs - maven

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

Related

How to pass profile while running mvn test for a springboot application

I am trying to run test cases of spring boot application.
For that I tried using mvn test -Dspring.profiles.active=test.
But it did not work.
Do I need to add profile in pom.xml
Could you please help.
The java arguments come before the maven lifecycle parameter:
mvn -Dspring.profiles.active=test test
You can also add <argLine> property to maven surefire plugin to apply to all tests.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<testEnvironment>true</testEnvironment>
</systemPropertyVariables>
<argLine>-Dspring.profiles.active=test</argLine>
</configuration>
</plugin>
The best way to run a test with a profile is using the ActiveProfiles annotation (assuming JUnit 5):
#SpringBootTest
#ActiveProfiles("test")
class MyApplicationTest {
#Test
void test() {
// implement actual test here
}
}
This will start your Spring Application with the test profile active.

why does my maven tests fails when run as a whole and passes when ran individually?

Hi I am having a strange scenario.
I have a spring boot java project . I have lots of Junit tests as well.
when i run the tests from the terminal by issuing the command mvn clean test, 2 tests belong to a class fails
however if run the those tests belonging to that class from the eclipse Run as >> Junit tests it passes .
any idea why does this happens ? and how can i fix it ?
the following is my sure fire plugin configuration
<maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<!-- Force alphabetical order to have a reproducible build -->
<runOrder>alphabetical</runOrder>
</configuration>
</plugin>
thank you

geb passing test case as a parameter from maven commandline

How do I pass the test case to geb via maven commandline:
I have a test case class named GebishOrgSpec2.groovy
I want to pass this test case maven and let geb run it over the command line.
I am having successful build but no run of the test case
How do I solve this problem?
This is my pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<includes>
<include>${inc.TestCase}</include>
</includes>
<systemPropertyVariables>
<geb.build.reportsDir>target/test-reports/geb</geb.build.reportsDir>
</systemPropertyVariables>
</configuration>
</plugin>
I have tried the following codes and they are all not running the testcase
mvn -Dinc.TestCase=GebishOrgSpec2.groovy test
mvn -Dinclude=GebishOrgSpec2.groovy test
mvn install -Dinc.TestCase=GebishOrgSpec2.groovy
mvn install -Dinclude=GebishOrgSpec2.groovy
I have tried these links
How to run Geb TestCases from windows command line?
Grails geb getting parameters from commandline
Geb exception when running tests in maven
they are all not working
System.getProperty("inc.TestCase") is returning null as well
I want to add that if I add the test case directly into the pom it does run successfully
can you please help
Don't use in-/excludes in your POM for something you want to do from the command line. It is much easier, both for
Surefire (unit tests) via mvn -Dtest=TestCircle test and
Failsafe (integration tests) via mvn -Dit.test=ITCircle verify
This works beautifully for Spock/Geb tests.

Specify which Junit test to run

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.

surfire plugin maven : env variable for specific tests

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.

Resources