【问题标题】:How to Run Py.test in PyScripter如何在 PyScripter 中运行 Py.test
【发布时间】:2012-11-14 06:52:01
【问题描述】:

我是 py.test 的新手,请告诉我如何在 PyScripter Editor 中运行 py.test。 我尝试了以下方式,但它不起作用。

导入 pytest

定义函数(x): 返回 x + 1

def test_answer(): 断言 func(3) == 5

pytest.main()

在运行上述脚本时,我收到一条错误消息

Traceback (most recent call last):
  File "<module1>", line 10, in <module>
  File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\core.py", line 474, in main
    exitstatus = config.hook.pytest_cmdline_main(config=config)
  File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\core.py", line 422, in __call__
    return self._docall(methods, kwargs)
  File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\core.py", line 433, in _docall
    res = mc.execute()
  File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\core.py", line 351, in execute
    res = method(**kwargs)
  File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\main.py", line 107, in pytest_cmdline_main
    return wrap_session(config, _main)
  File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\main.py", line 92, in wrap_session
    config.pluginmanager.notify_exception(excinfo, config.option)
  File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\core.py", line 285, in notify_exception
    res = self.hook.pytest_internalerror(excrepr=excrepr)
  File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\core.py", line 422, in __call__
    return self._docall(methods, kwargs)
  File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\core.py", line 433, in _docall
    res = mc.execute()
  File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\core.py", line 351, in execute
    res = method(**kwargs)
  File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\terminal.py", line 152, in pytest_internalerror
    self.write_line("INTERNALERROR> " + line)
  File "C:\Python27\lib\site-packages\pytest-2.3.2-py2.7.egg\_pytest\terminal.py", line 140, in write_line
    self._tw.line(line, **markup)
  File "C:\Python27\lib\site-packages\py-1.4.11-py2.7.egg\py\_io\terminalwriter.py", line 181, in line
    self.write(s, **kw)
  File "C:\Python27\lib\site-packages\py-1.4.11-py2.7.egg\py\_io\terminalwriter.py", line 225, in write
    self._file.write(msg)
  File "C:\Python27\lib\site-packages\py-1.4.11-py2.7.egg\py\_io\terminalwriter.py", line 241, in write
    self._writemethod(data)
TypeError: 'AsyncStream' object is not callable

请帮帮我

【问题讨论】:

  • 它是否在 PyScripter 之外运行?你用的是什么版本的 PyScripter?
  • 是的,如果以下面的方式调用脚本“python -m py.test sample.py”,它会在 PyScripter 之外运行。我正在使用 pyscripter 2.5.3
  • 听起来像是 PyScripter 兼容性问题...我会向他们报告问题。从追溯看来,pytest 在从其中运行时遇到了某种“内部错误”。同时尝试其他东西(或者什么都不做——只需使用文本编辑器和命令行)。

标签: python pytest pyscripter


【解决方案1】:

脚本似乎是错误的,据我所知,如果您希望 pytest 应该由 python 解释器运行,可以使用 pytest.main。您需要在目录下进行测试。 例如,您的 pytest 文件名为 test_sample,其中包含以下内容 ====================================pytests/test_sample============ ====================

    import pytest
    def func(x): return x + 1
    def test_answer(): assert func(3) == 5

================================================ ==================================== 然后您可以在运行您的 test_sample 的 python 文件中包含以下代码

   pytest.main(args=['-s', os.path.abspath('pytests')])

这应该可以解决您的问题

如果你不想让 python 运行你的 pytests,你可以通过 tools-->configure tools 配置 pyscripter runner 以由 pytests 运行,然后添加 pytests 命令行参数。然后,您可以从工具中运行 pytest。默认情况下,pyscripter 使用 python 解释器作为运行器

【讨论】:

    【解决方案2】:

    您可以在 pydev 中使用 pytest :
    http://pypi.python.org/pypi/pytest-pydev/0.1

    【讨论】:

    • 有了这个,我将能够在 Eclipse 上配置 Pydev,但不能在 pyscripter 上配置。请帮助我如何在 PyScripter 中运行。
    【解决方案3】:

    似乎 PyScripter 设置了一些 pytest 无法识别的 sys.stdout 流。我只是试图从 pytest 方面改进处理。您可以尝试使用它:

    pip install -i http://pypi.testrun.org -U pytest
    

    它还应该安装包含 TerminalWriting 代码的“py”库。如果这不起作用,请file an file 提供您正在使用的确切环境/PyScripter 版本。还有“import py ; print py.version”的值。

    HTH, 霍尔格

    【讨论】:

      【解决方案4】:
      import pytest,os
      os.chdir("C:\Users\Public\Documents\Development\Python\<test_pytest.py>") #your file location
      pytest.main(['-s', 'test_pytest.py'])
      

      保存文件。在 Pyscripter 上转到 Run-->Python Engine--> Internal 运行测试,它应该可以工作。 Somtime pyscripter 不接受更改,您可能需要重新初始化 python 引擎 (Ctrl+F2)

      【讨论】:

        猜你喜欢
        • 2020-02-09
        • 1970-01-01
        • 1970-01-01
        • 2011-09-17
        • 2014-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多