Skip to content

TST: make _skip_if into pytest decorators #18190

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

Closed
14 of 16 tasks
jreback opened this issue Nov 9, 2017 · 11 comments · Fixed by #18844
Closed
14 of 16 tasks

TST: make _skip_if into pytest decorators #18190

jreback opened this issue Nov 9, 2017 · 11 comments · Fixed by #18844
Labels
Testing pandas testing functions or related to the test suite
Milestone

Comments

@jreback
Copy link
Contributor

jreback commented Nov 9, 2017

we should move the _skip_if_* functions out of pandas.util.testing to another (private module)

then we can add skipif decorators

and use like this

@skip_if_windows_py3
def test_.......():

rather than calling tm._skip_if_windows_py390 in the body of the function (sometimes you also need to do that, so we leave the functions themselves as well).

this makes much more idiomatic and readable pytest code and removes the need to roll your own when using the decorator.

@jreback jreback added Difficulty Intermediate Testing pandas testing functions or related to the test suite labels Nov 9, 2017
@jreback jreback added this to the Next Major Release milestone Nov 9, 2017
@WillAyd
Copy link
Member

WillAyd commented Nov 11, 2017

I could take a stab at this. What private module do you want the decorators to be in? Something like _test_decorators.py to enable from pandas.util._test_decorators import *?

@jreback
Copy link
Contributor Author

jreback commented Nov 11, 2017

i wouldn’t import * them but the name is ok

@WillAyd
Copy link
Member

WillAyd commented Nov 22, 2017

For the skipif decorators, we need to provide a function that evaluates to True to get it to skip whatever is being wrapped. A lot of the pandas-internal skip functions use pytest's importorskip, which raises a Skipped when the required module cannot be imported.

I would propose here to catch a Skipped in the decorator function and return True if that occurs. Sample code is provided below to illustrate for mpl.

Before I go too far - do you agree with this direction? I'm somewhat hesitant to use it given the Skipped exception needs to be imported from _pytest. However, the alternative would be to refactor the existing functions to not use importorskip, which has its own downsides. FWIW if we wanted to go with the try...except methodology I'd create a wrapper function internal to the _test_decorators module to handle that rather than coding that block for each decorator that we want to create

import pytest
from _pytest.outcomes import Skipped

def _skip_if_no_mpl():
    try:
        mpl = pytest.importorskip("matplotlib")
        mpl.use("Agg", warn=False)
    except Skipped:
        return True

skip_if_no_mpl = pytest.mark.skipif(_skip_if_no_mpl(), reason="No matplotlib")

@jreback
Copy link
Contributor Author

jreback commented Nov 22, 2017

I would refactor like your 2nd method, IOW have a safe importer that returns a boolean, then you could actually auto-create the functions & skip decorators.

@jreback
Copy link
Contributor Author

jreback commented Nov 22, 2017

certainly if you want a PR for a simple version would be great to see before you convert everything.

key is that has to be a separate module that is only imported when tests are run and not in pandas import (so deps are not imported until then)

WillAyd added a commit to WillAyd/pandas that referenced this issue Nov 22, 2017
WillAyd added a commit to WillAyd/pandas that referenced this issue Nov 22, 2017
WillAyd added a commit to WillAyd/pandas that referenced this issue Nov 22, 2017
WillAyd added a commit to WillAyd/pandas that referenced this issue Nov 22, 2017
WillAyd added a commit to WillAyd/pandas that referenced this issue Nov 22, 2017
WillAyd added a commit to WillAyd/pandas that referenced this issue Nov 26, 2017
WillAyd added a commit to WillAyd/pandas that referenced this issue Nov 26, 2017
WillAyd added a commit to WillAyd/pandas that referenced this issue Nov 26, 2017
WillAyd added a commit to WillAyd/pandas that referenced this issue Nov 26, 2017
WillAyd added a commit to WillAyd/pandas that referenced this issue Nov 26, 2017
WillAyd added a commit to WillAyd/pandas that referenced this issue Nov 27, 2017
@WillAyd
Copy link
Member

WillAyd commented Dec 4, 2017

EDIT - moved checklist to top of thread

@WillAyd WillAyd mentioned this issue Dec 18, 2017
4 tasks
@WillAyd
Copy link
Member

WillAyd commented Dec 18, 2017

@jreback as far as the max_version argument goes in the current testing module's skip_if_no_package function I only since one usage in the following location:

tm.skip_if_no_package('scipy', max_version='0.19.0')

I believe that was introduced by 56b5a30 and I looked through the related comments in #15668 but am not clear on why that particular test needs the v <= 0.19.0 criteria. Any insight as to if that is needed or not?

FWIW that test runs on my machine with SciPy 1.0, save the dok_matrix fixture which may have an unrelated issue.

@jreback
Copy link
Contributor Author

jreback commented Dec 19, 2017

@WillAyd I updated the top list, but I think I missed a couple.

@jreback jreback modified the milestones: Next Major Release, 0.22.0 Dec 19, 2017
@WillAyd WillAyd mentioned this issue Dec 19, 2017
4 tasks
@max-sixty
Copy link
Contributor

max-sixty commented Dec 21, 2017

I'm looking at how we should skip tests in xarray.

Why did you do this rather than pytest's importorskip?

@jreback
Copy link
Contributor Author

jreback commented Dec 21, 2017

importerskip is a hammer
we need fine grain skipping

@WillAyd
Copy link
Member

WillAyd commented Dec 21, 2017

Some other things to consider:

  • The mark option is easy to apply to functions or classes interchangeably
  • If using importorskip for a class (in the setup) you lose visibility to how many tests get skipped
  • The mark option gives you metadata about your tests you miss out on with importorskip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants