Can We Improve TestNG DataProvider Templates?

IDEA could do with a few enhancements to make TestNG more intelliJent. :p

If I write this I get no warnings or compile errors

@Test(dataProvider = "simpleDataProvider")
public void testFormatDecimalNumber(String value, int places, String expected) throws Exception {
    assertEquals(Helper.formatDecimalNumber(value, places), expected);
}


However, when I run the test I get this:

org.testng.TestNGException:
Method public void com.helper.HelperTest.testFormatDecimalNumber(java.lang.String,int,java.lang.String) throws java.lang.Exception requires a @DataProvider named : simpleDataProvider


I would expect IDEA to do the following:

  • Identify that there is no DataProvider call "simpleDataProvider" available
  • Alt-Enter would fix it and create the DataProvider method
  • The Template would be better than current (see next)


Right now if I do Alt-Insert "Parameters Method" I get this (I typed simpleDataProvider to replace the "Name" that appears in the template):

@DataProvider(name = "simpleDataProvider")
public static Object[][] simpleDataProvider() {
    

}


This could easily be improved by adding something to the body
e.g.

@DataProvider
public Object[][] simpleDataProvider() {
    return new Object[][]{
            new Object[]{"Variable1", "Variable2"},
    };
}


This means the user doesn't have to remember the annoying array syntax and provides an easy way to add new values (duplicate line with Ctrl-D)
However, if it was doing it from Alt-Enter on the method it should recognise the variables and inteliigently create the array:
e.g.

@DataProvider
public Object[][] simpleDataProvider() {
    return new Object[][]{
            new Object[]{"1.499", 1, "1.5"},
            new Object[]{"1.499", 2, "1.50"},
            new Object[]{"1.499", 3, "1.499"},
    };
}


If you were being fancy you might want to add some nulls and negative numbers to the template to encourage people to test more outliers.

I can do a bit of this myself by editing the template but then I have to remember to save this for the next PC I go to or the next time I install.

2 comments
Comment actions Permalink

I've add missed inspection which check dataProviders the same way e.g. dependsOnMethods are checked. Quickfix to create missed method is provided. I am not sure that I agree with suggestions to the data provider templates, so could you please create a YouTrack ticket with that part so we would see how much attention it would get?

Thanks,
Anna

0
Comment actions Permalink

You have to return Object[][] from the dataprovider so why wouldn't you include it in the template?

0

Please sign in to leave a comment.