diff --git a/docs/sphinx/source/whatsnew/v0.8.0.rst b/docs/sphinx/source/whatsnew/v0.8.0.rst index dc6bf5015c..2e9f12c484 100644 --- a/docs/sphinx/source/whatsnew/v0.8.0.rst +++ b/docs/sphinx/source/whatsnew/v0.8.0.rst @@ -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 ~~~~~~~~~~~~~ diff --git a/pvlib/tests/conftest.py b/pvlib/tests/conftest.py index dedb0ce836..29b4549f9a 100644 --- a/pvlib/tests/conftest.py +++ b/pvlib/tests/conftest.py @@ -6,6 +6,7 @@ import pandas as pd from pkg_resources import parse_version import pytest +from functools import wraps import pvlib @@ -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' diff --git a/pvlib/tests/test_conftest.py b/pvlib/tests/test_conftest.py index 14cc7e5d79..300f58d325 100644 --- a/pvlib/tests/test_conftest.py +++ b/pvlib/tests/test_conftest.py @@ -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') @@ -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)