ut icon indicating copy to clipboard operation
ut copied to clipboard

[feature] Add property based testing

Open Theramar opened this issue 7 months ago • 0 comments

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

Theramar avatar Nov 17 '23 12:11 Theramar