mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-10 09:35:34 +00:00
reorganize tests, start using pytest
This commit is contained in:
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
@@ -5,7 +5,7 @@ import vcr
|
|||||||
from vcr.cassette import Cassette
|
from vcr.cassette import Cassette
|
||||||
import urllib2
|
import urllib2
|
||||||
from urllib import urlencode
|
from urllib import urlencode
|
||||||
import json
|
from utils import assert_httpbin_responses_equal
|
||||||
|
|
||||||
TEST_CASSETTE_FILE = 'cassettes/test_req.yaml'
|
TEST_CASSETTE_FILE = 'cassettes/test_req.yaml'
|
||||||
|
|
||||||
@@ -17,11 +17,6 @@ class TestHttpRequest(unittest.TestCase):
|
|||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def strip_origin(self, body):
|
|
||||||
body = json.loads(body)
|
|
||||||
del body['origin']
|
|
||||||
return body
|
|
||||||
|
|
||||||
def test_response_code(self):
|
def test_response_code(self):
|
||||||
code = urllib2.urlopen('http://httpbin.org/').getcode()
|
code = urllib2.urlopen('http://httpbin.org/').getcode()
|
||||||
with vcr.use_cassette(TEST_CASSETTE_FILE):
|
with vcr.use_cassette(TEST_CASSETTE_FILE):
|
||||||
@@ -42,18 +37,16 @@ class TestHttpRequest(unittest.TestCase):
|
|||||||
def test_multiple_requests(self):
|
def test_multiple_requests(self):
|
||||||
body1 = urllib2.urlopen('http://httpbin.org/').read()
|
body1 = urllib2.urlopen('http://httpbin.org/').read()
|
||||||
body2 = urllib2.urlopen('http://httpbin.org/get').read()
|
body2 = urllib2.urlopen('http://httpbin.org/get').read()
|
||||||
body2 = self.strip_origin(body2)
|
|
||||||
with vcr.use_cassette(TEST_CASSETTE_FILE):
|
with vcr.use_cassette(TEST_CASSETTE_FILE):
|
||||||
self.assertEqual(body1, urllib2.urlopen('http://httpbin.org/').read())
|
self.assertEqual(body1, urllib2.urlopen('http://httpbin.org/').read())
|
||||||
new_body2 = urllib2.urlopen('http://httpbin.org/get').read()
|
new_body2 = urllib2.urlopen('http://httpbin.org/get').read()
|
||||||
new_body2 = self.strip_origin(new_body2)
|
|
||||||
self.assertEqual(body2, new_body2)
|
assert_httpbin_responses_equal(body2, new_body2)
|
||||||
|
|
||||||
self.assertEqual(body1, urllib2.urlopen('http://httpbin.org/').read())
|
self.assertEqual(body1, urllib2.urlopen('http://httpbin.org/').read())
|
||||||
new_body2 = urllib2.urlopen('http://httpbin.org/get').read()
|
new_body2 = urllib2.urlopen('http://httpbin.org/get').read()
|
||||||
new_body2 = self.strip_origin(new_body2)
|
|
||||||
self.assertEqual(body2, new_body2)
|
|
||||||
|
|
||||||
|
assert_httpbin_responses_equal(body2, new_body2)
|
||||||
|
|
||||||
class TestHttps(unittest.TestCase):
|
class TestHttps(unittest.TestCase):
|
||||||
|
|
||||||
@@ -109,10 +102,6 @@ class TestCassette(unittest.TestCase):
|
|||||||
self.assertEqual(c1.requests, c2.requests)
|
self.assertEqual(c1.requests, c2.requests)
|
||||||
self.assertEqual(c1.responses, c2.responses)
|
self.assertEqual(c1.responses, c2.responses)
|
||||||
|
|
||||||
try:
|
|
||||||
from test_requests import *
|
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
@@ -2,18 +2,20 @@
|
|||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
import vcr
|
import vcr
|
||||||
import requests
|
import pytest
|
||||||
|
from utils import assert_httpbin_responses_equal
|
||||||
|
|
||||||
|
requests = pytest.importorskip("requests")
|
||||||
|
|
||||||
TEST_CASSETTE_FILE = 'cassettes/test_req.yaml'
|
TEST_CASSETTE_FILE = 'cassettes/test_req.yaml'
|
||||||
|
|
||||||
|
|
||||||
class TestRequestsGet(unittest.TestCase):
|
class TestRequestsGet(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.unmolested_response = requests.get('http://httpbin.org/')
|
self.unmolested_response = requests.get('http://httpbin.org/get')
|
||||||
with vcr.use_cassette(TEST_CASSETTE_FILE):
|
with vcr.use_cassette(TEST_CASSETTE_FILE):
|
||||||
self.initial_response = requests.get('http://httpbin.org/')
|
self.initial_response = requests.get('http://httpbin.org/get')
|
||||||
self.cached_response = requests.get('http://httpbin.org/')
|
self.cached_response = requests.get('http://httpbin.org/get')
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
try:
|
try:
|
||||||
@@ -34,10 +36,10 @@ class TestRequestsGet(unittest.TestCase):
|
|||||||
self.assertEqual(self.unmolested_response.headers['content-type'], self.cached_response.headers['content-type'])
|
self.assertEqual(self.unmolested_response.headers['content-type'], self.cached_response.headers['content-type'])
|
||||||
|
|
||||||
def test_initial_response_text(self):
|
def test_initial_response_text(self):
|
||||||
self.assertEqual(self.unmolested_response.text, self.initial_response.text)
|
assert_httpbin_responses_equal(self.unmolested_response.text, self.initial_response.text)
|
||||||
|
|
||||||
def test_cached_response_text(self):
|
def test_cached_response_text(self):
|
||||||
self.assertEqual(self.unmolested_response.text, self.cached_response.text)
|
assert_httpbin_responses_equal(self.unmolested_response.text, self.cached_response.text)
|
||||||
|
|
||||||
|
|
||||||
class TestRequestsAuth(unittest.TestCase):
|
class TestRequestsAuth(unittest.TestCase):
|
||||||
@@ -64,7 +66,6 @@ class TestRequestsAuth(unittest.TestCase):
|
|||||||
auth_fail_cached = requests.get('https://httpbin.org/basic-auth/user/passwd', auth=('user', 'passwdzzz'))
|
auth_fail_cached = requests.get('https://httpbin.org/basic-auth/user/passwd', auth=('user', 'passwdzzz'))
|
||||||
self.assertNotEqual(self.unmolested_response.status_code, auth_fail_cached.status_code)
|
self.assertNotEqual(self.unmolested_response.status_code, auth_fail_cached.status_code)
|
||||||
|
|
||||||
|
|
||||||
class TestRequestsPost(unittest.TestCase):
|
class TestRequestsPost(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@@ -81,10 +82,10 @@ class TestRequestsPost(unittest.TestCase):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def test_initial_post_response_text(self):
|
def test_initial_post_response_text(self):
|
||||||
self.assertEqual(self.unmolested_response.text, self.initial_response.text)
|
assert_httpbin_responses_equal(self.unmolested_response.text, self.initial_response.text)
|
||||||
|
|
||||||
def test_cached_post_response_text(self):
|
def test_cached_post_response_text(self):
|
||||||
self.assertEqual(self.unmolested_response.text, self.cached_response.text)
|
assert_httpbin_responses_equal(self.unmolested_response.text, self.cached_response.text)
|
||||||
|
|
||||||
|
|
||||||
class TestRequestsHTTPS(unittest.TestCase):
|
class TestRequestsHTTPS(unittest.TestCase):
|
||||||
@@ -103,7 +104,7 @@ class TestRequestsHTTPS(unittest.TestCase):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def test_initial_https_response_text(self):
|
def test_initial_https_response_text(self):
|
||||||
self.assertEqual(self.unmolested_response.text, self.initial_response.text)
|
assert_httpbin_responses_equal(self.unmolested_response.text, self.initial_response.text)
|
||||||
|
|
||||||
def test_cached_https_response_text(self):
|
def test_cached_https_response_text(self):
|
||||||
self.assertEqual(self.unmolested_response.text, self.cached_response.text)
|
assert_httpbin_responses_equal(self.unmolested_response.text, self.cached_response.text)
|
||||||
13
tests/utils.py
Normal file
13
tests/utils.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
def assert_httpbin_responses_equal(body1, body2):
|
||||||
|
"""
|
||||||
|
httpbin.org returns a different `origin` header
|
||||||
|
each time, so strip this out since it makes testing
|
||||||
|
difficult.
|
||||||
|
"""
|
||||||
|
body1, body2 = json.loads(body1), json.loads(body2)
|
||||||
|
del body1['origin']
|
||||||
|
del body2['origin']
|
||||||
|
assert body1 == body2
|
||||||
|
|
||||||
6
tox.ini
6
tox.ini
@@ -8,24 +8,28 @@ envlist = py26, py27, pypy, py26requests, py27requests, pypyrequests
|
|||||||
|
|
||||||
[testenv]
|
[testenv]
|
||||||
commands =
|
commands =
|
||||||
python test.py
|
py.test
|
||||||
deps =
|
deps =
|
||||||
|
pytest
|
||||||
PyYAML
|
PyYAML
|
||||||
|
|
||||||
[testenv:py26requests]
|
[testenv:py26requests]
|
||||||
basepython = python2.6
|
basepython = python2.6
|
||||||
deps =
|
deps =
|
||||||
|
pytest
|
||||||
PyYAML
|
PyYAML
|
||||||
requests
|
requests
|
||||||
|
|
||||||
[testenv:py27requests]
|
[testenv:py27requests]
|
||||||
basepython = python2.7
|
basepython = python2.7
|
||||||
deps =
|
deps =
|
||||||
|
pytest
|
||||||
PyYAML
|
PyYAML
|
||||||
requests
|
requests
|
||||||
|
|
||||||
[testenv:pypyrequests]
|
[testenv:pypyrequests]
|
||||||
basepython = pypy
|
basepython = pypy
|
||||||
deps =
|
deps =
|
||||||
|
pytest
|
||||||
PyYAML
|
PyYAML
|
||||||
requests
|
requests
|
||||||
|
|||||||
Reference in New Issue
Block a user