Skip to content

Commit cd51bdd

Browse files
authored
DOC: add section on how to use parametrize to contributing.rst (pandas-dev#15883)
closes pandas-dev#15608
1 parent ca7207f commit cd51bdd

File tree

1 file changed

+108
-24
lines changed

1 file changed

+108
-24
lines changed

doc/source/contributing.rst

+108-24
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,9 @@ Bug reports must:
5151
...
5252
```
5353

54-
#. Include the full version string of *pandas* and its dependencies. In versions
55-
of *pandas* after 0.12 you can use a built in function::
56-
57-
>>> from pandas.util.print_versions import show_versions
58-
>>> show_versions()
59-
60-
and in *pandas* 0.13.1 onwards::
54+
#. Include the full version string of *pandas* and its dependencies. You can use the built in function::
6155

56+
>>> import pandas as pd
6257
>>> pd.show_versions()
6358

6459
#. Explain why the current behavior is wrong/not desired and what you expect instead.
@@ -209,7 +204,7 @@ At this point you can easily do an *in-place* install, as detailed in the next s
209204
Creating a Windows development environment
210205
------------------------------------------
211206

212-
To build on Windows, you need to have compilers installed to build the extensions. You will need to install the appropriate Visual Studio compilers, VS 2008 for Python 2.7, VS 2010 for 3.4, and VS 2015 for Python 3.5.
207+
To build on Windows, you need to have compilers installed to build the extensions. You will need to install the appropriate Visual Studio compilers, VS 2008 for Python 2.7, VS 2010 for 3.4, and VS 2015 for Python 3.5 and 3.6.
213208

214209
For Python 2.7, you can install the ``mingw`` compiler which will work equivalently to VS 2008::
215210

@@ -219,7 +214,7 @@ or use the `Microsoft Visual Studio VC++ compiler for Python <https://www.micros
219214

220215
For Python 3.4, you can download and install the `Windows 7.1 SDK <https://www.microsoft.com/en-us/download/details.aspx?id=8279>`__. Read the references below as there may be various gotchas during the installation.
221216

222-
For Python 3.5, you can download and install the `Visual Studio 2015 Community Edition <https://www.visualstudio.com/en-us/downloads/visual-studio-2015-downloads-vs.aspx>`__.
217+
For Python 3.5 and 3.6, you can download and install the `Visual Studio 2015 Community Edition <https://www.visualstudio.com/en-us/downloads/visual-studio-2015-downloads-vs.aspx>`__.
223218

224219
Here are some references and blogs:
225220

@@ -544,26 +539,26 @@ signatures and add deprecation warnings where needed.
544539
Testing Thru Continuous Integration
545540
-----------------------------------
546541

547-
The pandas testing suite will run automatically on Travis-CI, Appveyor, and Circle CI
548-
continuous integration services, once your pull request is submitted.
542+
The *pandas* testing suite will run automatically on `Travis-CI <https://travis-ci.org/>`__,
543+
`Appveyor <https://www.appveyor.com/>`__, and `Circle CI <https://circleci.com/>`__ continuous integration
544+
services, once your pull request is submitted.
549545
However, if you wish to run the test suite on a branch prior to submitting the pull request,
550-
then Travis-CI, Appveyor and/or CircleCI need to be hooked up to your GitHub repository.
551-
Instructions for doing so are `here <http://about.travis-ci.org/docs/user/getting-started/>`__ for
552-
Travis-CI, `here <https://www.appveyor.com/docs/>`__ for Appveyor, and
553-
`here <https://circleci.com/>`__ for CircleCI.
546+
then the continuous integration services need to be hooked to your GitHub repository. Instructions are here
547+
for `Travis-CI <http://about.travis-ci.org/docs/user/getting-started/>`__,
548+
`Appveyor <https://www.appveyor.com/docs/>`__ , and `CircleCI <https://circleci.com/>`__.
554549

555-
A pull-request will be considered for merging when you have an all 'green' build. See
556-
this example.
550+
A pull-request will be considered for merging when you have an all 'green' build. If any tests are failing,
551+
then you will get a red 'X', where you can click thru to see the individual failed tests.
552+
This is an example of a green build.
557553

558554
.. image:: _static/ci.png
559555

560-
561556
.. note::
562557

563-
Pushing to *your* branch will cancel any non-currently-running tests for that
564-
same pull-request for Appveyor. For Travis CI, you can enable the auto-cancel feature
565-
`here <https://docs.travis-ci.com/user/customizing-the-build/#Building-only-the-latest-commit>`__ and
566-
for CircleCI `here <https://circleci.com/changelog-legacy/#option-to-auto-cancel-redundant-builds>`__.
558+
Each time you push to *your* fork, a *new* run of the tests will trigger on the CI. Appveyor will auto-cancel
559+
any non-currently-running tests for that same pull-request. You can enable the auto-cancel feature for
560+
`Travis-CI here <https://docs.travis-ci.com/user/customizing-the-build/#Building-only-the-latest-commit>`__ and
561+
for `CircleCI here <https://circleci.com/changelog-legacy/#option-to-auto-cancel-redundant-builds>`__.
567562

568563
.. _contributing.tdd:
569564

@@ -620,8 +615,96 @@ the expected correct result::
620615

621616
assert_frame_equal(pivoted, expected)
622617

618+
How to use ``parametrize``
619+
~~~~~~~~~~~~~~~~~~~~~~~~~~
620+
621+
`pytest <http://doc.pytest.org/en/latest/>`__ has a nice feature `parametrize <https://docs.pytest.org/en/latest/parametrize.html>`__ to allow
622+
testing of many cases in a concise way that enables an easy-to-read syntax.
623+
624+
.. note::
625+
626+
.. code-block:: python
627+
628+
*pandas* existing test structure is *mostly* classed based, meaning that you will typically find tests wrapped in a class, inheriting from ``tm.TestCase``.
629+
630+
class TestReallyCoolFeature(tm.TestCase):
631+
....
632+
633+
Going forward we are moving to a more *functional* style, please see below.
634+
635+
636+
Here is an example of a self-contained set of tests that illustrate multiple features that we like to use.
637+
638+
- functional style: tests are like ``test_*`` and *only* take arguments that are either fixtures or parameters
639+
- using ``parametrize``: allow testing of multiple cases
640+
- ``fixture``, code for object construction, on a per-test basis
641+
- using bare ``assert`` for scalars and truth-testing
642+
- ``tm.assert_series_equal`` (and its counter part ``tm.assert_frame_equal``), for pandas object comparisons.
643+
- the typical pattern of constructing an ``expected`` and comparing versus the ``result``
644+
645+
We would name this file ``test_cool_feature.py`` and put in an appropriate place in the ``pandas/tests/`` sturcture.
646+
647+
.. code-block:: python
648+
649+
import pytest
650+
import numpy as np
651+
import pandas as pd
652+
from pandas.util import testing as tm
653+
654+
@pytest.mark.parametrize('dtype', ['int8', 'int16', 'int32', 'int64'])
655+
def test_dtypes(dtype):
656+
assert str(np.dtype(dtype)) == dtype
657+
658+
@pytest.fixture
659+
def series():
660+
return pd.Series([1, 2, 3])
661+
662+
@pytest.fixture(params=['int8', 'int16', 'int32', 'int64'])
663+
def dtype(request):
664+
return request.param
665+
666+
def test_series(series, dtype):
667+
result = series.astype(dtype)
668+
assert result.dtype == dtype
669+
670+
expected = pd.Series([1, 2, 3], dtype=dtype)
671+
tm.assert_series_equal(result, expected)
672+
673+
674+
A test run of this yields
675+
676+
.. code-block:: shell
677+
678+
((pandas) bash-3.2$ pytest test_cool_feature.py -v
679+
=========================== test session starts ===========================
680+
platform darwin -- Python 3.5.2, pytest-3.0.5, py-1.4.31, pluggy-0.4.0
681+
collected 8 items
682+
683+
tester.py::test_dtypes[int8] PASSED
684+
tester.py::test_dtypes[int16] PASSED
685+
tester.py::test_dtypes[int32] PASSED
686+
tester.py::test_dtypes[int64] PASSED
687+
tester.py::test_series[int8] PASSED
688+
tester.py::test_series[int16] PASSED
689+
tester.py::test_series[int32] PASSED
690+
tester.py::test_series[int64] PASSED
691+
692+
Tests that we have ``parametrized`` are now accessible via the test name, for example we could run these with ``-k int8`` to sub-select *only* those tests which match ``int8``.
693+
694+
695+
.. code-block:: shell
696+
697+
((pandas) bash-3.2$ pytest test_cool_feature.py -v -k int8
698+
=========================== test session starts ===========================
699+
platform darwin -- Python 3.5.2, pytest-3.0.5, py-1.4.31, pluggy-0.4.0
700+
collected 8 items
701+
702+
test_cool_feature.py::test_dtypes[int8] PASSED
703+
test_cool_feature.py::test_series[int8] PASSED
704+
705+
623706
Running the test suite
624-
~~~~~~~~~~~~~~~~~~~~~~
707+
----------------------
625708
626709
The tests can then be run directly inside your Git clone (without having to
627710
install *pandas*) by typing::
@@ -675,7 +758,8 @@ Furthermore one can run
675758
with an imported pandas to run tests similarly.
676759
677760
Running the performance test suite
678-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
761+
----------------------------------
762+
679763
Performance matters and it is worth considering whether your code has introduced
680764
performance regressions. *pandas* is in the process of migrating to
681765
`asv benchmarks <https://github.com/spacetelescope/asv>`__

0 commit comments

Comments
 (0)