.. _`skip and xfail`: skip 㨠xfail: æåããªããã¹ããæ±ã ===================================== .. Skip and xfail: dealing with tests that can not succeed ===================================================================== .. If you have test functions that cannot be run on certain platforms or that you expect to fail you can mark them accordingly or you may call helper functions during execution of setup or test functions. ãã¹ãé¢æ°ããç¹å®ã®ãã©ãããã©ã¼ã ã§å®è¡ã§ããªããã¾ãã¯ãã¼ã¯ãããã®ã失æãããã¨ãæå¾ ãããã¾ãã¯ãã¹ãé¢æ°ã setup é¢æ°ã®å®è¡ä¸ã«ãã«ãã¼é¢æ°ãå¼ã³åºãã¨ãã£ãå ´åãããã¾ãã .. A *skip* means that you expect your test to pass unless a certain configuration or condition (e.g. wrong Python interpreter, missing dependency) prevents it to run. And *xfail* means that your test can run but you expect it to fail because there is an implementation problem. *skip* ã¯ãç¹å®ã®è¨å®ãæ¡ä»¶ (誤ã£ã Python ã¤ã³ã¿ã¼ããªã¿ã¼ãä¾åé¢ä¿ã®æ¬ è½ãªã©) ããã¹ãé¢æ°ãå®è¡ãããªãå ´åãé¤ãã°ããã¹ããæåããã®ãæå¾ ãã¾ãã *xfail* ã¯ããã¹ããã®ãã®ã¯å®è¡ãã¾ãããå®è£ ä¸ã®åé¡ãããã®ãã¹ãã失æãããã¨ãæå¾ ãã¾ãã .. py.test counts and lists *skip* and *xfail* tests separately. However, detailed information about skipped/xfailed tests is not shown by default to avoid cluttering the output. You can use the ``-r`` option to see details corresponding to the "short" letters shown in the test progress:: py.test 㯠*skip* 㨠*xfail* ãã¹ããå¥ã ã«æ°ãã¦ä¸è¦§è¡¨ç¤ºãã¾ããããããã¹ãããããï¼å¤±æãããã¹ããã¹ãã«ã¤ãã¦ã®è©³ç´°ãªæ å ±ã¯ãåºåå 容ããã¡ããã¡ãã«ãªããªãããã«ããã©ã«ãã§ã¯è¡¨ç¤ºããã¾ããããã¹ãã®é²æç¶æ³ã表示ãã "çã" æåã«å¯¾å¿ãã詳細ãè¦ãããã« ``-r`` ãªãã·ã§ã³ã使ãã¾ã:: py.test -rxs # skips 㨠xfails ã®è£è¶³æ å ±ã表示 .. (See :ref:`how to change command line options defaults`) (:ref:`how to change command line options defaults` ãåç §) .. _skipif: ã¹ããããããã¹ãé¢æ°ã®ãã¼ã¯ ------------------------------ .. Marking a test function to be skipped ------------------------------------------- .. Here is an example of marking a test function to be skipped when run on a Python3 interpreter:: Python 3 ã¤ã³ã¿ã¼ããªã¿ã¼ã§å®è¡ããã¨ãã«ã¹ããããããã¹ãé¢æ°ããã¼ã¯ããä¾ãç´¹ä»ãã¾ã:: import sys @pytest.mark.skipif("sys.version_info >= (3,0)") def test_function(): ... .. During test function setup the skipif condition is evaluated by calling ``eval('sys.version_info >= (3,0)', namespace)``. (*New in version 2.0.2*) The namespace contains all the module globals of the test function so that you can for example check for versions of a module you are using:: ãã¹ãé¢æ°ã® setup å¦çä¸ã« skipif ã®æ¡ä»¶ã ``eval('sys.version_info >= (3,0)', namespace)`` ãå¼ã³åºããã¨ã«ããè©ä¾¡ããã¾ã (*ãã¼ã¸ã§ã³ 2.0.2 ã§è¿½å *) ããã®åå空éã¯ãä¾ãã°ä½¿ã£ã¦ããã¢ã¸ã¥ã¼ã«ã®ãã¼ã¸ã§ã³ããã§ãã¯ã§ããããã«ããã¹ãé¢æ°ã®å ¨ã¢ã¸ã¥ã¼ã«ã® globals ãå«ã¿ã¾ã:: import mymodule @pytest.mark.skipif("mymodule.__version__ < '1.2'") def test_function(): ... .. The test function will not be run ("skipped") if ``mymodule`` is below the specified version. The reason for specifying the condition as a string is mainly that py.test can report a summary of skip conditions. For information on the construction of the ``namespace`` see `evaluation of skipif/xfail conditions`_. ãã®ãã¹ãé¢æ°ã¯ã ``mymodule`` ãæå®ãããã¼ã¸ã§ã³ããä½ãã¨ãã«ã¯å®è¡ããã¾ãã ("ã¹ãããããã") ããããã£ãæ¡ä»¶ãæååã§æå®ãã主ãªçç±ã¯ãpy.test ã skip æ¡ä»¶ã®æ¦è¦ãã¬ãã¼ãã§ããããã§ãã ``namespace`` ã®æ§ç¯ã«é¢ãã詳細㯠:ref:`evaluation of skipif/xfail conditions` ãåç §ãã¦ãã ããã .. You can of course create a shortcut for your conditional skip decorator at module level like this:: 次ã®ããã«ã¢ã¸ã¥ã¼ã«ã¬ãã«ã§æ¡ä»¶ä»ãã® skipif ãã³ã¬ã¼ã¿ã¼ã®ã·ã§ã¼ãã«ãããä½æã§ãã¾ã:: win32only = pytest.mark.skipif("sys.platform != 'win32'") @win32only def test_function(): ... .. Skip all test functions of a class -------------------------------------- ã¯ã©ã¹ã®å ¨ãã¹ãé¢æ°ã®ã¹ããã ------------------------------ .. As with all function :ref:`marking <mark>` you can skip test functions at the `whole class- or module level`_. Here is an example for skipping all methods of a test class based on the platform:: å ¨ã¦ã®é¢æ°ã :ref:`ãã¼ã¯ãã <mark>` ã®ã¨åæ§ã« `ã¯ã©ã¹å ¨ä½ã¾ãã¯ã¢ã¸ã¥ã¼ã«ã¬ãã«`_ ã§ãã¹ãé¢æ°ãã¹ãããã§ãã¾ãããã©ãããã©ã¼ã ã«ãããã¹ãã¯ã©ã¹ã®å ¨ã¡ã½ãããã¹ããããããµã³ãã«ãç´¹ä»ãã¾ã:: class TestPosixCalls: pytestmark = pytest.mark.skipif("sys.platform == 'win32'") def test_function(self): "will not be setup or run under 'win32' platform" .. The ``pytestmark`` special name tells py.test to apply it to each test function in the class. If your code targets python2.6 or above you can more naturally use the skipif decorator (and any other marker) on classes:: ``pytestmark`` ã¨ããç¹å¥ãªååã使ã£ã¦ãã¯ã©ã¹å ã®åãã¹ãé¢æ°ã¸ã»ããããé¢æ°ã pytest ã«é©ç¨ããã¾ãããã¹ãã³ã¼ãã Python 2.6 以ä¸ãæ³å®ãã¦ãããªãããã£ã¨èªç¶ã« skipif ãã³ã¬ã¼ã¿ã¼ (ãã®ä»ã®ä»»æã®ãã¼ã«ã¼) ãã¯ã©ã¹ã«å¯¾ãã¦é©ç¨ã§ãã¾ã:: @pytest.mark.skipif("sys.platform == 'win32'") class TestPosixCalls: def test_function(self): "will not be setup or run under 'win32' platform" .. Using multiple "skipif" decorators on a single function is generally fine - it means that if any of the conditions apply the function execution will be skipped. 1ã¤ã®é¢æ°ã«è¤æ°ã® "skipif" ãã³ã¬ã¼ã¿ã¼ã使ããã¨ã¯ä¸è¬çã«è¯ããã¨ã§ããããããã®æ¡ä»¶ãé©ç¨ãããå ´åã«ãã®ãã¹ãé¢æ°ã®å®è¡ã¯ã¹ãããããããã¨ã«ãªãã¾ãã .. _`whole class- or module level`: mark.html#scoped-marking .. _`ã¯ã©ã¹å ¨ä½ã¾ãã¯ã¢ã¸ã¥ã¼ã«ã¬ãã«`: mark.html#scoped-marking .. _xfail: 失æãæå¾ ãããã¹ãé¢æ°ã®ãã¼ã¯ -------------------------------- .. Mark a test function as expected to fail ------------------------------------------------------- .. You can use the ``xfail`` marker to indicate that you expect the test to fail:: ãã¹ãã®å¤±æãæå¾ ãã¦ãããã¨ã表ãã®ã« ``xfail`` ãã¼ã«ã¼ã使ãã¾ã:: @pytest.mark.xfail def test_function(): ... .. This test will be run but no traceback will be reported when it fails. Instead terminal reporting will list it in the "expected to fail" or "unexpectedly passing" sections. ãã®ãã¹ãã¯å®è¡ããã¾ããã失æããã¨ãã«ãã¬ã¼ã¹ããã¯ã表示ãã¾ããããã®ä»£ãããã¿ã¼ããã«ä¸ã« "expected to fail" ã "unexpectedly passing" ã»ã¯ã·ã§ã³ã«ãã®ä¸è¦§ã表示ããã¾ãã .. By specifying on the commandline:: ã³ãã³ãã©ã¤ã³ã§æ¬¡ã®ããã«æå®ããã¨:: pytest --runxfail .. you can force the running and reporting of an ``xfail`` marked test as if it weren't marked at all. ``xfail`` ã§ãã¼ã¯ããã¦ããªããã®ããã« ``xfail`` ã§ãã¼ã¯ããããã¹ãé¢æ°ãå®è¡ãã¦ã¬ãã¼ãã®è¡¨ç¤ºãå¼·å¶ã§ãã¾ãã .. As with skipif_ you can also mark your expectation of a failure on a particular platform:: skipif_ ã¨åæ§ã«ãç¹å®ã®ãã©ãããã©ã¼ã ã§ã®å¤±æãæå¾ ããããã«ããã¼ã¯ã§ãã¾ã:: @pytest.mark.xfail("sys.version_info >= (3,0)") def test_function(): ... .. You can furthermore prevent the running of an "xfail" test or specify a reason such as a bug ID or similar. Here is a simple test file with the several usages: ããã«ããã° ID ã¨ãã£ã reason ãæå®ã㦠"xfail" ãã¹ããå®è¡ããªãããã«ãã§ãã¾ããä»ã«ã使ç¨ä¾ã¨ç°¡åãªãã¹ããç´¹ä»ãã¾ã: .. literalinclude:: example/xfail_demo.py .. Running it with the report-on-xfail option gives this output:: 次ã®ããã« xfail ã®ã¬ãã¼ãã表示ãããªãã·ã§ã³ãæå®ãã¦å®è¡ãã¾ã:: example $ py.test -rx xfail_demo.py =========================== test session starts ============================ platform linux2 -- Python 2.7.1 -- pytest-2.2.4 collecting ... collected 6 items xfail_demo.py xxxxxx ========================= short test summary info ========================== XFAIL xfail_demo.py::test_hello XFAIL xfail_demo.py::test_hello2 reason: [NOTRUN] XFAIL xfail_demo.py::test_hello3 condition: hasattr(os, 'sep') XFAIL xfail_demo.py::test_hello4 bug 110 XFAIL xfail_demo.py::test_hello5 condition: pytest.__version__[0] != "17" XFAIL xfail_demo.py::test_hello6 reason: reason ======================== 6 xfailed in 0.03 seconds ========================= .. _`evaluation of skipif/xfail conditions`: skipif/xfail å¼ã®è©ä¾¡ --------------------- .. Evaluation of skipif/xfail expressions ---------------------------------------------------- .. versionadded:: 2.0.2 .. The evaluation of a condition string in ``pytest.mark.skipif(conditionstring)`` or ``pytest.mark.xfail(conditionstring)`` takes place in a namespace dictionary which is constructed as follows: ``pytest.mark.skipif(conditionstring)`` ã¾ã㯠``pytest.mark.xfail(conditionstring)`` ã®æ¡ä»¶æååã®è©ä¾¡ã¯ã次ã®ããã«æ§ç¯ãããåå空éãã£ã¯ã·ã§ããªã®ä¸ã§è¡ããã¾ã: .. * the namespace is initialized by putting the ``sys`` and ``os`` modules and the pytest ``config`` object into it. * ``sys`` 㨠``os`` ã¢ã¸ã¥ã¼ã«ã¨ pytest ã® ``config`` ãªãã¸ã§ã¯ããå ãã¦åå空éãåæåããã .. * updated with the module globals of the test function for which the expression is applied. * ãã®å¼ã®è©ä¾¡ãé©ç¨ãããã¹ãé¢æ°ã®ã¢ã¸ã¥ã¼ã«ã® globals ãæ´æ°ããã .. The pytest ``config`` object allows you to skip based on a test configuration value which you might have added:: pytest ã® ``config`` ãªãã¸ã§ã¯ãã使ã£ã¦ã追å ãããã¹ãè¨å®å¤ã«ããã¹ãããããã¾ã:: @pytest.mark.skipif("not config.getvalue('db')") def test_function(...): ... .. Imperative xfail from within a test or setup function ------------------------------------------------------ ãã¹ãé¢æ°ã¾ã㯠setup é¢æ°å ããå½ä»¤å xfail --------------------------------------------- .. If you cannot declare xfail-conditions at import time you can also imperatively produce an XFail-outcome from within test or setup code. Example:: ã¤ã³ãã¼ãæã« xfail ã®æ¡ä»¶ã宣è¨ã§ããªãå ´åããã¹ãé¢æ°ã¾ã㯠setup ã³ã¼ãå ãã xfail ããããã«å½ä»¤çã«è¨è¿°ã§ãã¾ãããµã³ãã«ãç´¹ä»ãã¾ã:: def test_function(): if not valid_config(): pytest.xfail("unsupported configuration") .. Skipping on a missing import dependency -------------------------------------------------- ã¤ã³ãã¼ãã®ä¾åé¢ä¿ã®æ¬ è½ãã¹ããã ------------------------------------ .. You can use the following import helper at module level or within a test or test setup function:: ã¢ã¸ã¥ã¼ã«ã¬ãã«ãã¾ãã¯ãã¹ãé¢æ°ã setup é¢æ°å ãã次ã®ã¤ã³ãã¼ããã«ãã¼ã使ãã¾ã:: docutils = pytest.importorskip("docutils") .. If ``docutils`` cannot be imported here, this will lead to a skip outcome of the test. You can also skip based on the version number of a library:: ãã ``docutils`` ããã®å ´æã§ã¤ã³ãã¼ãã§ããªããªãããã®ãã¹ãã¯ã¹ãããããã¾ããã©ã¤ãã©ãªã®ãã¼ã¸ã§ã³ã«ããã¹ãããããããã¨ãã§ãã¾ã:: docutils = pytest.importorskip("docutils", minversion="0.3") .. The version will be read from the specified module's ``__version__`` attribute. ãã®ãã¼ã¸ã§ã³ã¯æå®ããã¢ã¸ã¥ã¼ã«ã® ``__ version__`` å±æ§ããèªã¿è¾¼ã¾ãã¾ãã .. Imperative skip from within a test or setup function ------------------------------------------------------ ãã¹ãé¢æ°ã¾ã㯠setup é¢æ°å ããå½ä»¤åã¹ããã ----------------------------------------------- .. If for some reason you cannot declare skip-conditions you can also imperatively produce a skip-outcome from within test or setup code. Example:: ä½ããã®çç±ã§ã¹ãããæ¡ä»¶ã宣è¨ã§ããªãå ´åãããã¹ãé¢æ°ã¾ã㯠setup ã³ã¼ãå ããã¹ãããããããã«å½ä»¤çã«è¨è¿°ã§ãã¾ãããµã³ãã«ãç´¹ä»ãã¾ã:: def test_function(): if not valid_config(): pytest.skip("unsupported configuration")