I touched pytest, so that note.
Python test framework pytest: helps you write better programs ? pytest documentation
If you install from pip normally, it's OK
Install pytest
pip install 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 not
return, it's ʻassert
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
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
Only the test specified by pytest tester program name
is executed.
Run pytest with a file
pytest test_program.py
--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 =====================================
--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 ================================
---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