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文件都進行測試。