标签: pytest

如何用py.test monkeypatch python的datetime.datetime.now?

我需要测试使用的功能datetime.datetime.now().最简单的方法是什么?

python datetime mocking pytest

23
推荐指数
5
解决办法
8490
查看次数

如何针对不同版本的python运行py.test?

是否可以在py.test没有插件(如xdist)或tox?的情况下运行不同版本的python ?

python pytest

22
推荐指数
3
解决办法
9909
查看次数

PyCharm对整个文件无影响?

是否可以在PyCharm中禁用对整个文件的检查?

这需要的原因是在处理py.test时.它使用看似影子函数参数的灯具,同时导致未解析的引用.例如:

from myfixtures import user  # Unused import statement warning

def test_is_awesome(user):  # Shadows name 'user' from outer scope warning
    assert user.is_awesome()
Run Code Online (Sandbox Code Playgroud)

py.test还有其他警告,例如使用pytest.py中pytest.raises()的"无法找到引用'引发'".

也许还有另一种方法来解决这些问题?也许我错误地使用了py.test?

python pytest pycharm

22
推荐指数
2
解决办法
6781
查看次数

当py.test静默挂起时该怎么办?

在使用py.test时,我有一些测试可以正常运行SQLite但在我切换到Postgresql时静默挂起.我该如何调试这样的东西?是否有"详细"模式我可以运行我的测试,或设置断点?更一般地说,当pytest无声地停止时,标准的攻击计划是什么?我已经尝试使用pytest-timeout,并使用$ py.test --timeout = 300运行测试,但测试仍然挂起,屏幕上没有任何活动

sqlite postgresql timeout hang pytest

22
推荐指数
4
解决办法
9425
查看次数

如何让PyCharm显示来自pytest的整个错误差异?

我正在使用Pycharm来运行我的pytest单元测试.我正在测试REST API,所以我经常需要验证JSON块.当测试失败时,我会看到这样的事情:

