This time I want to discuss a pretty useful idea for library authors and their users: there are better ways to test your code!
I give three examples (in #python, but the idea itself applies to almost any language) 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.Short 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 monadsI really like this idea! And I would appreciate your feedback on it.
Link: https://sobolevn.me/2021/02/make-tests-a-part-of-your-app