当f.result通常被调用时,它返回1。但是,在测试f时,我希望将my_var的返回值传递给f,以便test_stuff中的assert返回True。
是否有一个可简化此功能的pytest功能?
# conftest.py
import pytest
@pytest.fixture()
def my_var():
b = 2
return b# test_stuff.py
import pytest
from mycode import f
def test_stuff():
assert f.result == 2
"""
./tests/test_stuff.py::test_stuff Failed: [undefined]assert 1 == 2
+ where 1 = f.result
def test_stuff():
> assert f.result == 2
E assert 1 == 2
E + where 1 = f.result
tests/test_stuff.py:5: AssertionError
"""# mycode.py
class f:
## Pytest fixture to cast b as my_var here?
def thing(a: int, b: int = None) -> int:
## Or maybe a way to cast b as my_var here?
if not b:
return a
else:
return b
result = thing(1)
if __name__ == "__main__":
print(f.result)发布于 2022-08-02 11:49:01
我认为,当您通过夹具测试函数并使用参数调用f时,它会工作的,如下所示:
def test_stuff(my_var):
f1 = f(my_var)
assert f1.result == my_varhttps://stackoverflow.com/questions/73178425
复制相似问题