Why do you need to talk to the database during unit tests? This makes your unit test a default integration test. Instead, create wrappers for all communication with the database and prepare / mock it during unit tests. Then you do not need to worry about the state of the database before and after.
Now, if you do not want this level of refactoring: the problem with transactions is that you need to open the connection. Thus, if your test method handles all communication on its own, itβs really difficult to enter a transaction that you can create during setup and roll back when disconnected.
source share