計算機科学のブログ

ほしい物リスト

Python - pytest - Primary Power - pytest Fixtures - setup show, scope, module, return, yield, docstring

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

コード

test_fixtures.py

import pytest

# @pytest.fixture(scope='module')
# def nums():
#     return [1, 2, 3, 4, 5]


# @pytest.fixture(scope='module')
# def d():
#     return {'a': 1, 'b': 2}


# @pytest.fixture(scope='module')
# def t():
#     return (1, 2)
@pytest.fixture(scope='module')
def nums():
    """docstring nums"""
    print('nums: before')
    yield [1, 2, 3, 4, 5]
    print('nums: after')


@pytest.fixture(scope='module')
def d():
    print('d: before')
    yield {'a': 1, 'b': 2}
    print('d: after')


@pytest.fixture(scope='module')
def t():
    print('t: before')
    yield (1, 2)
    print('t: after')


def test_nums(nums):
    assert nums == [1, 2, 3, 4, 5]


def test_d(d):
    assert d == {'a': 1, 'b': 2}


def test_t(t):
    assert t == (1, 2)


def test_nums_len(nums):
    assert len(nums) == 5

入出力結果(Terminal, Zsh)

% pytest 
============================= test session starts ==============================
platform darwin -- Python 3.13.5, pytest-8.4.1, pluggy-1.6.0
rootdir: /Users/.../ch3
plugins: anyio-4.9.0
collected 3 items                                                              

test_fixtures.py ...                                                     [100%]

============================== 3 passed in 0.02s ===============================
% pytest
============================= test session starts ==============================
platform darwin -- Python 3.13.5, pytest-8.4.1, pluggy-1.6.0
rootdir: /Users/.../ch3
plugins: anyio-4.9.0
collected 4 items                                                              

test_fixtures.py ....                                                    [100%]

============================== 4 passed in 0.02s ===============================
% pytest --setup-show test_fixtures.py 
============================= test session starts ==============================
platform darwin -- Python 3.13.5, pytest-8.4.1, pluggy-1.6.0
rootdir: /Users/.../ch3
plugins: anyio-4.9.0
collected 4 items                                                              

test_fixtures.py 
        SETUP    F nums
        test_fixtures.py::test_nums (fixtures used: nums).
        TEARDOWN F nums
        SETUP    F d
        test_fixtures.py::test_d (fixtures used: d).
        TEARDOWN F d
        SETUP    F t
        test_fixtures.py::test_t (fixtures used: t).
        TEARDOWN F t
        SETUP    F nums
        test_fixtures.py::test_nums_len (fixtures used: nums).
        TEARDOWN F nums

============================== 4 passed in 0.01s ===============================
% pytest --setup-show test_fixtures.py
============================= test session starts ==============================
platform darwin -- Python 3.13.5, pytest-8.4.1, pluggy-1.6.0
rootdir: /Users/.../ch3
plugins: anyio-4.9.0
collected 4 items                                                              

test_fixtures.py 
    SETUP    M nums
        test_fixtures.py::test_nums (fixtures used: nums).
    SETUP    M d
        test_fixtures.py::test_d (fixtures used: d).
    SETUP    M t
        test_fixtures.py::test_t (fixtures used: t).
        test_fixtures.py::test_nums_len (fixtures used: nums).
    TEARDOWN M t
    TEARDOWN M d
    TEARDOWN M nums

============================== 4 passed in 0.02s ===============================
% pytest -s -v test_fixtures.py       
============================= test session starts ==============================
platform darwin -- Python 3.13.5, pytest-8.4.1, pluggy-1.6.0 -- /opt/local/Library/Frameworks/Python.framework/Versions/3.13/bin/python3.13
cachedir: .pytest_cache
rootdir: /Users/.../ch3
plugins: anyio-4.9.0
collected 4 items                                                              

test_fixtures.py::test_nums nums: before
PASSED
test_fixtures.py::test_d d: before
PASSED
test_fixtures.py::test_t t: before
PASSED
test_fixtures.py::test_nums_len PASSEDt: after
d: after
nums: after


============================== 4 passed in 0.01s ===============================
% pytest --fixtures 
============================= test session starts ==============================
platform darwin -- Python 3.13.5, pytest-8.4.1, pluggy-1.6.0
rootdir: /Users/.../ch3
plugins: anyio-4.9.0
collected 4 items                                                              
cache -- .../_pytest/cacheprovider.py:555
    Return a cache object that can persist state between testing sessions.

