python - Pytest 抛出 typerror : positional arguments missing

标签 python python-3.x typeerror pytest

我从官方示例中获取了代码:

import pytest
@pytest.mark.parametrize("test_input,expected", [
    ("3+5", 8),
    ("2+4", 6),
    ("6*9", 42),
])
def test_eval(test_input, expected):
    assert eval(test_input) == expected

但它给出了错误:
.E
======================================================================
ERROR: test_config.test_eval
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Anaconda3\lib\site-packages\nose\case.py", line 197, in runTest
    self.test(*self.arg)
TypeError: test_eval() missing 2 required positional arguments: 'test_input' and 'expected'

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (errors=1)

Error
Traceback (most recent call last):
  File "C:\Anaconda3\lib\unittest\case.py", line 59, in testPartExecutor
    yield
  File "C:\Anaconda3\lib\unittest\case.py", line 601, in run
    testMethod()
  File "C:\Anaconda3\lib\site-packages\nose\case.py", line 197, in runTest
    self.test(*self.arg)
TypeError: test_eval() missing 2 required positional arguments: 'test_input' and 'expected'


Process finished with exit code 1


pytest version 5.0.1

这里有什么问题?

最佳答案

我认为这是 OP 借用代码的地方:https://docs.pytest.org/en/latest/parametrize.html

您需要将文件重命名为 test_expectation.py(根据文档)并在命令行中运行命令“pytest”(根据文档),这将返回预期的输出:

jonas@mint-vm /media/sf_Downloads $ mv mytest.py test_expectation.py
jonas@mint-vm /media/sf_Downloads $ pytest
============================================================================================================ test session starts =============================================================================================================
platform linux -- Python 3.5.2, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: /media/sf_Downloads
collected 3 items                                                                                                                                                                                                                            

test_expectation.py ..F                                                                                                                                                                                                                [100%]

================================================================================================================== FAILURES ==================================================================================================================
_____________________________________________________________________________________________________________ test_eval[6*9-42] ______________________________________________________________________________________________________________

test_input = '6*9', expected = 42

    @pytest.mark.parametrize("test_input, expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)])
    def test_eval(test_input, expected):
>       assert eval(test_input) == expected
E       AssertionError: assert 54 == 42
E        +  where 54 = eval('6*9')

test_expectation.py:4: AssertionError
===================================================================================================== 1 failed, 2 passed in 0.54 seconds ======================================================================

希望这可以帮助。

命令“pytest”将在当前目录中运行任何带有前缀“test”的python文件(以.py扩展名结尾的文件)。

关于python - Pytest 抛出 typerror : positional arguments missing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57511160/

相关文章:

javascript - 未捕获( promise )TypeError : Request failed

python - 类型错误:+ 不支持的操作数类型: 'int' 和 'NoneType'

python - 类型错误:write() 参数必须是 str,而不是 bytes,UTF-16

python - 在Python中提取数据帧的groupby值

python - 使用 Google 云函数写入 Google 云 Spanner 时出错

python - 当我们传递魔术模拟对象时,python 中的 List 方法返回空列表

python - 如何使用\u 转义码对 Python 3 字符串进行编码?

python - 将python pickled对象存储到mysql + django中

python - 如何在 linux 中安装 python 的 sphinx 文档生成器?

python - 训练回归网络时的 NaN 损失