Reputation: 6653
I do some cleanup in external systems using testng @AfterClass annotation. But when tests are failed I really need that data. Can I make testng perform some actions if only tests are passed?
Upvotes: 2
Views: 1960
Reputation: 370
Example, where you can delete test data just for current test class if only tests are passed:
@AfterClass
public void deleteCreatedData(ITestContext context) {
if (hasClassFailedTests(context)) return;
//do your cleanup for current test class
}
protected boolean hasClassFailedTests(ITestContext context) {
Class clazz = this.getClass();
return context.getFailedTests().getAllMethods().stream().anyMatch(it ->
it.getRealClass().equals(clazz));
}
Upvotes: 0
Reputation: 136
There is an option to get information about all failed tests till current moment. You have to inject ITestContext into your "afterClass" method.
@AfterClass
public void after(ITestContext context) {
context.getFailedTests().getAllResults()
}
Iterate through all results and filter by TestClass
Upvotes: 4
Reputation: 8531
AFAIK there is nothing at afterclass/aftersuite level. What you can do is couple of things:
Or
Upvotes: 1