capsys -- .../_pytest/capture.py:1000
    Enable text capturing of writes to ``sys.stdout`` and ``sys.stderr``.

capteesys -- .../_pytest/capture.py:1028
    Enable simultaneous text capturing and pass-through of writes
    to ``sys.stdout`` and ``sys.stderr`` as defined by ``--capture=``.

capsysbinary -- .../_pytest/capture.py:1063
    Enable bytes capturing of writes to ``sys.stdout`` and ``sys.stderr``.

capfd -- .../_pytest/capture.py:1091
    Enable text capturing of writes to file descriptors ``1`` and ``2``.

capfdbinary -- .../_pytest/capture.py:1119
    Enable bytes capturing of writes to file descriptors ``1`` and ``2``.

doctest_namespace [session scope] -- .../_pytest/doctest.py:740
    Fixture that returns a :py:class:`dict` that will be injected into the
    namespace of doctests.

pytestconfig [session scope] -- .../_pytest/fixtures.py:1424
    Session-scoped fixture that returns the session's :class:`pytest.Config`
    object.

record_property -- .../_pytest/junitxml.py:277
    Add extra properties to the calling test.

record_xml_attribute -- .../_pytest/junitxml.py:300
    Add extra xml attributes to the tag for the calling test.

record_testsuite_property [session scope] -- .../_pytest/junitxml.py:338
    Record a new ``<property>`` tag as child of the root ``<testsuite>``.

tmpdir_factory [session scope] -- .../_pytest/legacypath.py:298
    Return a :class:`pytest.TempdirFactory` instance for the test session.

tmpdir -- .../_pytest/legacypath.py:305
    Return a temporary directory (as `legacy_path`_ object)
    which is unique to each test function invocation.
    The temporary directory is created as a subdirectory
    of the base temporary directory, with configurable retention,
    as discussed in :ref:`temporary directory location and retention`.

caplog -- .../_pytest/logging.py:596
    Access and control log capturing.

monkeypatch -- .../_pytest/monkeypatch.py:31
    A convenient fixture for monkey-patching.

recwarn -- .../_pytest/recwarn.py:34
    Return a :class:`WarningsRecorder` instance that records all warnings emitted by test functions.

tmp_path_factory [session scope] -- .../_pytest/tmpdir.py:240
    Return a :class:`pytest.TempPathFactory` instance for the test session.

tmp_path -- .../_pytest/tmpdir.py:255
    Return a temporary directory (as :class:`pathlib.Path` object)
    which is unique to each test function invocation.
    The temporary directory is created as a subdirectory
    of the base temporary directory, with configurable retention,
    as discussed in :ref:`temporary directory location and retention`.


------------------ fixtures defined from anyio.pytest_plugin -------------------
anyio_backend [module scope] -- ../../../../Library/Python/3.13/lib/python/site-packages/anyio/pytest_plugin.py:174
    no docstring available

anyio_backend_name -- ../../../../Library/Python/3.13/lib/python/site-packages/anyio/pytest_plugin.py:179
    no docstring available

anyio_backend_options -- ../../../../Library/Python/3.13/lib/python/site-packages/anyio/pytest_plugin.py:187
    no docstring available

free_tcp_port_factory [session scope] -- ../../../../Library/Python/3.13/lib/python/site-packages/anyio/pytest_plugin.py:256
    no docstring available

free_udp_port_factory [session scope] -- ../../../../Library/Python/3.13/lib/python/site-packages/anyio/pytest_plugin.py:261
    no docstring available

free_tcp_port -- ../../../../Library/Python/3.13/lib/python/site-packages/anyio/pytest_plugin.py:266
    no docstring available

free_udp_port -- ../../../../Library/Python/3.13/lib/python/site-packages/anyio/pytest_plugin.py:271
    no docstring available


--------------------- fixtures defined from test_fixtures ----------------------
nums [module scope] -- test_fixtures.py:17
    no docstring available

d [module scope] -- test_fixtures.py:24
    no docstring available

t [module scope] -- test_fixtures.py:31
    no docstring available


============================ no tests ran in 0.01s =============================
% pytest --fixtures
...
--------------------- fixtures defined from test_fixtures ----------------------
nums [module scope] -- test_fixtures.py:17
    docstring nums

d [module scope] -- test_fixtures.py:25
    no docstring available

t [module scope] -- test_fixtures.py:32
    no docstring available


============================ no tests ran in 0.02s =============================
%