python - 通过 IDLE gui 运行 pytest

标签 python python-idle pytest

我已经安装了 python 3.4.2,然后运行 ​​pip install -U pytest 来安装 pytest。

我有以下示例脚本:

# content of test_sample.py
import pytest

def func(x):
    return x + 1

def test_answer():
    assert func(3) == 4

当我在 IDLE 中按 F5 时,它似乎没有运行任何东西。如何通过 IDLE 测试我的脚本?

更新
我尝试添加 pytest.main(),但收到错误。

File "C:\Python34\lib\site-packages\_pytest\config.py", line 32, in main
    config = _prepareconfig(args, plugins)
  File "C:\Python34\lib\site-packages\_pytest\config.py", line 85, in _prepareconfig
    pluginmanager=pluginmanager, args=args)
  File "C:\Python34\lib\site-packages\_pytest\core.py", line 413, in __call__
    return self._docall(methods, kwargs)
  File "C:\Python34\lib\site-packages\_pytest\core.py", line 424, in _docall
    res = mc.execute()
  File "C:\Python34\lib\site-packages\_pytest\core.py", line 315, in execute
    res = method(**kwargs)
  File "C:\Python34\lib\site-packages\_pytest\helpconfig.py", line 27, in pytest_cmdline_parse
    config = __multicall__.execute()
  File "C:\Python34\lib\site-packages\_pytest\core.py", line 315, in execute
    res = method(**kwargs)
  File "C:\Python34\lib\site-packages\_pytest\config.py", line 636, in pytest_cmdline_parse
    self.parse(args)
  File "C:\Python34\lib\site-packages\_pytest\config.py", line 747, in parse
    self._preparse(args)
  File "C:\Python34\lib\site-packages\_pytest\config.py", line 719, in _preparse
    args=args, parser=self._parser)
  File "C:\Python34\lib\site-packages\_pytest\core.py", line 413, in __call__
    return self._docall(methods, kwargs)
  File "C:\Python34\lib\site-packages\_pytest\core.py", line 424, in _docall
    res = mc.execute()
  File "C:\Python34\lib\site-packages\_pytest\core.py", line 315, in execute
    res = method(**kwargs)
  File "C:\Python34\lib\site-packages\_pytest\capture.py", line 49, in pytest_load_initial_conftests
    capman.init_capturings()
  File "C:\Python34\lib\site-packages\_pytest\capture.py", line 78, in init_capturings
    self._capturing.start_capturing()
  File "C:\Python34\lib\site-packages\_pytest\capture.py", line 257, in start_capturing
    self.in_.start()
  File "C:\Python34\lib\site-packages\_pytest\capture.py", line 345, in start
    raise ValueError("saved filedescriptor not valid anymore")
ValueError: saved filedescriptor not valid anymore

最佳答案

尝试将 pytest.main() 添加到脚本底部。

checkout :http://pytest.org/latest/usage.html#specifying-tests-selecting-tests对于一些不同的选项和语法。

编辑: 在 IDLE 中运行时,您还需要像这样启动 pytest:pytest.main("--capture=sys")

关于python - 通过 IDLE gui 运行 pytest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26830796/

相关文章:

c++ - 如何重定向 python 解释器输出并将其捕获在 C++ 程序中的字符串中?

Python 使用追加写入元组错误

python - .py 文件从 Linux 到 Windows : is it going to just work?

shell - 在 Python 的 IDLE 中扩展 shell 的宽度

python - pytest中fixture和yield_fixture的区别

python - Pytest 模块未找到错误

pytest - 当 pytest 测试失败时是否可以抑制所有参数化参数的显示

python - 计算一段文本中最常见的标题词

python - 如何去掉字符串中的某些字符? .replace() 不起作用

python - 检测彩色圆圈