You are currently viewing How to Use Pytest – Fixtures, Parametrization, Markers, and more…

How to Use Pytest – Fixtures, Parametrization, Markers, and more…

You may have done unit testing or heard the term unit test, which involves breaking down your code into smaller units and testing them to see if they are producing the correct output.

Python has a robust unit testing library called unittest that provides a comprehensive set of testing features. However, some developers believe that unittest is more verbose than other testing frameworks.

In this article, you’ll look at how to use the pytest library to create small, concise test cases for your code. Throughout the process, you’ll learn about the pytest library’s key features.

Installation

Pytest is a third-party library that must be installed in your project environment. In your terminal window, type the following command.

Pytest has been installed in your project environment, and all of its functions and classes are now available for use.

Getting Started With Pytest

Before getting into what pytest can do, let’s take a look at how to use it to test the code.

Here’s a Python file test_square.py that contains a square function and a test called test_answer.

To run the above test, simply enter the pytest command into your terminal, and the rest will be handled by the pytest library.

The above test failed, as evidenced by the output generated by the pytest library. You might be wondering how pytest discovered and ran the test when no arguments were passed.

This occurred because pytest uses standard test discovery. This includes the conventions that must be followed for testing to be successful.

  • When no argument is specified, pytest searches files that are in *_test.py or test_*.py format.
  • Pytest collects test functions and methods that are prefixed with test, as well as test prefixed test functions and modules inside Test prefixed test classes that do not have a __init__ method, from these files.
  • Pytest also finds tests in subdirectories, making it simple to organize your tests within the context of your project structure.

Why do Most Prefer pytest?

If you’ve used the unittest library before, you’ll know that even writing a small test requires more code than pytest. Here’s an example to demonstrate.

Assume you want to write a unittest test suite to test your code.

Now, from the command line, run these tests with unittest.

As you can see, the test_query test failed while the test_capitalize test passed, as expected by the code.

However, writing those tests requires more lines of code, which include:

  • Importing the unittest module.
  • A test class (TestWithUnittest) is created by subclassing TestCase.
  • Making assertions with unittest’s assert methods (assertTrue, assertFalse, and assertEqual).

However, this is not the case with pytest, if you wrote those tests with pytest, they must look like this:

It’s as simple as that, there’s no need to import the package or use pre-defined assertion methods. With a detailed description, you will get a nicer output.

The following information can be found in the output:

  • The platform on which the test is run, the library versions used, the root directory where the test files are stored, and the plugins used.
  • The Python file from the test, in this case, test_pytest.py, was collected.
  • The test result, which is a "F" and a dot (.). An "F" indicates a failed test, a dot (.) indicates a passed test, and a "E" indicates an unexpected condition that occurred during testing.
  • Finally, a test summary, which prints the results of the tests.

Parametrize Tests

What exactly is parametrization? Parametrization is the process of running multiple sets of tests on the same test function or class, each with a different set of parameters or arguments. This allows you to test the expected results of different input values.

If you want to write multiple tests to evaluate various arguments for the square function, your first thought might be to write them as follows:

But wait, there’s a twist, pytest saves you from writing even more boilerplate code. To allow the parametrization of arguments for a test function, pytest provides the @pytest.mark.parametrize decorator.

Using parametrization, you can eliminate code duplication and significantly reduce your test code.

The @pytest.mark.parametrize decorator defines four different ("num, expected") tuples in the preceding code. The test_square test function will execute each of them one at a time, and the test report will be generated by determining whether the num evaluated is equal to the expected value.

Pytest Fixtures

Using pytest fixtures, you can avoid duplicating setup code across multiple tests. By defining a function with the @pytest.fixture decorator, you create a reusable setup that can be shared across multiple test functions or classes.

In testing, a fixture provides a defined, reliable, and consistent context for the tests. This could include environment (for example a database configured with known parameters) or content (such as a dataset). Source

Here’s an example of when you should use fixtures. Assume you have a continuous stream of dynamic vehicle data and want to write a function collect_vehicle_number_from_delhi() to extract vehicle numbers belonging to Delhi.

To check whether the function works properly, you would write a test that looks like the following:

The test function test_collect_vehicle_number_from_delhi() above determines whether or not the collect_vehicle_number_from_delhi() function extracts the data as expected. Now you might want to extract the vehicle number belonging to another state, then you will write another function collect_vehicle_number_from_haryana().

Following the creation of this function, you will create another test function and repeat the process.

This is analogous to repeatedly writing the same code. To avoid writing the same code multiple times, create a function decorated with @pytest.fixture here.

As you can see from the code above, the number of lines has been reduced to some extent, and you can now write a few more tests by reusing the @pytest.fixture decorated function vehicle_data.

