Pytest : helps you write better programs
Table of Contents
Definition
pytest: helps you write better programs
The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.
Documentation
At the official web-site
Prerequisites
Check up does pytest installed already:
#+BEGIN_SRC sh :results ouput pp :exports both pip freeze | grep pytest #+END_SRC #+RESULTS: : pytest==3.0.2
Seems like it's installed properly and we going on.
Tip If you got an empty string as a result of code execution above it signed on necessity to install pytest.
Working sample
Lets create a separate test file:
#+BEGIN_SRC python :noweb yes :tangle tests/test_sample.py """This is an initial pytest sample file""" def func(x): return x + 1 def test_answer(): assert func(3) == 5 #+END_SRC
Tip Type
C-c C-v tto generatetests/test_sample.py
Run pytest
#+BEGIN_SRC sh :results output :exports both ls tests | grep py py.test -v # -v means =verbose= #+END_SRC #+RESULTS: #+begin_example __pycache__ test_built-in_constants.py test_built-in_constants.pyc test_sample.py test_sample.py~ test_sample.pyc ============================= test session starts ============================== platform linux -- Python 3.4.3, pytest-3.0.2, py-1.4.31, pluggy-0.3.1 -- /usr/local/share/DVCS/lib/Python/venv/bin/python3 cachedir: .cache rootdir: /usr/local/share/DVCS/org-pub, inifile: collecting ... collected 4 items tests/test_built-in_constants.py::TestBuiltInConstants::test_false PASSED tests/test_built-in_constants.py::TestBuiltInConstants::test_none PASSED tests/test_built-in_constants.py::TestBuiltInConstants::test_true PASSED tests/test_sample.py::test_answer FAILED =================================== FAILURES =================================== _________________________________ test_answer __________________________________ def test_answer(): > assert func(3) == 5 E assert 4 == 5 E + where 4 = func(3) tests/test_sample.py:6: AssertionError ====================== 1 failed, 3 passed in 0.10 seconds ====================== #+end_example
pytest evaluated all tests in folder test, but what if you want
to test something particular?
#+BEGIN_SRC sh :results output :exports both py.test -v tests/test_sample.py #+END_SRC #+RESULTS: #+begin_example ============================= test session starts ============================== platform linux -- Python 3.4.3, pytest-3.0.2, py-1.4.31, pluggy-0.3.1 -- /usr/local/share/DVCS/lib/Python/venv/bin/python3 cachedir: .cache rootdir: /usr/local/share/DVCS/org-pub, inifile: collecting ... collected 1 items tests/test_sample.py::test_answer FAILED =================================== FAILURES =================================== _________________________________ test_answer __________________________________ def test_answer(): > assert func(3) == 5 E assert 4 == 5 E + where 4 = func(3) tests/test_sample.py:6: AssertionError =========================== 1 failed in 0.05 seconds =========================== #+end_example
Conclusion
Features
- Detailed info on failing assert statements (no need to remember self.assert* names);
- Auto-discovery of test modules and functions;
- Modular fixtures for managing small or parametrized long-lived test resources;
- Can run unittest (including trial) and nose test suites out of the box;
- Python2.6+, Python3.3+, PyPy-2.3, Jython-2.5 (untested);
- Rich plugin architecture, with over 150+ external plugins and thriving community;
Calling pytest from Python code
Test all detected suites
import pytest
pytest.main()
>>> ============================================================ test session starts =============================================================
platform linux -- Python 3.4.2, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: /usr/local/share/DVCS/org-pub, inifile:
collecting 0 items
collecting 3 items
collecting 3 items
collecting 6 items
collecting 17 items
collecting 22 items
collecting 25 items
collecting 29 items
collecting 33 items
collecting 38 items
collecting 42 items
collecting 47 items
collecting 49 items
collecting 51 items
collecting 54 items
collecting 55 items
collecting 56 items
collecting 58 items
collecting 60 items
collecting 62 items
collecting 64 items
collecting 67 items
collecting 71 items
collecting 73 items
collecting 75 items
collecting 78 items
collecting 79 items
collecting 81 items
collecting 84 items
collecting 86 items
collecting 88 items
collecting 90 items
collecting 91 items
collecting 95 items
collecting 97 items
collecting 99 items
collecting 102 items
collecting 104 items
collecting 108 items
collecting 111 items
collecting 112 items
collecting 117 items
collecting 118 items
collecting 121 items
collecting 123 items
collecting 124 items
collecting 127 items
collecting 128 items
collecting 130 items
collecting 133 items
collecting 134 items
collecting 138 items
collecting 140 items
collecting 142 items
collecting 145 items
collecting 147 items
collecting 150 items
collecting 153 items
collecting 157 items
collecting 158 items
collecting 159 items
collecting 163 items
collecting 164 items
collecting 168 items
collecting 169 items
collecting 171 items
collecting 172 items
collecting 172 items
collecting 172 items
collecting 172 items
collecting 173 items
collected 173 items
tests/test_built-in_constants.py ...
tests/test_built-in_functions.py ..........................................................................................................................................................F..............
tests/test_sample.py F
================================================================== FAILURES ==================================================================
___________________________________________________ TestStaticmethod.test_simple_behaviour ___________________________________________________
self = <test_built-in_functions.TestStaticmethod testMethod=test_simple_behaviour>
def test_simple_behaviour(self):
> self.assertEqual(staticmethod())
E TypeError: staticmethod expected 1 arguments, got 0
tests/test_built-in_functions.py:997: TypeError
________________________________________________________________ test_answer _________________________________________________________________
def test_answer():
> assert func(3) == 5
E assert 4 == 5
E + where 4 = func(3)
tests/test_sample.py:8: AssertionError
==================================================== 2 failed, 171 passed in 0.95 seconds ====================================================
1
Test particular case
import pytest pytest.main(['-k', 'test_answer'])
>>> ============================================================ test session starts =============================================================
platform linux -- Python 3.4.2, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: /usr/local/share/DVCS/org-pub, inifile:
collecting 0 items
collecting 3 items
collecting 3 items
collecting 6 items
collecting 17 items
collecting 22 items
collecting 25 items
collecting 29 items
collecting 33 items
collecting 38 items
collecting 42 items
collecting 47 items
collecting 49 items
collecting 51 items
collecting 54 items
collecting 55 items
collecting 56 items
collecting 58 items
collecting 60 items
collecting 62 items
collecting 64 items
collecting 67 items
collecting 71 items
collecting 73 items
collecting 75 items
collecting 78 items
collecting 79 items
collecting 81 items
collecting 84 items
collecting 86 items
collecting 88 items
collecting 90 items
collecting 91 items
collecting 95 items
collecting 97 items
collecting 99 items
collecting 102 items
collecting 104 items
collecting 108 items
collecting 111 items
collecting 112 items
collecting 117 items
collecting 118 items
collecting 121 items
collecting 123 items
collecting 124 items
collecting 127 items
collecting 128 items
collecting 130 items
collecting 133 items
collecting 134 items
collecting 138 items
collecting 140 items
collecting 142 items
collecting 145 items
collecting 147 items
collecting 150 items
collecting 153 items
collecting 157 items
collecting 158 items
collecting 159 items
collecting 163 items
collecting 164 items
collecting 168 items
collecting 169 items
collecting 171 items
collecting 172 items
collecting 172 items
collecting 172 items
collecting 172 items
collecting 173 items
collected 173 items
tests/test_sample.py F
================================================================== FAILURES ==================================================================
________________________________________________________________ test_answer _________________________________________________________________
def test_answer():
> assert func(3) == 5
E assert 4 == 5
E + where 4 = func(3)
tests/test_sample.py:8: AssertionError
============================================================ 172 tests deselected ============================================================
================================================== 1 failed, 172 deselected in 0.31 seconds ==================================================
1
Conclusion
These samples illustrate how to use pytest out from the shell.
But this approach is pertinent for testing file content and not
suitable for literate programming itself.
blog comments powered by Disqus
Published
11 September 2016