如何使用pytest aiohttp在python中运行异步http测试
我想测试对我不拥有的api的http调用(集成测试)。我创建了一个带有异步函数的类,该类使用aiohttp进行http调用。然后我制作了一个测试用例来运行这个函数并断言它的状态 测试/测试coinbase.py:如何使用pytest aiohttp在python中运行异步http测试,python,python-3.x,pytest,pytest-aiohttp,Python,Python 3.x,Pytest,Pytest Aiohttp,我想测试对我不拥有的api的http调用(集成测试)。我创建了一个带有异步函数的类,该类使用aiohttp进行http调用。然后我制作了一个测试用例来运行这个函数并断言它的状态 测试/测试coinbase.py: import json import os from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop from src.coinbase import CoinbaseService class Coinba
import json
import os
from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop
from src.coinbase import CoinbaseService
class CoinbaseTestCase():
async def test_get_current_user(self):
coinbase_service = CoinbaseService(os.getenv('COINBASE_APIURL'), os.getenv('COINBASE_APIKEY'), os.getenv('COINBASE_APISECRET'))
status, user = await coinbase_service.show_current_user()
assert status == 200
import json, hmac, hashlib, time
import aiohttp
class CoinbaseService:
def __init__(self, API_URL, API_KEY, API_SECRET):
self.API_URL = API_URL
self.API_KEY = API_KEY
self.API_SECRET = API_SECRET
def generateHeaders(self, method, path_url, body = ''):
timestamp = str(int(time.time()))
message = timestamp + method + path_url + body
signature = hmac.new(self.API_SECRET, message, hashlib.sha256).hexdigest()
headers = {
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': self.API_KEY
}
return headers
async def show_current_user(self):
path_url = 'user'
headers = self.generateHeaders('GET', path_url)
async with aiohttp.ClientSession() as session:
async with session.get(path_url, headers=headers) as response:
status = response.status
user = await response.json()
return status, user
src/coinbase.py:
import json
import os
from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop
from src.coinbase import CoinbaseService
class CoinbaseTestCase():
async def test_get_current_user(self):
coinbase_service = CoinbaseService(os.getenv('COINBASE_APIURL'), os.getenv('COINBASE_APIKEY'), os.getenv('COINBASE_APISECRET'))
status, user = await coinbase_service.show_current_user()
assert status == 200
import json, hmac, hashlib, time
import aiohttp
class CoinbaseService:
def __init__(self, API_URL, API_KEY, API_SECRET):
self.API_URL = API_URL
self.API_KEY = API_KEY
self.API_SECRET = API_SECRET
def generateHeaders(self, method, path_url, body = ''):
timestamp = str(int(time.time()))
message = timestamp + method + path_url + body
signature = hmac.new(self.API_SECRET, message, hashlib.sha256).hexdigest()
headers = {
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': self.API_KEY
}
return headers
async def show_current_user(self):
path_url = 'user'
headers = self.generateHeaders('GET', path_url)
async with aiohttp.ClientSession() as session:
async with session.get(path_url, headers=headers) as response:
status = response.status
user = await response.json()
return status, user
当我在我的根项目中运行以下命令时,我得到以下结果
平台darwin——Python 3.7.4、pytest-5.3.5、py-1.8.1、Plugy-0.13.1
rootdir:/Users/helloworld/Krypto
插件:aiohttp-0.3.0
收集了0个项目
套餐
[packages]
aiohttp = "*"
backoff = "*"
requests = "*"
asyncio = "*"
vadersentiment = "*"
python-dateutil = "*"
pytest = "*"
pytest-aiohttp = "*"
CoinbaseTestCase
与被视为测试类的默认pytest
命名规则不匹配,请参阅。将类重命名为smth,如TestCoinbase
,或pytest
不支持运行异步测试。安装:
并使用asyncio
标记标记异步测试:
class TestCoinbase:
@pytest.mark.asyncio
async def test_get_current_user(self):
await coinbase_service.show_current_user()
...