There are some application domains(e.g. GameDev) in which a lot of functions should be created using random values for producing their output. One of examples is presented below:
def generate_key(monster_key_drop_coef):
key_letters = string.ascii_uppercase
rand = random.random()
if monster_key_drop_coef < rand:
return None
button = {}
button["type"] = random.choice([1,2,3])
button["letter"] = random.choice(key_letters)
return button
This function generates the item's drop based on several random operations. The problem appears if you want to validate automatically correctness of this function. Generated values are not deterministic and writing of regression tests seems to be impossible.
My questions are:
One of useful unit-tests is presented below:
def test_generate_key():
button_list = []
for _ in range(1, 1000):
button_list.append(generate_key(0.2))
is_all_none = True
is_not_none = False
for key in button_list:
is_all_none &= (key is None)
is_not_none |= (key is not None)
assert is_all_none == False
assert is_not_none == True
It validates function signature, cover all lines of function's code(good probability) and will pass in 99.999% cases. Also validated that function produces some drop at least one from 1000 and sometimes doesn't generate drop. 0.2 is probability of an item's drop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With