Skip to content

conftest.fail_on_pvlib_version applied to functions that require args or kwargs #973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 10, 2020
2 changes: 2 additions & 0 deletions docs/sphinx/source/whatsnew/v0.8.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Bug fixes

Testing
~~~~~~~
* Decorator :py:func:`pvlib.conftest.fail_on_pvlib_version` can now be
applied to functions that require args or kwargs. (:pull:`973`)

Documentation
~~~~~~~~~~~~~
Expand Down
8 changes: 4 additions & 4 deletions pvlib/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pandas as pd
from pkg_resources import parse_version
import pytest
from functools import wraps

import pvlib

Expand All @@ -16,25 +17,24 @@
# decorator takes one argument: the base version for which it should fail
# for example @fail_on_pvlib_version('0.7') will cause a test to fail
# on pvlib versions 0.7a, 0.7b, 0.7rc1, etc.
# test function may not take args, kwargs, or fixtures.
def fail_on_pvlib_version(version):
# second level of decorator takes the function under consideration
def wrapper(func):
# third level defers computation until the test is called
# this allows the specific test to fail at test runtime,
# rather than at decoration time (when the module is imported)
def inner():
@wraps(func)
def inner(*args, **kwargs):
# fail if the version is too high
if pvlib_base_version >= parse_version(version):
pytest.fail('the tested function is scheduled to be '
'removed in %s' % version)
# otherwise return the function to be executed
else:
return func()
return func(*args, **kwargs)
return inner
return wrapper


# commonly used directories in the tests
TEST_DIR = Path(__file__).parent
DATA_DIR = TEST_DIR.parent / 'data'
Expand Down
24 changes: 24 additions & 0 deletions pvlib/tests/test_conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from conftest import fail_on_pvlib_version

from pvlib._deprecation import pvlibDeprecationWarning, deprecated

@pytest.mark.xfail(strict=True,
reason='fail_on_pvlib_version should cause test to fail')
Expand All @@ -19,3 +20,26 @@ def test_fail_on_pvlib_version_pass():
@fail_on_pvlib_version('100000.0')
def test_fail_on_pvlib_version_fail_in_test():
raise Exception


# set up to test using fixtures with function decorated with
# conftest.fail_on_pvlib_version
@pytest.fixture()
def some_data():
return "some data"


def alt_func(*args):
return args


deprec_func = deprecated('350.8', alternative='alt_func',
name='deprec_func', removal='350.9')(alt_func)


@fail_on_pvlib_version('350.9')
def test_use_fixture_with_decorator(some_data):
# test that the correct data is returned by the some_data fixture
assert some_data == "some data"
with pytest.warns(pvlibDeprecationWarning): # test for deprecation warning
deprec_func(some_data)