• Stars
    star
    344
  • Rank 123,066 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created about 5 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An extensions for Hypothesis that provides fully automatic testing for type hinted functions

hypothesis-auto - Fully Automatic Tests for Type Annotated Functions Using Hypothesis.


PyPI version Build Status codecov Join the chat at https://gitter.im/timothycrosley/hypothesis-auto License Downloads


Read Latest Documentation - Browse GitHub Code Repository


hypothesis-auto is an extension for the Hypothesis project that enables fully automatic tests for type annotated functions.

Hypothesis Pytest Auto Example

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 run auto_test(FUNCTION) to run dozens of test.
  • pytest Compatible: Like Hypothesis itself, hypothesis-auto has built-in compatibility with the popular pytest testing 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 of Hypothesis, including custom strategies per a parameter.

Installation:

To get started - install hypothesis-auto into your projects virtual environment:

pip3 install hypothesis-auto

OR

poetry add hypothesis-auto

OR

pipenv install hypothesis-auto

Usage 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 as auto_verify_= to keep the likely hood of conflicting low while avoiding the connotation of private parameters.

Framework independent usage

Basic auto_test usage:

from hypothesis_auto import auto_test


def add(number_1: int, number_2: int = 1) -> int:
    return number_1 + number_2


auto_test(add)  # 50 property based scenarios are generated and ran against add
auto_test(add, auto_runs_=1_000)  # Let's make that 1,000

Adding an allowed exception:

from hypothesis_auto import auto_test


def divide(number_1: int, number_2: int) -> int:
    return number_1 / number_2

auto_test(divide)

-> 1012                     raise the_error_hypothesis_found
   1013
   1014         for attrib in dir(test):

<ipython-input-2-65a3aa66e9f9> in divide(number_1, number_2)
      1 def divide(number_1: int, number_2: int) -> int:
----> 2     return number_1 / number_2
      3

0/0

ZeroDivisionError: division by zero


auto_test(divide, auto_allow_exceptions_=(ZeroDivisionError, ))

Using auto_test with a custom verification method:

from hypothesis_auto import Scenario, auto_test


def add(number_1: int, number_2: int = 1) -> int:
    return number_1 + number_2


def my_custom_verifier(scenario: Scenario):
    if scenario.kwargs["number_1"] > 0 and scenario.kwargs["number_2"] > 0:
        assert scenario.result > scenario.kwargs["number_1"]
        assert scenario.result > scenario.kwargs["number_2"]
    elif scenario.kwargs["number_1"] < 0 and scenario.kwargs["number_2"] < 0:
        assert scenario.result < scenario.kwargs["number_1"]
        assert scenario.result < scenario.kwargs["number_2"]
    else:
        assert scenario.result >= min(scenario.kwargs.values())
        assert scenario.result <= max(scenario.kwargs.values())


auto_test(add, auto_verify_=my_custom_verifier)

Custom verification methods should take a single Scenario and raise an exception to signify errors.

For the full set of parameters, you can pass into auto_test see its API reference documentation.

pytest usage

Using auto_pytest_magic to auto-generate dozens of pytest test cases:

from hypothesis_auto import auto_pytest_magic


def add(number_1: int, number_2: int = 1) -> int:
    return number_1 + number_2


auto_pytest_magic(add)

Using auto_pytest to run dozens of test case within a temporary directory:

from hypothesis_auto import auto_pytest


def add(number_1: int, number_2: int = 1) -> int:
    return number_1 + number_2


@auto_pytest()
def test_add(test_case, tmpdir):
    tmpdir.mkdir().chdir()
    test_case()

Using auto_pytest_magic with a custom verification method:

from hypothesis_auto import Scenario, auto_pytest


def add(number_1: int, number_2: int = 1) -> int:
    return number_1 + number_2


def my_custom_verifier(scenario: Scenario):
    if scenario.kwargs["number_1"] > 0 and scenario.kwargs["number_2"] > 0:
        assert scenario.result > scenario.kwargs["number_1"]
        assert scenario.result > scenario.kwargs["number_2"]
    elif scenario.kwargs["number_1"] < 0 and scenario.kwargs["number_2"] < 0:
        assert scenario.result < scenario.kwargs["number_1"]
        assert scenario.result < scenario.kwargs["number_2"]
    else:
        assert scenario.result >= min(scenario.kwargs.values())
        assert scenario.result <= max(scenario.kwargs.values())


