Pytest抛出typerror:位置参数丢失

时间:2019-08-15 14:26:00

标签: 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

这是怎么了?

2 个答案:

答案 0 :(得分:2)

我认为这是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扩展名的文件)。

答案 1 :(得分:1)

这是 Pytest 的示例,而您正在通过 Nose 运行它,这是一个与Python不同(且不兼容)的测试框架。要在Pytest中运行测试,您可以从命令行调用它:

pytest test_config.py

如果尝试从IDE启动测试,则需要对其进行配置以使用正确的测试运行程序。例如,in PyCharm:设置-> Python集成工具->默认测试运行器。然后更新现有的运行配置。