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.
Testing with hypothesis - Python Tutorial
From the course: Testing Python Data Science Code
Testing with hypothesis
- Let's write a simple test using hypothesis. Here we have our activation function. If n is smaller than zero, we return zero. Otherwise we return n. Now for the test. We import our code, the activation. And then from hypothesis we import two things, we import given, which is a decorator, and from strategies we import floats, which is going to generate a new float. And then we decorate our test function with given floats. Which means our function is going to get a single value which is n, call activation, and make sure that it's bigger or less than zero. Let's run this function. And this function is going to fail. We're going to fail because we have a nan. Nan is not bigger or equal to zero and this is something hypothesis found. I find it really useful when working with floating point. So let's import numpy as np, and then do if not np.isnan of n. Only then we're going to do the check. Okay, now we can rerun the…