标签: pytest

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万
查看次数

TypeError:使用 pytest 夹具时缺少 1 个必需的位置参数

我已经在文件中编写了测试类,并且尝试使用pytest 固定装置,这样我就不必在每个测试函数中创建相同的输入数据。下面是最小的工作示例。

import unittest
import pytest

@pytest.fixture
def base_value():
    return 5

class Test(unittest.TestCase):

    def test_add_two(self, base_value):
        result = base_value + 2
        self.assertEqual(result, 7, "Result doesn't match")
Run Code Online (Sandbox Code Playgroud)

但是,当我使用 pytest-3 对此进行测试时,出现以下错误:

类型错误:test_add_two() 缺少 1 个必需的位置参数:'base_value'

这让我感到困惑,因为 base_value 明确作为 的参数之一给出test_add_two。非常感谢任何帮助。

python fixtures pytest python-3.x

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

VSCode:如何中断正在运行的 Python 测试?

我正在使用 VSCode Test Explorer 来运行我的 Python 单元测试。我的代码中有一个错误,我的测试方法永远不会完成。

我如何中断我的测试?我找不到如何使用 GUI 来完成此操作。我必须关闭 VSCode 才能中断它。

我正在使用 pytest 框架。

python testing pytest visual-studio-code

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

使用 pytest 的“间接夹具”错误。怎么了?

 def fatorial(n):
    if n <= 1:
        return 1
    else:
        return n*fatorial(n - 1)


import pytest

@pytest.mark.parametrize("entrada","esperado",[
    (0,1),
    (1,1),
    (2,2),
    (3,6),
    (4,24),
    (5,120)
])

def testa_fatorial(entrada,esperado):
    assert fatorial(entrada)  == esperado
Run Code Online (Sandbox Code Playgroud)

错误:

 ERROR collecting Fatorial_pytest.py ____________________________________________________________________
In testa_fatorial: indirect fixture '(0, 1)' doesn't exist
Run Code Online (Sandbox Code Playgroud)

我不知道为什么我得到了“间接夹具”。知道吗?我使用的是 python 3.7 和 windows 10 64 位。

python pytest python-3.x python-3.7

20
推荐指数
1
解决办法
6256
查看次数

使用docstrings列出py.test中的测试

这是一个简单的测试文件:

# test_single.py
def test_addition():
    "Two plus two is still four"
    assert 2 + 2 == 4

def test_addition2():
    "One plus one is still two"
    assert 1 + 1 == 2
Run Code Online (Sandbox Code Playgroud)

py.test中的默认输出就像

$ py.test test_single.py -v
[...]
test_single.py::test_addition PASSED
test_single.py::test_addition2 PASSED
Run Code Online (Sandbox Code Playgroud)

我想拥有

Two plus two is still four PASSED
One plus one is still two PASSED
Run Code Online (Sandbox Code Playgroud)

即使用docstrings作为测试的描述.

我试图在conftest.py文件中使用自定义:

import pytest

@pytest.mark.tryfirst
def pytest_runtest_makereport(item, call, __multicall__):
    # execute all other hooks to obtain the report object
    rep = __multicall__.execute() …
Run Code Online (Sandbox Code Playgroud)

python unit-testing pytest

19
推荐指数
4
解决办法
6389
查看次数

有没有办法改变pytest的.cache目录的位置?

我需要能够将pytest的.cache目录的位置更改为env变量WORKSPACE.由于服务器权限超出我的控制范围,我遇到此错误,因为我的用户没有权限在运行测试的目录中写入:

py.error.EACCES: [Permission denied]: open('/path/to/restricted/directory/tests/.cache/v/cache/lastfailed', 'w')
Run Code Online (Sandbox Code Playgroud)

有没有办法将.cache目录的路径设置为环境变量WORKSPACE?

python directory caching pytest

19
推荐指数
3
解决办法
6672
查看次数

在jupyter笔记本中运行pytest测试函数

我正在编写关于python测试选项的演示文稿,我想要演示的技术之一是pytest.我打算用jupyter/ipython笔记本做演示.理想情况下,我希望能够在单元格中定义测试函数,然后使用pytest运行该函数; 这样我就可以展示pytest等的个性化特征.

有现成的方法吗?我在pytest api中看到的所有交互功能似乎都不符合要求,但我当然不是pytest专家.我很乐意写一个插件来做这个,所以关于这个方向的最佳方法的任何建议都会很棒.

我确实看到了pytest-ipynb,但它似乎没有做我需要的.如果评估错误,关于如何使用它的建议也会很好.

python pytest jupyter jupyter-notebook

19
推荐指数
3
解决办法
5611
查看次数

pytest fixture从外部范围重新定义名称[pylint]

我正在学习pytest,我用pylint把代码搞定了.但是pylint仍抱怨:
W0621: Redefining name %r from outer scope (line %s)

对于pytest的以下示例:

# test_wallet.py

@pytest.fixture
def my_wallet():
    '''Returns a Wallet instance with a zero balance'''
    return Wallet()

@pytest.mark.parametrize("earned,spent,expected", [
    (30, 10, 20),
    (20, 2, 18),
])
def test_transactions(my_wallet, earned, spent, expected):
    my_wallet.add_cash(earned)
    my_wallet.spend_cash(spent)
    assert my_wallet.balance == expected
Run Code Online (Sandbox Code Playgroud)

my_wallet从外部范围重新定义名称.

我找到了解决方法,_为夹具名称添加前缀:_my_wallet.

如果我想将灯具保存在与功能相同的文件中,那么最佳做法是什么?

  1. 预装所有灯具_
  2. 禁用此pylint检查以进行测试?
  3. 更好的建议?

python fixture pylint pytest

19
推荐指数
4
解决办法
5134
查看次数

使用 mocker 修补 pytest

我安装了pytest-mock并使用了一个模拟程序,我试图像补丁一样运行,但是我得到“类型错误:需要一个有效的目标来修补。你提供了‘返回 a + b’”

# test_capitalize.py
import time


def sum(a, b):
    time.sleep(10)
    return a + b

def test_sum(mocker):
    mocker.patch('return a + b');
    assertEqual(sum(2, 3), 9)
Run Code Online (Sandbox Code Playgroud)

python mocking pytest

19
推荐指数
1
解决办法
4万
查看次数