Few weeks ago, I written this is post on Missing integer Python exercise. This time, we’re going to give the function I written for that exercise some seriously hard time using the Hypothesis test framework. 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. Here is the official documentation.
Basically, all you need to do, to get setup here, is to make sure you’ve got the Pytest package installed, and of course install Hypothesis using Pip.
pip3 install pytest
pip3 install hypothesis
The function calculating the missing integer will be stored in a file called main.py
and will look like this:
def function_a(input_list):
if not isinstance(input_list, list) or len(input_list) <= 1:
return 1
input_list.append(0)
sorted_input_list = sorted(input_list)
sorted_input_list_iterator = iter(sorted_input_list)
next(sorted_input_list_iterator)
for item in sorted_input_list:
item_plus_1 = item + 1
try:
next_item = next(sorted_input_list_iterator)
if item <= 0 and next_item > 1:
return 1
if next_item not in (item, item_plus_1) and next_item > 0:
return item_plus_1
except StopIteration:
return item_plus_1
The Pytest test runner file for testing the function_a function will be in this project’s subfolder called tests/
, will be named test_with_hypothesis.py
and will look like this:
from hypothesis import given, reject
from hypothesis.strategies import integers, lists
from main import function_a
@given(lists(integers()))
def test_some_stuff(input):
try:
result = function_a(input)
except Exception:
reject()
assert result
assert isinstance(result, int)
This is how the sample project would look like in Pycharm:

