Skip to content

Commit e15d464

Browse files
authored
conftest.fail_on_pvlib_version applied to functions that require args or kwargs (#973)
* add *args to fail_on_pvlib_version * working * try this * fix * documentation * review responses * changes from review * remove vestigial * remove old comment * inflate version number in test * remove *args, **kwargs from fail_on_pvlib * edits * update whatsnew comment
1 parent e04d959 commit e15d464

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

docs/sphinx/source/whatsnew/v0.8.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Bug fixes
1414

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

1820
Documentation
1921
~~~~~~~~~~~~~

pvlib/tests/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pandas as pd
77
from pkg_resources import parse_version
88
import pytest
9+
from functools import wraps
910

1011
import pvlib
1112

@@ -16,25 +17,24 @@
1617
# decorator takes one argument: the base version for which it should fail
1718
# for example @fail_on_pvlib_version('0.7') will cause a test to fail
1819
# on pvlib versions 0.7a, 0.7b, 0.7rc1, etc.
19-
# test function may not take args, kwargs, or fixtures.
2020
def fail_on_pvlib_version(version):
2121
# second level of decorator takes the function under consideration
2222
def wrapper(func):
2323
# third level defers computation until the test is called
2424
# this allows the specific test to fail at test runtime,
2525
# rather than at decoration time (when the module is imported)
26-
def inner():
26+
@wraps(func)
27+
def inner(*args, **kwargs):
2728
# fail if the version is too high
2829
if pvlib_base_version >= parse_version(version):
2930
pytest.fail('the tested function is scheduled to be '
3031
'removed in %s' % version)
3132
# otherwise return the function to be executed
3233
else:
33-
return func()
34+
return func(*args, **kwargs)
3435
return inner
3536
return wrapper
3637

37-
3838
# commonly used directories in the tests
3939
TEST_DIR = Path(__file__).parent
4040
DATA_DIR = TEST_DIR.parent / 'data'

pvlib/tests/test_conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from conftest import fail_on_pvlib_version
44

5+
from pvlib._deprecation import pvlibDeprecationWarning, deprecated
56

67
@pytest.mark.xfail(strict=True,
78
reason='fail_on_pvlib_version should cause test to fail')
@@ -19,3 +20,26 @@ def test_fail_on_pvlib_version_pass():
1920
@fail_on_pvlib_version('100000.0')
2021
def test_fail_on_pvlib_version_fail_in_test():
2122
raise Exception
23+
24+
25+
# set up to test using fixtures with function decorated with
26+
# conftest.fail_on_pvlib_version
27+
@pytest.fixture()
28+
def some_data():
29+
return "some data"
30+
31+
32+
def alt_func(*args):
33+
return args
34+
35+
36+
deprec_func = deprecated('350.8', alternative='alt_func',
37+
name='deprec_func', removal='350.9')(alt_func)
38+
39+
40+
@fail_on_pvlib_version('350.9')
41+
def test_use_fixture_with_decorator(some_data):
42+
# test that the correct data is returned by the some_data fixture
43+
assert some_data == "some data"
44+
with pytest.warns(pvlibDeprecationWarning): # test for deprecation warning
45+
deprec_func(some_data)

0 commit comments

Comments
 (0)