pytestを䜿甚したPythonテスト。 プラグむン第5ç« 

戻る 次ぞ


すぐに䜿える匷力なpytestは、プラグむンのミックスを远加するずさらに良くなりたす。 pytestコヌドベヌスは蚭定ず拡匵機胜で構成されおおり、プラグむンを介しお倉曎および改善できるフックがありたす。



この本の䟋は、Python 3.6ずpytest 3.2を䜿甚しお曞かれおいたす。 pytest 3.2は、Python 2.6、2.7、およびPython 3.3+をサポヌトしおいたす。


Tasksプロゞェクトの゜ヌスコヌドず、この本に瀺されおいるすべおのテストの゜ヌスコヌドは、 pragprog.comにある本のWebペヌゞのリンクから入手できたす。 テストコヌドを理解するために゜ヌスコヌドをダりンロヌドする必芁はありたせん。 テストコヌドは、䟋では䟿利な圢匏で瀺されおいたす。 ただし、プロゞェクトのタスクを実行したり、テストサンプルを調敎しお自分のプロゞェクトをテストしたりするには手を瞛っおいない、曞籍のWebペヌゞにアクセスしお䜜品をダりンロヌドする必芁がありたす。 曞籍のWebペヌゞには、 正誀衚のメッセヌゞぞのリンクずディスカッションフォヌラムがありたす。

ネタバレの䞋には、このシリヌズの蚘事のリストがありたす。



さらに進めたしょう


この本の前の章で䜜業した堎合、すでにいく぀かのプラグむンを䜜成しおいるこずに驚くかもしれたせん。 プロゞェクトのトップレベルのconftest.pyファむルにフィクスチャやフック関数を配眮するconftest.py 、ロヌカルのconftestプラグむンを䜜成したす。 これらのconftest.pyファむルを、プロゞェクト間、他の人、たたは䞖界ず共有できるむンストヌル可胜なプラグむンに倉換するのはほんの少しの远加䜜業です。


この章は、サヌドパヌティのプラグむンを探す堎所の質問に答えるこずから始めたす。 かなりの数のプラグむンが利甚可胜であるため、誰かがpytest行いたい倉曎をすでに曞いおいる可胜性がかなりありたす。 私たちはオヌプン゜ヌスのプラグむンを怜蚎するので、プラグむンがほがあなたがやりたいこずをしおいるが、そうでない堎合は、プラグむンを開発するか、参照ずしお䜿甚しお独自のプラグむンを䜜成できたす。 この章は独自のプラグむンの䜜成に関するものですが、付録3、 Sampler Packプラグむン163ペヌゞが含たれおおり、可胜なこずの抂芁を説明しおいたす。


この章では、プラグむンの䜜成方法を孊び、テスト、パッケヌゞング、配垃の正しい方向を瀺したす。 Pythonのパッケヌゞず配垃に関する完党なトピックは広範であり、独自の本であるず䞻匵しおいるため、すべおを網矅するわけではありたせん。 しかし、あなたはあなたのチヌムずプラグむンを亀換するこずができるのに十分な情報を埗たす。 たた、PyPIサポヌトず最小限の䜜業でプラグむンを䜜成するいく぀かの簡単な方法に぀いおも説明したす。


怜玢プラグむン


サヌドパヌティのpytestプラグむンはいく぀かの堎所にありたす。 付録3、プラグむンサンプラパック163ペヌゞにリストされおいるプラ​​グむンは、PyPIからダりンロヌドできたす。 ただし、これが玠晎らしいpytestプラグむンを芋぀ける唯䞀の堎所ではありたせん。


https://docs.pytest.org/en/latest/plugins.html


メむンのpytestドキュメントサむトには、pytestプラグむンのむンストヌルず䜿甚に぀いお説明し、いく぀かの䞀般的なプラグむンをリストしたペヌゞがありたす。


https://pypi.python.org


Python Package IndexPyPIは、倚くのPythonパッケヌゞを入手するのに最適な堎所ですが、pytestプラグむンを怜玢するのにも最適です。 ほずんどのpytestプラグむンは「pytest-」で始たるか「-pytest」で終わるため、pytestプラグむンを怜玢するずきは、怜玢フィヌルドに「pytest」、「pytest-」たたは「-pytest」ず入力したす。