auto_pytest_magic(add, auto_verify_=my_custom_verifier)

Custom verification methods should take a single Scenario and raise an exception to signify errors.

For the full reference of the pytest integration API see the API 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 find hypothesis-auto useful!

~Timothy Crosley

More Repositories

1

streamdeck-ui

A Linux compatible UI for the Elgato Stream Deck.
Python
1,137
star
2

portray

Your Project with Great Documentation.
Python
860
star
3

jiphy

Your client side done in a jiphy. Python to JavaScript 2-way converter.
Python
583
star
4

concentration

Stay focused on work when you want, and goof off when you don't.
Python
328
star
5

deprecated.frosted

A simple program which checks Python source files for errors.
Python
251
star
6

deprecated.pies

The simplest (and tastiest) way to write one program that runs on both Python 2 and Python 3.
Python
148
star
7

quickpython

A retro interactive coding environment powered by Python and nostalgia
Python
109
star
8

pdocs

A simple program and library to auto generate API documentation for Python modules.
Python
74
star
9

connectable

A very simple implementation of QTs signal / slot pattern in Python
Python
61
star
10

examples

Tests and Documentation Done by Example.
Python
24
star
11

thedom

A python framework to generate html and JavaScript from reusable and combine-able widgets.
Python
23
star
12

GuiBuilder

Click and Drop Editor For WebElements
Python
23
star
13

preconvert

A Library to enable preconversion of any Python type into one that is easily serializable
Python
17
star
14

cookiecutter-python

A cookie cutter template for modern Python projects using Python3.6+, poetry, mypy, py.test, and linters.
Shell
16
star
15

WebBot

WebBot is a collection of several tools that enables building Python web applications the same way native ones are built. As a result, the WebBot framework encourages reuse, concise code, rapid development, and happy developers.
Python
14
star
16

blog

Personal Blog
Python
7
star
17

DynamicForm

Ajax Abstraction Library for Python
Python
6
star
18

Html2Shpaml

Converts html to the more condensed and DRY sphaml format
Python
5
star
19

sigy

A library to enable reusing and composing Python function signatures.
Python
4
star
20

linux_environment

My common environment across all linux computers
Shell
4
star
21

CleanHTML

A Very Forgiving HTML/XML Cleaner
Python
4
star
22

RestClient

A Simple RestClient for Python - Works With AppEngine
Python
3
star
23

instantly

A Python Tool To Create and Expend Project Templates
Python
3
star
24

python3-project-template

Basic cookiecutter template for new Python3 projects
Python
3
star
25

website_change_alert

A simpe script to send a text when a website changes.
Python
3
star
26

camera_name_normalizer

Camera Name Normalization
Python
3
star
27

blox

Build it with Python
Python
3
star
28

preconvert_numpy

Preconverts common numpy types to their serializable (jsonifiable, msgpackable, etc...) form. A plugin for the preconvert Python library.
Python
2
star
29

simple_ci

An extremely simple ci server built with hug
Python
2
star
30

RedisStore

RedisStore enables interacting with Redis keys from within python as if they where native object types.
Python
2
star
31

dpy

A tool that automatically generates quality documentation for Python projects.
Python
2
star
32

todo_list

A remotely available todo list
Python
2
star
33

black_isort

A guaranteed black compatible import sorter with minimal settings.
Python
2
star
34

signature

An expiremental framework for composing Python function signature
Shell
1
star
35

webbot_example_postboard

Code for post board example shown in screen-cast http://www.youtube.com/watch?v=0L8TsmrZPLg
Python
1
star
36

sprints_kit

Evolving kit for leading open source sprints
1
star
37

intrigue_icon_set

A set of 100% free and original icons - with a unique minimalistic look
Perl
1
star
38

webelements_site

The code that powers the WebElements homepage
Python
1
star
39

mde

CLI Markdown Editor
Python
1
star
40

mac_environment

Personal MacOS environment setup instructions
Shell
1
star