Skip to content

Commit 780b247

Browse files
committed
Switch to vcrpy for tests
1 parent 9126945 commit 780b247

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mock
2+
vcrpy
23
pytest
34
pytest-cov
45
pytest-xdist

test_cfn_resource.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import os
12
import json
2-
import mock
33

4-
import cfn_resource
4+
import vcr
5+
import pytest
56

7+
import cfn_resource
68

79
class FakeLambdaContext(object):
810
def __init__(self, name='Fake', version='LATEST'):
@@ -58,37 +60,43 @@ def wrapper(*args):
5860

5961
### Tests for the wrapper function
6062

61-
62-
@mock.patch('urllib2.urlopen')
63-
def test_client_code_failure(urlmock):
63+
@pytest.fixture
64+
def cassette(request):
65+
recordings_file = os.path.join(
66+
request.fspath.dirname,
67+
'vcr_recordings',
68+
request.function.__name__ + '.yaml'
69+
).replace('test_', '')
70+
with vcr.use_cassette(recordings_file, record_mode='none') as cass:
71+
yield cass
72+
73+
def test_client_code_failure(cassette):
6474
rsrc = cfn_resource.Resource()
6575

6676
@rsrc.delete
6777
def flaky_function(*args):
6878
raise KeyError('Oopsie')
6979

7080
resp = rsrc(base_event.copy(), FakeLambdaContext())
81+
assert resp is None
82+
print(cassette)
7183

72-
args = urlmock.call_args
73-
sent_req = args[0][0]
84+
assert json.loads(cassette.requests[0].body)
7485

75-
reply = json.loads(sent_req.data)
86+
reply = json.loads(cassette.requests[0].body)
7687

88+
assert cassette.requests[0].method == 'PUT'
7789
assert reply['Status'] == cfn_resource.FAILED
7890
assert reply['StackId'] == base_event['StackId']
7991
assert reply['Reason'] == "Exception was raised while handling custom resource"
8092

8193

82-
@mock.patch('urllib2.urlopen')
83-
def test_sends_put_request(urlmock):
94+
def test_sends_put_request(cassette):
8495
rsrc = cfn_resource.Resource()
8596

8697
resp = rsrc(base_event.copy(), FakeLambdaContext())
8798

88-
args = urlmock.call_args
89-
sent_req = args[0][0]
90-
91-
assert sent_req.get_method() == 'PUT'
99+
assert cassette.requests[0].method == 'PUT'
92100

93101
### Tests for the Resource object and its decorator for wrapping
94102
### user handlers

0 commit comments

Comments
 (0)