Sunday, 15 September 2013

Unit testing for "FileNotFoundError" in python -



Unit testing for "FileNotFoundError" in python -

i have next code , wanted untitest when given function raises "filenotfounderror

def get_token(): try: auth = get_auth() # function returns auth ,if file exists else throws "filenotfounderror except filenotfounderror: auth= create_auth() homecoming auth

i having problem figuring out how test status raises "filenotfounderror" , doesn't phone call create_auth.

any hint appreciated

thanks

in unit test you'd need mock get_auth function , cause raise filenotfounderror using .side_effect attribute:

@mock.patch('path.to.my.file.get_auth') def test_my_test(self, mock_get_auth): mock_get_auth.side_effect = filenotfounderror

you can test whether create_auth called:

@mock.patch('path.to.my.file.create_auth') @mock.patch('path.to.my.file.get_auth') def test_my_test(self, mock_get_auth, mock_create_auth): mock_get_auth.side_effect = filenotfounderror get_token() self.asserttrue(mock_create_auth.called)

python unit-testing python-3.x mocking

No comments:

Post a Comment