1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-09 01:03:24 +00:00

Use pytest-httpbin

This will help the test flakiness and speed up test runs.
This commit is contained in:
Kevin McCarthy
2015-04-11 13:07:17 -10:00
parent cfc483a08d
commit 4e36997e1a
20 changed files with 380 additions and 369 deletions

View File

@@ -3,62 +3,62 @@ import vcr
from six.moves.urllib.request import urlopen
def test_once_record_mode(tmpdir):
def test_once_record_mode(tmpdir, httpbin):
testfile = str(tmpdir.join('recordmode.yml'))
with vcr.use_cassette(testfile, record_mode="once"):
# cassette file doesn't exist, so create.
urlopen('http://httpbin.org/').read()
urlopen(httpbin.url).read()
with vcr.use_cassette(testfile, record_mode="once"):
# make the same request again
urlopen('http://httpbin.org/').read()
urlopen(httpbin.url).read()
# the first time, it's played from the cassette.
# but, try to access something else from the same cassette, and an
# exception is raised.
with pytest.raises(Exception):
urlopen('http://httpbin.org/get').read()
urlopen(httpbin.url + '/get').read()
def test_once_record_mode_two_times(tmpdir):
def test_once_record_mode_two_times(tmpdir, httpbin):
testfile = str(tmpdir.join('recordmode.yml'))
with vcr.use_cassette(testfile, record_mode="once"):
# get two of the same file
urlopen('http://httpbin.org/').read()
urlopen('http://httpbin.org/').read()
urlopen(httpbin.url).read()
urlopen(httpbin.url).read()
with vcr.use_cassette(testfile, record_mode="once"):
# do it again
urlopen('http://httpbin.org/').read()
urlopen('http://httpbin.org/').read()
urlopen(httpbin.url).read()
urlopen(httpbin.url).read()
def test_once_mode_three_times(tmpdir):
def test_once_mode_three_times(tmpdir, httpbin):
testfile = str(tmpdir.join('recordmode.yml'))
with vcr.use_cassette(testfile, record_mode="once"):
# get three of the same file
urlopen('http://httpbin.org/').read()
urlopen('http://httpbin.org/').read()
urlopen('http://httpbin.org/').read()
urlopen(httpbin.url).read()
urlopen(httpbin.url).read()
urlopen(httpbin.url).read()
def test_new_episodes_record_mode(tmpdir):
def test_new_episodes_record_mode(tmpdir, httpbin):
testfile = str(tmpdir.join('recordmode.yml'))
with vcr.use_cassette(testfile, record_mode="new_episodes"):
# cassette file doesn't exist, so create.
urlopen('http://httpbin.org/').read()
urlopen(httpbin.url).read()
with vcr.use_cassette(testfile, record_mode="new_episodes") as cass:
# make the same request again
urlopen('http://httpbin.org/').read()
urlopen(httpbin.url).read()
# all responses have been played
assert cass.all_played
# in the "new_episodes" record mode, we can add more requests to
# a cassette without repurcussions.
urlopen('http://httpbin.org/get').read()
urlopen(httpbin.url + '/get').read()
# one of the responses has been played
assert cass.play_count == 1
@@ -71,9 +71,9 @@ def test_new_episodes_record_mode(tmpdir):
assert len(cass.responses) == 2
def test_new_episodes_record_mode_two_times(tmpdir):
def test_new_episodes_record_mode_two_times(tmpdir, httpbin):
testfile = str(tmpdir.join('recordmode.yml'))
url = 'http://httpbin.org/bytes/1024'
url = httpbin.url + '/bytes/1024'
with vcr.use_cassette(testfile, record_mode="new_episodes"):
# cassette file doesn't exist, so create.
original_first_response = urlopen(url).read()
@@ -96,12 +96,12 @@ def test_new_episodes_record_mode_two_times(tmpdir):
urlopen(url).read()
def test_all_record_mode(tmpdir):
def test_all_record_mode(tmpdir, httpbin):
testfile = str(tmpdir.join('recordmode.yml'))
with vcr.use_cassette(testfile, record_mode="all"):
# cassette file doesn't exist, so create.
urlopen('http://httpbin.org/').read()
urlopen(httpbin.url).read()
with vcr.use_cassette(testfile, record_mode="all") as cass:
# make the same request again
@@ -109,7 +109,7 @@ def test_all_record_mode(tmpdir):
# in the "all" record mode, we can add more requests to
# a cassette without repurcussions.
urlopen('http://httpbin.org/get').read()
urlopen(httpbin.url + '/get').read()
# The cassette was never actually played, even though it existed.
# that's because, in "all" mode, the requests all go directly to
@@ -117,26 +117,26 @@ def test_all_record_mode(tmpdir):
assert cass.play_count == 0
def test_none_record_mode(tmpdir):
def test_none_record_mode(tmpdir, httpbin):
# Cassette file doesn't exist, yet we are trying to make a request.
# raise hell.
testfile = str(tmpdir.join('recordmode.yml'))
with vcr.use_cassette(testfile, record_mode="none"):
with pytest.raises(Exception):
urlopen('http://httpbin.org/').read()
urlopen(httpbin.url).read()
def test_none_record_mode_with_existing_cassette(tmpdir):
def test_none_record_mode_with_existing_cassette(tmpdir, httpbin):
# create a cassette file
testfile = str(tmpdir.join('recordmode.yml'))
with vcr.use_cassette(testfile, record_mode="all"):
urlopen('http://httpbin.org/').read()
urlopen(httpbin.url).read()
# play from cassette file
with vcr.use_cassette(testfile, record_mode="none") as cass:
urlopen('http://httpbin.org/').read()
urlopen(httpbin.url).read()
assert cass.play_count == 1
# but if I try to hit the net, raise an exception.
with pytest.raises(Exception):
urlopen('http://httpbin.org/get').read()
urlopen(httpbin.url + '/get').read()