I use this code to create data:
Method OnBeforeAllTests() As %Status
{
// Create one MyClass row
Do ##class(MyClass).Populate(1)
Quit $$$OK
}
and this to delete data:
Method OnAfterAllTests() As %Status
{
Do ##class(MyClass).%KillExtent()
Quit $$$OK
}
How do I change OnAfterAllTests
to only delete the data I populated using OnBeforeAllTests
and not delete any other data?
The only thing that might auto-magically keep track of what you have done and roll it back would be a transaction.
Other than that you need to keep track of it yourself, or just not have your unit tests actually create data. It's not a very good practice to have test specific code in your production system, so a flag field "testData" isn't great, but I guess you could.
My personal solution has been to create tests that don't actually insert data in the first place, but this has required using dependency injection and creating a mocking framework to make writing the unit tests feasible.
A somewhat easier solution might be to have an object that keeps track of all your data changes and rolls them back at the end, but probably just using a transaction will do a better job of this.