Skip to content

Commit 6504c2d

Browse files
committed
DOC: Demo using pytest features in test classes
[ci skip]
1 parent 1dea6ef commit 6504c2d

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

doc/source/contributing.rst

+22-12
Original file line numberDiff line numberDiff line change
@@ -614,23 +614,34 @@ the expected correct result::
614614

615615
assert_frame_equal(pivoted, expected)
616616

617-
How to use ``parametrize``
618-
~~~~~~~~~~~~~~~~~~~~~~~~~~
617+
Transitioning to ``pytest``
618+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
619619

620-
`pytest <http://doc.pytest.org/en/latest/>`__ has a nice feature `parametrize <https://docs.pytest.org/en/latest/parametrize.html>`__ to allow
621-
testing of many cases in a concise way that enables an easy-to-read syntax.
620+
*pandas* existing test structure is *mostly* classed based, meaning that you will typically find tests wrapped in a class, inheriting from ``tm.TestCase``.
622621

623-
.. note::
622+
.. code-block:: python
624623
625-
*pandas* existing test structure is *mostly* classed based, meaning that you will typically find tests wrapped in a class, inheriting from ``tm.TestCase``.
624+
class TestReallyCoolFeature(tm.TestCase):
625+
....
626626
627-
.. code-block:: python
627+
Going forward, we are moving to a more *functional* style using the `pytest <http://doc.pytest.org/en/latest/>`__ framework, which offers a richer testing
628+
framework that will facilitate testing and developing. Thus, instead of writing test classes, we will write test functions like this:
628629

629-
class TestReallyCoolFeature(tm.TestCase):
630-
....
630+
.. code-block:: python
631631
632-
Going forward we are moving to a more *functional* style, please see below.
632+
def test_really_cool_feature():
633+
....
633634
635+
Sometimes, it does make sense to bundle test functions together into a single class, either because the test file is testing multiple functions from a single module, and
636+
using test classes allows for better organization. However, instead of inheriting from ``tm.TestCase``, we should just inherit from ``object``:
637+
638+
.. code-block:: python
639+
640+
class TestReallyCoolFeature(object):
641+
....
642+
643+
Using ``pytest``
644+
~~~~~~~~~~~~~~~~
634645

635646
Here is an example of a self-contained set of tests that illustrate multiple features that we like to use.
636647

@@ -641,7 +652,7 @@ Here is an example of a self-contained set of tests that illustrate multiple fea
641652
- ``tm.assert_series_equal`` (and its counter part ``tm.assert_frame_equal``), for pandas object comparisons.
642653
- the typical pattern of constructing an ``expected`` and comparing versus the ``result``
643654

644-
We would name this file ``test_cool_feature.py`` and put in an appropriate place in the ``pandas/tests/`` sturcture.
655+
We would name this file ``test_cool_feature.py`` and put in an appropriate place in the ``pandas/tests/`` structure.
645656

646657
.. code-block:: python
647658
@@ -701,7 +712,6 @@ Tests that we have ``parametrized`` are now accessible via the test name, for ex
701712
test_cool_feature.py::test_dtypes[int8] PASSED
702713
test_cool_feature.py::test_series[int8] PASSED
703714
704-
705715
Running the test suite
706716
----------------------
707717

0 commit comments

Comments
 (0)