1

My goal: using parameters to switch environments in my tests for instance:

mvn test google -> Tests goes to google site

mvn test bing -> bing site

"I need to feed my tests which environment is the target and it should comes from the pom.xml and using them as arguments."

It would be very useful for teamcity/jenkins integration as well. Apart from that, I need to use url as variables in my tests. How can I do that?

Profiles can be a solution in pom.xml?

<profiles>
    <profile>
        <id>google</id>
        <properties>
            <base.url>http://www.google.com</base.url>
        </properties>
    </profile>
    <profile>
        <id>bing</id>
        <properties>
            <base.url>http://www.bing.com</base.url>
        </properties>
    </profile>
</profiles>

From build section:

<configuration>
   <systemProperties>
       <base.url>${base.url}</base.url>
   </systemProperties>
</configuration>

But How can I use system properties and overall the approach is good? Thanks!

brobee
  • 231
  • 1
  • 5
  • 25
  • Not a good design, Maven will just build and give the automation test code. But as your test are general on all the environment. it would complex for execution and more decoupling will be required. Instead of that , just create a template file and that would be refreshed depending on the parameters passed through the maven as goals. – Sam Jan 08 '18 at 22:50
  • Are you using any testing framework? jUnit? TestNG? Or perhaps you are using Spring or Cucumber? – Eugene S Jan 09 '18 at 01:49
  • Cucumber, junit and pure java. My tests are general on all environments, just the URL is different. Thats why I would like to find a good solution to switch between the environments (URLs). My current implementation: urls are coming from config files, but it hard coded. mvn test clean google should be easier... – brobee Jan 09 '18 at 16:35

1 Answers1

1

You can configure the maven-surefire-plugin to include only specific test classes and the run mvn test. By default, mvn will run all these:

  • "**/Test*.java" - includes all of its subdirectories and all Java filenames that start with "Test".
  • "**/*Test.java" - includes all of its subdirectories and all Java filenames that end with "Test".
  • "**/*Tests.java" - includes all of its subdirectories and all Java filenames that end with "Tests".
  • "**/*TestCase.java" - includes all of its subdirectories and all Java filenames that end with "TestCase".

but you could specify the tests you want to include like this:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20.1</version>
        <configuration>
          <includes>
            <include>Sample.java</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

or exclude:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20.1</version>
        <configuration>
          <excludes>
            <exclude>**/TestCircle.java</exclude>
            <exclude>**/TestSquare.java</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

Having said that, this is probably not the best design and generally, you should be using some testing framework which you then can configure according to your needs. Few examples are (or combination of): jUnit, TestNG, Cucumber, Spring.

In Cucumber, for example, you can have tags which then you can configure as a part of your test execution. If you use Jenkins, you might have something like this in your build fild:

clean install -Dcucumber.options="--tags @Google

or

clean install -Dcucumber.options="--tags @Bing

In Spring, you can have profiles that you can run like this as Jenkins job:

mvn clean test -Dspring.profiles.active="google"

EDIT

Alternatively, you can define a custom property in your pom like this:

<properties>
   <myProperty>command line argument</myProperty>
</properties>

And then pass it from command line like this:

mvn install "-DmyProperty=google"

EDIT2

Providing a -D prefixed value in command line is a way of setting a system property. You can perform this action from Java code itself like this:

Properties props = System.getProperties();
props.setProperty("myPropety", "google");

or simply:

System.setProperty("myPropety", "google");
Eugene S
  • 6,709
  • 8
  • 57
  • 91
  • Eugene, thank you for your answer. Unfortunately, the cucumber.options is not a good solution here, because it can drives scenarios in feature files, and I'm using it this approach. Here, I need to feed my tests which environment is the target and it should comes from the pom.xml using arguments. Profiles can be used with pure java Instead of spring? – brobee Jan 09 '18 at 16:31
  • Thanks Eugene! is there any way to use that property as variable in the java code? Meaning, as you can see in my example google is the id, and I wanna use base.url (www.google.com) in my code. As far as I know, system prop doesn't work, additional maven dependency is needed. Thanks! – brobee Jan 16 '18 at 22:17