2012-11-14 36 views
3

我是py.test的新手,請讓我知道如何在PyScripter編輯器中運行py.test。 我嘗試了貝沃路,但它不起作用。如何在PyScripter編輯器中運行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 

Pleae做幫我

+0

它是否在PyScripter之外運行?你使用的是什麼版本的PyScripter? – martineau

+0

是的,如果以belwo方式調用腳本「python -m py.test sample.py」,它會在PyScripter旁邊運行。我正在使用pyscripter 2.5.3 – user1822881

+0

聽起來像是一個PyScripter兼容性問題...我會向他們報告問題。從traceback看來,pytest在它內部運行時會遇到某種「INTERNALERROR」。同時嘗試別的東西(或者什麼都不要 - 只需使用文本編輯器和命令行)。 – martineau

回答

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 

========== ================================================== ====================== 然後你就可以在Python文件下面的代碼是運行在test_sample

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

這應該解決您的問題

如果你不想python運行你的pytests,你可以配置pyscripter runner通過pytests通過工具運行 - >配置工具,然後添加pytests命令行參數。然後你可以從工具運行pytest。默認情況下,pyscripter使用python解釋器作爲跑步者

0

好像PyScripter設置一些sys.stdout的流,其pytest不承認。我只是試圖改進pytest的處理。你可以嘗試使用它:

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

它應該特別安裝包含TerminalWriting代碼的「py」lib。如果這不起作用,請使用file an file以及您正在使用的確切環境/ PyScripter版本。還有「import py; print py。version」的值。

HTH, 霍爾格

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去運行 - > Python引擎 - >內部 運行測試,它應該工作。 Somtime pyscripter沒有選擇變化,您可能需要重新初始化python引擎(Ctrl + F2)

相關問題