14
votes

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!

1
Can you show us the code that calls this? Are you sure it's not somehow wrapped in a try/catch? - Chris Thompson
The method referring to the dataProvider is not aware of any exception in any way. - Doe Johnson
As far as I understand it the test method itself doesen*t call the dataprovider. A test method gets called with params provided by the Dataprovider. - Doe Johnson
The DataProvider is part of the preamble for tests, and, as such, does not get reported as an error. It is your responsibility to catch and log any errors in the DataProvider method. - Bob Dalgleish
Too bad. I wonder if there is any possibility to trigger an error directly. It does not need to be done in a DataProvider. I mean generally, is there a way? - Doe Johnson

1 Answers

13
votes

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.

}