3

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

导入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

4 回答 4

3

该脚本似乎是错误的,据我所知,如果您希望 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 解释器作为运行器

于 2012-11-15T08:54:02.443 回答
0

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

于 2012-11-14T08:39:20.020 回答
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 上转到Run-->Python Engine--> Internal Run the test ,它应该可以工作。Somtime pyscripter 不接受更改,您可能需要重新初始化 python 引擎 (Ctrl+F2)

于 2013-03-01T14:40:02.477 回答
0

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

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

特别是还应该安装包含 TerminalWriting 代码的“py”库。如果这不起作用,请使用您正在使用的确切环境/PyScripter 版本归档一个文件。还有“import py ; print py.version”的

HTH,霍尔格

于 2012-11-15T08:49:13.987 回答