Alpha
Alpha

Reputation: 14026

How to have dynamic description for data provider tests?

I want to use @DataProvider annotation for my tests like following example:

@DataProvider(name="testdata")
 public Object[][] testData(){
    return new Object[][]{
        {"http://www.google.com", "Google"}, 
        {"http://twitter.com", "Twitter"}
    };
  }

  @Test(dataProvider = "testdata")
  public void test(String url, String title) {
    driver.get(url);
    Assert.assertTrue(driver.getTitle().contains(title));
  }

In above case, we've two test cases with different purposes hence I want to have different descriptions.

But if I use @Test(dataProvider = "testdata", description = "some description"), for both tests, I'll have same description. But if I want to have different description for each test, is there any way to make description dynamic and have description according to test?

Upvotes: 2

Views: 778

Answers (1)

luboskrnac
luboskrnac

Reputation: 24561

 @DataProvider(name="testdata")
 public Object[][] testData(){
    return new Object[][]{
        {"http://www.google.com", "Google", "description1"}, 
        {"http://twitter.com", "Twitter", "description2"}
    };
  }

  @Test(dataProvider = "testdata")
  public void test(String url, String title, Spring description) {
    driver.get(url);
    Assert.assertTrue(driver.getTitle().contains(title), description);
  }

If you are doing this, description is written to console for each test case (as part of parameters output). Also you would know which description failed if one of assertions fail. I was using similar technique for identifying test cases, when parameters aren't self descriptive and there are a lot of test cases.

Upvotes: 1

Related Questions