Testing using dependency mocking
One of the biggest advantages of using Python is having a rich library of resources at our disposal. That includes modules in the standard library, like the csv
module to read and write CSV files or the re
library to use regexes.
Others can be external, like Beautiful Soup
to parse HTML or matplotlib
to generate graphs. We can also create our own libraries or structure our code in different files and encapsulate the functionality in a way that's reusable and improves readability.
When creating tests, sometimes using external elements and library calls to the core of the test is not advisable. For example, to test that a report is correctly processed, it may be necessary to read a CSV file that contains the report. But preparing the test by preparing a file, in this case, becomes cumbersome and can lose focus on the actual objective of the test.
In these cases, it can be convenient to simulate those dependencies to simplify the tests...