計算機科学のブログ

ほしい物リスト

Python - pytest - Primary Power - Getting Started with pytest - assert

Python Testing with pytest, Second Edition - Simple, Rapid, Effective, and ScalableBrian Okken (著)、 Pragmatic Bookshelf のPart Ⅰ(Primary Power)、Chapter 1(Getting Started with pytest)、Exercises、5.(54/598)の解答を求めてみる。

コード

test_ex4.py

def test_true():
    assert 1 in [2, 3, 4]
    assert a < b
    assert 'fizz' not in 'Fizzbuzz'

入出力結果(Terminal, Zsh)

% source venv/bin/activate
(venv) ...% pytest 
============================= test session starts ==============================
platform darwin -- Python 3.13.3, pytest-8.3.5, pluggy-1.6.0
rootdir: /Users/kamimura/posts/cs/python-testing-with-pytest/ch1
collected 2 items                                                              

test_ex4.py F                                                            [ 50%]
test_ex5.py F                                                            [100%]

=================================== FAILURES ===================================
__________________________________ test_true ___________________________________

    def test_true():
        assert True
>       assert False
E       assert False

test_ex4.py:3: AssertionError
__________________________________ test_true ___________________________________

    def test_true():
>       assert 1 in [2, 3, 4]
E       assert 1 in [2, 3, 4]

test_ex5.py:2: AssertionError
=========================== short test summary info ============================
FAILED test_ex4.py::test_true - assert False
FAILED test_ex5.py::test_true - assert 1 in [2, 3, 4]
============================== 2 failed in 0.01s ===============================
(venv) ...% pytest -v
============================= test session starts ==============================
platform darwin -- Python 3.13.3, pytest-8.3.5, pluggy-1.6.0 -- /Users/kamimura/posts/cs/python-testing-with-pytest/ch1/venv/bin/python3.13
cachedir: .pytest_cache
rootdir: /Users/kamimura/posts/cs/python-testing-with-pytest/ch1
collected 2 items                                                              

test_ex4.py::test_true FAILED                                            [ 50%]
test_ex5.py::test_true FAILED                                            [100%]

=================================== FAILURES ===================================
__________________________________ test_true ___________________________________

    def test_true():
        assert True
>       assert False
E       assert False

test_ex4.py:3: AssertionError
__________________________________ test_true ___________________________________

    def test_true():
>       assert 1 in [2, 3, 4]
E       assert 1 in [2, 3, 4]

test_ex5.py:2: AssertionError
=========================== short test summary info ============================
FAILED test_ex4.py::test_true - assert False
FAILED test_ex5.py::test_true - assert 1 in [2, 3, 4]
============================== 2 failed in 0.01s ===============================
(venv) ...% deactivate
...%