How to delete data only after all tests, based on

2019-09-13 03:46发布

问题:

I've a test with different inputs, hence used @DataProvider but before inputs, from Object[][], passed to a test, I want to create some data which is common to test with all different inputs

@DataProvider(name = "test")
public Object[][] createData() {
    //create some data which is common for both john and bob
    return new Object[][] { { "john" }, { "bob" } };
}

@Test(dataProvider = "test")
public void userOp(String name) {
    //Perform some operations with user mention in `name`. For now let's just print the names
    System.out.println(name);
}

Once ALL tests, with different inputs(ie. john and bob) are run, I want to delete data that I created in data provider method.

NOTE I can use @AfterClass to delete this data but that can mess my other tests in test class hence I want to delete data, once I'm done with the test, for which it was created.

Can someone suggest how this can be achieved?

回答1:

Declare @AfterMethod with ITestResult parameter. In ITestResult you can find exact parameters used in your test method and delete related data. In @AfterClass just delete you common data.