Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • Do you have control over the API code? Is there a "test" version of the API? Commented Jun 9 at 12:27
  • If you are using Entity framework core you might consider the in-memory database provider Commented Jun 9 at 12:51
  • You probably want to use a separate test version of a database, otherwise this is just doomed to fail rather badly. Do you have such a version? And can you easily control if transactions are test or not? Commented Jun 9 at 12:51
  • "most of my tests interact with the application via its API endpoints" this is where your problem is. Most of your tests should be unit tests. You are describing integration tests and they should be very much the minority. You say that you can run direct sql from your tests, so for the very small number of integration tests that cause data changes, your test code can insert test data and clean up afterwards. Commented Jun 9 at 13:27
  • To expand on that. You have an API endpoint A. A does nothing apart from pass the parameters directly to class B. B converts your parameters into the internal data model and calls C. C contains the business logic and needs to save or retrieve data. It calls class D which contains only the database CRUD (or equivalent). You test D by writing tests that instantiate D and interact with the dev database, in a transaction. This proves D works. You test C by injecting a mock or stub D which returns test data. You know your mock D behaves the same as real D because you have tests of D by itself... Commented Jun 9 at 13:39