How to Monkey Patch Global Variables in pytest

Florian Dahlitz
8 min
Aug. 17, 2021

In this pytest tutorial, you'll learn how to monkey patch global variables.

First, we create the first part of a Python pipeline, which collects files.

Secondly, we try to test the implemented function without monkey patching the global variable pointing to the data directory.

Last but not least, I'll show you how you can monkey patch this global variable to use a temporary directory and get the test pass.