https://github.com/pytest-dev


GitHubのpytest-devグルヌプには、pytestの゜ヌスコヌドが保存されおいたす。 さらに、ここでは、pytestカヌネルチヌムが長期的にサポヌトする必芁がある人気のあるpytestプラグむンを芋぀けるこずができたす。


プラグむンのむンストヌル


Pytestプラグむンは、他のPythonパッケヌゞず同様に、pipずずもにむンストヌルされたす。 しかし
プラグむンをむンストヌルするには、いく぀かの方法でpipを䜿甚できたす。


PyPIからむンストヌルする


PyPIはpipのデフォルトの堎所であるため、PyPIからプラグむンをむンストヌルするのが最も簡単な方法です。 pytest-covプラグむンをむンストヌルしたしょう。


 $ pip install pytest-cov 

PyPIの最新の安定バヌゞョンがむンストヌルされたす。


PyPIから特定のバヌゞョンをむンストヌルする


プラグむンの特定のバヌゞョンが必芁な堎合は、 ==埌にバヌゞョンを指定できたす。


 $ pip install pytest-cov==2.4.0 

.tar.gzたたは.whlファむルからむンストヌルする


PyPIパッケヌゞは、拡匵子が.tar.gzたたは.whl zipファむルずしお配垃されたす。 圌らはしばしば「タヌルボヌル」ず「ホむヌル」ず呌ばれたす。 PyPIを盎接操䜜しようずするず問題が発生する堎合ファむアりォヌルやその他のネットワヌクの耇雑な.whlで発生する可胜性がありたす .whlたたは.whlをダりンロヌドしお、 .whlからむンストヌルできたす。


タンバリンで開梱したりダンスしたりする必芁はありたせん。 ピップを向けるだけです


 $ pip install pytest-cov-2.4.0.tar.gz # or $ pip install pytest_cov-2.4.0-py2.py3-none-any.whl 

ロヌカルディレクトリからのむンストヌル


.tar.gzたたは.whlロヌカルたたは共有ディレクトリにプラグむンおよび他のPythonパッケヌゞ甚のプラグむンを甚意し、PyPIの代わりにこれを䜿甚しおプラグむンをむンストヌルできたす。


 $ mkdir some_plugins $ cp pytest_cov-2.4.0-py2.py3-none-any.whl some_plugins/ $ pip install --no-index --find-links=./some_plugins/ pytest-cov 

--no-indexは、PipにPyPIに接続しないように指瀺したす。 --find-links=./some_plugins/は、pipにsome_pluginsディレクトリを調べるように指瀺したす。 この方法は、サヌドパヌティずネむティブの䞡方のプラグむンがロヌカルに保存されおいる堎合、および継続的統合たたはtoxを䜿甚しお新しい仮想環境を䜜成しおいる堎合に特に䟿利です。 Toxず継続的むンテグレヌションの䞡方に぀いおは、第7章でpytestを他のツヌルず䜿甚しお、125ペヌゞで説明したす。


ロヌカルディレクトリのむンストヌル方法を䜿甚するず、==ずバヌゞョン番号を远加しお、耇数のバヌゞョンをむンストヌルし、必芁なバヌゞョンを指定できるこずに泚意しおください。


 $ pip install --no-index --find-links=./some_plugins/ pytest-cov==2.4.0 

Gitリポゞトリからむンストヌルする


この堎合はGitHubのGitリポゞトリからプラグむンを盎接むンストヌルできたす。


 $ pip install git+https://github.com/pytest-dev/pytest-cov 

バヌゞョンタグを指定するこずもできたす。


 $ pip install git+https://github.com/pytest-dev/pytest-cov@v2.4.0 

たたは、ブランチを指定できたす


 $ pip install git+https://github.com/pytest-dev/pytest-cov@master 

Gitリポゞトリからのむンストヌルは、Gitに独自の䜜業を保存しおいる堎合、たたはプラグむンたたはプラグむンの必芁なバヌゞョンがPyPIにない堎合に特に䟿利です。


翻蚳者泚

pipはGit、Mercurial、Subversion、およびBazaarからのむンストヌルをサポヌトし、URLプレフィックスを䜿甚しおVCSタむプを定矩したす「git +」、「hg +」、「svn +」、「bzr +」。
詳现に぀いおは、 PyPIのドキュメントを参照しおください。

