-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
OK so that didn't work, I'll fool with it some more. |
I can't say whether passing values through the decorator is the best approach -- maybe @wholmgren's suggestion about mocking is better. I am much more familiar with decorators than mocking though, so this is how to get your test to pass: $ git diff
diff --git a/pvlib/tests/conftest.py b/pvlib/tests/conftest.py
index f347f86..115ba1c 100644
--- a/pvlib/tests/conftest.py
+++ b/pvlib/tests/conftest.py
@@ -19,11 +19,11 @@ pvlib_base_version = \
# test function may not take args, kwargs, or fixtures.
def fail_on_pvlib_version(version, *args):
# second level of decorator takes the function under consideration
- def wrapper(func, *args):
+ 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(*args):
+ def inner():
# fail if the version is too high
if pvlib_base_version >= parse_version(version):
pytest.fail('the tested function is scheduled to be '
diff --git a/pvlib/tests/test_conftest.py b/pvlib/tests/test_conftest.py
index eb186f5..5ec634f 100644
--- a/pvlib/tests/test_conftest.py
+++ b/pvlib/tests/test_conftest.py
@@ -37,6 +37,6 @@ deprec_func = deprecated('0.8', alternative='alt_func',
@fail_on_pvlib_version('0.9', some_data)
-def test_deprecated_09():
+def test_deprecated_09(some_data):
with pytest.warns(pvlibDeprecationWarning):
- deprec_func(some_data())
+ deprec_func(some_data) |
Thanks @kanderso-nrel but that doesn't run locally when I test it. I don't have an opinion whether modifying
|
Hmm, strange. I just installed it into a fresh environment and it seems to work for me. Here's the branch I was using: https://github.com/kanderso-nrel/pvlib-python/tree/failtest The logic being that you want to give |
@kanderso-nrel @wholmgren I think this works to pass arguments from the decorator to the decorated function. I checked that the value passed through the decorator is the same as the value inside the decorated function. I'm not sure how this all works so your review is needed. The advantage here (from my perspective) is to test a deprecated function using the same pattern of fixtures as is used before deprecation. |
@cwhanse I'm sorry, I think I had partially misunderstood the problem this PR is trying to solve. With the way this PR currently works, I don't think pytest is automatically replacing the Is the goal to be able to just tack Click to expand(base) kevin@carrots-deluxe:~/projects/pvlib-python[conftest|!?P]$ git diff
diff --git a/pvlib/tests/conftest.py b/pvlib/tests/conftest.py
index 981d115..6e2d3fd 100644
--- a/pvlib/tests/conftest.py
+++ b/pvlib/tests/conftest.py
@@ -6,6 +6,7 @@ import numpy as np
import pandas as pd
from pkg_resources import parse_version
import pytest
+import functools
import pvlib
@@ -17,13 +18,14 @@ pvlib_base_version = \
# for example @fail_on_pvlib_version('0.7') will cause a test to fail
# on pvlib versions 0.7a, 0.7b, 0.7rc1, etc.
# args and kwargs will be passed to the function being decorated.
-def fail_on_pvlib_version(version, *args, **kwargs):
+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():
+ @functools.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 '
diff --git a/pvlib/tests/test_conftest.py b/pvlib/tests/test_conftest.py
index 6ed5974..ec22477 100644
--- a/pvlib/tests/test_conftest.py
+++ b/pvlib/tests/test_conftest.py
@@ -36,9 +36,11 @@ deprec_func = deprecated('0.8', alternative='alt_func',
name='deprec_func', removal='0.9')(alt_func)
-@fail_on_pvlib_version('0.9', some_data, test_string="test")
-def test_deprecated_09(some_data, test_string=None):
+@fail_on_pvlib_version('0.9')
+def test_deprecated_09(some_data, test_string="test"):
with pytest.warns(pvlibDeprecationWarning): # test for deprecation warning
+ # check that pytest is passing in the fixture value, not the function
+ assert some_data == "some data"
# use assert to test that alternate function was called
assert some_data == deprec_func(some_data)[0]
# check that the kwarg was accepted |
Yes exactly |
thanks @kanderso-nrel @wholmgren please take another look. |
@wholmgren @kanderso-nrel this one good to merge? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit below, but ok to merge without further review.
pvlib/tests/test_conftest.py
Outdated
|
||
|
||
@fail_on_pvlib_version('350.9') | ||
def test_deprecated_09(some_data): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: shouldn't the function name be something like test_fail_on_pvlib_version_args_kwargs
?
pvlib/tests/test_conftest.py
Outdated
@@ -19,3 +20,25 @@ 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 passing arguments to conftest.fail_on_pvlib_version |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another minor nit: this comment could be improved, arguments are passed to the test, not the decorator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the whatsnew entry could still use an update. Other than that LGTM
Thanks @cwhanse and @kanderso-nrel ! |
docs/sphinx/source/whatsnew
for all changes. Includes link to the GitHub Issue with:issue:`num`
or this Pull Request with:pull:`num`
. Includes contributor name and/or GitHub username (link with:ghuser:`user`
).Adding capability to pass arguments to a function decorated with
fail_on_pvlib_version
. Allows use of fixtures when testing for deprecation message.