Get Started

Integration testing with pytest

1. Integration testing with pytest

Let's learn about integration testing.

2. What is integration testing?

Integration testing is a software testing method that allows one to verify the interactions between different system modules. In other words, integration testing verifies an integration.

3. Integrations in real-life projects

One example of integration is the power cable. It integrates some hardware with a power supply socket. Another example is the internet connection that integrates a device with the Internet. A file reading driver integrates the file system and the software that wants to read it. A connection to a database integrates the client you connect with the database. It allows us to communicate with the database and do such operations as downloading data, creating tables, and executing queries. Finally, an Application Programming Interface (or "API") is an integration between the client and the service with an API.

4. What can go wrong

But why should one worry about creating such tests? There are a lot of things that can go wrong if integrations are not tested. For example, the connection can be lost completely or partially, which can cause a loss of data. Interactions may be too slow or have insufficient bandwidth. Modules may have conflicting versions which is a very common issue in working with data. Or modules may interact with each other using different notations. The latter is called "interface mismatch". There are also many other potential issues.

5. Example of integration testing

Let's take a look at the example. Assume we want to check the integration of the file system with the Python interpreter. In other words, we want to make sure, that Python can create and open files on our computer. In the code, we have the "setup file" fixture function that creates the file and removes it on the teardown phase, that starts with "yield". Hence, the test function "test fs" will ensure the file exists. And finally, the file will be removed by the fixture teardown. The key idea of the test is that we want to check that Python can create a file, and we are checking it by the method "os dot path dot exists". Thus, if the file exists, means that Python could create it, and therefore, the integration works.

6. Summary

We learned about integration testing. Integration testing is a software testing method that allows one to verify that integration works as expected. Real-life projects usually include many integrations, such as various connections and APIs. This type of testing helps to prevent potential problems related to how modules of a system interact with each other. Finally, we dived into the example where we checked integration between Python and a file system.

7. Let's practice!

Now you're familiar with integration testing. Try to create some tests!