Using Fixtures with pifpaf

Learn how to use fixtures with pifpaf.

While it is possible to run pifpaf globally around a test suite, it is also possible to use it inside tests and orchestrate some of its behavior.

pifpaf exports its drivers as test fixtures. In unit testing, fixtures represent components that are set up before a test and cleaned up after the test is finished. It is usually a good idea to build a specific kind of component for them, as they are reused in a lot of different places. In this case, pifpaf exports objects that represent the daemon launched. The object is initialized before each test and reset to its default values when the test is completed.

Testing using pifpaf and fixtures

In the following example, the test sets up a memcached instance before running each test. While this is slower and more expensive than using a memcached for all the tests, it makes sure that each test runs on top of a clean and fresh memcached instance, avoiding side effects that could result from having several tests running one after another on the same instance.

In the application below, click Run and use the command sudo -u memcache python -m unittest test_memcached_fixtures.py.

Get hands-on with 1200+ tech skills courses.