You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Copy file name to clipboardExpand all lines: doc/source/contributing.rst
+60
Original file line number
Diff line number
Diff line change
@@ -775,6 +775,66 @@ Tests that we have ``parametrized`` are now accessible via the test name, for ex
775
775
test_cool_feature.py::test_dtypes[int8] PASSED
776
776
test_cool_feature.py::test_series[int8] PASSED
777
777
778
+
Transitioning to ``hypothesis``
779
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
780
+
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
+
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.
782
+
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.
783
+
e.g suppose we have to test python's sum function for a list of int.
========================== 4 passed in 0.06 seconds ===========================
810
+
811
+
812
+
Compare it with below example for the same test case using hypothesis.
813
+
.. code-block:: python
814
+
from hypothesis import strategies as st
815
+
from hypothesis import given
816
+
817
+
818
+
@given(st.lists(st.integers()))
819
+
def test_sum(seq):
820
+
total = 0
821
+
foritemin seq:
822
+
total += item
823
+
assert sum(seq) == total
824
+
825
+
output of test cases:
826
+
.. code-block:: shell
827
+
collecting ... collected 1 item
828
+
hypothesis_example.py::test_sum PASSED [100%]
829
+
830
+
========================== 1 passed in 0.33 seconds ===========================
831
+
832
+
The main difference in above example is use of a decorator "@given(st.lists(st.integers()))" which if applied to testcase function, generates some random list of int, which is then assigned to parameter of test case.
833
+
For more information about hypothesis or in general about property based testing, check below links:
0 commit comments