Pytest 抛出 typerror:位置参数丢失

Roc*_*etq 5 python typeerror pytest python-3.x

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

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
Run Code Online (Sandbox Code Playgroud)

但它给出了错误:

.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
Run Code Online (Sandbox Code Playgroud)

这里有什么问题?

Jon*_*s M 3

我认为这是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 ======================================================================
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。

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