[Python-ideas] PEP 484: Generating test inputs from type hints
Terry Reedy
tjreedy at udel.edu
Tue Mar 31 02:41:26 CEST 2015
PEP 484 proposes "a standard syntax for type annotations", principally
for static analysis (and other static uses), with runtime checking or
optimization mentioned as a possibility.
Another (real) runtime use is generating unit test inputs. Today on
python-list, David MacIver announced Hypothesis, a testing library that
does just that. (The thread title is "Hypothesis 1.0: A production
quality property-based testing library for Python".) Here is an example
from http://hypothesis.readthedocs.org/en/latest/quickstart.html
from hypothesis import given
@given([int])
def test_reversing_twice_gives_same_list(xs):
assert xs == list(reversed(reversed(xs)))
When a test runner such as pytest calls the wrapper, it calls the real
function with a variety of int lists (including [], but details not
important here). @given() also works with unittest TestCase methods.
When I mentioned PEP 484 and the alternative syntax List(int), he
replied "Having something like that as standard would be great for
Hypothesis and I intend to support it once it becomes available."
I presume 'support' could and would include a version of given() that
accessed .__annotations__. Of course, if every 'test_xyz' were to be
decorated with the same decorator, then the decorator could be omitted
and the fuzzing moved into the test runner.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list