我创建这个是为了回答你的问题和我自己的困惑。我希望它有所帮助。注意PYTHONPATH
在py.test
命令行和tox.ini
.
示例项目在这里,也在下面:
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
并b
从“ boto3
”中删除,您将看到以下命令失败。这是一件好事。同样,如果您想看到实际测试失败,只需简单编辑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+*] $
毒物
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+*] $