3

私はpy.testの初心者です。PyScripterEditorでpy.testを実行する方法を教えてください。私は信じられない方法で試しましたが、うまくいきません。

pytestをインポートする

def func(x):return x + 1

def test_answer():assert 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を実行させたくない場合は、ツール-> configure toolsを使用して、pytestsによって実行されるようにpyscripter runnerを構成してから、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

PyScripterが、pytestが認識しないsys.stdoutストリームを設定しているようです。pytest側からのハンドリングを改良してみました。あなたはそれを一緒に使うことを試みることができます:

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

特に、TerminalWritingコードを含む「py」libもインストールする必要があります。それでも問題が解決しない場合は、使用している環境/PyScripterの正確なバージョンでファイルを提出してください。また、「import py;printpy.version」の値もあります

HTH、ホルガー

于 2012-11-15T08:49:13.987 に答える
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)

于 2013-03-01T14:40:02.477 に答える