📜 ⬆️ ⬇️

We are testing integration with external services.

Modern applications rarely work in isolation, most often they are integrated with many third-party services. Therefore, there is an urgent need to test this integration in the framework of integration or functional testing. And then you can step on many of the rakes, which I am going to talk about. For whom this topic is relevant, please under the cat.



What is the actual problem?


')
The first solution that comes to mind is to test directly on a real service. In some cases this may be justified, but often leads to the following problems:



The problems described cause you to look for alternative solutions and they exist.

Plugs rule the world of testing!



You can come to the aid of various options for stubs (I did not find a better translation of the term mock objects ). There are several options for stubs: mock, stub, dummy, spy, fake. Knowing and distinguishing between them is necessary and important, so it’s worthwhile to read and understand the specifics, but this is not about this. We only need one of the types of stubs that are most suitable for working with external services - fake implementation .

Fake implementation is a simplified version of this service that works completely predictably, locally, gives full access to data and settings, and also does not require any additional work in each test. Thus, we can transparently replace the real service in the tests for fake implementation and solve all the problems. Sounds easy? Not really!

First, we will need to implement this simplified version of the service, which takes time and the efforts of developers. You are lucky if your external service provides fake implementation for local testing. Then you simply download the library, configure it, launch it and use it for health. Some services allow you to access a special version of the external service for testing, which is not subject to the problems listed in the first part. But such a service unit. Almost always you will have to write a fake implementation yourself.

Secondly, you have to take care that your fake implementation behaves in the same way as a real service. This is one of the most frequent rakes, which are attacked by supporters of the described approach. You make a fake implementation today, and after a week the real service starts to behave differently. Your tests still pass and you live in peace until you push your code onto the live servers. And there you will find an unpleasant surprise!

This problem is very easy to fix, but it will take a little more effort. You need to write another set of tests, now for a real service, the purpose of which will be to control and maintain the reliability of the protocol between your application and the third-party service. This is some kind of smoke test for an external service that can be run once an hour, day or week depending on the stability of your external service. Then you really control and test integration with an external service and avoid surprises when it changes.

I am not ready for such changes!



The described solution is not for everyone (in principle, like any other). Therefore, I will give some tips with which you can smooth out the problems from the first part of this article.



Someone even this set of measures will significantly improve their testing.

Conclusion



In general, the most important thing is to understand the problems described and to have the desire to solve them. Then you can come up with many alternative solutions and approaches that will work even better than those given in the article for your particular case. And then your tests will again be fast, reliable and even more useful for your team.

Source: https://habr.com/ru/post/170637/


All Articles