Get startedGet started for free

Applying test markers

1. Applying test markers

Now, we will learn about test markers.

2. Overview of test markers

Let's say we want to skip the specific test if we don't need it to run right now. Or if we created a test that is expected to fail. Then, we can mark it with the corresponding marker. Pytest will process the marker and run the test accordingly. A test marker is a tag of a test in the pytest library. Any test function can be attributed to one or more markers. We use markers to specify behavior for a particular test.

3. Markers syntax

As you already know, a decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. To mark a test function, we add the decorator starting with "at pytest dot mark". Then we continue by another "dot" and the marker name we want to use. The "test_get_len" function is marked with a "pytest skip" marker. We will talk more about it in the next slide. What is important here is that the syntax of pytest markers is based on decorators.

4. Skip and skipif markers

Let's talk more about the specific markers. Pytest has some markers that can be used out of the box. One is the "skip" marker: any test marked with it will be skipped during the pytest run. This is useful if one wants to wait to run that test. Another one is "skipif": any test marked with it will be skipped, but only if the given condition is true.

5. Skip marker example

For example, take a look at this snippet. We have two functions here: "get_length" to get the length of the string and the test function "test_get_len" which tests the first one. Now let's look at the output.

6. Skip marker example: output

Since the "test_get_len" function was marked with a "skip" marker, the test will be skipped and zero tests will run. So if we actually run that test, we would see something like this: one test was found, but also one test was skipped.

7. Skipif marker example

Another example shows how "skipif" marker can be applied to the same snippet. To do that, we have to pass a string with a condition right to the "skipif" decorator. In this trivial example, we passed two by two equals five, which is always false.

8. Skipif marker example: output

Thus the test will not be skipped and will be run. But you can also pass variables to the skipif, for example, to verify the module's versions.

9. Xfail marker

Sometimes, it is OK for a test to fail. For example, that might be the case if we have a bug that we are already aware of. So we want to know if that the bug is still there. To do that, use the pytest "xfail" marker. Like before, add the marker before the test function and run the test. Here we want to test a function that generates a sequence of numbers from 1 to n by passing it minus one. This test will end as "successful" because it will fail exactly as we expected.

10. Xfail marker: output

Let's run the test that is expected to fail. In the output, we see that 1 test was "collected" and one test "xfailed", which means that it failed as expected.

11. Summary

Let's recap. The test marker is an tag of a test in pytest. It is used to specify test functions behavior. To add a test marker, write "at pytest dot mark dot marker name". Pytest already has some out-of-the-box markers, like "xfail", "skip", and "skipif".

12. Let's practice!

You are now familiar with pytest markers basics. Go and try to build some tests using it!