How should unit testing work with external resources? What is proper approach?
Question:
I have recently learned about unit testing and know that you shouldn’t be unit testing external resources. So, that leads me to a problem for testing a simple function that I want to rewrite to the standards of unit testing.
An example of the function is below for simplicity sake:
def longTask(somearg):
# Check somearg
# Begin infinite loop
# Check remote resource loop based on somearg
# Write results to a database or file
# Check to break loop
# Cleanup upon end
I want to make sure the above code has been unit tested (now that I know of unit testing).
My main confusion comes from the fact of how can I unit test simple functions that are making external resource calls when you aren’t supposed to unit test external resources?
Note: I have read the other posts about this on SO but those do not answer my question.
Answers:
Have you considered using something like a Fake object [1] to test your code. You could provide a wrapper/interface for the external resource and for your tests, use a version of that wrapper/interface that offers behavior to facilitate testing.
[1] https://en.wikipedia.org/wiki/Mock_object#Mocks.2C_fakes_and_stubs
My main confusion comes from the fact of how can I unit test
simple functions that are making external resource
calls when you aren’t supposed to unit test external resources?
What I usually do in cases such as this is use a mock of some sort. There are some excellent mock packages for python, for instance http://www.voidspace.org.uk/python/mock that make these kind of substitutions of a test object for a real object much easier
For example,
def test_au(self):
user_id=124
def apple(req):
return 104
with patch('pyramid.security.authenticated_userid', return_value=user_id):
authenticated_userid = apple
self.assertEqual("104", authenticated_userid(self.request).__str__())
patch
is a method imported from mock
. It alters the behaviour of other packages within the scope it is given.
In this example, the predefined library method for authenticated_userid
is imported with from pyramid.security import authenticated_userid
and works within the pyramid framework. To test that it is returning the correct value, after my setup function has run, I can "override" the method during the test with apple
.
I have recently learned about unit testing and know that you shouldn’t be unit testing external resources. So, that leads me to a problem for testing a simple function that I want to rewrite to the standards of unit testing.
An example of the function is below for simplicity sake:
def longTask(somearg):
# Check somearg
# Begin infinite loop
# Check remote resource loop based on somearg
# Write results to a database or file
# Check to break loop
# Cleanup upon end
I want to make sure the above code has been unit tested (now that I know of unit testing).
My main confusion comes from the fact of how can I unit test simple functions that are making external resource calls when you aren’t supposed to unit test external resources?
Note: I have read the other posts about this on SO but those do not answer my question.
Have you considered using something like a Fake object [1] to test your code. You could provide a wrapper/interface for the external resource and for your tests, use a version of that wrapper/interface that offers behavior to facilitate testing.
[1] https://en.wikipedia.org/wiki/Mock_object#Mocks.2C_fakes_and_stubs
My main confusion comes from the fact of how can I unit test
simple functions that are making external resource
calls when you aren’t supposed to unit test external resources?
What I usually do in cases such as this is use a mock of some sort. There are some excellent mock packages for python, for instance http://www.voidspace.org.uk/python/mock that make these kind of substitutions of a test object for a real object much easier
For example,
def test_au(self):
user_id=124
def apple(req):
return 104
with patch('pyramid.security.authenticated_userid', return_value=user_id):
authenticated_userid = apple
self.assertEqual("104", authenticated_userid(self.request).__str__())
patch
is a method imported from mock
. It alters the behaviour of other packages within the scope it is given.
In this example, the predefined library method for authenticated_userid
is imported with from pyramid.security import authenticated_userid
and works within the pyramid framework. To test that it is returning the correct value, after my setup function has run, I can "override" the method during the test with apple
.