Exploring Hypothesis with pytest - Part 1

I found Hypothesis library very intriguing so have started exploring it.  Here are my notes from the first hypothesis exploratory session

What is Hypothesis?
From their webpage - Hypothesis is a Python library for creating unit tests which are simpler to write and more powerful when run, finding edge cases in your code you wouldn’t have thought to look for. It is stable, powerful and easy to add to any existing test suite.
You can read more about it and its claims here -> https://hypothesis.readthedocs.io/en/latest/index.html

I am definitely not a fan of tall claims like - "finding edge cases in your code you wouldn’t have thought to look for"! Anyways I will come to that later on.

For now I wanted to explore its ability to add to any existing test suite. So I picked pytest

What is pytest?
Again from their webpage - The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.
More here -> https://docs.pytest.org/en/latest/

Now I wanted a program to explore this unit test library. So I picked the first practise python from https://www.practicepython.org/exercise/2014/01/29/01-character-input.html

Here is my program for
#Create a program that asks the user to enter their name and their age. Print out a message addressed to them that tells them the year that they will turn 100 years old.

Filename - whenYouAre100.py

Then I started adding some unit tests in pytest for the code. I started with the below checks
  • What happens if I pass 100? expected = 2019
  • What happens if I pass 0? expected = 2119
  • What happens if I pass 101? expected = 2018
  • What happens if I pass a random number like 50? expected = 2069

Filename - test_whenYouAre100.py

On running the tests (command to run the test - pytest -v test_whenYouAre100.py) All tests passed.


Time to bring in hypothesis

Instead of all the above tests I defined just this one test


On running the test (Command to run the test - pytest --hypothesis-show-statistics test_whenYouAre100.py)



It appears that hypothesis has run 100 examples from range min_value=0 to max_value=101. 
I was not sure what those examples were run. Tried to get it to log (its called database in hypothesis) all the examples but for now I couldn't figure it out. So instead I started testing it.

I modified the function in the above code to 


With this change in code if I run the 4 unit tests (the 4 with values, 100, 101, 0 and 50) it will not catch the bug. Hopefully hypothesis can catch it?

So I ran the test written using hypothesis again and boom. The test failed at the value of i = 25.



I then changed self.age to 99 in the program and again the result was a fail 



That was pretty cool. 

So from the first bit of exploring hypothesis it does look pretty handy. Slick, easy to integrate, like how it prints failed tests and I can let a library pick examples for me.

But, I absolutely hate the tall claims. Words like "finding edge cases in your code you wouldn’t have thought to look for". Really?

If I were to run with only 

without integers range defined - I might have had to run this test n times to get the test to catch the bug!

Saying that I do like the library for now. So stay tuned for more notes on it.



Comments

Popular posts from this blog

Exploratory testing, Session based testing, Scripted testing…concertedly

What do you do when you find a bug?

Regression Checks + Regression testing = Regression testing?!