Skip to content

Enable Easy TestRunner Subclassing - #5505

Merged
eteq merged 14 commits into
astropy:masterfrom
Cadair:runner_subclassing
Dec 6, 2016
Merged

eteq merged 14 commits into
astropy:masterfrom
Cadair:runner_subclassing

Conversation

@Cadair

@Cadair Cadair commented Nov 25, 2016

Copy link
Copy Markdown
Member

This PR is designed to make it simple for affiliated packages to add or remove arguments from the astropy test runner based on custom needs not shared by astropy core.

The way this works is by separating each kwarg out into a separate method which can be overloaded by a subclass or new methods added. The __new__ constructor then looks for all the methods defined on the class which have been decorated with the keyword decorator and adds them to a dictionary which forms the allowed keyword arguments and their default values for the run_tests method. Documentation is also delegated to the keyword methods.

Removal of keyword arguments is supported by overloading the method and returning NotImplemented.

One (unintended) side effect of this is that the run_tests method is now keyword argument only.

Finally, I would be interested in extending this to automatically generate the setup.py command, that requires that command importing the TestRunner class, I don't know if that is going to break the setup.py machinery in any way?

ping @wkerzendorf @astrofrog

@Cadair Cadair changed the title [WIP] Enable TestRunner subclassing [WIP] Enable Easy TestRunner Subclassing Nov 25, 2016
@Cadair
Cadair force-pushed the runner_subclassing branch from 479f3eb to c6cc5f8 Compare November 25, 2016 14:48
@Cadair
Cadair force-pushed the runner_subclassing branch 2 times, most recently from e450f2f to a2c4368 Compare November 25, 2016 21:11
@Cadair Cadair changed the title [WIP] Enable Easy TestRunner Subclassing Enable Easy TestRunner Subclassing Nov 25, 2016
@Cadair

Cadair commented Nov 25, 2016

Copy link
Copy Markdown
Member Author

This still needs tests and some better developer documentation, but before I put that work in I would like some buy-in on the idea. Are people generally supportive of this somewhat crazy suggestion?!

@pllim

pllim commented Nov 27, 2016

Copy link
Copy Markdown
Member

Is this backward compatible? How would this affect external customization like astropy/package-template#176 ?

@Cadair

Cadair commented Nov 27, 2016

Copy link
Copy Markdown
Member Author

The tests pass, so I am hoping that means it's backwards compatible.

What's in astropy/package-template#176 is a pytest hook right, so it would not affect the run_tests() method, right? I also don't think that it should affect that kind of hooks in anyway, as this layer calls out to pytest.

@astrofrog astrofrog left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great idea, and it would be nice to get this into 1.3. I left a few small comments - my only other comment is that if you would make use of this in SunPy or in another package, I would recommend trying to do a PR to that package now just to make sure that it will suit your needs (in case you find any fundamental design flaws with this).

Comment thread astropy/tests/runner.py Outdated
from ..utils.exceptions import AstropyWarning

class keyword(object):
def __init__(self, default_value, priority=0):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make the spacing here PEP8-compliant, i.e. one more space above and below class keyword?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I think it would be good to maybe add a comment or a docstring somewhere here explaining what this is for and what the priority does.

Comment thread astropy/tests/runner.py
keyword._default_value = self.default_value
keyword._priority = self.priority
keyword.__doc__ = f.__doc__
return keyword

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make a decorator that preserves call signature and docstring, try instead using the wraps function from astropy.utils (rather than explicitly copying __doc__).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wraps will not work because I explicitly use the function name below.

Comment thread astropy/tests/runner.py Outdated
package : str, optional
The name of a specific package to test, e.g. 'io.fits' or 'utils'.
If nothing is specified all default Astropy tests are run.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about not putting extraneous spaces here and then in __new__ adding spaces if needed between parameters?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't it good form to end a docstring with a blank line anyway?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never do... It looks weird to me. What is it with style guides and docstrings? Why not just:

"""
This is a docstring
"""

How does:

""" This is a docstring

"""

make any sense? 😛

Anyway, up to you - what I mean is more that if you don't assume that sub-classes include the whitespace, you can strip whitespace and add it back so that the output is always predictable in the generated docstring?

Comment thread astropy/tests/runner.py Outdated
all_args = []
return []