独自のプラグむンを曞く


倚くのサヌドパヌティプラグむンには、非垞に倚くのコヌドが含たれおいたす。 これは、これらすべおを自分で開発する時間を節玄するために䜿甚する理由の1぀です。 ただし、特定のコヌドに぀いおは、テストに圹立぀特別なフィクスチャず修正を間違いなく思い付きたす。 プラグむンを䜜成するこずにより、耇数のプロゞェクト間で共有したい耇数のフィクスチャを簡単に共有できたす。 独自のプラグむンを開発および配垃するこずにより、これらの倉曎をいく぀かのプロゞェクト堎合によっおは他の地域ず共有できたす。 これは非垞に簡単です。 このセクションでは、pytestの動䜜の小さな倉曎を開発し、プラグむンずしおパックし、テストしお、配垃方法を怜蚎したす。


プラグむンには、pytestの動䜜を倉曎するフック関数を含めるこずができたす。 pytestはプラグむンがpytestの動䜜をわずかに倉曎できるように蚭蚈されおいるため、倚くのフック関数が利甚可胜です。 pytestのフックは、pytestドキュメントサむトにリストされおいたす 。 この䟋では、テストステヌタスの倖芳を倉曎するプラグむンを䜜成したす。 コマンドラむンパラメヌタヌを远加しお、この新しい動䜜を有効にしたす。 出力ヘッダヌにテキストを远加したす。 特に、すべおのFAILEDステヌタスむンゞケヌタ倱敗を「改善のためにOPPORTUNITY芋蟌み」に倉曎し、FをOに倉曎し、「テストを実行しおいただきありがずうございたす」をヘッダヌに远加したす。 これを行うには、 --niceオプションを䜿甚したす。


動䜜の倉曎をプラグむンのメカニズムの説明ずは別にするために、conftest.pyを再配垃可胜なプラグむンに倉える前に倉曎を加えたす。 この方法でプラグむンを実行する必芁はありたせん。 しかし、倚くの堎合、1぀のプロゞェクトでのみ䜿甚するこずを意図した倉曎は、共有しおプラグむンに倉換するのに十分に圹立ちたす。 したがっお、conftest.pyファむルに機胜を远加するこずから始め、conftest.pyですべおが機胜した埌、コヌドをパッケヌゞに移動したす。


タスクプロゞェクトに戻りたす。 30ペヌゞの「䟋倖の埅機」セクションでは、誰かがAPI関数を誀っお呌び出した堎合に䟋倖がスロヌされるかどうかを確認するいく぀かのテストを䜜成したした。 少なくずもいく぀かの考えられる゚ラヌ状態を芋逃したようです。


さらにいく぀かのテストを瀺したす。


ch5 / a / tasks_proj / tests / func / test_api_exceptions.py

 """     API wrong.""" import pytest import tasks from tasks import Task @pytest.mark.usefixtures('tasks_db') class TestAdd(): """,   tasks.add().""" def test_missing_summary(self): """  ,  summary missing.""" with pytest.raises(ValueError): tasks.add(Task(owner='bob')) def test_done_not_bool(self): """  ,  done   bool.""" with pytest.raises(ValueError): tasks.add(Task(summary='summary', done='True')) 

それらを実行しお、合栌するかどうかを確認したしょう。


 $ cd /path/to/code/ch5/a/tasks_proj $ pytest ===================== test session starts ====================== collected 57 items tests/func/test_add.py ... tests/func/test_add_variety.py ............................ tests/func/test_add_variety2.py ............ tests/func/test_api_exceptions.py .F....... tests/func/test_unique_id.py . tests/unit/test_task.py .... =========================== FAILURES =========================== __________________ TestAdd.test_done_not_bool __________________ self = <func.test_api_exceptions.TestAdd object at 0x103a71a20> def test_done_not_bool(self): """Should raise an exception if done is not a bool.""" with pytest.raises(ValueError): > tasks.add(Task(summary='summary', done='True')) E Failed: DID NOT RAISE <class 'ValueError'> tests/func/test_api_exceptions.py:20: Failed ============= 1 failed, 56 passed in 0.28 seconds ============== 

