Integration testing approach

Posted by PrydwenParkingOnly@reddit | ExperiencedDevs | View on Reddit | 18 comments

I recently wrote integration tests for a .NET API and I'm second-guessing the approach. Curious what others think.

The setup:

In contrary to our unit tests, the tests exercise the full stack from controller down to services. The only things mocked are external services (.e.g. external APIs, databases etc)

There are two assertion types:

  1. Before external API calls, asserting the entire request that has been built by the service

The responses of the external API calls/database are mocked, so that the behavior should always be the same.

  1. Exact match on the result that the .NET API returns

    test → controller (real) → service (real) → external API (mocked)

             ↑                                    ↑
    
     ASSERT response                     ASSERT outgoing requests
    

Why?

We have API tests in postman, but they are slow and flaky. Slow because the external APIs have to do a lot of calculations, flaky because the external API is strongly dependent on dates in the datamodel, compared to the current date. Each API test takes 2-5 seconds, but each integration test only takes 100ms, can run in parallel.

On the other hand the unit tests do not cover enough functional requirements. A lot of our results are depending on how the units connect to each other. Let's say we want to replace automapper for something else, then the unit tests will have to be updated, but the new mappings could be totally breaking functionality we had before.

So, what do you think about this approach, is this a reasonable middle ground, or are we getting the worst of both worlds?