Testing Class Material
This commit is contained in:
commit
05b1f6cdd5
85 changed files with 102796 additions and 0 deletions
28
testing_project/demos/conftest_example.py
Normal file
28
testing_project/demos/conftest_example.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
import numpy as np
|
||||
import pytest
|
||||
|
||||
|
||||
# add a commandline option to pytest
|
||||
def pytest_addoption(parser):
|
||||
"""Add random seed option to py.test.
|
||||
"""
|
||||
parser.addoption('--seed', dest='seed', type=int, action='store',
|
||||
help='set random seed')
|
||||
|
||||
|
||||
# configure pytest to automatically set the rnd seed if not passed on CLI
|
||||
def pytest_configure(config):
|
||||
seed = config.getvalue("seed")
|
||||
# if seed was not set by the user, we set one now
|
||||
if seed is None or seed == ('NO', 'DEFAULT'):
|
||||
config.option.seed = int(np.random.randint(2 ** 31 - 1))
|
||||
|
||||
|
||||
def pytest_report_header(config):
|
||||
return f'Using random seed: {config.option.seed}'
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def random_state(request):
|
||||
random_state = np.random.RandomState(request.config.option.seed)
|
||||
return random_state
|
28
testing_project/demos/test_parametrize.py
Normal file
28
testing_project/demos/test_parametrize.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
import pytest
|
||||
|
||||
|
||||
def test_for_loop_simple():
|
||||
cases = [1, 2, 3]
|
||||
for a in cases:
|
||||
assert a > 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize('a', [1, 2, 3])
|
||||
def test_parametrize_simple(a):
|
||||
# This test will be run 3 times, with a=1, a=2, and a=3
|
||||
assert a > 0
|
||||
|
||||
|
||||
def test_for_loop_multiple():
|
||||
cases = [(1, 'hi', 'hi'), (2, 'no', 'nono')]
|
||||
for a, b, expected in cases:
|
||||
result = b * a
|
||||
assert result == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('a, b, expected', [(1, 'hi', 'hi'), (2, 'no', 'nono')])
|
||||
def test_parametrize_multiple(a, b, expected):
|
||||
# This test will be run 2 times, with a=1, b='hi', expected='hi'
|
||||
# and a=2, b='no', expected='nono'
|
||||
result = b * a
|
||||
assert result == expected
|
Loading…
Add table
Add a link
Reference in a new issue