詳现に぀いおは、 -vで再床実行しおみたしょう。 トレヌスはすでに衚瀺されおいるので、-- --tb=no抌しお--tb=noオフにできたす。


-k TestAddを䜿甚した新しいテストに焊点を圓おたしょう。これは、「TestAdd」を含む名前のテストが他にないため機胜したす。


「すべおを萜ずしお」このテストの修正を詊みるこずもできたすそしお、埌でテストを行いたすが、今床は開発者にずっお倱敗をより快適にするこずに焊点を圓おたす。


たず、ヘッダヌに「ありがずう」ずいうメッセヌゞを远加したす。これは、 pytest_report_header()ずいうpytest_report_header()フックを䜿甚しお実行できたす。


ch5 / b / tasks_proj / tests / conftest.py

 def pytest_report_header(): """    .""" return "Thanks for running the tests." 

お瀌のメモを入力するのは明らかに愚かです。 ただし、ヘッダヌに情報を远加する機胜は拡匵できたす。 ナヌザヌ名を远加し、䜿甚する機噚ずテストするバヌゞョンを指定できたす。 䞀般に、文字列に倉換できるものはすべおテストのタむトルに挿入できたす。


次に、テストステヌタスレポヌトを倉曎しお、 FをO 、 FAILEDをOPPORTUNITY for improvementたす。 このこずを可胜にするフックがありたす pytest_report_teststatus() 


ch5 / b / tasks_proj / tests / conftest.py

 def pytest_report_teststatus(report): """   .""" if report.when == 'call' and report.failed: return (report.outcome, 'O', 'OPPORTUNITY for improvement') 

そしお今、私たちは探しおいた゜リュヌションを手に入れたした。 --verboseフラグを䜿甚しないテストセッションでは、倱敗に察しおOが衚瀺されたす。぀たり、改善が可胜です。


 $ cd /path/to/code/ch5/b/tasks_proj/tests/func $ pytest --tb=no test_api_exceptions.py -k TestAdd ===================== test session starts ====================== Thanks for running the tests. collected 9 items test_api_exceptions.py .O ====================== 7 tests deselected ====================== ======= 1 failed, 1 passed, 7 deselected in 0.06 seconds ======= 

-vたたは--verboseフラグを䜿甚した方が良いでしょう


 $ pytest -v --tb=no test_api_exceptions.py -k TestAdd ===================== test session starts ====================== Thanks for running the tests. collected 9 items test_api_exceptions.py::TestAdd::test_missing_summary PASSED test_api_exceptions.py::TestAdd::test_done_not_bool OPPORTUNITY for improvement ====================== 7 tests deselected ====================== ======= 1 failed, 1 passed, 7 deselected in 0.07 seconds ======= 

最埌に行う倉曎は、コマンドラむンパラメヌタヌ--nice,を远加するこずです--nice,これにより、ステヌタスの倉曎は、-- --niceを眮き換えた堎合にのみ発生したす。


 def pytest_addoption(parser): """ nice    --nice.""" group = parser.getgroup('nice') group.addoption("--nice", action="store_true", help="nice: turn failures into opportunities") def pytest_report_header(): """    .""" if pytest.config.getoption('nice'): return "Thanks for running the tests." def pytest_report_teststatus(report): """   .""" if report.when == 'call': if report.failed and pytest.config.getoption('nice'): return (report.outcome, 'O', 'OPPORTUNITY for improvement') 

このプラグむンでは、いく぀かのフックしか䜿甚しないこずに泚意しおください。 メむンのPytestドキュメントサむトには、他にも倚数のサむトがありたす。


これで、䟋でプラグむンを実行するだけで、プラグむンを手動でテストできたす。 最初に、 --niceオプションを䜿甚せずに、ナヌザヌ名のみが衚瀺されるようにしたす。


 $ cd /path/to/code/ch5/c/tasks_proj/tests/func $ pytest --tb=no test_api_exceptions.py -k TestAdd ===================== test session starts ====================== collected 9 items test_api_exceptions.py .F ====================== 7 tests deselected ====================== ======= 1 failed, 1 passed, 7 deselected in 0.07 seconds ======= 

--nice 


 $ pytest --nice --tb=no test_api_exceptions.py -k TestAdd ===================== test session starts ====================== Thanks for running the tests. collected 9 items test_api_exceptions.py .O ====================== 7 tests deselected ====================== ======= 1 failed, 1 passed, 7 deselected in 0.07 seconds ======= 

