test isolation between pytest-hypothesis runs
Asked Answered
C

1

4

I just migrated a pytest test suite from quickcheck to hypothesis. This worked quite well (and immediately uncovered some hidden edge case bugs), but one major difference I see is related to test isolation between the two property managers.

quickcheck seems to simply run the test function multiple times with different parameter values, each time running my function-scoped fixtures. This also results in many more dots in pytest's output.

hypothesis however seems to run only the body of the test function multiple times, meaning for example no transaction rollbacks between individual runs. This then means I cannot reliably assert for a number of DB entries when my test inserts something into the DB for example, since all the entries from the previous run would still be hanging around.

Am I missing something obvious here or is this expected behaviour? If so, is there a way to get the number of runs hypothesis has done as a variable to use inside the test?

Cowfish answered 30/8, 2017 at 9:26 Comment(0)
A
3

I'm afraid you're a bit stuck and there isn't currently any good solution to this problem.

The way Hypothesis needs to work (which is the source of a lot of its improvements over pytest-quickcheck) doesn't meet pytest's assumptions about test execution. The problem is mostly on the pytest side - the current pytest fixture system has some very baked in assumptions about how you run a test that do not play well with taking control of the test execution, and the last time I tried to work around this I ended up sinking about a week of work into it before giving up and basically saying that either something needs to change on the pytest side or someone needs to fund this work if it's going to get any better.

Aney answered 1/9, 2017 at 8:8 Comment(1)
I see. Thank you for taking the time to answer. I am currently managing to work around this, by taking this into account in my asserts. In fact, it also has some advantages to it. :-) By the way, is there perhaps a way to get the number of runs hypothesis has done as a variable to use inside the test? Thanks again!Cowfish

© 2022 - 2024 — McMap. All rights reserved.