[PYTHON] Super basic usage of pytest

Notes on how to use pytest

I touched pytest, so that note.

What is pytest

Python test framework pytest: helps you write better programs ? pytest documentation

Install pytest

If you install from pip normally, it's OK

Install pytest


pip install pytest

Write a test for pytest

The basic writing method is as follows.

--Write as a Python program --Write the part to be executed as a test with one of the following --Make the function start with test_ --Make the method start with test_ of the class starting with Test --Whether the result is OK or NG is judged by whether the result of the ʻassert conditional expressionis True or False. ――It's notreturn, it's ʻassert

Sample of the side to be tested

Tester (program executed from pytest)

test_program.py


from my_funcs import add, sub


def test_add():
    assert add(1, 2) == 3


def test_sub():
    assert sub(3, 1) == 2


class TestCase:
    def test_true(self):
        assert True

    def test_add_zero(self):
        assert add(1, 0) == 1

Side to be tested

my_funcs.py


def add(a, b):
    return a + b

def sub(a, b):
    return a - b

Run pytest

If you run pytest normally, it will be in the current directory. Automatically execute files whose filenames start with test_ or end with _test.

Run pytest


pytest

When specifying a test program

Only the test specified by pytest tester program name is executed.

Run pytest with a file


pytest test_program.py

How to read the execution result

Example that passed the test

--First there is a header, followed by the result in the format filename .. --Each of these . Corresponds to result = OK in the test function. --The number of passes is displayed at the end and the process ends

Execution result


==================================== test session starts ====================================
platform linux -- Python 3.6.3, pytest-5.3.0, py-1.8.0, pluggy-0.13.1
rootdir: /mnt/c/Users/nab391/pytest
collected 2 items

test_example1-1.py ..                                                                 [100%]

===================================== 2 passed in 0.02s =====================================

An example of a test

--It's the same that there is a header first, followed by the result in the format filename .. --Tests that do not pass will show F --After that, the part that did not pass to FAILURES is displayed. --The number of OK and NG is displayed at the end and the process ends.

Test NG


==================================== test session starts ====================================
platform linux -- Python 3.6.3, pytest-5.3.0, py-1.8.0, pluggy-0.13.1
rootdir: /mnt/c/Users/nab391/pytest
collected 3 items

test_example1-1.py ..F                                                                [100%]

========================================= FAILURES ==========================================
________________________________________ test_false _________________________________________

    def test_false():
>       assert False
E       assert False

test_example1-1.py:14: AssertionError
================================ 1 failed, 2 passed in 0.08s ================================

Execution options

---s: Output standard output (default: not) ---v: Output the details of the result (default: not)

Detailed output example


test_example1-1.py::test_add PASSED                                                   [ 50%]
test_example1-1.py::test_sub PASSED                                                   [100%]
###How to read the execution result

Recommended Posts

Super basic usage of pytest
Basic usage of flask-classy
Basic usage of Jinja2
Basic usage of SQLAlchemy
Basic usage of PySimple GUI
Basic usage of Pandas Summary
Basic usage of Python f-string
Installation and easy usage of pytest
Basic usage of Btrfs on Arch Linux
pytest usage summary
[Introduction to Python] Basic usage of lambda expressions
Basic operation of pandas
Summary of pyenv usage
Usage of Python locals ()
Basic operation of Pandas
Basic knowledge of Python
python memorandum super basic
Basic processing of librosa
Make a note of the list of basic Pandas usage
(Beginner) Basic usage of Datastore on Google App Engine
[Introduction to Python] Basic usage of the library matplotlib
[Python] Correct usage of map
Pre-processing and post-processing of pytest
Convenient usage summary of Flask
Basic flow of anomaly detection
virtualenv Basic command usage memo
(Minimal) usage of django logger
XPath Basics (1) -Basic Concept of XPath
Sample usage of Python pickle
One-liner basic graph of HoloViews
[Python] Correct usage of join
Index of certain pandas usage
[2020/06 latest version] Basic usage of python dependency management tool poetry
[Python] Basic pattern and usage of if statement (comparison operator and Boolean operator)
Basic knowledge of Linux and basic commands
Summary of basic knowledge of PyPy Part 1
Summary of basic implementation by PyTorch
Easy usage memo of Anaconda (conda)
About the basic type of Go
Basic grammar of Python3 system (dictionary)
[python] Correct usage of if statement
About Linux commands Super basic edition
Basic study of OpenCV with Python