Setting up py.test
The Code to Test
Say we are testing an addition function in
The Testing Code
We create a test file in
projectroot/tests/test_code.py. The file must begin with
test_ to be recognized as a testing file.
Running The Test
projectroot we simply run
A failing test will provide helpful output as to what went wrong:
Intro to Test Fixtures
More complicated tests sometimes need to have things set up before you run the code you want to test. It is possible to do this in the test function itself, but then you end up with large test functions doing so much that it is difficult to tell where the setup stops and the test begins. You can also get a lot of duplicate setup code between your various test functions.
Our code file:
Our test file:
These are pretty simple examples, but if our
Stuff object needed a lot more setup, it would get unwieldy. We see that there is some duplicated code between our test cases, so let's refactor that into a separate function first.
This looks better but we still have the
my_stuff = get_prepped_stuff() call cluttering up our test functions.
py.test fixtures to the rescue!
Fixtures are much more powerful and flexible versions of test setup functions. They can do a lot more than we're leveraging here, but we'll take it one step at a time.
First we change
get_prepped_stuff to a fixture called
prepped_stuff. You want to name your fixtures with nouns rather than verbs because of how the fixtures will end up being used in the test functions themselves later. The
@pytest.fixture indicates that this specific function should be handled as a fixture rather than a regular function.
Now we should update the test functions so that they use the fixture. This is done by adding a parameter to their definition that exactly matches the fixture name. When py.test executes, it will run the fixture before running the test, then pass the return value of the fixture into the test function through that parameter. (Note that fixtures don't need to return a value; they can do other setup things instead, like calling an external resource, arranging things on the filesystem, putting values in a database, whatever the tests need for setup)
Now you can see why we named it with a noun. but the
my_stuff = prepped_stuff line is pretty much useless, so let's just use
prepped_stuff directly instead.
Now we're using fixtures! We can go further by changing the scope of the fixture (so it only runs once per test module or test suite execution session instead of once per test function), building fixtures that use other fixtures, parametrizing the fixture (so that the fixture and all tests using that fixture are run multiple times, once for each parameter given to the fixture), fixtures that read values from the module that calls them... as mentioned earlier, fixtures have a lot more power and flexibility than a normal setup function.
Cleaning up after the tests are done.
Let's say our code has grown and our Stuff object now needs special clean up.
We could add some code to call the clean up at the bottom of every test function, but fixtures provide a better way to do this. If you add a function to the fixture and register it as a finalizer, the code in the finalizer function will get called after the test using the fixture is done. If the scope of the fixture is larger than a single function (like module or session), the finalizer will be executed after all the tests in scope are completed, so after the module is done running or at the end of the entire test running session.
Using the finalizer function inside a function can be a bit hard to understand at first glance, especially when you have more complicated fixtures. You can instead use a yield fixture to do the same thing with a more human readable execution flow. The only real difference is that instead of using
return we use a
yield at the part of the fixture where the setup is done and control should go to a test function, then add all the cleanup code after the
yield. We also decorate it as a
yield_fixture so that py.test knows how to handle it.
And that concludes the Intro to Test Fixtures!