我可以选择性地运行test_function_1
,覆盖我的conftest.py
装置中的仪表1
def test_function_1(instrumentation: dict[str, float]) -> None:
assert instrumentation['a'] > instrumentation['b']
def test_function_2(capsys) -> None:
print("Hello, pytest!")
captured = capsys.readouterr()
assert captured.out == "Hello, pytest!\n"
当我尝试调用时test_function_2
,我不知道如何传递capsys
给它2:
import tests
import pytest # <--- doesn't help ...
def test_callee_with_instrumentation():
tests.test_function_1({'a': 110, 'b': 55, 'g': 6000})
def test_callee_with_capsys():
# tests.test_function_2() # <--- TypeError: test_function_2() missing 1 required positional argument: 'capsys'
# tests.test_function_2(capsys) # <--- NameError: name 'capsys' is not defined
# tests.test_function_2(pytest.capsys) # <--- AttributeError: module 'pytest' has no attribute 'capsys'
pass
test_callee_with_instrumentation()
test_callee_with_capsys()
我很确定这些conftest.py
装置是无关紧要的,但为了完整起见:
import pytest
@pytest.fixture(scope='function')
def instrumentation():
return { 'a': 800, 'b': 620, 'c': 44 }
1在我的实际代码中,是众多capsys
参数之一。
2这里有类似的问题。在我看来,这不是重复的,因为我问的是编程运行测试,而不是的正确含义capsys
。