I've had this discussion recently and would appreciate some more opinions and experiences of this community.
When setting up data for integration tests (API or UI), is the favored approach via SQL or via API calls?
Pros for doing API calls:
- if business logic or database changes, data will be treated accordingly and still be correctly inserted
- no need to maintain separate scripts
- realistic as this is how data is added in production as well
Pros for using SQL scripts:
- if the API contains bugs, you won't be blocked during setup already (no dependency on other API endpoints)
- easier to get data into a specific state
- full control over which data is inserted
Additional question When using APIs, I'm kind of worried that tests will inevitably become end-to-end (rather than integration) with too many points of failure. Let me clarify with an example, supposing we do setup via API calls.
- Create customer test: calls CreateCustomer route
- Create order test: calls CreateCustomer > CreateOrder routes
- Create invoice test: calls CreateCustomer > CreateOrder > CreateInvoice routes
... And so on. If CreateCustomer fails, then all tests fail while they shouldn't (as merely used as setup step in most).