1 - Getting Started#

https://docs.pytest.org/en/stable/getting-started.html

Within the file test_sample.py, we create the function func and the test test_answer:

def func(x):
    return x + 1

def test_wrong_answer():
    assert func(3) == 5

def test_right_answer():
    assert func(3) == 4

When we run pytest in the directory, all files in the form test_*.py or *_test.py are run.

!pytest
============================= test session starts =============================
platform win32 -- Python 3.13.3, pytest-8.4.0, pluggy-1.6.0
rootdir: c:\Users\michael.moen\OneDrive - Veterans United Home Loans\Documents\notes\other\pytest
collected 2 items

test_sample.py F.                                                        [100%]

================================== FAILURES ===================================
______________________________ test_wrong_answer ______________________________

    def test_wrong_answer():
>       assert func(3) == 5
E       assert 4 == 5
E        +  where 4 = func(3)

test_sample.py:5: AssertionError
=========================== short test summary info ===========================
FAILED test_sample.py::test_wrong_answer - assert 4 == 5
========================= 1 failed, 1 passed in 0.07s =========================

Assert that a Certain Exception is Raised#

In test_sysexit.py, we put the following:

import pytest

def f():
    raise SystemExit(1)

def test_mytest():
    with pytest.raises(SystemExit):
        f()

The raises() function asserts that some code raises an exception.

!pytest
============================= test session starts =============================
platform win32 -- Python 3.13.3, pytest-8.4.0, pluggy-1.6.0
rootdir: c:\Users\michael.moen\OneDrive - Veterans United Home Loans\Documents\notes\other\pytest
collected 3 items

test_sample.py F.                                                        [ 66%]
test_sysexit.py .                                                        [100%]

================================== FAILURES ===================================
______________________________ test_wrong_answer ______________________________

    def test_wrong_answer():
>       assert func(3) == 5
E       assert 4 == 5
E        +  where 4 = func(3)

test_sample.py:5: AssertionError
=========================== short test summary info ===========================
FAILED test_sample.py::test_wrong_answer - assert 4 == 5
========================= 1 failed, 2 passed in 0.10s =========================

We can use -q to have the output be more brief:

!pytest -q
F..                                                                      [100%]
================================== FAILURES ===================================
______________________________ test_wrong_answer ______________________________

    def test_wrong_answer():
>       assert func(3) == 5
E       assert 4 == 5
E        +  where 4 = func(3)

test_sample.py:5: AssertionError
=========================== short test summary info ===========================
FAILED test_sample.py::test_wrong_answer - assert 4 == 5
1 failed, 2 passed in 0.07s

Group Multiple Tests in a Class#

We can group tests into a class and run them. Note that any test classes must be prefixed with Test. We create test_class.py:

class TestClass:
    def test_one(self):
        x = "this"
        assert "h" in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, "check")
!pytest -q test_class.py
.F                                                                       [100%]
================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________

self = <test_class.TestClass object at 0x0000025574830050>

    def test_two(self):
        x = "hello"
>       assert hasattr(x, "check")
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class.py:8: AssertionError
=========================== short test summary info ===========================
FAILED test_class.py::TestClass::test_two - AssertionError: assert False
1 failed, 1 passed in 0.08s

Note that each test has its own instance of the class.

Request a Unique Temporary Directory for Functional Test#

def test_needsfiles(tmp_path):
    print(tmp_path)
    assert 0
!pytest -q test_tmp_path.py
F                                                                        [100%]
================================== FAILURES ===================================
_______________________________ test_needsfiles _______________________________

tmp_path = WindowsPath('C:/Users/michael.moen/AppData/Local/Temp/pytest-of-Michael.Moen/pytest-4/test_needsfiles0')

    def test_needsfiles(tmp_path):
        print(tmp_path)
>       assert 0
E       assert 0

test_tmp_path.py:3: AssertionError
---------------------------- Captured stdout call -----------------------------
C:\Users\michael.moen\AppData\Local\Temp\pytest-of-Michael.Moen\pytest-4\test_needsfiles0
=========================== short test summary info ===========================
FAILED test_tmp_path.py::test_needsfiles - assert 0
1 failed in 0.06s
!pytest
============================= test session starts =============================
platform win32 -- Python 3.13.3, pytest-8.4.0, pluggy-1.6.0
rootdir: c:\Users\michael.moen\OneDrive - Veterans United Home Loans\Documents\notes\other\pytest
collected 6 items

test_class.py .F                                                         [ 33%]
test_sample.py F.                                                        [ 66%]
test_sysexit.py .                                                        [ 83%]
test_tmp_path.py F                                                       [100%]

================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________

self = <test_class.TestClass object at 0x00000228FF660050>

    def test_two(self):
        x = "hello"
>       assert hasattr(x, "check")
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class.py:8: AssertionError
______________________________ test_wrong_answer ______________________________

    def test_wrong_answer():
>       assert func(3) == 5
E       assert 4 == 5
E        +  where 4 = func(3)

test_sample.py:5: AssertionError
_______________________________ test_needsfiles _______________________________

tmp_path = WindowsPath('C:/Users/michael.moen/AppData/Local/Temp/pytest-of-Michael.Moen/pytest-5/test_needsfiles0')

    def test_needsfiles(tmp_path):
        print(tmp_path)
>       assert 0
E       assert 0

test_tmp_path.py:3: AssertionError
---------------------------- Captured stdout call -----------------------------
C:\Users\michael.moen\AppData\Local\Temp\pytest-of-Michael.Moen\pytest-5\test_needsfiles0
=========================== short test summary info ===========================
FAILED test_class.py::TestClass::test_two - AssertionError: assert False
FAILED test_sample.py::test_wrong_answer - assert 4 == 5
FAILED test_tmp_path.py::test_needsfiles - assert 0
========================= 3 failed, 3 passed in 0.15s =========================