This article is a translation of the Welcome to Hypothesis page ! taken from the official manual.
I could not find any useful information on the use of the Hypothesis in Russian except for the speech on December 14. 2017 Alexander Shorin on "Moscow Python Meetup 50"
Hypothesis is a Python library for creating unit tests that are simpler to write and more efficient at startup, detecting boundary cases in code that you wouldn’t think of looking for. It is stable, powerful and easily added to any existing test suite.
The algorithm of his work, allowing you to write tests that claim that something must be true for each case, and not just what you guessed it to think.
A normal unit test is something like the following:
Hypothesis allows you to write tests that look like this:
This is often called property based testing , and has been popularized in the Haskell Quickcheck library .
It works by generating random data that matches your specification and verifying that your function or method still holds and does not fall in this case. If he finds an example where this is not the case, he will take this example and reduce its size, simplifying it until it finds a much smaller example that still causes a problem. Then he will save this example for later, so that as soon as he finds a problem with your code, he will not forget
this in the future.
Writing tests in this form usually consists of a decision about the guarantees by which your code should make make - properties , which should always be true
, regardless of what the world gives you. Examples of such guarantees may include:
assert
).Now you know the basics of what the hypothesis
does, the rest in the documentation will help you understand how and why. It is divided into sections, which you can see in the sidebar (or at the top of the menu, if you are on a mobile phone). But most likely, you want to start with the Quick start guide or Quick Start Guide , which will give you working examples of using hypothesis and a detailed outline of what you need to know to start testing code with it, or check out some of the introductory articles .
Source: https://habr.com/ru/post/354134/
All Articles