こすたろーんエンジニアの試行錯誤部屋

作成物の備忘録を書いていきますー

【pytest】Basic usage in combination with assert and pytest

I tried pytest, which allows unit testing of python
This is a memorandum.

目次

スポンサーリンク

abstract

how to use pytest.

1.requirements

Jetson Xavier NX
ubuntu18.04
docker
python3.x

2.Install necessary modules

pip install pytest pytest-cov

3.Create files and directories

pytest directory : contains a set of test code
test_function.py : test code
function:target function

workspace
├──  pytest/
│    ├── __init__.py
│    └──  test_function.py
│
├── __init__.py
└── function.py

4.Write code in function.py and test_function.py

function.py

def inc(x):
    return x + 1

test_function.py

from .. import function
# import function
def test_answer():
    assert function.inc(4) == 5

5.test

Execute the following command in the directory where function.py is stored

pytest

pass case

=============================test session starts  =======================================
platform linux -- Python 3.7.16, pytest-7.2.1, pluggy-1.0.0
rootdir: /workspace
plugins: cov-4.0.0, flake8-1.1.1
collected 1 item

pytest/test_function.py .
=============================1 passed in 0.05s =======================================

error case

=============================FAILURES =======================================
_______________________________________________ test_answer _______________________________________

    def test_answer():
>       assert function.inc(3) == 5
E       assert 4 == 5
E        +  where 4 = <function inc at 0x7fafe5e7a0>(3)
E        +    where <function inc at 0x7fafe5e7a0> = function.inc

pytest/test_function.py:6: AssertionError
=============================short test summary info =======================================
FAILED pytest/test_function.py::test_answer - assert 4 == 5
============================= 1 failed in 0.13s ============================================

6. Coverage

pytest --cov .
=============================test session starts  =======================================
platform linux -- Python 3.7.16, pytest-7.2.1, pluggy-1.0.0
rootdir: /workspace
plugins: cov-4.0.0, flake8-1.1.1
collected 1 item

pytest/test_function.py .                                                                                                                                                                                                             [100%]

---------- coverage: platform linux, python 3.7.16-final-0 -----------
Name                      Stmts   Miss  Cover
---------------------------------------------
__init__.py                   0      0   100%
function.py                   2      0   100%
pytest/__init__.py            0      0   100%
pytest/test_function.py       3      0   100%
---------------------------------------------
TOTAL                         5      0   100%


=============================1 passed in 0.15s =======================================

スポンサーリンク

deeplearning related articles

technoxs-stacker.hatenablog.com technoxs-stacker.hatenablog.com technoxs-stacker.hatenablog.com

reference

pyteyon.hatenablog.com docs.pytest.org