Before we run our Pytest tests runner, let’s make sure we get to see all the input combinations from Hypothesis that were used simply by adding print(input, result)
statement to the test function. So we modify the function to look like this:
@given(lists(integers()))
def test_some_stuff(input):
try:
result = function_a(input)
except Exception:
reject()
print(input, result)
assert result
assert isinstance(result, int)
And once we run Pytest, for every input generated by Hypothesis, the 2 assertion statements are executed. These are the various test inputs Hypothesis generated in this test run:
PASSED [100%][] 1
[10] 1
[15294, 531, 0] 1
[0] 1
[26695, -22054, 0] 1
[0] 1
[0] 1
[0] 1
[0] 1
[0] 1
[] 1
[116, 106269151377797082934974390172316169794, 31424, -31, 50, 0] 1
[-4863, -1667, 0] 1
[-16157, 94, -23611, -1, 78, -1014034003, 11426, -6491, 0] 1
[-16157, -23611, -23611, -1, 78, -1014034003, 11426, -6491, 0] 1
[738, -14781, 69, 22090, 1, 29916, -49250859699495267074789931210936954004, -73, -2532, 99, 26325, 65, 11363, 3521813628522614813, -32587, 44, -43, 0] 2
[6650071126684864406, -18406, 1342450230, 82, 63971691232398135368522975465193238990, 0] 1
[-21690, -112, 28400, 0] 1
[1332272385158754537, -19666, 79, 42, 27240, -4035472154411183264, 0] 1
[-3007, -17202, -106, 5948, 10666, 0] 1
[-3007, -17202, -106, 5948, 10666, 0] 1
[86, -66, -1137478758, -41, -23100, 0] 1
[-9379, 27594, 13088, 0] 1
[-9379, 27594, 13088, 0] 1
[13088, 27594, 13088, 0] 1
[-31209, -123, 357237431, -15139, 0] 1
[-31209, -123, 357237431, -15139, 0] 1
[-61, 2310027603164367674, -17914, 105, 111, -12391, -15144, 1731392719, -3872260813340282230, 0] 1
[-25836, 75655317988143660925603206215277718983, -9609, 750173150, 31681, 98, 0] 1
[256, -58851512, 26308, 0] 1
[26825] 1
[26825, 26825, 26850, 0] 1
[26825, 26850, 26850, 0] 1
[26825, 26850, 26850, 0] 1
[15803, -29698, -29505, -7, -39, 822791089, 69, -20596, -1658, -93, 27573, 0] 1
[-72, -82, 30093, 0] 1
[-72] 1
[20506, -40, 0] 1
[-16964, 24527, 22686, 88, 80, 9291, -71, 23320, -10751, 0] 1
[84800811309668880829811978997916166776, -72, -1540788812, 0] 1
[-63] 1
[-2246, 52, -87, -259, 884110608, -33, 0] 1
[-28] 1
[-23969, 1594091873301085806, 0] 1
[531261904, -22115, 25045, 21939, -283, -71823575265068420995799035873944096879, 428551993317954714, -20680, -14239, 16344, -9261, 1097744801, 4489, 20184, 55, 26, -9, -2561702789346947892, 1795642808, 27611, 935014051, 26094, -28, 785159553338897973, -23540, 19652, -6444212787438133303, -4815, 162979844072243057210062703177497391349, -97, 0] 1
[13215] 1
[13215, 13215, -30735, 5437043399508957559, -665758433, 0] 1
[13215, -665758433, -30735, 5437043399508957559, -665758433, 0] 1
[-665758433, -665758433, -30735, 5437043399508957559, -665758433, 0] 1
[1352133560, 85, -15290, 14067, -27071, 5442723013430277027, -127, 17773, 7368170948219587529, -14731, 89, 64, 50022797994927627053737428777938319704, 62, -28497, 0] 1
[0] 1
[-8359914706234088707] 1
[-8359914706234088707, -8359914706234088707, -26, 0] 1
[-8359914706234088707, -8359914706234088707, -1909620066562526127, -8005, 101, 4753, -24324, 0] 1
[-8359914706234088707, -1909620066562526127, -1909620066562526127, -8005, 101, 4753, -24324, 0] 1
[-8359914706234088707, -5038, 73447561143034809623942017177250973719, 18, -21385, -29883, 5327, 22, 1571, -18811, -47, -12420, -3198671763051887895, 0] 1
[-8359914706234088707, 22, 73447561143034809623942017177250973719, 18, -21385, -29883, 5327, 22, 1571, -18811, -47, -12420, -3198671763051887895, 0] 1
[-5926811867032019664, -17444, -5814788238586876888, 0] 1
[-5926811867032019664, 2, 0] 1
[21056, 8389120, 0] 1
[21056, 8389120, 0] 1
[-5926737672302264448] 1
[-5926737672302264448, -5926737672302264448, 0] 1
[-25825, 1902, -29, -2790, 0] 1
[1902, 1902, -29, -2790, 0] 1
[-24659, 28894, -352105788, -94, 2520187554308692200, 16122, -69, -22, -4609402082944272128, 0] 1
[-24659, 28894, -352105788, -94, 2520187554308692200, 16122, -69, -22, -4609402082944272128, 0] 1
[23226, 463278862, -28060, 99, -84, -23181, -5910842997000785862, 23925, 109, 32532, 30878, -31876, 0] 1
[23226, 463278862, -28060, 99, -84, -23181, -5910842997000785862, 23925, 109, 32532, 30878, -31876, 0] 1
[10845, -72, -128891632, -23, 0] 1
[10845, 1, 0] 2
[10845, 1, 58, 0] 2
[10845, 58, 58, 0] 1
[256, -1, -4652328488159891767070554708652156414, 0] 1
[256] 1
[31253, -6588299761822078497, 104, -119, 835, 11823, -1885221123, 5993, 15292, 20335, 19257, 0] 1
[452313367, 4734, 21998, 1265607759790469680, -3446, -25231, -782650966, -14685, 0] 1
[21010, 31459, 19172, 86, 0] 1
[210, -122, -74, 0] 1
[-1190, -3388, 0] 1
[-3388, -3388, 0] 1
[-3388, 13, 31962, 99, -25147, 31928, -1836987774, 24439, 0] 1
[-3388, -3328, 0] 1
[512, -3, -100, -90, 0] 1
[512, -100, -100, -90, 0] 1
[-2842741521326321115391965608576135341] 1
[-2842741521326321115391965608576135341, -12637, -7013452532555885562, -97, 31511, 0] 1
[-32538, -7214, -6662, 1072, 0] 1
[-119389789869000676161737041481982141754, -55, -17743, -110, 90, -18157, -33, -5421, 0] 1
[6184606076545796580, 3787879747147418944, -79, 1105, 0] 1
[6184606076545796580, 3787879747147418944, -79, 1105, 0] 1
[3787879747147418944, 3787879747147418944, -79, 1105, 0] 1
[74, 3524868262113989158, -27605, 23, -60738829120480306099612123430163860640, -6022, 1143426831, -52, 2, -1075777159704476303, 13943, 30488, 32055, -23503, 0] 1
[-22368, 67498080928536154904002613360106515583, 0] 1
[67498080928536154904002613360106515583, 67498080928536154904002613360106515583, 0] 1
[67498080928536154904002613360106515583] 1
[23400] 1
[-623804689, 20689, 0] 1
[-8552579] 1
[-230274] 1