diff --git a/pytest_variables/__init__.py b/pytest_variables/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pytest_variables/errors.py b/pytest_variables/errors.py new file mode 100644 index 0000000..4b8e0d9 --- /dev/null +++ b/pytest_variables/errors.py @@ -0,0 +1,8 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + + +class ValueError(Exception): + """pytest-variables usage error.""" + pass diff --git a/pytest_variables.py b/pytest_variables/plugin.py similarity index 68% rename from pytest_variables.py rename to pytest_variables/plugin.py index 9ebf9f5..6f07335 100644 --- a/pytest_variables.py +++ b/pytest_variables/plugin.py @@ -4,8 +4,11 @@ import os.path import sys + import pytest +from pytest_variables import errors + def default(module, path): return module.load(path) @@ -38,18 +41,27 @@ def pytest_addoption(parser): help='path to variables file.') -@pytest.fixture(scope='session') -def variables(request): - """Provide test variables from a specified file""" - data = {} - for path in request.config.getoption('variables'): +def pytest_configure(config): + config._variables = {} + for path in config.getoption('variables'): ext = os.path.splitext(path)[1][1:].lower() or 'json' with open(path) as f: try: - data.update(import_parser(f, *parser_table[ext])) - except (TypeError, KeyError, ValueError): + variables = import_parser(f, *parser_table[ext]) + print(variables) + config._variables.update(variables) + except KeyError: print("Could not find a parser for the file extension '{0}'. " 'Supported extensions are: {1}'.format( ext, ', '.join(sorted(parser_table.keys())))) - data.update(import_parser(f, *parser_table['json'])) - return data + config._variables.update( + import_parser(f, *parser_table['json'])) + except ValueError as e: + raise errors.ValueError('Unable to parse {0}: {1}'.format( + path, e)) + + +@pytest.fixture(scope='session') +def variables(pytestconfig): + """Provide test variables from a specified file""" + return pytestconfig._variables diff --git a/setup.py b/setup.py index 441973f..4a7795c 100644 --- a/setup.py +++ b/setup.py @@ -7,8 +7,8 @@ author='Dave Hunt', author_email='dhunt@mozilla.com', url='https://github.com/pytest-dev/pytest-variables', - py_modules=['pytest_variables'], - entry_points={'pytest11': ['variables = pytest_variables']}, + packages=['pytest_variables'], + entry_points={'pytest11': ['variables = pytest_variables.plugin']}, install_requires=['pytest>=2.4.2'], extras_require={ 'hjson': ['hjson'], diff --git a/test_variables.py b/test_variables.py index 3006479..3f74513 100644 --- a/test_variables.py +++ b/test_variables.py @@ -14,17 +14,17 @@ def pytest_generate_tests(metafunc): metafunc.parametrize('file_format', ['json', 'hjson', 'yaml']) -def run(testdir, file_format='json', variables=None): +def run(testdir, file_format='json', variables=None, raw=False): variables = variables or [{"foo": "bar"}] args = [] for i, v in enumerate(variables): - if file_format == 'hjson': + if file_format == 'hjson' and not raw: hjson = pytest.importorskip('hjson') v = hjson.dumps(v) - elif file_format == 'yaml': + elif file_format == 'yaml' and not raw: yaml = pytest.importorskip('yaml') v = yaml.dump(v) - else: + elif not raw: import json v = json.dumps(v) args.append('--variables') @@ -75,12 +75,9 @@ def test(variables): def test_invalid_format(testdir, file_format): testdir.makepyfile('def test(variables): pass') - result = run(testdir, file_format, ['invalid']) - assert result.ret == 1 - if sys.version_info < (3, 5, 0): - result.stdout.fnmatch_lines(['*ValueError: *']) - else: - result.stdout.fnmatch_lines(['*JSONDecodeError: *']) + result = run(testdir, file_format, ['invalid'], raw=True) + assert result.ret == 3 + result.stderr.fnmatch_lines(['*ValueError: Unable to parse*']) def test_key_error(testdir, file_format):