Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Add property based testing #595

Open
Theramar opened this issue Nov 17, 2023 · 0 comments
Open

[feature] Add property based testing #595

Theramar opened this issue Nov 17, 2023 · 0 comments

Comments

@Theramar
Copy link

property testing

taken from hypothesis (emphasis mine)
It works by letting you write tests that assert that something should be true for every case, not just the ones you happen
to think of.
Think of a normal unit test as being something like the following:

  • Set up some data.
  • Perform some operations on the data.
  • Assert something about the result.

Hypothesis lets you write tests which instead look like this:

  • For all data matching some specification.
  • Perform some operations on the data.
  • Assert something about the result.

python example:

@given(text())
def test_decode_inverts_encode(s):
    assert decode(encode(s)) == s
  • the text function returns what Hypothesis calls a search strategy. An object with methods that describe how to generate and simplify certain kinds of values.
Falsifying example: test_decode_inverts_encode(s='')
UnboundLocalError: local variable 'character' referenced before assignment

Hypothesis correctly points out that this code is simply wrong if called on an empty string.

  • great for regression tests because Hypothesis will remember failing examples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant