Speaker: Zong-han, Xie
https://hackmd.io/PdkOGp6NQDWJVLCxz06vWg
Spaker only has written almost no test in his projects.
conda install pytest
or
pip install pytest
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 4
pytest
or
pytest -q test_simple.py
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 5 # Error
import pytest
def add(x, y):
return x + y
@pytest.mark.parametrize("x, y, z", [
(1, 2, 3),
(4, 5, 9),
(3, 8, 12) # pop error
])
def test_answer(x, y, z):
print(x, y, z)
assert add(x, y) == z
import pytest
def add(x, y):
return x + y
def test_func():
assert 4 == 5
@pytest.mark.skip
@pytest.mark.parametrize("x, y, z", [
(3, 8, 12) # pop error
])
def test_add(x, y, z):
assert add(x, y) == z
import pytest
import sys
def add(x, y):
return x + y
@pytest.mark.skipif(sys.platform != 'win32', reason='Only run on win32')
def test_func():
assert 4 == 5
@pytest.mark.parametrize("x, y, z", [
(3, 8, 12) # pop error
])
def test_add(x, y, z):
assert add(x, y) == z
import pytest
def add(x, y):
return x + y
@pytest.mark.xfail
def test_func():
assert 4 == 5
import pytest
class DBConnection(object):
def query(self, stmt):
return [('a', 'b', 1), ('d', 'ff', 9)]
@pytest.fixture
def db():
return DBConnection()
def test_query(db):
data = db.query('some stmt')
# use here to check data property
assert len(data[0]) == 4
# conftest.py
import pytest
class DBConnection(object):
def query(self, stmt):
return [('a', 'b', 1), ('d', 'ff', 9)]
@pytest.fixture
def db():
return DBConnection()
# test_answer.py
def test_query(db):
data = db.query('some stmt')
# use here to check data property
assert len(data[0]) == 4
# __init__.py
# conftest.py
import pytest
class DBConnection(object):
def query(self, stmt):
return [('a', 'b', 1), ('d', 'ff', 9)]
@pytest.fixture(autouse=True)
def db():
return DBConnection()
# test_answer.py
def test_query(db):
data = db.query('some stmt')
assert len(data[0]) == 4 # Pop error
# subdir/__init__.py
# subdir/conftest.py
import pytest
class DBConnection(object):
def query(self, stmt):
return [('a', 'b', 1, 9), ('d', 'ff', 9, 11)]
@pytest.fixture(autouse=True)
def db():
return DBConnection()
# subdir/test_answer.py
def test_query(db):
data = db.query('some stmt')
assert len(data[0]) == 3 # Pop error
Scope | Desc |
---|---|
function | Run once per test |
class | Run once per class of tests |
module | Run once per module |
session | Run once per session |
import pytest
@pytest.fixture(scope="session")
def resource_session(request):
print('resource_session INIT')
request.addfinalizer(
lambda: print('\nIn resource_session\'s end'))
@pytest.fixture(scope="module")
def resource_module(request):
print('resource_module INIT')
request.addfinalizer(
lambda: print('\nIn resource_module\'s end'))
@pytest.fixture(scope="function")
def resource_func(request):
print('resource_func INIT')
request.addfinalizer(
lambda: print('\nIn resource_func\'s end'))
# test_1_2.py
def test_one(resource_func):
print('In test_one()')
def test_two(resource_module):
print('\nIn test_two()')
# test_3_4.py
def test_three(
resource_func,
resource_session,
resource_module):
print('\nIn test_three()')
def test_four(resource_func, resource_module):
print('\nIn test_four()')
# conftest.py
import pytest
@pytest.fixture(scope='module', params=['a', 'b'])
def resource_1(request):
param = request.param
return 'resource_1_' + param
@pytest.fixture(scope='module', params=['a', 'b'])
def resource_2(request):
param = request.param
return 'resource_2_' + param
# test_answer.py
def test_1(resource_1):
print('In test_1: ' + resource_1)
def test_2(resource_2):
print('In test_2: ' + resource_2)
def test_3(resource_1, resource_2):
print('In test_3: ' + resource_1 + ', ' + resource_2)
# conftest.py
import pytest
import sys
def pytest_addoption(parser):
parser.addoption(
"--cmdopt", action="store",
default="type1",
help="my option: type1 or type2")
def pytest_configure(config):
sys._called_from_test = 'True'
def pytest_unconfigure(config):
del sys._called_from_test
def pytest_report_header(config):
return "This is header"
@pytest.fixture
def cmdopt(request):
return request.config.getoption("--cmdopt")
# test_answer.py
import argparse
import sys
def test_answer(cmdopt):
print("sys._called_from_test: ", sys._called_from_test)
if cmdopt == 'type1':
print('first')
elif cmdopt == 'type2':
print('second')
assert True
More detail please go to