通过传递在conftest.py中定义的@pytest.mark.parametrize中定义的夹具,我得到了以下错误:
pytest --alist="0220,0221" test_1.py -v -s
NameError: name 'alist' is not defined
conftest.py:
def pytest_addoption(parser):
parser.addoption("--alist", action="store")
@pytest.fixture
def alist(request
我一直在试图找到一种方法来同时使用嘲弄装潢师和闷热的capsys,但我无法找到正确的方法。
import pytest
import requests_mock
@requests_mock.mock()
def test_with_mock(m):
pass
def test_with_capsys(capsys):
pass
# how to write a test that works with both?
这是我正在测试的代码:
def test_sum():
test_list=[10,20,30,40,50]
result=sum(test_list)
assert result == 150
我得到的错误是:
Microsoft Windows [Version 10.0.19041.630]
(c) 2020 Microsoft Corporation. All rights reserved.
C:\Users\97155>python -m pytest
==========================================
设置
假设我有conftest.py,其中定义了用于测试的资源
class ClassifyRequest:
pass
class OCRRequest:
pass
@pytest.fixture(params=[ClassifyRequest, OCRRequest])
def api_request(request):
return request.param() # return one of the request instances
然后,在test_api.py中,我使用参数化的夹具来测试服务:
def test_service(api_request
使用Pytest固定装置,我正在寻找一种将设置重写传递给我的应用程序的方法,这样我就可以测试不同的设置,而不必定义不同的安装。
在为Flask创建测试时,我使用了一个常见的模式,即初始化应用程序和数据库,如下所示。请注意,db夹具将app夹具硬编码为参数。
from myapp import create_app
@pytest.fixture
def app():
settings_override = {} # By setting values here, I can pass in different Flask config variables
app = cre