TypeError: parametrize() 的 ‘indirect’ 参数传递了多个数值

huangapple go评论55阅读模式
英文:

TypeError: parametrize() got multiple values for argument 'indirect'

问题

我正在尝试通过从外部函数获取值来对我的 pytest 进行参数化,我的方法如下:

import pytest
from Utilities.MiscUtils import generate_random_email_for_registration
from Utilities.TextFileUtils import read_text_file

generate_random_email_for_registration()


这里的 `generate_random_email_for_registration()` 将生成随机的电子邮件地址并将其存储到文本文件中。

而我的 pytest 标记如下:

@pytest.mark.parametrize("email", "password", [(read_text_file("email"), "Test@12345")], indirect=["email"])


`(read_text_file("email")` 这个方法将从 email.txt 文件中获取值。

现在在执行时,我遇到以下错误:

/Users/tp/AndroidPythonAppium/lib/python3.9/site-packages/pluggy/_hooks.py:265: in call
return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
/Users/tp/AndroidPythonAppium/lib/python3.9/site-packages/pluggy/_manager.py:80: in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
/Users/tp/AndroidPythonAppium/lib/python3.9/site-packages/_pytest/python.py:272: in pytest_pycollect_makeitem
return list(collector._genfunctions(name, obj))
/Users/tp/AndroidPythonAppium/lib/python3.9/site-packages/_pytest/python.py:499: in _genfunctions
self.ihook.pytest_generate_tests.call_extra(methods, dict(metafunc=metafunc))
/Users/tp/AndroidPythonAppium/lib/python3.9/site-packages/pluggy/_hooks.py:292: in call_extra
return self(**kwargs)
/Users/tp/AndroidPythonAppium/lib/python3.9/site-packages/pluggy/_hooks.py:265: in call
return self._hookexec(this.name, self.get_hookimpls(), kwargs, firstresult)
/Users/tp/AndroidPythonAppium/lib/python3.9/site-packages/pluggy/_manager.py:80: in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
/Users/tp/AndroidPythonAppium/lib/python3.9/site-packages/_pytest/python.py:151: in pytest_generate_tests
metafunc.parametrize(*marker.args, **marker.kwargs, _param_mark=marker)
E TypeError: parametrize() got multiple values for argument 'indirect'


我需要知道我在哪里出错,因为这也可以不使用 pytest.parametrize 来完成,但我正在使用良好的实践方式。
英文:

I am trying to parametrize my pytest, by getting value from external function, my method is like that:

import pytest
from Utilities.MiscUtils import generate_random_email_for_registration
from Utilities.TextFileUtils import read_text_file


generate_random_email_for_registration()

Here generate_random_email_for_registration() will be generating random email address and storing it to Text file.

and my pytest mark goes like that:

@pytest.mark.parametrize("email", "password", [(read_text_file("email"), "Test@12345")], indirect=["email"])

(read_text_file("email") as this method, will be getting my value from email.txt file.

now while I execute , I am getting following error:

/Users/tp/AndroidPythonAppium/lib/python3.9/site-packages/pluggy/_hooks.py:265: in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
/Users/tp/AndroidPythonAppium/lib/python3.9/site-packages/pluggy/_manager.py:80: in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
/Users/tp/AndroidPythonAppium/lib/python3.9/site-packages/_pytest/python.py:272: in pytest_pycollect_makeitem
    return list(collector._genfunctions(name, obj))
/Users/tp/AndroidPythonAppium/lib/python3.9/site-packages/_pytest/python.py:499: in _genfunctions
    self.ihook.pytest_generate_tests.call_extra(methods, dict(metafunc=metafunc))
/Users/tp/AndroidPythonAppium/lib/python3.9/site-packages/pluggy/_hooks.py:292: in call_extra
    return self(**kwargs)
/Users/tp/AndroidPythonAppium/lib/python3.9/site-packages/pluggy/_hooks.py:265: in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
/Users/tp/AndroidPythonAppium/lib/python3.9/site-packages/pluggy/_manager.py:80: in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
/Users/tp/AndroidPythonAppium/lib/python3.9/site-packages/_pytest/python.py:151: in pytest_generate_tests
    metafunc.parametrize(*marker.args, **marker.kwargs, _param_mark=marker)
E   TypeError: parametrize() got multiple values for argument 'indirect'

I need to know, where I am making mistake, as this can be done by not using pytest.parametrize as well, but I am using good practice here.

答案1

得分: 1

通过删除间接参数的位置参数,并将其仅作为关键字参数传递应该解决这个问题:

@pytest.mark.parametrize("email, password", [(read_text_file("email"), "Test@12345")], indirect={"email"})
英文:

Removing the positional argument for indirect and passing it as a keyword argument only should fix this:

@pytest.mark.parametrize("email, password", [(read_text_file("email"), "Test@12345")], indirect={"email"})

huangapple
  • 本文由 发表于 2023年2月19日 09:29:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定