Rudi
Rudi

Reputation: 1179

Choose Selenium test by parameter

We built some tests using Selenium Webdriver. Using JUnit annotations we select manually which tests shall run (@Test, @Ignore).

Something like this:

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; 
import ...

@RunWith(JUnit4.class)
public class MainclassTest {

  @Test
  @Ignore
  public void Test01() {
    ...
  }

  @Test
  // @Ignore
  public void Test02() {
    ...
  }

}

Above we would like to run Test02 only.

But now we would like to run this tests by Jenkins and select one or more tests by parameters instead of comment out @Ignore. In Jenkins we just provide the POM file and some parameters with -Dxxxx.

What are good practices to run different test combinations within different jenkins jobs? It's better to split the tests into different classes? Or could I better configure the wanted test within the maven pom file?

Upvotes: 0

Views: 88

Answers (1)

Würgspaß
Würgspaß

Reputation: 4840

You can use JUnit categories.

As a simple example for priority, declare interfaces and extend a hierarchy like this:

/** Marker for test case priority. * /
package priority;
public interface Low {
}
public interface Medium extends Low {
}
public interface High extends Medium {
}

Then annotate your methods as needed, for example:

public class MainclassTest {
  @Test
  @Category(priority.High.class)
  public void Test01() {
    ...
  }
  @Test
  @Category(priority.Low.class)
  public void Test02() {
    ...
  }
}

Finally make your POM configurable

<build>
  <plugins>
    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <groups>${testcase.priority}</groups>
      </configuration>
    </plugin>
  </plugins>
</build>

and let Jenkins run it with a parameter as needed:

mvn test -Dtestcase.priority=priority.High

(note, that due to extension of the interfaces, Low will run all categories. If you do not want that, simply remove the extension).

Upvotes: 2

Related Questions