首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将pytest夹具传递给非pytest代码

将pytest夹具传递给非pytest代码
EN

Stack Overflow用户
提问于 2022-07-30 19:14:37
回答 1查看 55关注 0票数 -1

f.result通常被调用时,它返回1。但是,在测试f时,我希望将my_var的返回值传递给f,以便test_stuff中的assert返回True

是否有一个可简化此功能的pytest功能?

代码语言:javascript
复制
# conftest.py
import pytest
@pytest.fixture()
def my_var():
  b = 2
  return b
代码语言:javascript
复制
# 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
"""
代码语言:javascript
复制
# 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)
EN

回答 1

Stack Overflow用户

发布于 2022-08-02 11:49:01

我认为,当您通过夹具测试函数并使用参数调用f时,它会工作的,如下所示:

代码语言:javascript
复制
def test_stuff(my_var):
  f1 = f(my_var)
  assert f1.result == my_var
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73178425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档