Reputation: 1414
I'm a bit confused.
How can I get TestNG to report an error for a test?
// ...
@DataProvider(name = "foo")
public Object[][] provideData () {
throw new SomeRuntimeException("Some error occurred. The test configuration "
+ "is somehow incorrect.");
}
This will just lead to test skipping. The exception doesn't even get logged. Moving this to a constructor will just get the exception logged but that's not enough...
I want a big fat error message.
At the moment, using a dedicated (self) test method does the job which at leasts shows some test failure...
Anyway, it would be nice to know how testNG's definition of an error looks like.
Thank you for any hints!
Upvotes: 14
Views: 6445
Reputation: 607
Today, the most recent version of TestNG allows the user to explicitly tell the TestNG runner to treat DataProvider exceptions as Failures.
If you're running TestNG programmatically, this can be done like so -
TestNG tng = new TestNG();
tng.propagateDataProviderFailureAsTestFailure();
If you're running TestNG conventionally, you can also set the attribute on the DataProvider annotation like so -
@DataProvider(name = "DataProviderName", propagateFailureAsTestFailure = true)
public Object[][] dataProviderMethod(){
return new Object[][]{{"row1-column1", "row1-column2"},
{"row2-column1", "row2-column2"}};
}
Reference:
https://github.com/testng-team/testng/issues/217
Upvotes: 0
Reputation: 13046
Here is the article which proposes fair set of alternatives with detailed enough explanations:
Fail instead of Skip a Test when TestNG’s DataProvider throws an Exception
For me best way happened to add test for data providers and here is brief illustration of idea:
public class MyClass {
@DataProvider(name = "my-data-provider")
private Object [][] myProvider() {
// ...
}
@Test
public void testDataProviders() {
Assert.assertTrue(myProvider().length > 0);
}
@Test
// ... Real tests.
}
Upvotes: 13