Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pluto-atom-4/e9dfe0b0e3ccc389870e3a33248ec9a3 to your computer and use it in GitHub Desktop.
Save pluto-atom-4/e9dfe0b0e3ccc389870e3a33248ec9a3 to your computer and use it in GitHub Desktop.
package internal.example.after_x;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Test
public class DeleteTestDataInAfterMethodTest {
private Map<String, List<String>> toBeDeleted = new HashMap<>();
@AfterMethod
private void afterMethod(ITestResult result) throws Exception {
String methodName = getCurrentMethodNameWithHashCode(result);
System.out.println("current result ->" + methodName);
if (toBeDeleted.containsKey(methodName)) {
toBeDeleted.get(methodName)
.forEach(System.out::println);
}
}
@Test
public void test1() throws Exception {
String subscription = "test-test-test....1";
Thread.sleep(6);
toBeDeleted.put(getCurrentMethodNameWithHashCode(Reporter.getCurrentTestResult()), Collections.singletonList(subscription));
}
@Test
public void test2() throws Exception {
String subscription = "test-test-test....2";
Thread.sleep(6);
toBeDeleted.put(getCurrentMethodNameWithHashCode(Reporter.getCurrentTestResult()), Collections.singletonList(subscription));
}
@Test
public void test4() throws Exception {
String subscription = "test-test-test....4";
Thread.sleep(6);
toBeDeleted.put(getCurrentMethodNameWithHashCode(Reporter.getCurrentTestResult()), Collections.singletonList(subscription));
}
@DataProvider(name = "provideFirstName")
private Object[][] provideFirstName() {
return new Object[][]{
new Object[]{"mary"},
new Object[]{"ken"},
new Object[]{"jane"},
new Object[]{"joe"}
};
}
@Test(dataProvider = "provideFirstName")
public void test3(String firstName) throws Exception {
String subscription = "test-test-test....3";
System.out.println(firstName);
Thread.sleep(2);
toBeDeleted.put(getCurrentMethodNameWithHashCode(Reporter.getCurrentTestResult()), Collections.singletonList(subscription));
}
private String getCurrentMethodNameWithHashCode(ITestResult result) {
return result.getMethod().getMethodName() + result.getMethod().getCurrentInvocationCount();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment