計算機科学のブログ

ほしい物リスト

Python - pytest - Primary Power - Getting Started with pytest - virtual environment(venv)、pip、test

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

コード

test_ex4.py

def test_true():
    assert True
    assert False

入出力結果(Terminal, Zsh)

...% python -m venv 
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
            [--upgrade] [--without-pip] [--prompt PROMPT] [--upgrade-deps]
            [--without-scm-ignore-files]
            ENV_DIR [ENV_DIR ...]
venv: error: the following arguments are required: ENV_DIR
...% python -m venv venv
...% ls venv 
bin		include		lib		pyvenv.cfg
...% source venv/bin/activate
(venv) ...% deactivate 
...% source venv/bin/activate
(venv) ...% python -V
Python 3.13.3
(venv) ...% which python
/Users/.../ch1/venv/bin/python
(venv) ...% deactivate 
...% source venv/bin/activate
(venv) ...% pip list
Package Version
------- -------
pip     25.0.1
(venv) ...% pip install pytest
Collecting pytest
  Downloading pytest-8.3.5-py3-none-any.whl.metadata (7.6 kB)
Collecting iniconfig (from pytest)
  Downloading iniconfig-2.1.0-py3-none-any.whl.metadata (2.7 kB)
Collecting packaging (from pytest)
  Using cached packaging-25.0-py3-none-any.whl.metadata (3.3 kB)
Collecting pluggy<2,>=1.5 (from pytest)
  Downloading pluggy-1.6.0-py3-none-any.whl.metadata (4.8 kB)
Downloading pytest-8.3.5-py3-none-any.whl (343 kB)
Downloading pluggy-1.6.0-py3-none-any.whl (20 kB)
Downloading iniconfig-2.1.0-py3-none-any.whl (6.0 kB)
Using cached packaging-25.0-py3-none-any.whl (66 kB)
Installing collected packages: pluggy, packaging, iniconfig, pytest
Successfully installed iniconfig-2.1.0 packaging-25.0 pluggy-1.6.0 pytest-8.3.5

[notice] A new release of pip is available: 25.0.1 -> 25.1.1
[notice] To update, run: pip install --upgrade pip
(venv) ...% pip install --upgrade pip
Requirement already satisfied: pip in ./venv/lib/python3.13/site-packages (25.0.1)
Collecting pip
  Downloading pip-25.1.1-py3-none-any.whl.metadata (3.6 kB)
Downloading pip-25.1.1-py3-none-any.whl (1.8 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 11.3 MB/s eta 0:00:00
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 25.0.1
    Uninstalling pip-25.0.1:
      Successfully uninstalled pip-25.0.1
Successfully installed pip-25.1.1
(venv) ...% pip list
Package   Version
--------- -------
iniconfig 2.1.0
packaging 25.0
pip       25.1.1
pluggy    1.6.0
pytest    8.3.5
(venv) ...% code test_ex4.py 
(venv) ...% pytest 
============================= test session starts ==============================
platform darwin -- Python 3.13.3, pytest-8.3.5, pluggy-1.6.0
rootdir: /Users/.../ch1
collected 0 items                                                              

============================ no tests ran in 0.00s =============================
(venv) ...% pytest
============================= test session starts ==============================
platform darwin -- Python 3.13.3, pytest-8.3.5, pluggy-1.6.0
rootdir: /Users/.../ch1
collected 1 item                                                               

test_ex4.py .                                                            [100%]

============================== 1 passed in 0.00s ===============================
(venv) ...% pytest -v
============================= test session starts ==============================
platform darwin -- Python 3.13.3, pytest-8.3.5, pluggy-1.6.0 -- /Users/.../ch1/venv/bin/python3.13
cachedir: .pytest_cache
rootdir: /Users/.../ch1
collected 1 item                                                               

test_ex4.py::test_true PASSED                                            [100%]

============================== 1 passed in 0.00s ===============================
(venv) ...% pytest   
============================= test session starts ==============================
platform darwin -- Python 3.13.3, pytest-8.3.5, pluggy-1.6.0
rootdir: /Users/.../ch1
collected 1 item                                                               

test_ex4.py F                                                            [100%]

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

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

test_ex4.py:3: AssertionError
=========================== short test summary info ============================
FAILED test_ex4.py::test_true - assert False
============================== 1 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/.../ch1/venv/bin/python3.13
cachedir: .pytest_cache
rootdir: /Users/.../ch1
collected 1 item                                                               

test_ex4.py::test_true FAILED                                            [100%]

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

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

test_ex4.py:3: AssertionError
=========================== short test summary info ============================
FAILED test_ex4.py::test_true - assert False
============================== 1 failed in 0.01s ===============================
(venv) ...% pytest test_ex4.py 
============================= test session starts ==============================
platform darwin -- Python 3.13.3, pytest-8.3.5, pluggy-1.6.0
rootdir: /Users/.../ch1
collected 1 item                                                               

test_ex4.py F                                                            [100%]

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

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

test_ex4.py:3: AssertionError
=========================== short test summary info ============================
FAILED test_ex4.py::test_true - assert False
============================== 1 failed in 0.01s ===============================
(venv) ...% pytest -v test_ex4.py
============================= test session starts ==============================
platform darwin -- Python 3.13.3, pytest-8.3.5, pluggy-1.6.0 -- /Users/.../ch1/venv/bin/python3.13
cachedir: .pytest_cache
rootdir: /Users/.../ch1
collected 1 item                                                               

test_ex4.py::test_true FAILED                                            [100%]

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

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

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