Skip to content

Commit d30d6fd

Browse files
committed
TST, fix for issue pandas-dev#17978.
Addition of "hypothesis usage" in test cases of tests/reshape/test_util.py as kind of POC. Incorporate review comments. Resolve flake8 warning. Add section for hypothesis in contributing.rst
1 parent fca259c commit d30d6fd

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

doc/source/contributing.rst

+13-1
Original file line numberDiff line numberDiff line change
@@ -776,14 +776,18 @@ Tests that we have ``parametrized`` are now accessible via the test name, for ex
776776
test_cool_feature.py::test_series[int8] PASSED
777777
778778
Transitioning to ``hypothesis``
779-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
779+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
780780
With the transition to pytest, things have become easier for testing by having reduced boilerplate for test cases and also by utilizing pytest's features like parametizing, skipping and marking test cases.
781+
781782
However, one has to still come up with input data examples which can be tested against the functionality. There is always a possibility to skip testing an example which could have failed the test case.
783+
782784
Imagine if some framework could generate random input examples based on the property/specification of the function being tested. That is exactly what hypothesis does by generating the input data based on some set of specifications provided by the user.
783785
e.g suppose we have to test python's sum function for a list of int.
784786
785787
Here is a sample test case using pytest:
788+
786789
.. code-block:: python
790+
787791
import pytest
788792
789793
@pytest.mark.parametrize('seq', [
@@ -799,7 +803,9 @@ Here is a sample test case using pytest:
799803
assert sum(seq) == total
800804
801805
output of test cases:
806+
802807
.. code-block:: shell
808+
803809
collecting ... collected 4 items
804810
pytest_example.py::test_sum_using_pytest[seq0] PASSED [ 25%]
805811
pytest_example.py::test_sum_using_pytest[seq1] PASSED [ 50%]
@@ -810,7 +816,9 @@ output of test cases:
810816
811817
812818
Compare it with below example for the same test case using hypothesis.
819+
813820
.. code-block:: python
821+
814822
from hypothesis import strategies as st
815823
from hypothesis import given
816824
@@ -822,15 +830,19 @@ Compare it with below example for the same test case using hypothesis.
822830
total += item
823831
assert sum(seq) == total
824832
833+
825834
output of test cases:
835+
826836
.. code-block:: shell
837+
827838
collecting ... collected 1 item
828839
hypothesis_example.py::test_sum PASSED [100%]
829840
830841
========================== 1 passed in 0.33 seconds ===========================
831842
832843
The main difference in above example is use of a decorator "@given(st.lists(st.integers()))" which if applied to test case function, generates some random list of int, which is then assigned to parameter of test case.
833844
For more information about hypothesis or in general about property based testing, check below links:
845+
834846
- https://hypothesis.readthedocs.io/en/latest/quickstart.html
835847
- https://hypothesis.works/articles/what-is-property-based-testing/
836848
- http://blog.jessitron.com/2013/04/property-based-testing-what-is-it.html

0 commit comments

Comments
 (0)