微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在测试中使用可调用对象时,Pytest/nose 会抛出错误“缺少所需的位置参数”

如何解决在测试中使用可调用对象时,Pytest/nose 会抛出错误“缺少所需的位置参数”

我正在尝试使用 setup() 对我的方法 pytest 进行单元测试。但是在当前的测试中,nose.py 会抛出一个 TypeError: setup() missing 1 required positional argument

setup_config.py(来自 setupmodule)

def setup(config):
      config.name = "prod"

run.py(来自 runmodule)

from typing import Callable

def run(setup_fn: Callable):
      config = get_config()
      setup_fn(config)
      return config

实际测试:test_run.py

from setupmodule.setup_config import setup
from runmodule.run import run
import pytest

def test_setup():
      assert run(setup).name == "prod"

这让我在 _pytest/nose.py 中出现错误

def call_optional(obj,name):
      method = getattr(obj,name,None)
      isfixture = hasattr(method,"_pytestfixturefunction")
      if method is not None and not isfixture and callable(method):
            # If there's any problems allow the exception to raise rather than
            # silently ignoring them
>           method()

E           TypeError: setup() missing 1 required positional argument: 'config'

我幼稚的猜测是 pytest 认为 setup一个测试方法,因此当我将它作为可调用参数传递给 run() 时尝试调用它,但失败了,因为我们'不要向它传递任何参数。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。