简体   繁体   中英

Java Selenium Junit Maven: Way to parametrize tests?

I have a test suite that is being run on a bunch of sites we have created. The majority of the sites work the same way so instead of having lots of duplicate code I can run all the tests on all sites.

I now have aa couple of tests that I only want run on a few of the sites, is there a way to only run these tests if the url is for site A,B or C?

For each site I read in a properties file which has the url and other information needed in the test, would it be possible to have a property ignoreTests= followed by the tests to ignore and then when I read the properties file in the @Before section of the tests I can set each @Test read in to be ignored.

Everything what you're asking here can be obtained with TestNG instead of Junit.

Parametrization of test cases is described in this good tutorial. As for selective run of test cases I think that grouping would be usefull, here is tutorial.

Generally TestNG hase greate documentation which I recomend you to read.

If you can't swith to TestNG take a look on this tutorial for test parametrization in Junit. Here is SO about Junit test grouping.

Junit uses Assume for dynamically ignoring tests.

Use the @Before method to write the URL to a field. Than you can use Assume for ingoring tests based on the URL. The following tests are ignored if the URL is not "firstValidUrl" or "secondValidUrl".

org.junit.Assume.assumeTrue(url.equals("firstValidUrl") || url.equals("secondValidUrl"));

You can use Hamcrest for a much nicer statement:

org.junit.Assume.assumeTrue(url, org.hamcrest.Matchers.isIn("firstValidUrl", "secondValidUrl"));

Using static imports and Hamcrest:

assumeTrue(url, isIn("firstValidUrl", "secondValidUrl");

Yes, you can Parameterize with JUnit but you need to make sure you are using JUNit 4.11 or newer. JUNit didn't have parameterization before that. JUnit has Data Providers and Annotations that are almost exactly like TestNG, using a 2-D object array (List).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM