Package: hypothesis-auto
Read Latest Documentation-Browse GitHub Code Repositoryhypothesis-autois an extension for theHypothesisproject that enables fully automatic tests for type annotated functions.Key Features:Type Annotation Powered: Utilize your function's existing type annotations to build dozens of test cases automatically.Low Barrier: Start utilizing property-based testing in the lowest barrier way possible. Just runauto_test(FUNCTION)to run dozens of test.pytest Compatible: Like Hypothesis itself, hypothesis-auto has built-in compatibility with the popularpytesttesting framework. This means that you can turn your automatically generated tests into individual pytest test cases with one line.Scales Up: As you find your self needing to customize your auto_test cases, you can easily utilize all the features ofHypothesis, including custom strategies per a parameter.Installation:To get started - installhypothesis-autointo your projects virtual environment:pip3 install hypothesis-autoORpoetry add hypothesis-autoORpipenv install hypothesis-autoUsage Examples:!!! warning
In old usage examples you will see_prefixed parameters like_auto_verify=. This was done to avoid conflicting with existing function parameters.
Based on community feedback the project switched to_suffixes, such asauto_verify_=to keep the likely hood of conflicting low while
avoiding the connotation of private parameters.Framework independent usageBasicauto_testusage:fromhypothesis_autoimportauto_testdefadd(number_1:int,number_2:int=1)->int:returnnumber_1+number_2auto_test(add)# 50 property based scenarios are generated and ran against addauto_test(add,auto_runs_=1_000)# Let's make that 1,000Adding an allowed exception:fromhypothesis_autoimportauto_testdefdivide(number_1:int,number_2:int)->int:returnnumber_1/number_2auto_test(divide)->1012raisethe_error_hypothesis_found10131014forattribindir(test):<ipython-input-2-65a3aa66e9f9>individe(number_1,number_2)1defdivide(number_1:int,number_2:int)->int:---->2returnnumber_1/number_230/0ZeroDivisionError:divisionbyzeroauto_test(divide,auto_allow_exceptions_=(ZeroDivisionError,))Usingauto_testwith a custom verification method:fromhypothesis_autoimportScenario,auto_testdefadd(number_1:int,number_2:int=1)->int:returnnumber_1+number_2defmy_custom_verifier(scenario:Scenario):ifscenario.kwargs["number_1"]>0andscenario.kwargs["number_2"]>0:assertscenario.result>scenario.kwargs["number_1"]assertscenario.result>scenario.kwargs["number_2"]elifscenario.kwargs["number_1"]<0andscenario.kwargs["number_2"]<0:assertscenario.result<scenario.kwargs["number_1"]assertscenario.result<scenario.kwargs["number_2"]else:assertscenario.result>=min(scenario.kwargs.values())assertscenario.result<=max(scenario.kwargs.values())auto_test(add,auto_verify_=my_custom_verifier)Custom verification methods should take a singleScenarioand raise an exception to signify errors.For the full set of parameters, you can pass into auto_test see itsAPI reference documentation.pytest usageUsingauto_pytest_magicto auto-generate dozens of pytest test cases:fromhypothesis_autoimportauto_pytest_magicdefadd(number_1:int,number_2:int=1)->int:returnnumber_1+number_2auto_pytest_magic(add)Usingauto_pytestto run dozens of test case within a temporary directory:fromhypothesis_autoimportauto_pytestdefadd(number_1:int,number_2:int=1)->int:returnnumber_1+number_2@auto_pytest()deftest_add(test_case,tmpdir):tmpdir.mkdir().chdir()test_case()Usingauto_pytest_magicwith a custom verification method:fromhypothesis_autoimportScenario,auto_pytestdefadd(number_1:int,number_2:int=1)->int:returnnumber_1+number_2defmy_custom_verifier(scenario:Scenario):ifscenario.kwargs["number_1"]>0andscenario.kwargs["number_2"]>0:assertscenario.result>scenario.kwargs["number_1"]assertscenario.result>scenario.kwargs["number_2"]elifscenario.kwargs["number_1"]<0andscenario.kwargs["number_2"]<0:assertscenario.result<scenario.kwargs["number_1"]assertscenario.result<scenario.kwargs["number_2"]else:assertscenario.result>=min(scenario.kwargs.values())assertscenario.result<=max(scenario.kwargs.values())auto_pytest_magic(add,auto_verify_=my_custom_verifier)Custom verification methods should take a singleScenarioand raise an exception to signify errors.For the full reference of the pytest integration API see theAPI reference documentation.Why Create hypothesis-auto?I wanted a no/low resistance way to start incorporating property-based tests across my projects. Such a solution that also encouraged the use of type hints was a win/win for me.I hope you too findhypothesis-autouseful!~Timothy Crosley