I'm using the stub module (http://ift.tt/1Lkev9l) to monkey patch the requests.get() function on my tests.
On different tests I want requests.get() to return different values, so I use stub.stub() in each test case is required. The problem is: it always returns the same value when running the tests with nosetests (apparently the one of the first run of stub.stub()).
A simplified version of my code would be:
common.py:
import stub
@contextlib.contextmanager
def stubbed(target, replacement):
stub(target, replacement)
try:
yield
finally:
target.unstub()
test_1.py:
def test_something_returns_200():
response = requests.Response()
response.status_code = 200
with stubbed(requests.get, lambda _: response):
assert function_under_test().status_code == 200
test_2.py:
def test_something_returns_500():
response = requests.Response()
response.status_code = 500
with stubbed(requests.get, lambda _: response):
assert function_under_test().status_code == 500
For some reason it will always return 200.
If I try to modify test_something_returns_500() to call requests.get.unstub() before calling stubbed() it fails with:
AttributeError: 'function' object has no attribute 'unstub'
Aucun commentaire:
Enregistrer un commentaire