我創造了這個作爲一個回答你的問題和我自己的困惑。我希望它有幫助。在py.test
命令行和tox.ini
中都要注意PYTHONPATH
。
實例項目是here,並且也低於:
mymodule.py
:
import boto3
def stuff():
print "Yep!"
tests/text_syntax_errors.py
:
import boto3
import mymodule
# Define a basic test that actually doesn't do much.
# I just wanted more than zero tests
def test_one_equals_one():
assert 1 == 1
tox.ini
:
[tox]
skipsdist = True
envlist = py27
[flake8]
max-line-length = 119
[testenv]
deps= -r{toxinidir}/requirements.txt
commands=py.test
setenv =
PYTHONPATH = {toxinidir}
requirements.txt
:
boto3
pytest
從我README.md
:
如何,運行這些例子
我來測試我的代碼最初的動機是,我不得不拼錯在腳本導入模塊我正在寫作工作。
如果您編輯mymodule.py
並從「boto3
」中刪除b
,您將看到下面的命令失敗。這是一件好事。同樣,如果您希望看到實際測試失敗,請簡單編輯tests/test_syntax_errors.py
並將1 == 1
更改爲1 == 0
。
py.test
mbp0 pytest_test[master+*] $ PYTHONPATH=. py.test
========================== test session starts ==========================
platform darwin -- Python 2.7.11, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /Users/jmacdonald/w/pytest_test, inifile:
collected 1 items
tests/test_syntax_errors.py .
======================= 1 passed in 0.11 seconds ========================
mbp0 pytest_test[master+*] $
TOX
mbp0 pytest_test[master+*] $ tox
py27 installed: boto3==1.3.1,botocore==1.4.37,docutils==0.12,futures==3.0.5,jmespath==0.9.0,py==1.4.31,pytest==2.9.2,python-dateutil==2.5.3,six==1.10.0
py27 runtests: PYTHONHASHSEED='713732044'
py27 runtests: commands[0] | py.test
========================== test session starts ==========================
platform darwin -- Python 2.7.11, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /Users/jmacdonald/w/pytest_test, inifile:
collected 1 items
tests/test_syntax_errors.py .
======================= 1 passed in 0.11 seconds ========================
________________________________ summary ________________________________
py27: commands succeeded
congratulations :)
mbp0 pytest_test[master+*] $
你不能在這裏發表您的代碼 – Damirchi
這是我在計算器上的第一答案之一。你能詳細說明你的意思嗎?我的代碼是這個問題的具體答案。這只是一個太精心的文章區域張貼。 –
@JeffMacDonald有一個特殊的代碼格式,你可以使用它! :) – gofr1