结合 httpretty 与 pytest tmpdir
combine httpretty with pytest tmpdir
以下 pytest-test 使用 httpretty 来模拟请求。它将获取的数据写入文件:
import requests
import httpretty
import json
from os import listdir
from os.path import join
@httpretty.activate
def test_write_file_from_datasource():
tmpdir = './mytestdir'
# mock the connection
concert_url = 'http://apis.is/concerts'
httpretty.register_uri(httpretty.GET, concert_url,
body = json.dumps({'results': []}),
content_type='application/json')
# fetch data
concerts = requests.get(concert_url).json()
# write data
with open(join(tmpdir, 'concerts.json'), 'w') as json_data:
json.dump(concerts, json_data, indent=2)
assert len(listdir(tmpdir)) == 1
我现在想做的是利用 pytest tmpdir 功能。为了达到这个目的,我写了一个这样的测试(导入与上面相同):
@httpretty.activate
def test_write_file_from_datasource_failing(tmpdir):
tmpdir = str(tmpdir)
# mock the connection
concert_url = 'http://apis.is/concerts'
httpretty.register_uri(httpretty.GET, concert_url,
body = json.dumps({'results': []}),
content_type='application/json')
# fetch data
concerts = requests.get(concert_url).json()
# write data
with open(join(tmpdir, 'concerts.json'), 'w') as json_data:
json.dump(concerts, json_data, indent=2)
assert len(listdir(tmpdir)) == 1
失败了,因为httpretty装饰器似乎对附加参数有问题:
TypeError: test_write_file_from_datasource_failing() takes exactly 1 argument (0 given)
任何想法,如何解决这个问题?
看来这个装饰器不能很好地与pytest的funcargs一起工作。
我看到的唯一解决方案是手动调用 httprertty.enable()
和 httpretty.disable()
方法。
或创建夹具:
@pytest.yield_fixture
def http_pretty_mock():
httpretty.enable()
yield
httpretty.disable()
def test_write_file_from_datasource_failing(http_pretty_mock, tmpdir):
tmpdir = str(tmpdir)
# mock the connection
以下 pytest-test 使用 httpretty 来模拟请求。它将获取的数据写入文件:
import requests
import httpretty
import json
from os import listdir
from os.path import join
@httpretty.activate
def test_write_file_from_datasource():
tmpdir = './mytestdir'
# mock the connection
concert_url = 'http://apis.is/concerts'
httpretty.register_uri(httpretty.GET, concert_url,
body = json.dumps({'results': []}),
content_type='application/json')
# fetch data
concerts = requests.get(concert_url).json()
# write data
with open(join(tmpdir, 'concerts.json'), 'w') as json_data:
json.dump(concerts, json_data, indent=2)
assert len(listdir(tmpdir)) == 1
我现在想做的是利用 pytest tmpdir 功能。为了达到这个目的,我写了一个这样的测试(导入与上面相同):
@httpretty.activate
def test_write_file_from_datasource_failing(tmpdir):
tmpdir = str(tmpdir)
# mock the connection
concert_url = 'http://apis.is/concerts'
httpretty.register_uri(httpretty.GET, concert_url,
body = json.dumps({'results': []}),
content_type='application/json')
# fetch data
concerts = requests.get(concert_url).json()
# write data
with open(join(tmpdir, 'concerts.json'), 'w') as json_data:
json.dump(concerts, json_data, indent=2)
assert len(listdir(tmpdir)) == 1
失败了,因为httpretty装饰器似乎对附加参数有问题:
TypeError: test_write_file_from_datasource_failing() takes exactly 1 argument (0 given)
任何想法,如何解决这个问题?
看来这个装饰器不能很好地与pytest的funcargs一起工作。
我看到的唯一解决方案是手动调用 httprertty.enable()
和 httpretty.disable()
方法。
或创建夹具:
@pytest.yield_fixture
def http_pretty_mock():
httpretty.enable()
yield
httpretty.disable()
def test_write_file_from_datasource_failing(http_pretty_mock, tmpdir):
tmpdir = str(tmpdir)
# mock the connection