FAILED
test_document_api.py:0 (test_create_documents)
{'items': [{'i...ages': 1, ...} != {'items': [{'...ages': 1, ...}

Expected :{'items': [{'...ages': 1, ...}
Actual   :{'items': [{'i...ages': 1, ...}
 <Click to see difference>
Run Code Online (Sandbox Code Playgroud)

当我点击"点击查看差异"链接时,大部分差异将转换为省略点,就像这样

Pycharm比较差异省略

这没用,因为它没有告诉我什么是不同的.对于大于单个字符串或数字的任何差异,我会得到此行为.

我假设Pycharm和/或pytest试图消除大输出差异的无信息部分.然而,它在这里过于咄咄逼人并且一切都在消失.

我如何让Pycharm和/或pytest向我展示完整的区别?

我已经尝试添加-vvv到pytest的附加参数,但这没有任何效果.


从原始帖子开始,我验证了当我从命令行运行单元测试时,我看到了相同的行为.所以这是pytest而不是Pycharm的问题.

看了我到目前为止得到的答案,我猜我真正问的是"在pytest中是否可以在maxDiff=None不改变测试源代码的情况下进行设置?" 我从阅读有关pytest的印象是,-vv开关是控制此设置的,但似乎并非如此.

python pytest pycharm difference

22
推荐指数
1
解决办法
1586
查看次数

无法收集测试类“TestMain”,因为它有一个 __init__ 构造函数(来自:Login.py)

我正在尝试从 cmd 运行 pytest ,当我执行pytest Login.py时,我得到了这个结果:

================================================== warnings summary ===================================================
C:\automation\test\test.py:36
  C:\automation\test\test.py:36: PytestCollectionWarning: cannot collect test class 'TestMain' because it has a __init__ constructor (from: Login.py)
    class TestBase():

-- Docs: https://docs.pytest.org/en/latest/warnings.html
================================================= 1 warning in 59.69s =================================================
Run Code Online (Sandbox Code Playgroud)

在 Login.py 中,我有一个导入,我认为这可能是问题所在,但我需要该导入来进行测试。有谁知道如何解决这一问题?

automation pytest pycharm

22
推荐指数
3
解决办法
2万
查看次数

如何在py.test中显示警告

我只是运行py.test我的代码并得到以下输出:

================== 6 passed, 2 pytest-warnings in 40.79 seconds =======================
Run Code Online (Sandbox Code Playgroud)

但是,我看不出有什么py.test想提醒我的.如何打开警告输出到控制台?

py.test --help给我--strict一面旗帜:

- 严格模式下运行pytest,警告成为错误.

但是我只是想看看输出,而不是让我的测试失败.

我检查了pytest.org这个问题,但他们只关心在python中声明警告,而不是在命令行上显示警告.

python testing django pytest pytest-django

21
推荐指数
1
解决办法
6288
查看次数

pytest使用fixtures作为参数化中的参数

我想使用fixtures作为pytest.mark.parametrize的参数或具有相同结果的东西.

例如:

import pytest
import my_package

@pytest.fixture
def dir1_fixture():
    return '/dir1'

@pytest.fixture
def dir2_fixture():
    return '/dir2'

@pytest.parametrize('dirname, expected', [(dir1_fixture, 'expected1'), (dir2_fixture, 'expected2')]
def test_directory_command(dirname, expected):
    result = my_package.directory_command(dirname)
    assert result == expected
Run Code Online (Sandbox Code Playgroud)

夹具参数的问题在于夹具的每个参数都会在每次使用时运行,但我不希望这样.我希望能够根据测试选择使用哪种灯具.

python pytest

21
推荐指数
4
解决办法
5209
查看次数

pytest - 抑制来自特定 3rd 方模块的 DeprecationWarning

当我运行 pytest 时,我收到了来自 3rd 方库的一些弃用警告。我想知道我自己代码中的任何弃用警告,而不是与另一个 3rd 方库捆绑在一起的库的供应商副本。

这个答案有助于让我中途到达那里。如果我像这样运行 pytest: $ pytest ./tests/我得到:

$ pytest ./tests/
============================= test session starts ==============================
platform linux -- Python 3.7.4, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: /home/whlt/repos/tj-image-resizer/tests, inifile: pytest.ini
collected 5 items                                                              

tests/test_file1.py .                                                   [ 20%]
tests/test_file2.py ....                                                [100%]

=============================== warnings summary ===============================
/home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1
/home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1
  /home/whlt/.local/lib/python3.7/site-packages/botocore/vendored/requests/packages/urllib3/_collections.py:1: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
    from collections import Mapping, MutableMapping

-- Docs: https://docs.pytest.org/en/latest/warnings.html …
Run Code Online (Sandbox Code Playgroud)

python pytest

21
推荐指数
2
解决办法
2970
查看次数

PytestUnknownMarkWarning:未知 pytest.mark.xxx - 这是一个错字吗?

我有一个名为 test.py 的文件,其中包含以下代码:

import pytest

@pytest.mark.webtest
def test_http_request():
    pass

class TestClass:
    def test_method(self):
        pass
Run Code Online (Sandbox Code Playgroud)

pytest -s test.py 通过但给出以下警告:

pytest -s test.py
=============================== test session starts ============================
platform linux -- Python 3.7.3, pytest-5.2.4, py-1.8.0, pluggy-0.13.1
rootdir: /home/user
collected 2 items

test.py ..

=============================== warnings summary ===============================
anaconda3/lib/python3.7/site-packages/_pytest/mark/structures.py:325
  ~/anaconda3/lib/python3.7/site-packages/_pytest/mark/structures.py:325:
    PytestUnknownMarkWarning: Unknown pytest.mark.webtest - is this a typo?  You can register
    custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
=============================== 2 passed, 1 warnings in 0.03s …
Run Code Online (Sandbox Code Playgroud)

pytest python-3.x anaconda3 pytest-markers

21
推荐指数
2
解决办法
1万
查看次数