Skip to content
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

Pytest documentation + build tweaks #8026

Merged
merged 14 commits into from Feb 20, 2017
Merged

Conversation

QuLogic
Copy link
Member

@QuLogic QuLogic commented Feb 5, 2017

I've added the developer documentation for setting up pytest and removed a few more cases of nose in the setup, package metadata, etc.

For the tweaks:

  • Fix tests.py when run with a module path instead of a file path.
  • Close figures before running image_comparison because that decorator expects all figures when it's done to correspond to figures from the test, but if never checks for/ignores any already open figures.
  • Delete test_mouseclicks.py example because it's getting collected and run by pytest and since it's not really written as a test it causes an extra figure to be opened which breaks any image tests if run directly (modulo the previous fix.) Also copy the use of event.dblclick into the documentation so it's not gone completely.
  • Use importorskip everywhere so that the skip reasons are always the same, since it's used in some places and not others.

Fixes #8015.

@QuLogic QuLogic added this to the 2.1 (next point release) milestone Feb 5, 2017
@QuLogic
Copy link
Member Author

QuLogic commented Feb 5, 2017

Added one more tweak to use importorskip everywhere so that the skip reasons are always the same, since it's used in some places and not others.

@QuLogic QuLogic changed the title Pytest tweaks Pytest documentation + build tweaks Feb 6, 2017
@QuLogic
Copy link
Member Author

QuLogic commented Feb 6, 2017

I've added the developer documentation for setting up pytest and removed a few more cases of nose in the setup, package metadata, etc.

@anntzer
Copy link
Contributor

anntzer commented Feb 6, 2017

Could we document the pytest interface rather than tests.py? I think it basically comes down to documenting -m "not network" instead of --no-network, the rest is the same.

After a quick look at tests.py, it also seems that there are some customizations (warnings filters, recursion limit) which should perhaps be moved to conftest.py?

@@ -12,7 +12,6 @@ commands =
sh -c 'rm -f $HOME/.matplotlib/fontList*'
Copy link
Member

Choose a reason for hiding this comment

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

Add python version 3.6 to envlist above

Copy link
Member Author

Choose a reason for hiding this comment

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

Added.

import pandas as pd
except ImportError:
pytest.skip("Pandas not installed")
pd = pytest.importorskip('pandas')
Copy link
Member

Choose a reason for hiding this comment

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

Is there a minimum required version of pandas needed for the pandas tests to work?

Copy link
Member

Choose a reason for hiding this comment

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

It doesn't appear that there is. Creating a Series from a list and DataFrame a from a dictionary of lists/arrays should work with versions of pandas several years old at this point.

@@ -1,32 +0,0 @@
from __future__ import print_function
Copy link
Member

@dstansby dstansby Feb 6, 2017

Choose a reason for hiding this comment

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

Also need to get rid of exammples/event_handling/test_mouseclicks.rst

Copy link
Member

Choose a reason for hiding this comment

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

Isn't that file autogenerated from the example? It's not in version control as far as I can see

Copy link
Member

Choose a reason for hiding this comment

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

🤦‍♂️ so it is


import matplotlib
matplotlib.test()

.. hint::

To run the tests you need to install nose and mock if using python 2.7::
To run the tests you need to install pytest and mock if using python 2.7::
Copy link
Member

Choose a reason for hiding this comment

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

Everything in this hint is covered above, so I think it can just be deleted

documentation for supported arguments. Some of the more important ones are given
here:
Additional arguments are passed on to pytest. See the pytest documentation for
supported arguments. Some of the more important ones are given here:
Copy link
Member

Choose a reason for hiding this comment

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

Might be good to put a link to the pytest documents that list supported arguments


python tests.py matplotlib.tests.test_simplification:test_clipping
python tests.py matplotlib.tests.test_simplification::test_clipping
Copy link
Member

Choose a reason for hiding this comment

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

