From the course: Testing Python Data Science Code
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Writing strategies - Python Tutorial
From the course: Testing Python Data Science Code
Writing strategies
- [Instructor] Sometimes you'd like Hypothesis to generate random values for your own types. There are ways to extend strategies. There are several external ones already built in, and you can write your own. In our example, we're going to generate sales information with a custom strategy. So, here is our code. We have a dataclass for a Sale, which has sku, identifier of the object being sold, and a price. And then we do sku_sales. So, we create a dictionary by_sku and go over the sales, accumulate, and return. Basically a GroupBy. And here is our test. This time we're going to import the composite strategy from Hypothesis. So, we use the composite decorator on our sales, which gets one parameter, which is draw. Draw is the random number generator that is being used by Hypothesis. So, we say that the sku is going to draw from a regular expression of a hexadecimal number. So A to F and 0 to 9, eight of them.…