如何在PyScripter中运行Py.test

时间:2012-11-14 06:52:02

标签: python pytest pyscripter

我是py.test的新手,请让我知道如何在PyScripter编辑器中运行py.test。 我试过了这种方式,但它不起作用。

import pytest

def func(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

请帮助我

4 个答案:

答案 0 :(得分:3)

脚本似乎是错误的,据我所知pytest.main可以使用如果你想你的pytest应该由python解释器运行。您需要在目录下进行测试。 例如,您的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,您可以配置pyscripter runner由pytests通过工具运行 - > gt;配置工具然后添加pytests命令行参数。然后你可以从工具中运行pytest。默认情况下,pyscripter使用python口译员作为跑步者

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

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

HTH, 霍尔格

答案 3 :(得分:0)

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

保存文件。在Pyscripter上转到 运行 - &gt; Python引擎 - &gt;内部 运行测试,它应该工作。 Somtime pyscripter没有接收到更改,您可能必须重新初始化python引擎(Ctrl + F2)