Is there any point documenting the method where tests have to be installed, if the method below (which doesn't require installed tests) works?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know; does anyone use this method?

On further though, using non-installed tests might be broken when trying mpl_toolkits tests because they import stuff from matplotlib.tests. I can move things around to get that working again.

Since this decorator assumes that the figures available after are the
ones to check, it should close any existing figures or it will get the
wrong count.
The name is getting collected as a test by pytest when it isn't one, and
it isn't much of an example anyway.
It's used in some places, but not all.
This may or may not become a requirement in the future.
* Check for installed test data only if testing installed package.
* Remove relative import out of tests.
* Replace assert_str_equal with plain assert (pytest does diffing
  already).
@QuLogic
Copy link
Member Author

QuLogic commented Feb 16, 2017

I updated the documentation to follow the requests here and also moved some bits around so that tests work without being installed. Everything but the determinism test in the SVG backend works (because that one imports itself, which doesn't exist when not installed); not too sure how to fix that one, but it's not a high priority since not-installed tests never worked before.

Copy link
Member

@NelleV NelleV left a comment

Choose a reason for hiding this comment

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

Here are general comments on the documentation part.
This are minor comments and should not prevent the pull requests from being merged. These are just overall suggestions of improvements.

I'll review the rest of the PR.

INSTALL Outdated
@@ -106,12 +106,10 @@ or example code.
If you want to try the many demos that come in the matplotlib source
distribution, download the :file:`*.tar.gz` file and look in the
:file:`examples` subdirectory.
To run the test suite, copy the :file:`lib\\matplotlib\\tests` and
To run the test suite, extract the :file:`lib\\matplotlib\\tests` or
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand this sentence.

Copy link
Member Author

Choose a reason for hiding this comment

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

I split things into bullet points; hopefully that's clearer.

README.rst Outdated
more information. Note that the test suite requires nose and on Python 2.7 mock
which are not installed by default. Please install with pip or your package
manager of choice.
more information. Note that the test suite requires pytest and on Python 2.7
Copy link
Member

Choose a reason for hiding this comment

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

I think this sentence would be better with comas (for the understanding):
Note that the test suite requires pytest and, on Python 2.7, mock.
The "which are not installed by default seems redundant.

I would be happy to push a fix to your branch. Let me know if you prefer this option.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed.

@@ -30,7 +30,7 @@ requirements:
- freetype 2.6*
- msinttypes # [win]
- cycler >=0.10
- nose
- pytest >=3.0.0
Copy link
Member

Choose a reason for hiding this comment

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

👍

<https://docs.python.org/dev/library/unittest.mock.html>`_ (if python < 3.3), `Ghostscript
<https://www.ghostscript.com/>`_, `Inkscape <https://inkscape.org>`_
**Additional dependencies for testing**: pytest_ (version 3.0 or later),
mock_ (if python < 3.3), Ghostscript_, Inkscape_
Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Member

Choose a reason for hiding this comment

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

Python 3 < 3.3 isn't supported but matplotlib, so might make more sense to change this to "if python 2.7"

please ignore it while we consolidate our testing to these locations.)

.. _nose: https://nose.readthedocs.io/en/latest/
Matplotlib has a testing infrastructure based on pytest_, making it easy to
Copy link
Member

Choose a reason for hiding this comment

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

Here are suggestions:

Matplotlib's testing infrastucture relies on pytest_. Test files are place in [path to the folder], and testing utility functions are in :mod:`matplotlib.testing`.

I'd drop the "making it easy to write new tests", as this is a judgement call that I also totally disagree with.
I'd also remove the "There is other old testing cruft around…"

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed; also switched to the file paths instead of modules since we're talking about developers who probably want to edit the existing files.


python tests.py matplotlib.tests.test_simplification:test_clipping
py.test lib/matplotlib/tests/test_simplification.py::test_clipping
Copy link
Member

Choose a reason for hiding this comment

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

👍

python tests.py matplotlib.tests.test_simplification:test_clipping
py.test lib/matplotlib/tests/test_simplification.py::test_clipping

or, if tests are installed, a dot-separated path to the module, optionally
Copy link
Member

Choose a reason for hiding this comment

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

I would remove this section. We only need to document one way to do this.


An alternative implementation that does not look at command line arguments
and works from within Python is to run the tests from the Matplotlib library
function :func:`matplotlib.test`::

import matplotlib
matplotlib.test()
Copy link
Member

Choose a reason for hiding this comment

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

I'd remove this section from our documentation. I think we should document (and have) only one way to run the tests.

attn @tacaswell (as I believe Thomas will disagree with me on that one).

"""
fig = figure()
...
Tests that have side effects that need to be cleaned up, such as created
Copy link
Member

Choose a reason for hiding this comment

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

That paragraph is very unclear to me. Here is a suggestions.

"Some tests have side effects that need to be cleaned up after their execution (such as created figures or modifiec rc params). The pytest fixture XXX will automatically clean these up:

add short example showing how to use the fixture"

Copy link
Member Author

Choose a reason for hiding this comment

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

There's no need for an example because the fixture gets used automatically; I've swapped the text around as suggested.

@@ -29,13 +29,13 @@ connect your function to the event manager, which is part of the
example that prints the location of the mouse click and which button
was pressed::

fig = plt.figure()
ax = fig.add_subplot(111)
fig, ax = plt.subplots()
Copy link
Member

Choose a reason for hiding this comment

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

👍

@madphysicist
Copy link
Contributor

Thanks for doing this. This is very helpful. Both the docs and switching to pytest. There is a bit of a learning curve with it, but it seems to be much more flexible than nose.

* Prefer py.test over tests.py
* Remove redundant hint
* Add link to further pytest documentation
* Tweak some sentence structure
The only difference between the two 3.5 builds is one uses tests.py and
the other uses py.test. But the 2.7, 3.4, and macOS builds all use
tests.py, to this is a waste of resources.
@QuLogic
Copy link
Member Author

QuLogic commented Feb 18, 2017

I've removed an extra 3.5 build on Travis because it's pretty redundant; hopefully that will get us to finished CI just a little bit faster.

@NelleV NelleV changed the title Pytest documentation + build tweaks [MRG+1] Pytest documentation + build tweaks Feb 18, 2017
@NelleV
Copy link
Member

NelleV commented Feb 18, 2017

LGTM 👍

An alternative implementation that does not look at command line
arguments works from within Python is to run the tests from the
matplotlib library function :func:`matplotlib.test`::
PYTHONHASHSEED=0 py.test --verbose -n 5
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we just specify a minimum version of pytest-xdist where this is no longer an issue? It looks like it is related to https://bitbucket.org/pytest-dev/pytest/issues/346/pytest-xdist-and-python-33-is-sort-of, which has been resolved in 2013.

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately, it's not fixed yet: pytest-dev/pytest#920

I just PR'd the same fix to the pandas build, so I know that recent versions still don't work.

Copy link
Member

@dstansby dstansby left a comment

Choose a reason for hiding this comment

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

I think this looks ready to go! I've just left a couple of comments for things that might need tweaking in the docs; let me know once you've read and/or changed them, and I will merge.

`mock <https://pypi.python.org/pypi/mock>`_, Pillow, MiKTeX, GhostScript,
ffmpeg, avconv, mencoder, ImageMagick, and `Inkscape
<https://inkscape.org/>`_;
* run ``py.test path\\to\\tests\\directory``.
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

This is about separate uninstalled tests; line 23 in README.rst is about running from the full source directory.

@@ -20,17 +20,16 @@ Testing

After installation, you can launch the test suite::

python tests.py
py.test
Copy link
Member

Choose a reason for hiding this comment

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

See previous comment about py.test vs. pytest

<https://docs.python.org/dev/library/unittest.mock.html>`_ (if python < 3.3), `Ghostscript
<https://www.ghostscript.com/>`_, `Inkscape <https://inkscape.org>`_
**Additional dependencies for testing**: pytest_ (version 3.0 or later),
mock_ (if python < 3.3), Ghostscript_, Inkscape_
Copy link
Member

Choose a reason for hiding this comment

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

Python 3 < 3.3 isn't supported but matplotlib, so might make more sense to change this to "if python 2.7"

Running the tests is simple. Make sure you have nose installed and run::
Running the tests is simple. Make sure you have pytest installed and run::

py.test
Copy link
Member

Choose a reason for hiding this comment

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

See above comment about py.test vs. pytest

or, if tests are installed, a dot-separated path to the module, optionally
followed by the function separated by two colons, such as::

py.test --pyargs matplotlib.tests.test_simplification::test_clipping

If you want to run the full test suite, but want to save wall time try
Copy link
Member

Choose a reason for hiding this comment

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

What is "wall time"?

Copy link
Member Author

Choose a reason for hiding this comment

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

Time on a clock on the wall, as opposed to cpu time.

@dstansby dstansby changed the title [MRG+1] Pytest documentation + build tweaks Pytest documentation + build tweaks Feb 20, 2017
@dstansby
Copy link
Member

Since all the lines I commented on are technically true and easily changed later, I'm going to merge (finally!), thanks for all the work on this @QuLogic 👍

@dstansby dstansby merged commit 0d5a02a into matplotlib:master Feb 20, 2017
@QuLogic QuLogic deleted the pytest-tweaks branch February 20, 2017 21:53
@dstansby dstansby moved this from Needs Review to Done in Migrate to `pytest` Mar 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Document new testing procedure
7 participants