pytest: Quick Start & Coverage Report

In this post, I am going to share how to use pytest.

Check https://docs.pytest.org/en/stable/getting-started.html for more detail.


What is pytest?

pytest is a testing framework for Python. https://docs.pytest.org/en/stable/

A similar technologies may include unittest, which is more like JUnit in my opinion.


Install

% pip install pytest

...

% pytest --version

pytest 6.2.1


Test

Let's start with a buggy code:

% cat test_sample.py 

def plus(x, y):

    return x * y


def test_answer():

    assert plus(2, 3) == 5


% pytest

==================================================================== test session starts =====================================================================

platform darwin -- Python 3.8.5, pytest-6.2.1, py-1.9.0, pluggy-0.13.1

rootdir: /Users/tomo/git/pytest

plugins: cov-2.10.1

collected 1 item                                                                                                                                             


test_sample.py F                                                                                                                                       [100%]


========================================================================== FAILURES ==========================================================================

________________________________________________________________________ test_answer _________________________________________________________________________


    def test_answer():

>       assert plus(2, 3) == 5

E       assert 6 == 5

E        +  where 6 = plus(2, 3)


test_sample.py:5: AssertionError

================================================================== short test summary info ===================================================================

FAILED test_sample.py::test_answer - assert 6 == 5

===================================================================== 1 failed in 0.06s ======================================================================

Fix it (replace * into +) and run again to see the success case.

Note "pytest will run all files of the form test_*.py or *_test.py in the current directory and its subdirectories" (https://docs.pytest.org/en/stable/getting-started.html).


Coverage

You can also use pytest-cov for coverage check.

% pip install pytest-cov
% pytest -v --cov=.

==================================================================== test session starts =====================================================================

platform darwin -- Python 3.8.5, pytest-6.2.1, py-1.9.0, pluggy-0.13.1 -- /Users/tomo/opt/anaconda3/bin/python

cachedir: .pytest_cache

rootdir: /Users/tomo/git/pytest

plugins: cov-2.10.1

collected 1 item                                                                                                                                             


test_sample.py::test_answer PASSED                                                                                                                     [100%]


---------- coverage: platform darwin, python 3.8.5-final-0 -----------

Name             Stmts   Miss  Cover

------------------------------------

test_sample.py       4      0   100%



===================================================================== 1 passed in 0.03s ======================================================================

To generate a HTML report,

% pytest -v --cov=. --cov-report=html
% open htmlcov/index.html



Comments

Popular posts from this blog

Selenide: Quick Start

Minikube Installation for M1 Mac

Three.js Quick Start - Run Spinning-Cube Example