Tag Archives: Pytest

How to Check if an Exception Is Raised (or Not) With pytest – Learn from Tutorial

How to Check if an Exception Is Raised (or Not) With pytest TL;DR Time is a precious resource so I won’t waste yours. Here’s how you can assert an exception is raised and how to check that in pytest. Solution: Use pytest.raises import pytest def test_raises_exception(): with pytest.raises(ZeroDivisionError): 1 / 0 Enter fullscreen mode Exit…

Read More

Testing Environment variable in pytest – Python Dev Feed

Testing Environment variable in pytest Suppose, we have a method that uses environment variables. import os def get_env_vars(): user = os.environ[‘USER’] token = os.environ[‘TOKEN’] return user, token Enter fullscreen mode Exit fullscreen mode We need to test this method. We will see 3 ways to test this method. Using monkeypatch def test_get_env_vars_with_monkeypatch(monkeypatch): monkeypatch.setenv(‘USER’, ‘hasan’) monkeypatch.setenv(‘TOKEN’,…

Read More

How to Test Jupyter Notebooks with Pytest and Nbmake – Python Dev Feed

How to Test Jupyter Notebooks with Pytest and Nbmake Jupyter notebook files have been one of the fastest-growing content types on GitHub in recent years. They provide a simple interface for iterating on visual tasks, whether you are analyzing datasets or writing code-heavy documentation. Their popularity comes with problems though: large numbers of ipynb files…

Read More