测试设置中的 PytestDeprecationWarning:funcargnames 属性是 fixturenames 的别名
PytestDeprecationWarning at test setup: the funcargnames attribute was an alias for fixturenames
按照 https://testdriven.io/ 上的教程,我创建了一个 websocket 测试来测试我的 websocket 连接:
# tests/test_websockets.py
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.test import Client
from channels.db import database_sync_to_async
from channels.layers import get_channel_layer
from channels.testing import WebsocketCommunicator
from nose.tools import assert_equal, assert_is_none, assert_is_not_none, assert_true
import pytest
from dc_wb.routing import application
from posts.models import Trip
TEST_CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels.layers.InMemoryChannelLayer',
},
}
@database_sync_to_async
def create_user(*,username='rider@example.com',password='pAssw0rd!',group='rider'):
# Create user.
user = get_user_model().objects.create_user(
username=username,
password=password
)
# Create user group.
user_group, _ = Group.objects.get_or_create(name=group)
user.groups.add(user_group)
user.save()
return user
@pytest.mark.asyncio
@pytest.mark.django_db(transaction=True)
class TestWebsockets:
async def test_authorized_user_can_connect(self, settings):
# Use in-memory channel layers for testing.
settings.CHANNEL_LAYERS = TEST_CHANNEL_LAYERS
print(settings.CHANNEL_LAYERS)
# Force authentication to get session ID.
client = Client()
user = await create_user()
client.force_login(user=user)
# Pass session ID in headers to authenticate.
communicator = WebsocketCommunicator(
application=application,
path='/taxi/',
headers=[(
b'cookie',
f'sessionid={client.cookies["sessionid"].value}'.encode('ascii')
)]
)
connected, _ = await communicator.connect()
assert_true(connected)
await communicator.disconnect()
我创建了 pytest.ini
:
[pytest]
DJANGO_SETTINGS_MODULE = dc_wb.settings
python_files = test_websockets.py
但是每当我 运行 pytest
时,我就得到这个错误:
collected 1 item
trips/tests/test_websockets.py E [100%]
===================================================================================================== ERRORS ======================================================================================================
________________________________________________________________________ ERROR at setup of TestWebsockets.test_authorized_user_can_connect ________________________________________________________________________
request = <SubRequest '_django_db_marker' for <Function test_authorized_user_can_connect>>
@pytest.fixture(autouse=True)
def _django_db_marker(request):
"""Implement the django_db marker, internal to pytest-django.
This will dynamically request the ``db``, ``transactional_db`` or
``django_db_reset_sequences`` fixtures as required by the django_db marker.
"""
marker = request.node.get_closest_marker("django_db")
if marker:
transaction, reset_sequences = validate_django_db(marker)
if reset_sequences:
request.getfixturevalue("django_db_reset_sequences")
elif transaction:
> request.getfixturevalue("transactional_db")
../env/lib/python3.7/site-packages/pytest_django/plugin.py:483:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
request = <SubRequest 'transactional_db' for <Function test_authorized_user_can_connect>>, django_db_setup = None, django_db_blocker = <pytest_django.plugin._DatabaseBlocker object at 0x7f743c9e09e8>
@pytest.fixture(scope="function")
def transactional_db(request, django_db_setup, django_db_blocker):
"""Require a django test database with transaction support.
This will re-initialise the django database for each test and is
thus slower than the normal ``db`` fixture.
If you want to use the database with transactions you must request
this resource.
If multiple database fixtures are requested, they take precedence
over each other in the following order (the last one wins): ``db``,
``transactional_db``, ``django_db_reset_sequences``.
"""
> if "django_db_reset_sequences" in request.funcargnames:
E pytest.PytestDeprecationWarning: The `funcargnames` attribute was an alias for `fixturenames`, since pytest 2.3 - use the newer attribute instead.
../env/lib/python3.7/site-packages/pytest_django/fixtures.py:199: PytestDeprecationWarning
================================================================================================ warnings summary =================================================================================================
/tmp/taxi-app/server/env/lib/python3.7/site-packages/nose/importer.py:12
/tmp/taxi-app/server/env/lib/python3.7/site-packages/nose/importer.py:12: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
from imp import find_module, load_module, acquire_lock, release_lock
-- Docs: https://docs.pytest.org/en/latest/warnings.html
======================================================================================= 1 warnings, 1 error in 0.32 seconds =======================================================================================
我完全不熟悉测试等等;我什至不确定它是如何工作的,我已经尝试研究了一下但不理解这些概念并且没有特别的东西可以解决这个问题。
本教程建议安装 pytest-django
的过时版本(3.4.8 与 pytest==5
不兼容)。通过
升级包
$ pip install --upgrade "pytest-django>=3.5"
错误应该会消失。原因是 pytest-django==3.4.8
使用 request.funcargnames
获取夹具名称列表,这在 pytest==5
中已弃用。新版本使用 request.fixturenames
属性,不会引发弃用错误。
按照 https://testdriven.io/ 上的教程,我创建了一个 websocket 测试来测试我的 websocket 连接:
# tests/test_websockets.py
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.test import Client
from channels.db import database_sync_to_async
from channels.layers import get_channel_layer
from channels.testing import WebsocketCommunicator
from nose.tools import assert_equal, assert_is_none, assert_is_not_none, assert_true
import pytest
from dc_wb.routing import application
from posts.models import Trip
TEST_CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels.layers.InMemoryChannelLayer',
},
}
@database_sync_to_async
def create_user(*,username='rider@example.com',password='pAssw0rd!',group='rider'):
# Create user.
user = get_user_model().objects.create_user(
username=username,
password=password
)
# Create user group.
user_group, _ = Group.objects.get_or_create(name=group)
user.groups.add(user_group)
user.save()
return user
@pytest.mark.asyncio
@pytest.mark.django_db(transaction=True)
class TestWebsockets:
async def test_authorized_user_can_connect(self, settings):
# Use in-memory channel layers for testing.
settings.CHANNEL_LAYERS = TEST_CHANNEL_LAYERS
print(settings.CHANNEL_LAYERS)
# Force authentication to get session ID.
client = Client()
user = await create_user()
client.force_login(user=user)
# Pass session ID in headers to authenticate.
communicator = WebsocketCommunicator(
application=application,
path='/taxi/',
headers=[(
b'cookie',
f'sessionid={client.cookies["sessionid"].value}'.encode('ascii')
)]
)
connected, _ = await communicator.connect()
assert_true(connected)
await communicator.disconnect()
我创建了 pytest.ini
:
[pytest]
DJANGO_SETTINGS_MODULE = dc_wb.settings
python_files = test_websockets.py
但是每当我 运行 pytest
时,我就得到这个错误:
collected 1 item
trips/tests/test_websockets.py E [100%]
===================================================================================================== ERRORS ======================================================================================================
________________________________________________________________________ ERROR at setup of TestWebsockets.test_authorized_user_can_connect ________________________________________________________________________
request = <SubRequest '_django_db_marker' for <Function test_authorized_user_can_connect>>
@pytest.fixture(autouse=True)
def _django_db_marker(request):
"""Implement the django_db marker, internal to pytest-django.
This will dynamically request the ``db``, ``transactional_db`` or
``django_db_reset_sequences`` fixtures as required by the django_db marker.
"""
marker = request.node.get_closest_marker("django_db")
if marker:
transaction, reset_sequences = validate_django_db(marker)
if reset_sequences:
request.getfixturevalue("django_db_reset_sequences")
elif transaction:
> request.getfixturevalue("transactional_db")
../env/lib/python3.7/site-packages/pytest_django/plugin.py:483:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
request = <SubRequest 'transactional_db' for <Function test_authorized_user_can_connect>>, django_db_setup = None, django_db_blocker = <pytest_django.plugin._DatabaseBlocker object at 0x7f743c9e09e8>
@pytest.fixture(scope="function")
def transactional_db(request, django_db_setup, django_db_blocker):
"""Require a django test database with transaction support.
This will re-initialise the django database for each test and is
thus slower than the normal ``db`` fixture.
If you want to use the database with transactions you must request
this resource.
If multiple database fixtures are requested, they take precedence
over each other in the following order (the last one wins): ``db``,
``transactional_db``, ``django_db_reset_sequences``.
"""
> if "django_db_reset_sequences" in request.funcargnames:
E pytest.PytestDeprecationWarning: The `funcargnames` attribute was an alias for `fixturenames`, since pytest 2.3 - use the newer attribute instead.
../env/lib/python3.7/site-packages/pytest_django/fixtures.py:199: PytestDeprecationWarning
================================================================================================ warnings summary =================================================================================================
/tmp/taxi-app/server/env/lib/python3.7/site-packages/nose/importer.py:12
/tmp/taxi-app/server/env/lib/python3.7/site-packages/nose/importer.py:12: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
from imp import find_module, load_module, acquire_lock, release_lock
-- Docs: https://docs.pytest.org/en/latest/warnings.html
======================================================================================= 1 warnings, 1 error in 0.32 seconds =======================================================================================
我完全不熟悉测试等等;我什至不确定它是如何工作的,我已经尝试研究了一下但不理解这些概念并且没有特别的东西可以解决这个问题。
本教程建议安装 pytest-django
的过时版本(3.4.8 与 pytest==5
不兼容)。通过
$ pip install --upgrade "pytest-django>=3.5"
错误应该会消失。原因是 pytest-django==3.4.8
使用 request.funcargnames
获取夹具名称列表,这在 pytest==5
中已弃用。新版本使用 request.fixturenames
属性,不会引发弃用错误。