[英]How to Run Py.test in PyScripter
I am a newbie to py.test , Please let me know how to run the py.test in PyScripter Editor.我是 py.test 的新手,请告诉我如何在 PyScripter Editor 中运行 py.test 。 I have tried in the belwo way but it doesn't work.
我以 belwo 的方式尝试过,但它不起作用。
import pytest导入pytest
def func(x): return x + 1 def func(x): 返回 x + 1
def test_answer(): assert func(3) == 5 def test_answer(): 断言 func(3) == 5
pytest.main() pytest.main()
and on running the above script i get an error saying在运行上述脚本时,我收到一条错误消息
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
Pleae do help me请帮助我
The script appears to be wrong , as far as i know pytest.main can be used if you want your pytest should be run by python interpreter.该脚本似乎是错误的,据我所知,如果您希望 pytest 应由 python 解释器运行,则可以使用 pytest.main。 You need have have your test under a directory.
您需要在目录下进行测试。 For example your pytest file is called test_sample which has following content ===================================pytests/test_sample===============================
例如,您的 pytest 文件名为 test_sample,其中包含以下内容 ====================================pytests/test_sample ================================
import pytest
def func(x): return x + 1
def test_answer(): assert func(3) == 5
================================================================================== Then you can have the following code in python file which runs your test_sample ================================================== ================================ 然后你可以在运行你的 test_sample 的 python 文件中有以下代码
pytest.main(args=['-s', os.path.abspath('pytests')])
This should solve your problem这应该可以解决您的问题
If you dont want python to run your pytests you can configure pyscripter runner to be run by pytests via tools-->configure tools and then add the pytests command line argument.如果你不想让 python 运行你的 pytests,你可以通过工具配置 pyscripter 运行器由 pytests 运行——>配置工具,然后添加 pytests 命令行参数。 you can then run you pytest from tools.
然后,您可以从工具运行 pytest。 By default pyscripter uses python interpreter as runner
默认情况下,pyscripter 使用 python 解释器作为运行器
You can use pytest in pydev :您可以在 pydev 中使用 pytest :
http://pypi.python.org/pypi/pytest-pydev/0.1 http://pypi.python.org/pypi/pytest-pydev/0.1
Seems like PyScripter sets some sys.stdout stream that pytest does not recognize.似乎 PyScripter 设置了一些 pytest 无法识别的 sys.stdout 流。 I just tried to refine the handling from pytest's side.
我只是试图从 pytest 方面改进处理。 You can try using it with:
您可以尝试将其用于:
pip install -i http://pypi.testrun.org -U pytest
which should in particular install also the "py" lib which contains the TerminalWriting code.它还应该特别安装包含 TerminalWriting 代码的“py”库。 If that doesn't work, please file an file with the exact environment/PyScripter version you are using.
如果这不起作用,请使用您正在使用的确切环境/PyScripter 版本提交一个文件。 And also the value of "import py ; print py. version ".
而且价值“的进口吡啶,吡啶打印版本。”
HTH, holger HTH,霍尔格
import pytest,os
os.chdir("C:\Users\Public\Documents\Development\Python\<test_pytest.py>") #your file location
pytest.main(['-s', 'test_pytest.py'])
save the file .保存文件。 On Pyscripter go to Run-->Python Engine--> Internal Run the test , it should work.
在 Pyscripter 上转到Run-->Python Engine--> Internal Run the test ,它应该可以工作。 Somtime pyscripter doesn't pick up the change, you may have to reinitialise the python engine (Ctrl+F2)
有时 pyscripter 不接受更改,您可能需要重新初始化 python 引擎 (Ctrl+F2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.