I have the below Python function code :
class ValidateHotelStars(AbsValidator):
@staticmethod
def validate(stars):
try:
star = int(stars)
if star < 0 or star > 5:
return False
else:
return True
except ValueError:
return False
Now i am trying to write a unit test for this function using pytest :
def test_star_value_less_than_zero():
invalid_stars = [-5, 4, "Five"]
assert not all(list(map(ValidateHotelStars.validate, invalid_stars)))
Now this passes with the below output :
(venv) C:\Users\SUSUBHAT.ORADEV\PycharmProjects\hoteldatareader>python -m pytest
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.9.1, py-1.7.0, pluggy-0.8.0
rootdir: C:\Users\SUSUBHAT.ORADEV\PycharmProjects\hoteldatareader, inifile:
collected 1 item
hoteldatareader\tests\test_validators.py . [100%]
========================== 1 passed in 0.31 seconds ===========================
This should ideally fail right since when i run this from the Python REPL:
>>> from hoteldatareader.fieldvalidators.validate_hotel_stars import ValidateHotelStars
>>> invalid_stars = [-5, 4, "Five"]
>>> list(map(ValidateHotelStars.validate, invalid_stars))
[False, True, False]
So not all of them are False .
I think the syntax of my test is not correct.
Can someone please correct me here .
Many thanks