I want to test the following function using pytest:
def validate_account(number):
try:
number = int(number)
except:
raise ValueError("Please enter a valid account number")
if int(number) not in accounts_dict:
raise Exception("Pleaseeee enter a valid account number.")
return number
accounts_dict
gets items added later on. For unit testing with pytest, how can I mimic this dictionary to test per whether or not a number is in dictionary?
As of now, accounts_dict
is empty. And, when I try to assert validate_account("1") == 1, it's throwing the 'Pleaseeee' exception (not in accounts_dict dictionary). I have googled and tried few different options like accounts_dict = Mock(return_value =n)
, accounts_dict.get('1') != None
etc., but they are not helping. Help will be much appreciated.
If the dict is an argument (as it should) you can do this
def validate_account(number, accounts_dict):
...
def test_validate_account__valid():
assert validate_account("1", {1:True}) == 1
def test_validate_account__invalid():
with pytest.raises(Exception):
validate_account("1", {2:True})