pytest-django无法使用,因为PytestDeprecationWarning:`funcargnames`属性是`fixturenames`的别名,

时间:2019-08-07 09:53:19

标签: python pytest pytest-django

我正在尝试使用Pytest-django = 3.5.1进行测试。但是,发生此错误:

[PytestDeprecationWarning: The `funcargnames` attribute was an alias for `fixturenames`, since pytest 2.3 - use the newer attribute instead.]

错误:

>       if "live_server" not in request.funcargnames:
E       pytest.PytestDeprecationWarning: The `funcargnames` attribute was an alias for `fixturenames`, since pytest 2.3 - use the newer attribute instead.

>/usr/local/lib/python3.7/site-packages/pytest_django/fixtures.py:397: PytestDeprecationWarning

我发现了这个问题。它似乎与我的问题相似,所以我尝试了一下。 PytestDeprecationWarning at test setup: the funcargnames attribute was an alias for fixturenames

但这并不能解决问题。

此外,我将“ funcargnames”更改为“ fixturenames”,但错误仍未解决。发生相同的错误。换句话说,即使它具有funcargnames或Fixtures名称,也会发生相同的错误。

@pytest.fixture(autouse=True, scope="function")
def _live_server_helper(request):
    """Helper to make live_server work, internal to pytest-django.

    This helper will dynamically request the transactional_db fixture
    for a test which uses the live_server fixture.  This allows the
    server and test to access the database without having to mark
    this explicitly which is handy since it is usually required and
    matches the Django behaviour.

    The separate helper is required since live_server can not request
    transactional_db directly since it is session scoped instead of
    function-scoped.

    It will also override settings only for the duration of the test.
    """
    if "live_server" not in request.fixturenames:
        return

    request.getfixturevalue("transactional_db")

    live_server = request.getfixturevalue("live_server")
    live_server._live_server_modified_settings.enable()
    request.addfinalizer(live_server._live_server_modified_settings.disable)

Pytest-django能够执行而没有任何错误。有什么建议为什么会发生这种情况?

2 个答案:

答案 0 :(得分:0)

我是你的同事。我和你有同样的错误。
如您所知,这是因为pytest-django版本不是最新的。

您使用此命令升级了pytest-django。
$ pip install --upgrade "pytest-django>=3.5"

但是您使用docker-compose。
您必须在docker容器中升级pytest-django。

后端项目中的Dockerfile调用了requirements-dev.txt文件。
有一个指定模块pytest-django==3.4.8

您只需更改为pytest-django==3.5.1

答案 1 :(得分:0)

多亏了同事,我才得以解决错误。

错误原因: 当我进入运行docker-compose exec backend bash的容器时,该容器中pytest-django的版本未升级。

解决方案: 用docker-compose down完成后。接下来,在不使用docker-compose build —no-cache的情况下进行构建。然后将pytest-django版本升级到3.5.1并解决了错误。