pytest学习(1)
2017-07-20 19:15 很大很老实 阅读(9701) 评论(1) 编辑 收藏 举报pytest是python的一个测试框架,主要是用来进行一些小的测试。
在pycharm中,缺省用的是unittest,这里说明如何设置为pytest。
当然,第一部是安装pytest
pip3 install pytest
然后,在pycharm中,files-》settings-》tools=》python integrated tools=》设定default test runner
然后,写一个简单的py程序(记住,test_*.py or *_test.py):
def inc(x): return x + 1 def test_answer(): assert inc(3) == 5 def test_wcf(): assert inc(3) > 5 def test_hy(): assert inc(3) < 5
运行后:
C:\Python35\python.exe C:\Users\wcf\AppData\Local\JetBrains\Toolbox\apps\PyCharm-P\ch-0\171.4694.67\helpers\pycharm\_jb_pytest_runner.py --path C:/fitme/work/nltk/test_5.py Testing started at 18:53 ... Launching py.test with arguments C:/fitme/work/nltk/test_5.py in C:\fitme\work\nltk ============================= test session starts ============================= platform win32 -- Python 3.5.3, pytest-3.1.3, py-1.4.34, pluggy-0.4.0 rootdir: C:\fitme\work\nltk, inifile: collected 3 items test_5.py F test_5.py:3 (test_answer) def test_answer(): > assert inc(3) == 5 E assert 4 == 5 E + where 4 = inc(3) test_5.py:5: AssertionError F test_5.py:6 (test_wcf) def test_wcf(): > assert inc(3) > 5 E assert 4 > 5 E + where 4 = inc(3) test_5.py:8: AssertionError . ================================== FAILURES =================================== _________________________________ test_answer _________________________________ def test_answer(): > assert inc(3) == 5 E assert 4 == 5 E + where 4 = inc(3) test_5.py:5: AssertionError __________________________________ test_wcf ___________________________________ def test_wcf(): > assert inc(3) > 5 E assert 4 > 5 E + where 4 = inc(3) test_5.py:8: AssertionError ===================== 2 failed, 1 passed in 0.05 seconds ====================== Process finished with exit code 0
如果只运行pytest,则对当前目录下所有的test_*.py or *_test.py文件都进行测试。