簡體   English   中英

如何在 PyScripter 中運行 Py.test

[英]How to Run Py.test in PyScripter

我是 py.test 的新手,請告訴我如何在 PyScripter Editor 中運行 py.test 。 我以 belwo 的方式嘗試過,但它不起作用。

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

請幫助我

該腳本似乎是錯誤的,據我所知,如果您希望 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,你可以通過工具配置 pyscripter 運行器由 pytests 運行——>配置工具,然后添加 pytests 命令行參數。 然后,您可以從工具運行 pytest。 默認情況下,pyscripter 使用 python 解釋器作為運行器

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

似乎 PyScripter 設置了一些 pytest 無法識別的 sys.stdout 流。 我只是試圖從 pytest 方面改進處理。 您可以嘗試將其用於:

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

它還應該特別安裝包含 TerminalWriting 代碼的“py”庫。 如果這不起作用,請使用您正在使用的確切環境/PyScripter 版本提交一個文件 而且價值“的進口吡啶,吡啶打印版本。”

HTH,霍爾格

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 ,它應該可以工作。 有時 pyscripter 不接受更改,您可能需要重新初始化 python 引擎 (Ctrl+F2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM