Make tests a part of your app ↦
Here’s a pretty useful idea for library authors and their users: there are better ways to test your code!
I give three examples of how user projects can be self-tested without actually writing any real test cases by the end-user. One is hypothetical about django
and two examples are real and working: featuring deal
and dry-python/returns
. A brief example with deal
:
import deal
@deal.pre(lambda a, b: a >= 0 and b >= 0)
@deal.raises(ZeroDivisionError) # this function can raise if `b=0`, it is ok
def div(a: int, b: int) -> float:
if a > 50: # Custom, in real life this would be a bug in our logic:
raise Exception('Oh no! Bug happened!')
return a / b
This bug can be automatically found by writing a single line of test code: test_div = deal.cases(div)
. As easy as it gets! From this article you will learn:
- How to use property-based testing on the next level
- How a simple decorator
@deal.pre(lambda a, b: a >= 0 and b >= 0)
can help you to generate hundreds of test cases with almost no effort - What “Monad laws as values” is all about and how
dry-python/returns
helps its users to build their own monads
I really like this idea! And I would appreciate your feedback on it.
Discussion
Sign in or Join to comment or subscribe