Presentation by Jean Cruypenynck/@nonatomiclabs
Coding is good…
… with testing, it is better…
… with automated testing, it is the best
Unit &
Component
Integration
System
3 main testing layers
pip install pytest
easy_install pytest
2. run tests
def test_division():
assert division(1, 1) == 1
1. create a function with the test_ prefix
py.test division.py
# Function to test
def division(a, b):
return a / float(b)
Passing test
Failing test
Any way you can imagine!!!
py.test
py.test test_module.py
py.test test_module.py::test_function
py.test test_module.py -k my_keyword
import pytest
@pytest.mark.skipif()
def test_skipped():
assert 1
import pytest
import sys
@pytest.mark.skipif(sys.version_info[0] == 3,
reason="Only Python 2")
def test_skipped():
assert 1
Simple
Conditional
@pytest.mark.xfail
def test_division_fail():
assert division(1, 0) == 1
We add exception handling to our function
def division(a, b):
try:
return a / float(b)
except ZeroDivisionError:
return 1
Output:
Output:
@pytest.mark.parametrize("a", [1, 2, 3, 4])
def test_division_param(a):
assert division(a, 1) == a
Example: tmpdir
def test_tmpdir(tmpdir):
assert 0
Output:
import pytest
@pytest.fixture
def structure():
class MyStruct(object):
text = ""
num = 10
return MyStruct()
def test_fix(structure):
assert isinstance(structure.text, str)
assert isinstance(structure.num, int)
unittest
nose
nonatomiclabs.com
git clone https://github.com/filaton/pytest-intro.git
http://nonatomiclabs.com/blog/?p=89