Skip to content

Commit

Permalink
tests reorganized
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-mixas committed Feb 6, 2020
1 parent e0fefe0 commit 648cc57
Show file tree
Hide file tree
Showing 118 changed files with 1,897 additions and 2,595 deletions.
30 changes: 23 additions & 7 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ def encode(self, data):
return data


def test_abstract_dumper_encode():
with pytest.raises(NotImplementedError):
cli.Dumper().encode('data')


def test_abstract_loader_decode():
with pytest.raises(NotImplementedError):
cli.Loader().decode('data')


def test_dumper_dump_default_with_tty(stringio_tty):
dumper = Dumper()
dumper.dump(stringio_tty, 'text')
Expand Down Expand Up @@ -71,21 +81,27 @@ class FakeFP(object):
'yml': 'yaml',
}
fake_fp = FakeFP()
app = cli.App(args=())

for ext in sorted(aliases):
fake_fp.name = 'filename.' + ext
assert aliases[ext] == app.guess_fmt(fake_fp, 'default')
assert aliases[ext] == cli.App(args=()).guess_fmt(fake_fp, 'default')


def test_guess_fmt_ignore_fp_defaults():
app = cli.App(args=())
for fp in sys.stdin, sys.stdout, sys.stderr:
assert 'default' == app.guess_fmt(fp, 'default')
assert 'default' == cli.App(args=()).guess_fmt(fp, 'default')


def test_get_loader_unsupported_fmt():
app = cli.App(args=())
def test_get_dumper_unsupported_fmt():
with pytest.raises(RuntimeError, match='Unsupported output format: garbage'):
cli.App(args=()).get_dumper('garbage')


def test_get_loader_unsupported_fmt():
with pytest.raises(RuntimeError, match='Unsupported input format: garbage'):
app.get_loader('garbage')
cli.App(args=()).get_loader('garbage')


def test_run():
with pytest.raises(NotImplementedError):
cli.App(args=()).run()
85 changes: 85 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import contextlib
import os
import pytest


def do_test_function(test, func):
with test['raises']:
assert test.get('result', None) == func(test)


def iterate_test_suite(tests_map, results_mod, func):
results_map = {} if 'CANONIZE' in os.environ else results_mod.RESULTS

for name, test in sorted(tests_map.items()):
if 'CANONIZE' in os.environ:
try:
test['result'] = func(test)
results_map[name] = {'result': test['result']}
except Exception as e:
results_map[name] = {
'raises': e if type(e) is type else type(e),
}
else:
try:
test['result'] = results_map[name]['result']
except KeyError:
test['raises'] = pytest.raises(results_map[name]['raises'])

marks = []
if 'raises' not in test:
test['raises'] = pytest_param_does_not_raise()
if 'result' not in test:
test['result'] = None
marks.append(pytest.mark.xfail)

yield pytest.param(test, func, id=name, marks=marks)

if 'CANONIZE' in os.environ:
save_test_suite(results_mod.__file__, results_map)


def save_test_suite(filename, results):
with open(filename, 'w') as f:
f.write('''"""
Autogenerated, do not edit manually!
"""
import sys
RESULTS = {
''')

for k, v in sorted(results.items()):
t = 'result' if 'result' in v else 'raises'
if 'raises' in v:
t = 'raises'
r = v['raises'].__name__
else:
t = 'result'
r = repr(v['result'])

f.write("""\
'{name}': {{
'{type}': {result},
}},
""".format(name=k, type=t, result=r))

f.write("""}
if __name__ == '__main__':
names = sys.argv[1:] if len(sys.argv) > 1 else sorted(RESULTS.keys())
headers = len(names) > 1
for name in names:
if headers:
print('========== ' + name + ' ==========')
print(RESULTS[name].get('result', None), end='')
""")


@contextlib.contextmanager
def pytest_param_does_not_raise():
yield

0 comments on commit 648cc57

Please sign in to comment.