【问题标题】:Pytest throws typerror: positional arguments missingPytest 抛出类型错误:缺少位置参数
【发布时间】:2019-12-21 23:35:24
【问题描述】:

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

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

这里有什么问题?

【问题讨论】:

  • 将所有参数化的东西放在同一行时,它有没有可能起作用?
  • 无法重现。使用 pytest 5.0.1 可以正常工作

标签: python python-3.x typeerror pytest


【解决方案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扩展名结尾的文件)。

【讨论】:

  • 看来你是对的。但是如何在pycarm里面运行呢?
  • 在 Pycharm 中转到工具 -> 运行 'pytest for test_expectation.py_eval' 或 SHIFT+F10 或在代码编辑器中右键单击并运行 'pytest for .....'
【解决方案2】:

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

pytest test_config.py

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

【讨论】:

    猜你喜欢
    • 2017-03-18
    • 2019-11-16
    • 2022-06-28
    • 2022-01-11
    • 2019-05-26
    • 2018-03-28
    • 2012-02-23
    • 1970-01-01
    相关资源
    最近更新 更多