Fixture for Database Connection

Consider the example of creating a database connection, in which a fixture is used to set up the resources and then tear them down.

A fixture database_connection() is created, which creates an SQLite database in memory and establishes a connection, then creates a table, yields the connection, and finally closes the connection once the work is completed.

This fixture can be passed as an argument to the test function. Assume you want to write a function to insert a value into a database, simply do the following:

The test_insert_data() test function takes the database_connection fixture as an argument, which eliminates the need to rewrite the database connection code.

You can now write as many test functions as you want without having to rewrite the database setup code.

Markers in Pytest

Pytest provides a few built-in markers to mark your test functions which can be handy while testing.

In the earlier section, you saw the parametrization of arguments using the @pytest.mark.parametrize decorator. Well, @pytest.mark.parametrize is a decorator that marks a test function for parametrization.

Skipping Tests

If you have a test function that you want to skip during testing for some reason, you can decorate it with @pytest.mark.skip.

In the test_pytest_fixture.py script, for example, you added two new test functions but want to skip testing them because you haven’t yet created the collect_vehicle_number_from_punjab() and collect_vehicle_number_from_maharashtra() functions to pass these tests.

Both test functions in this script are marked with @pytest.mark.skip and provide a reason for skipping. When you run this script, pytest will bypass these tests.

The report shows that two tests were passed and two were skipped.

If you want to conditionally skip a test function. In that case, use the @pytest.mark.skipif decorator to mark the test function. Here’s an illustration.

In this example, two test functions (test_collect_vehicle_number_from_punjab and test_collect_vehicle_number_from_karnataka) are decorated with @pytest.mark.skipif. The condition specified in each case is pytest.version_tuple < (7, 2), which means that these tests will be skipped if the installed pytest version is less than 7.2. The reason parameter provides a message explaining why the tests are being skipped.

Filter Warnings

You can add warning filters to specific test functions or classes using the @pytest.mark.filterwarnings function, allowing you to control which warnings are captured during tests.

Here’s an example of the code from above.

In this example, a warning message is emitted by a helper warning function (warning_function()).

Both test functions (test_collect_vehicle_number_from_punjab and test_collect_vehicle_number_from_karnataka) are decorated with @pytest.mark.filterwarnings which specifies that any UserWarning with the message “Not implemented yet” should be treated as an error during the execution of these tests.

These test functions call warning_function which, in turn, emits a UserWarning with the specified message.

You can see in the summary of the report generated by pytest, the warning is displayed.

Pytest Command-line Options

Pytest provides numerous command-line options that allow you to customize or extend the behavior of test execution. You can list all the available pytest options using the following command in your terminal.

Here are some pytest command-line options that you can try when you execute tests.

Running Tests Using Keyword

You can specify which tests to run by following the -k option with a keyword or expression. Assume you have the Python file test_sample.py, which contains the tests listed below.

If you want to run tests that contain "test_special", use the following command.

The tests that have "test_special" in their name were selected, and the others were deselected.

If you want to run all other tests but not the ones with “test_special” in their names, use the following command.

The expression "not test_special" in the above command indicates that run only those tests that don’t have “test_special” in their name.

Customizing Output

You can use the following options to customize the output and the report:

  • -v, --verbose – Increases verbosity
  • --no-header – Disables header
  • --no-summary – Disables summary
  • -q, --quiet – Decreases verbosity

Output of the tests with increased verbosity.

Output of the tests with decreased verbosity.

When you use --no-header and --no-summary together, it is equivalent to using -q (decreased verbosity).

Test Collection

Using the --collect-only, --co option, pytest collects all the tests but doesn’t execute them.

Ignore Path or File during Test Collection

If you don’t want to collect tests from a specific path or file, use the --ignore=path option.

The test_sample.py file is ignored by pytest during test collection in the above example.

Exit on First Failed Test or Error

When you use the -x, --exitfirst option, pytest exits the test execution on the first failed test or error that it finds.

Pytest immediately exits the test execution when it finds a failed test and a stopping message appears in the report summary.

Conclusion

Pytest is a testing framework that allows you to write small and readable tests to test or debug your code.

In this article, you’ve learned:

  • How to use pytest for testing your code
  • How to parametrize arguments to avoid code duplication
  • How to use fixtures in pytest
  • Pytest command-line options

🏆Other articles you might be interested in if you liked this one

Debug/Test your code using the unittest module in Python.

What is assert in Python and how to use it for debugging?

Create a WebSocket server and client in Python.

Create multi-threaded Python programs using a threading module.

Create and integrate MySQL database with Flask app using Python.

Upload and display images on the frontend using Flask.


That’s all for now

Keep Coding✌✌