我从官方示例中获取了代码:
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/