--niceず--verbose 


 $ pytest -v --nice --tb=no test_api_exceptions.py -k TestAdd ===================== test session starts ====================== Thanks for running the tests. collected 9 items test_api_exceptions.py::TestAdd::test_missing_summary PASSED test_api_exceptions.py::TestAdd::test_done_not_bool OPPORTUNITY for improvement ====================== 7 tests deselected ====================== ======= 1 failed, 1 passed, 7 deselected in 0.06 seconds ======= 

いいね conftest.pyファむルの玄10行のコヌドで、 conftest.py倉曎をすべお行いたした。 次に、このコヌドをプラグむン構造に移動したす。


むンストヌル可胜なプラグむンを䜜成する


プラグむンを他のナヌザヌず共有するプロセスは明確に定矩されおいたす。 独自のプラグむンをPyPIに含めない堎合でも、このプロセスを実行するず、オヌプン゜ヌスプラグむンからコヌドを読みやすくなり、それらが圹立぀かどうかを評䟡する機䌚が増えたす。


このトピックは他の堎所で十分に文曞化されおいるため、この本でPythonパッケヌゞのパッケヌゞ化ず配垃を完党にカバヌするこずは䞍芁です。 ここずここずロシア語。 ただし、前のセクションで䜜成したロヌカル構成プラグむンから、pipを䜿甚しおむンストヌルされたものに移行するこずは倧したこずではありたせん。 、


最初に、プラグむンコヌドをホストする新しいディレクトリを䜜成する必芁がありたす。 呌び方は関係ありたせんが、ナむスフラグ甚のプラグむンを䜜成しおいるので、pytest-niceず呌びたしょう。 この新しいディレクトリには、pytest_nice.pyずsetup.pyの2぀のファむルがありたす。 テストカタログに぀いおは、105ペヌゞの「テストプラグむン」セクションで説明したす。


 │ LICENSE │ pytest_nice.py │ setup.py │ └───tests │ conftest.py │ test_nice.py 

pytest_nice.py 、この関数に関連付けられたconftest.pyの正確なコンテンツを配眮したすそしおtasks_proj/tests/conftest.pyから抜出しtasks_proj/tests/conftest.py 


ch5 / pytest-nice / pytest_nice.py


 """  pytest-nice .""" import pytest def pytest_addoption(parser): """ nice    --nice.""" group = parser.getgroup('nice') group.addoption("--nice", action="store_true", help="nice: turn FAILED into OPPORTUNITY for improvement") def pytest_report_header(): """    .""" if pytest.config.getoption('nice'): return "Thanks for running the tests." def pytest_report_teststatus(report): """   .""" if report.when == 'call': if report.failed and pytest.config.getoption('nice'): return (report.outcome, 'O', 'OPPORTUNITY for improvement') 

setup.py最小限のsetup()呌び出しが必芁です。


ch5 / pytest-nice / setup.py

 """Setup  pytest-nice plugin.""" from setuptools import setup setup( name='pytest-nice', version='0.1.0', description=' Pytest,   FAILURE into OPPORTUNITY', url='https:////////', author=' ', author_email='your_email@somewhere.com', license='proprietary', py_modules=['pytest_nice'], install_requires=['pytest'], entry_points={'pytest11': ['nice = pytest_nice', ], }, ) 

幅広いオヌディ゚ンスたたはむンタヌネットに配信する堎合は、蚭定でさらに情報が必芁になりたす。 ただし、小さなチヌムの堎合や自分自身の堎合はこれで十分です。


setup()他のパラメヌタヌを含めるこずができたす。 ここには必須フィヌルドのみがありたす。 バヌゞョンフィヌルドは、このプラグむンのバヌゞョンです。 そしお、あなたがバヌゞョンを立ち䞊げるずき、それは完党にあなた次第です。 URLフィヌルドは必須です。 空癜のたたにできたすが、譊告が衚瀺されたす。 author_emailずauthor_emailは、 maintainerずmaintainer_email眮き換えるこずができたすが、これらのペアのいずれかが存圚するはずです。 licenseフィヌルドは短いテキストフィヌルドです。 それは、倚くのオヌプン゜ヌスラむセンスの1぀、あなたの名前や䌚瀟、たたはあなたに適したものです。 py_modulesは、このプラグむンの唯䞀のモゞュヌルずしおpy_modulesリストされおいたす。 これはリストであり、耇数のモゞュヌルを含めるこずができたすが、耇数ある堎合は、パッケヌゞを䜿甚しおすべおのモゞュヌルを1぀のディレクトリに配眮したす。


これたでのずころ、すべおのsetup()オプションは暙準であり、すべおのPythonむンストヌラヌによっお䜿甚されたす。 Pytestプラグむンで異なる郚分は、 entry_pointsパラメヌタヌです。 entry_points={'pytest11': ['nice = pytest_nice', ], },.をリストしentry_points={'pytest11': ['nice = pytest_nice', ], },. entry_points関数はsetuptools暙準ですが、pytest11はpytestが探しおいる特別な識別子です。 この行では、 niceはプラグむンの名前であり、 pytest_niceプラグむンが存圚するモゞュヌルpytest_nice名前であるこずをpytestに䌝えたす。 パッケヌゞを䜿甚した堎合、ここでの゚ントリは次のようになりたす。


README.rstファむルに぀いおはREADME.rst話しおREADME.rst 。 䜕らかの圢匏のREADMEがsetuptoolsの芁件です。 スキップするず、これが埗られたす


 ... warning: sdist: standard file not found: should have one of README, README.rst, README.txt ... 

ずにかく、プロゞェクト情報を含めるための暙準的な方法ずしおREADMEを保持するこずは良い考えです。 pytest-niceのファむルに入れたものは次のずおりです。


ch5 / pytest-nice / README.rst

 pytest-nice : A pytest plugin =============================   pytest     .  -------- -   ,     pytest. -  ``--nice`` , : -  ``F``  ``O`` -  ``-v``,  ``FAILURE``  ``OPPORTUNITY for improvement``  ------------ ,    Pytest    .tar.gz    PATH,  : :: $ pip install PATH/pytest-nice-0.1.0.tar.gz $ pip install --no-index --find-links PATH pytest-nice  ----- :: $ pytest --nice 

READMEファむルの内容に぀いお倚くの意芋がありたす。 これは倧幅にトリミングされたバヌゞョンですが、動䜜したす。


プラグむンのテスト


プラグむンは、他のコヌドず同様にテストする必芁があるコヌドです。 ただし、テストツヌルぞの倉曎のテストはもう少し耇雑です。 98ペヌゞの「独自のプラグむンの䜜成」セクションでプラグむンコヌドを開発したずき、サンプルテストファむルを䜿甚しお手動でチェックし、pytestを実行しお、出力が正しいこずを確認したした。 pytesterに付属しおいるがデフォルトでは無効になっおいるpytesterずいうプラグむンを䜿甚しお、自動モヌドでも同じこずができたす。


pytest-niceのテストディレクトリには、conftest.pyずtest_nice.pyの2぀のファむルがありたす。 pytesterを䜿甚するには、 pytesterに1行だけ远加する必芁がありconftest.py 。


ch5 / pytest-nice / tests / conftest.py

 """pytester is needed for testing plugins.""" pytest_plugins = 'pytester' 

この行には、 pytesterプラグむンが含たれおpytesterたす。 testdirずいうフィクスチャを䜿甚したす。 pytester 、 pytesterオンになったずきに䜿甚可胜になりたす。
倚くの堎合、プラグむンのテストは、手動で説明した圢匏を取りたす。


  1. サンプルのテストファむルを䜜成したす。
  2. サンプルファむルを含むディレクトリでいく぀かのパラメヌタヌを䜿甚しお、たたは䜿甚せずにpytestを実行したす。
  3. 出力を確認しおください。
  4. 可胜な堎合、結果コヌドをチェックするには、すべおのパスで0、䞀郚の倱敗で1になりたす。

1぀の䟋を芋おみたしょう。


ch5 / pytest-nice / tests / test_nice.py

 def test_pass_fail(testdir): #     Pytest testdir.makepyfile(""" def test_pass(): assert 1 == 1 def test_fail(): assert 1 == 2 """) #  pytest result = testdir.runpytest() # fnmatch_lines    result.stdout.fnmatch_lines([ '*.F', # .  Pass, F  Fail ]) # ,      '1'  testsuite assert result.ret == 1 

Fixture testdirは、テストファむルをホストするための䞀時ディレクトリを自動的に䜜成したす。 makepyfile() , . : , , .


pytest testdir.runpytest() . , . RunResult .


stdout ret . , , , fnmatch_lines , , , , ret 0 1 . , fnmatch_lines , . . , , :


ch5/pytest-nice/tests/test_nice.py

 @pytest.fixture() def sample_test(testdir): testdir.makepyfile(""" def test_pass(): assert 1 == 1 def test_fail(): assert 1 == 2 """) return testdir 

, , sample_test , . :


ch5/pytest-nice/tests/test_nice.py

 def test_with_nice(sample_test): result = sample_test.runpytest('--nice') result.stdout.fnmatch_lines(['*.O', ]) # . for Pass, O for Fail assert result.ret == 1 def test_with_nice_verbose(sample_test): result = sample_test.runpytest('-v', '--nice') result.stdout.fnmatch_lines([ '*::test_fail OPPORTUNITY for improvement', ]) assert result.ret == 1 def test_not_nice_verbose(sample_test): result = sample_test.runpytest('-v') result.stdout.fnmatch_lines(['*::test_fail FAILED']) assert result.ret == 1 

. , :


ch5/pytest-nice/tests/test_nice.py

 def test_header(sample_test): result = sample_test.runpytest('--nice') result.stdout.fnmatch_lines(['Thanks for running the tests.']) def test_header_not_nice(sample_test): result = sample_test.runpytest() thanks_message = 'Thanks for running the tests.' assert thanks_message not in result.stdout.str() 

, , .


:


ch5/pytest-nice/tests/test_nice.py

 def test_help_message(testdir): result = testdir.runpytest('--help') # fnmatch_lines    result.stdout.fnmatch_lines([ 'nice:', '*--nice*nice: turn FAILED into OPPORTUNITY for improvement', ]) 

, , , .


pytest-nice , . .zip.gz , :


 $ cd /path/to/code/ch5/pytest-nice/ $ pip install . Processing /path/to/code/ch5/pytest-nice Requirement already satisfied: pytest in /path/to/venv/lib/python3.6/site-packages (from pytest-nice==0.1.0) Requirement already satisfied: py>=1.4.33 in /path/to/venv/lib/python3.6/site-packages (from pytest->pytest-nice==0.1.0) Requirement already satisfied: setuptools in /path/to/venv/lib/python3.6/site-packages (from pytest->pytest-nice==0.1.0) Building wheels for collected packages: pytest-nice Running setup.py bdist_wheel for pytest-nice ... done ... Successfully built pytest-nice Installing collected packages: pytest-nice Successfully installed pytest-nice-0.1.0 

, , :


 $ pytest -v ===================== test session starts ====================== plugins: nice-0.1.0 collected 7 items tests/test_nice.py::test_pass_fail PASSED tests/test_nice.py::test_with_nice PASSED tests/test_nice.py::test_with_nice_verbose PASSED tests/test_nice.py::test_not_nice_verbose PASSED tests/test_nice.py::test_header PASSED tests/test_nice.py::test_header_not_nice PASSED tests/test_nice.py::test_help_message PASSED =================== 7 passed in 0.34 seconds =================== 

ご泚意 : , . .
 platform win32 -- Python 3.6.5, pytest-3.9.3, py-1.7.0, pluggy-0.8.0 -- c:\venv36\scripts\python.exe collected 7 items tests/test_nice.py::test_pass_fail FAILED [ 14%] tests/test_nice.py::test_with_nice OPPORTUNITY for improvement [ 28%] tests/test_nice.py::test_with_nice_verbose OPPORTUNITY for improvement [ 42%] tests/test_nice.py::test_not_nice_verbose FAILED [ 57%] tests/test_nice.py::test_header PASSED [ 71%] tests/test_nice.py::test_header_not_nice PASSED [ 85%] tests/test_nice.py::test_help_message PASSED [100%] ================================== FAILURES =================================== _______________________________ test_pass_fail ________________________________ 



  result.stdout.fnmatch_lines([ '*.F', # . for Pass, F for Fail ]) 


に
  result.stdout.fnmatch_lines([ '*.F*', # . for Pass, F for Fail ]) 


* F

test_with_nice , test_with_nice_verbose , test_not_nice_verbose

pytest.
c
'test_with_nice.py .O [100%]'
.
,

RemovedInPytest4Warning: usage of Session.Class is deprecated, please use pytest.Class instead

!
 (venv36) c:\_BOOKS_\pytest_si\bopytest-code\code\ch5\pytest-nice>pytest -v ============================= test session starts ============================= platform win32 -- Python 3.6.5, pytest-3.9.3, py-1.7.0, pluggy-0.8.0 -- c:\venv36\scripts\python.exe cachedir: .pytest_cache rootdir: c:\_BOOKS_\pytest_si\bopytest-code\code\ch5\pytest-nice, inifile: plugins: nice-0.1.0 collected 7 items tests/test_nice.py::test_pass_fail PASSED [ 14%] tests/test_nice.py::test_with_nice PASSED [ 28%] tests/test_nice.py::test_with_nice_verbose PASSED [ 42%] tests/test_nice.py::test_not_nice_verbose PASSED [ 57%] tests/test_nice.py::test_header PASSED [ 71%] tests/test_nice.py::test_header_not_nice PASSED [ 85%] tests/test_nice.py::test_help_message PASSED [100%] ============================== warnings summary =============================== tests/test_nice.py::test_pass_fail c:\venv36\lib\site-packages\_pytest\compat.py:332: RemovedInPytest4Warning: usage of Session.Class is deprecated, please use pytest.Class instead return getattr(object, name, default) 

やった . (pytest-nice), Python
pytest-:


 $ pip uninstall pytest-nice Uninstalling pytest-nice-0.1.0: Would remove: \path\to\venv\lib\site-packages\pytest_nice-0.1.0.dist-info\* ... Proceed (y/n)? y Successfully uninstalled pytest-nice-0.1.0 

— , pytest, PyPI.



, . , setup.py :


 $ cd /path/to/code/ch5/pytest-nice $ python setup.py sdist running sdist running egg_info creating pytest_nice.egg-info ... running check creating pytest-nice-0.1.0 ... creating dist Creating tar archive ... $ ls dist pytest-nice-0.1.0.tar.gz 

( , sdist source distribution — “ .”)


pytest-nice dist pytest-nice-0.1.0.tar.gz .


, , :


 $ pip install dist/pytest-nice-0.1.0.tar.gz Processing ./dist/pytest-nice-0.1.0.tar.gz ... Installing collected packages: pytest-nice Successfully installed pytest-nice-0.1.0 

.tar.gz , .



pip , , , , , , .tar.gz . , pytest-nice-0.1.0.tar.gz myplugins .


pytest-nice myplugins :


 $ pip install --no-index --find-links myplugins pytest-nice 

--no-index pip PyPI, , .
The --find-links myplugins tells PyPI to look in myplugins for packages to install. And of course, pytest-nice is what we want to install.
--find-links myplugins PyPI myplugins . , pytest-nice — , .


myplugins , , --upgrade :


 $ pip install --upgrade --no-index --find-links myplugins pytest-nice 

pip , --no-index --find-links myplugins .


PyPI


, , . , . , , , Python Packaging .


pytest, — cookiecutter-pytest-plugin :


 $ pip install cookiecutter $ cookiecutter https://github.com/pytest-dev/cookiecutter-pytest-plugin 

. , . ; , , . pytest, , .



ch4/cache/test_slower.py autouse fixture, check_duration() . 4. .


  1. pytest-slower, , , « » . 102.
  2. , pytest-slower , .
  3. プラグむンのテストコヌドを䜜成したす。
  4. 芋おみたしょうPythonのパッケヌゞむンデックスをず«pytest-»を探したす。おもしろそうなpytestプラグむンを芋぀けおください。
  5. 遞択したプラグむンをむンストヌルし、タスクテストで詊しおください。

次は䜕ですか


conftest.pyこの本ではこれたで䜕床も䜿甚したした。たずえば、pytestの実行に圱響する蚭定ファむルもありたすpytest.ini。次の章では、さたざたな構成ファむルに粟通し、テスト時に生掻を楜にするためにできるこずを芋぀けたす。


戻る 次ぞ

Source: https://habr.com/ru/post/J448794/


All Articles