Should I mock APIs in end-to-end testing?
Asked Answered
D

2

13

When you are doing e2e tests for your application, you want to test the whole application, not some portions of it like unit tests or integration testing.

But in some situations, people do mock APIs.
For example, when you have a massive microservice as your back-end, which makes your e2e tests very slow, or beside your own API, you rely on other third-party APIs, which makes your e2e tests fail occasionally.
So you only want to make sure that your front-end application works well, what should you do?

In my company, we have a massive system with a really heavy database which makes e2e testing very ineffective. Is it right to mock APIs in such a scenario?

Dresser answered 22/4, 2022 at 12:54 Comment(0)
G
19

My understanding here is that if you want to test only your front-end application (what is not E2E testing in my opinion) you can use unit tests instead. If you still want to test the user interface from the browser, then you can mock the APIs' responses, but still not being E2E testing. In case you want to perform an end-to-end testing, then you shouldn't mock any database or API call. The exception here is a third-party API that is not under your control. In that specific case you can mock it to have less external dependency in your tests, but if that third party changes and you are not aware of it, you wont't notice if it's mocked. Said that, if you mock third-party APIs be sure you have a fluent communication with the API provider to get alerts on changes before your app fails.

Gilda answered 12/7, 2022 at 21:7 Comment(0)
M
0

Firstly, definitions and implementations of test types are different depending on who you ask in the industry, but unit tests should only test a single module - That is pretty much the only agreement on testing between different Developers and Languages that I have seen. That said, Microsoft have a good definition guide.

As you state, End-to-end (E2E) tests generally use "live" but "dummy" data, usually from a test environment like SIT or UAT. Your team chose to automate those tests - they probably have good reason. In my experience, at least for medium sized projects and smaller, it is more common that those types of tests are performed manually, and more often by QA. However, this is somewhat tangential to your question.

I believe what you really want are Systems Tests.

Unit Tests -> Integration Tests -> Systems Tests

Systems Tests are tests that generally test an application endpoint (HTTP controller, CLI command) response with a simulated client request, using either mocked dependencies (speed, simplicity) or test harness dependencies, like an in-memory database (accuracy).

Thus you get the benefit of executing a request, which travels throughout the entirety (or most of) your application, not actually using live data, and returns a response, which you can test against.

Understand however, that it is a trade-off.

You gain speed in testing that your applications framework returns a response as you expect, but simultaneously it is somewhat brittle, because mocks by their nature are not always accurate.

A live dependency could at any time, change its data structure or fix issues which impact responses - which your mocks won't account for until someone manually discovers that change.

Depending on the application, the delay in discovering that your test suite of mocked dependencies that is passing as all green, doesn't actually match reality, might be unacceptable.

That reason could be why your team chose to use automated live E2E tests.

Marietta answered 15/7 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.