@keyword(None, priority=1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the priority is >0, it would be good to add a comment above as to why that is, and what it depends on.

Comment thread astropy/tests/runner.py Outdated

return []

@keyword(None)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you make the default be None and just do @keyword in those cases?

Comment thread astropy/tests/runner.py Outdated
"""
if args:
return shlex.split(args,
posix=not sys.platform.startswith('win'))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8 gone mad - just put it one one line? :)

@Cadair
Cadair force-pushed the runner_subclassing branch 2 times, most recently from 2c57c44 to c9e6c2b Compare November 29, 2016 09:55
Comment thread astropy/tests/runner.py Outdated
"""

# Don't import pytest until it's actually needed to run the tests
from .helper import pytest

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put this somewhere else in this file, so we don't enforce the use of vendored pytest? (SunPy dosen't use it.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question - I'm not sure why it had to be this way. Maybe just try and put it top-level?

Cadair referenced this pull request in chianti-atomic/ChiantiPy Dec 1, 2016
@astrofrog astrofrog added this to the v1.3.0 milestone Dec 2, 2016
@astrofrog

Copy link
Copy Markdown
Member

Hey @Cadair I rebased your PR. One interesting side-effect is that your commits are no longer verified. Oopsie.

@Cadair
Cadair force-pushed the runner_subclassing branch from 9f392c7 to a482523 Compare December 2, 2016 23:39
@eteq

eteq commented Dec 3, 2016

Copy link
Copy Markdown
Member

@Cadair - this is looking great to me - If you think its done I think its basically there.

One thing that's missing , though: documentation. Can you add something to the testing part of the docs that shows how someone might add this extra one?

@Cadair
Cadair force-pushed the runner_subclassing branch from a482523 to 7bc7540 Compare December 5, 2016 18:40
@Cadair
Cadair force-pushed the runner_subclassing branch from 7bc7540 to fb6e109 Compare December 5, 2016 18:45
Comment thread astropy/tests/runner.py Outdated
"""
pep8 : bool, optional
Turn on PEP8 checking via the pytest-pep8 plugin and disable normal
tests. Same as specifying `--pep8 -k pep8` in `args`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and elsewhere, you need double backticks, otherwise the Sphinx build fails looking for references

@eteq eteq left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Cadair - a few teaks required to make the docs finish without warnings (including @astrofrog's comment above), and a couple quick additions to the docs, but then I think this is good!

Comment thread docs/development/testguide.rst Outdated
above.

Astropy Test Function
#####################

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be ^ instead of #, because Sphinx needs to have consistent section headings or else it spurts out all kinds of warnings.

Comment thread docs/testhelpers.rst Outdated
Astropy Testing helpers (`astropy.tests.helper`)
************************************************
***************
Astropy Testing

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "Astropy Testing Tools" or "Astropy Testing Machinery"? That would make it clearer that this is the machinery, while the guidelines are below.

Along the same lines, perhaps under this title (but before the helper module heading), you should add a sentence with a link to the testing guidelines. Something like This section is primarily a reference for developers that want to understand or add to the Astropy testing machinery. See :doc:/development/testguide for an overview of running or writing the tests.

Comment thread docs/testhelpers.rst Outdated
Astropy Test Runner
===================

When executing tests with either `astropy.test` or ``python setup.py test`` the call to pytest is controlled by the `astropy.tests.runner.TestRunner` class.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a few sentences explaining the changes in this PR and maybe giving a very simple example of how to write your own test keyword? (even if it just produces "hello world").

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put an example of writing a keyword in the docstring for TestRunnerBase, but I will add something here as well.

@Cadair
Cadair force-pushed the runner_subclassing branch from 80cb8fe to 298df93 Compare December 6, 2016 12:18
@Cadair

Cadair commented Dec 6, 2016

Copy link
Copy Markdown
Member Author

@eteq all sorted I hope.

@eteq

eteq commented Dec 6, 2016

Copy link
Copy Markdown
Member

@Cadair - had to make one change in Cadair/astropy@d7d3391 - you were missing backticks around a link, but the rest looks good. Merging!

@eteq
eteq merged commit 9564a9e into astropy:master Dec 6, 2016
@astrobot

astrobot commented Dec 6, 2016

Copy link
Copy Markdown

@eteq - thanks for merging this! However, I noticed the following issue with this pull request:

  • Changelog entry not present (or pull request number missing) and neither the Affects-dev nor the no-changelog-entry-needed label are set

Would it be possible to fix this? Thanks!

This is an experimental bot being written by @astrofrog - let me know if the message above is incorrect!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants