Last active
January 5, 2020 15:07
-
-
Save mrzechonek/41e52f81e24fadc1cbb809238d880bcb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
from typing import NamedTuple | |
class FixtureConfig: | |
FIXTURE = None | |
@classmethod | |
def from_request(cls, request): | |
kwargs = {} | |
for marker in request.node.iter_markers(cls.FIXTURE): | |
kwargs.update(marker.kwargs) | |
return cls(**kwargs) | |
class AppConfig(FixtureConfig): | |
FIXTURE = 'app' | |
addr = 0x5f2 | |
iv_index = 0 | |
def __init__(self, addr=0x5f2, iv_index=0): | |
self.addr = addr | |
self.iv_index = iv_index | |
@pytest.fixture() | |
def app(request): | |
config = AppConfig.from_request(request) | |
class App: | |
addr = config.addr | |
iv_index = config.iv_index | |
return App() | |
def test_default(app): | |
assert app.addr == AppConfig().addr | |
@pytest.mark.app(addr=0x420, iv_index=5) | |
def test_custom(app): | |
assert app.addr == 0x420 | |
assert app.iv_index == 5 | |
@pytest.mark.parametrize('app.addr, app.iv_index', [ | |
pytest.param(0x420, 5), | |
pytest.param(0x420, 6), | |
]) | |
def test_parametrized(request, app): | |
config = AppConfig.from_request(request) | |
assert config.addr == 0x420 | |
assert config.iv_index in (5, 6) | |
assert app.addr == config.addr | |
assert app.iv_index == config.iv_index | |
@pytest.mark.parametrize('app.addr', [ | |
pytest.param(0x420), | |
]) | |
@pytest.mark.parametrize('app.iv_index', [ | |
pytest.param(5), | |
pytest.param(6), | |
]) | |
def test_parametrized_nesting(request, app): | |
config = AppConfig.from_request(request) | |
assert config.addr == 0x420 | |
assert config.iv_index in (5, 6) | |
assert app.addr == config.addr | |
assert app.iv_index == config.iv_index | |
@pytest.mark.parametrize('app.*', [ | |
pytest.param(dict(addr=0x420)), | |
]) | |
def test_parametrized_kwargs(request, app): | |
config = AppConfig.from_request(request) | |
assert config.addr == 0x420 | |
assert app.addr == config.addr | |
@pytest.mark.parametrize('app.addr, foo, app.iv_index', [ | |
pytest.param(0x420, 'foo', 5), | |
]) | |
def test_parametrized_mixed(app, foo): | |
assert app.addr == 0x420 | |
assert app.iv_index == 5 | |
assert foo == 'foo' | |
from collections import defaultdict | |
from _pytest import mark | |
class ParameterSet(mark.ParameterSet): | |
@classmethod | |
def _for_parametrize(cls, argnames, argvalues, func, config, function_definition): | |
args, params = super()._for_parametrize(argnames, argvalues, func, config, | |
function_definition) | |
argnames = {} | |
parameters = [] | |
for param in params: | |
markargs = defaultdict(dict) | |
values = [] | |
for name, value in zip(args, param.values): | |
try: | |
prefix, name = name.split('.', 1) | |
if name == '*': | |
if not isinstance(value, dict): | |
raise TypeError( | |
'{nodeid}: in "parametrize", when using "{prefix}.*" as the name, ' | |
'corresponding value must be a dictionary'.format( | |
nodeid=function_definition.nodeid, | |
prefix=prefix, | |
) | |
) | |
markargs[prefix].update(**value) | |
else: | |
markargs[prefix][name] = value | |
except ValueError: | |
argnames[name] = None | |
values.append(value) | |
marks = tuple(mark.Mark(name=k, args=(), kwargs=v) for k, v in markargs.items()) | |
parameters.append(cls(values=values, marks=param.marks + marks, id=param.id)) | |
return argnames.keys(), parameters | |
mark.ParameterSet = ParameterSet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
from uuid import uuid4 | |
@pytest.fixture() | |
def elements(): | |
None | |
@pytest.fixture() | |
def crpl(): | |
return None | |
@pytest.fixture() | |
def random_uuid(uuid=None): | |
return uuid or uuid4() | |
@pytest.fixture | |
def app_key(): | |
return bytes.fromhex('63964771734fbd76e3b40519d1d94a48') | |
@pytest.fixture | |
def net_key(): | |
return bytes.fromhex('7dd7364cd842ad18c17c2b820c84c3d6') | |
@pytest.fixture | |
def dev_key(): | |
return bytes.fromhex('9d6dd0e96eb25dc19a40ed9914f8f03f') | |
@pytest.fixture | |
def node_addr(): | |
return 0x5f2 | |
@pytest.fixture | |
def iv_index(): | |
return 0 | |
@pytest.fixture | |
def import_flags(): | |
return {} | |
@pytest.fixture() | |
def application(node_addr, elements, random_uuid, dev_key, net_key, | |
app_key, crpl, iv_index, import_flags): | |
pass | |
@pytest.mark.parametrize('node_addr,iv_index', [ | |
pytest.param(0x420, 5), | |
]) | |
def test_application(application): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment