.. _usage: 使ç¨æ¹æ³ã¨ãã¹ãå®è¡ ==================== .. Usage and Invocations ========================================== .. _cmdline: ``python -m pytest`` ã«ãã pytest å¼ã³åºã ------------------------------------------- .. Calling pytest through ``python -m pytest`` ----------------------------------------------------- .. versionadded:: 2.0 .. If you use Python-2.5 or later you can invoke testing through the Python interpreter from the command line:: Python 2.5 ãããã以ä¸ã®ãã¼ã¸ã§ã³ã使ã£ã¦ãããªããã³ãã³ãã©ã¤ã³ãã Python ã¤ã³ã¿ã¼ããªã¿ã¼ã§ãã¹ããå®è¡ã§ãã¾ã:: python -m pytest [...] .. This is equivalent to invoking the command line script ``py.test [...]`` directly. ããã¯ç´æ¥çã«ã³ãã³ãã©ã¤ã³ã¹ã¯ãªãã ``py.test [...]`` ãå®è¡ããã®ã¨åãã§ãã .. Getting help on version, option names, environment variables -------------------------------------------------------------- ãã¼ã¸ã§ã³ããªãã·ã§ã³åãç°å¢å¤æ°ã®ãã«ãã表示 ------------------------------------------------ :: py.test --version # pytest ãã¤ã³ãã¼ããããå ´æã表示 py.test --fixtures # å©ç¨ã§ããçµã¿è¾¼ã¿ã®é¢æ°å¼æ°ã表示 py.test -h | --help # ã³ãã³ãã©ã¤ã³ã¨è¨å®ãã¡ã¤ã«ãªãã·ã§ã³ã®ãã«ãã表示 .. Stopping after the first (or N) failures --------------------------------------------------- æå (ã¾ã㯠N å) 失æããã¨ãã«ãã¹ããä¸æ¢ --------------------------------------------- .. To stop the testing process after the first (N) failures:: py.test -x # stop after first failure py.test --maxfail=2 # stop after two failures æå (N å) 失æããã¨ãã«ãã¹ãããã»ã¹ãä¸æ¢ããã«ã¯ã次ã®ããã«ãã¾ã:: py.test -x # æåã«å¤±æããã¨ãã«ä¸æ¢ py.test --maxfail=2 # 2 å失æããã¨ãã«ä¸æ¢ .. Specifying tests / selecting tests --------------------------------------------------- ãã¹ãã®æå®ã¨é¸æ ------------------ .. Several test run options:: 次ã®ãããªãã¹ãå®è¡ã®ãªãã·ã§ã³ãããã¾ã:: py.test test_mod.py # ã¢ã¸ã¥ã¼ã«å ã®ãã¹ããå®è¡ py.test somepath # æå®ãããã¹ã®å ¨ã¦ã®ãã¹ããå®è¡ py.test -k string # string ãå«ãååããã¤ãã¹ãã®ã¿ãå®è¡ .. Import 'pkg' and use its filesystem location to find and run tests:: 'pkg' ãã¤ã³ãã¼ããã¦ããã®ãã¡ã¤ã«ã·ã¹ãã ä¸ã®ä½ç½®ãããã¹ããæ¢ãã¦å®è¡ãã¾ã:: py.test --pyargs pkg # pkg ã®ãã£ã¬ã¯ããªé ä¸ã«ããå ¨ã¦ã®ãã¹ããå®è¡ .. Modifying Python traceback printing ---------------------------------------------- Python ã®ãã¬ã¼ã¹ããã¯è¡¨ç¤ºãå¤æ´ --------------------------------- .. Examples for modifying traceback printing:: ãã¬ã¼ã¹ããã¯è¡¨ç¤ºãå¤æ´ããä¾ã§ã:: py.test --showlocals # ãã¬ã¼ã¹ããã¯ã®ãã¼ã«ã«å¤æ°ã表示 py.test -l # ãã¬ã¼ã¹ããã¯ã®ãã¼ã«ã«å¤æ°ã表示 (çããªãã·ã§ã³) py.test --tb=long # ããã©ã«ãã®è©³ç´°ãªãã¬ã¼ã¹ããã¯å½¢å¼ py.test --tb=native # Python æ¨æºã©ã¤ãã©ãªã®å½¢å¼ py.test --tb=short # çããã¬ã¼ã¹ããã¯å½¢å¼ py.test --tb=line # 失æãããã¹ãã1è¡è¡¨ç¤º .. Dropping to PDB (Python Debugger) on failures ---------------------------------------------- 失æããã¨ãã« PDB (Python ãããã¬ã¼) ãèµ·å --------------------------------------------- .. _PDB: http://docs.python.org/library/pdb.html .. Python comes with a builtin Python debugger called PDB_. ``py.test`` allows one to drop into the PDB prompt via a command line option:: Python ã«ã¯ PDB_ ã¨ããçµã¿è¾¼ã¿ã® Python ãããã¬ã¼ãä»å±ãã¦ãã¾ãã ``py.test`` ã¯ãã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã§ PDB ããã³ãããèµ·åã§ãã¾ã:: py.test --pdb .. This will invoke the Python debugger on every failure. Often you might only want to do this for the first failing test to understand a certain failure situation:: ãã®ãªãã·ã§ã³ã¯ããã¹ãã失æããã¨ãã« Python ãããã¬ã¼ãèµ·åãã¾ããå¤ãã®å ´åãç¹å®ã®ã¨ã©ã¼ç¶æ³ãææ¡ããã®ã«æåã«å¤±æããã¨ãã®ã¿ãããã¬ã¼ãèµ·åãããã¯ãã§ã:: py.test -x --pdb # æåã«ãã¹ãã失æããã¨ãã« PDB ãèµ·åãã¦ãã¹ãã»ãã·ã§ã³ãçµäº py.test --pdb --maxfail=3 # æåã® 3 åã®å¤±æã«å¯¾ã㦠PDB ãèµ·å .. Setting a breakpoint / aka ``set_trace()`` ---------------------------------------------------- ``set_trace()`` ã¨ãããã¬ã¼ã¯ãã¤ã³ãã®è¨å® -------------------------------------------- .. If you want to set a breakpoint and enter the ``pdb.set_trace()`` you can use a helper:: ãã¬ã¼ã¯ãã¤ã³ããè¨å®ã㦠``pdb.set_trace()`` ãè¡ããããªãããã«ãã¼é¢æ°ã使ãã¾ã:: import pytest def test_function(): ... pytest.set_trace() # PDB ãããã¬ã¼ãèµ·åãã¦ãã¬ã¼ã¹ãã .. versionadded: 2.0.0 .. In previous versions you could only enter PDB tracing if you disabled capturing on the command line via ``py.test -s``. 以åã®ãã¼ã¸ã§ã³ã§ã¯ãã³ãã³ãã©ã¤ã³ãã ``py.test -s`` ã§æ¨æºåºåã®åå¾ãç¡å¹ã«ããå ´åã®ã¿ãPDB ãã¬ã¼ã¹ãå¯è½ã§ããã .. _durations: ãã¹ãã®å®è¡æéããããã¡ã¤ãªã³ã° ---------------------------------- .. Profiling test execution duration ------------------------------------- .. versionadded: 2.2 .. To get a list of the slowest 10 test durations:: æãé ã 10 åã®ãã¹ãä¸è¦§ãåå¾ããã«ã¯ã次ã®ããã«ãã¾ã:: py.test --durations=10 .. Creating JUnitXML format files ---------------------------------------------------- JUnitXML å½¢å¼ã®ãã¡ã¤ã«ä½æ --------------------------- .. To create result files which can be read by Hudson_ or other Continuous integration servers, use this invocation:: Hudson_ ããã®ä»ã®ç¶ç¶çã¤ã³ãã°ã¬ã¼ã·ã§ã³ãµã¼ãã¼ã§èªã¿è¾¼ããçµæãã¡ã¤ã«ãä½æããã«ã¯ã次ã®ããã«å®è¡ãã¾ã:: py.test --junitxml=path .. to create an XML file at ``path``. ``path`` ã« XML ãã¡ã¤ã«ãä½æããã¾ãã .. Creating resultlog format files ---------------------------------------------------- resultlog å½¢å¼ã®ãã¡ã¤ã«ä½æ ---------------------------- .. To create plain-text machine-readable result files you can issue:: ã³ã³ãã¥ã¼ã¿ã¼ãèªããå¹³æã®çµæãã¡ã¤ã«ãä½æããã«ã¯ã次ã®ããã«ãã¾ã:: py.test --resultlog=path .. and look at the content at the ``path`` location. Such files are used e.g. by the `PyPy-test`_ web page to show test results over several revisions. ``path`` ã«ä½æããããã¡ã¤ã«ãããã¾ãããããã£ããã¡ã¤ã«ã¯ãä¾ãã° `PyPy-test`_ ã§è¤æ°ãã¼ã¸ã§ã³ã§ã®ãã¹ãçµæã表示ããã®ã«ä½¿ããã¾ãã .. _`PyPy-test`: http://codespeak.net:8099/summary .. Sending test report to pocoo pastebin service ----------------------------------------------------- pocoo pastbin ãµã¼ãã¹ã«ãã¹ãçµæãæ稿 ---------------------------------------- .. **Creating a URL for each test failure**:: **ãã¹ãã失æããæ¯ã« URL ãä½æãã¾ã**:: py.test --pastebin=failed .. This will submit test run information to a remote Paste service and provide a URL for each failure. You may select tests as usual or add for example ``-x`` if you only want to send one particular failure. ããã¯ãªã¢ã¼ãã® Paste ãµã¼ãã¹ã¸ãã¹ãã®å®è¡æ å ±ãæ稿ãã¦ã失æãããã¹ãæ¯ã« URL ãæä¾ãã¾ããç¹å®ã®ã¨ã©ã¼ã®ã¿ãæ稿ãããå ´åãæ®éã«ãã¹ããé¸æããããä¾ãã° ``-x`` ã追å ãã¾ãã .. **Creating a URL for a whole test session log**:: **å ¨ã¦ã®ãã¹ãã»ãã·ã§ã³ãã°ã«å¯¾ãã¦1ã¤ã® URL ãä½æãã¾ã**:: py.test --pastebin=all .. Currently only pasting to the http://paste.pocoo.org service is implemented. ãã¾ã®ã¨ããã¯ãhttp://paste.pocoo.org ãµã¼ãã¹ã¸ã®ãã¼ã¹ãã®ã¿ãå®è£ ããã¦ãã¾ãã .. Calling pytest from Python code ---------------------------------------------------- Python ã³ã¼ãããã® pytest å¼ã³åºã ----------------------------------- .. versionadded:: 2.0 .. You can invoke ``py.test`` from Python code directly:: Python ã³ã¼ãããç´æ¥ ``py.test`` ãå¼ã³åºãã¾ã:: pytest.main() .. this acts as if you would call "py.test" from the command line. It will not raise ``SystemExit`` but return the exitcode instead. You can pass in options and arguments:: ããã¯ã³ãã³ãã©ã¤ã³ãã "py.test" ãå¼ã³åºãããã«åä½ãã¾ãã ``SystemExit`` ãçºçãããªã代ããã«çµäºã³ã¼ããè¿ãã¾ãã次ã®ããã«ãªãã·ã§ã³ã¨å¼æ°ã渡ãã¾ã:: pytest.main(['-x', 'mytestdir']) .. or pass in a string:: ã¾ãã¯ãæååã§æ¸¡ãã¾ã:: pytest.main("-x mytestdir") .. You can specify additional plugins to ``pytest.main``:: ``pytest.main`` ã«è¿½å ã®ãã©ã°ã¤ã³ãæå®ã§ãã¾ã:: # myinvoke.py ã®å 容 import pytest class MyPlugin: def pytest_sessionfinish(self): print("*** test run reporting finishing") pytest.main("-qq", plugins=[MyPlugin()]) .. Running it will show that ``MyPlugin`` was added and its hook was invoked:: ãã®ã³ã¼ããå®è¡ãã㨠``MyPlugin`` ã追å ããããã®ããã¯ãå®è¡ããããã¨ã表示ãã¾ã:: $ python myinvoke.py collecting ... collected 0 items in 0.00 seconds *** test run reporting finishing .. include:: links.inc