.. _`mark examples`: ã«ã¹ã¿ã ãã¼ã«ã¼ã使ã ====================== .. Working with custom markers ================================================= .. Here are some example using the :ref:`mark` mechanism. ããã§ã¯ :ref:`mark` ã®ä»çµã¿ã使ã£ããµã³ãã«ãç´¹ä»ãã¾ãã .. Marking test functions and selecting them for a run ---------------------------------------------------- ãã¹ãé¢æ°ããã¼ã¯ãã¦å®è¡æã«é¸æ ---------------------------------- .. You can "mark" a test function with custom metadata like this:: 次ã®ããã«ã«ã¹ã¿ã ã¡ã¿ãã¼ã¿ã§ãã¹ãé¢æ°ã "ãã¼ã¯" ã§ãã¾ã:: # test_server.py ã®å 容 import pytest @pytest.mark.webtest def test_send_http(): pass # ã¢ããªã® webtest ãã¹ããå®è¡ def test_something_quick(): pass .. versionadded:: 2.2 .. You can then restrict a test run to only run tests marked with ``webtest``:: ``webtest`` ã§ãã¼ã¯ããããã¹ãã®ã¿ãå®è¡ããããã«å¶éã§ãã¾ã:: $ py.test -v -m webtest =========================== test session starts ============================ platform linux2 -- Python 2.7.1 -- pytest-2.2.4 -- /home/hpk/venv/0/bin/python collecting ... collected 2 items test_server.py:3: test_send_http PASSED =================== 1 tests deselected by "-m 'webtest'" =================== ================== 1 passed, 1 deselected in 0.00 seconds ================== .. Or the inverse, running all tests except the webtest ones:: ãããã¯éã« webtest ãé¤ãå ¨ã¦ã®ãã¹ããå®è¡ãã¾ã:: $ py.test -v -m "not webtest" =========================== test session starts ============================ platform linux2 -- Python 2.7.1 -- pytest-2.2.4 -- /home/hpk/venv/0/bin/python collecting ... collected 2 items test_server.py:6: test_something_quick PASSED ================= 1 tests deselected by "-m 'not webtest'" ================= ================== 1 passed, 1 deselected in 0.00 seconds ================== .. Registering markers ------------------------------------- ãã¼ã«ã¼ã®ç»é² -------------- .. versionadded:: 2.2 .. Registering markers for your test suite is simple:: .. ini-syntax for custom markers: ãã¹ãã¹ã¤ã¼ãã«ãã¼ã«ã¼ãç»é²ããã®ã¯ç»é²ã§ã:: # pytest.ini ã®å 容 [pytest] markers = webtest: mark a test as a webtest. .. You can ask which markers exist for your test suite - the list includes our just defined ``webtest`` markers:: ãã¹ãã¹ã¤ã¼ãã«åå¨ãããã¼ã«ã¼ã調ã¹ã¾ãã次ã®ä¸è¦§ã§ã¯ãå ã»ã©å®ç¾©ãã ``webtest`` ãã¼ã«ã¼ãããã¾ã:: $ py.test --markers @pytest.mark.webtest: mark a test as a webtest. @pytest.mark.skipif(*conditions): skip the given test function if evaluation of all conditions has a True value. Evaluation happens within the module global context. Example: skipif('sys.platform == "win32"') skips the test if we are on the win32 platform. @pytest.mark.xfail(*conditions, reason=None, run=True): mark the the test function as an expected failure. Optionally specify a reason and run=False if you don't even want to execute the test function. Any positional condition strings will be evaluated (like with skipif) and if one is False the marker will not be applied. @pytest.mark.parametrize(argnames, argvalues): call a test function multiple times passing in multiple different argument value sets. Example: @parametrize('arg1', [1,2]) would lead to two calls of the decorated test function, one with arg1=1 and another with arg1=2. @pytest.mark.tryfirst: mark a hook implementation function such that the plugin machinery will try to call it first/as early as possible. @pytest.mark.trylast: mark a hook implementation function such that the plugin machinery will try to call it last/as late as possible. .. For an example on how to add and work with markers from a plugin, see :ref:`adding a custom marker from a plugin`. ãã©ã°ã¤ã³ãããã¼ã«ã¼ã追å ãã¦å¦çãããµã³ãã«ã«ã¤ãã¦ã¯ :ref:`adding a custom marker from a plugin` ãåç §ãã¦ãã ããã .. It is recommended to explicitely register markers so that: * there is one place in your test suite defining your markers * asking for existing markers via ``py.test --markers`` gives good output * typos in function markers are treated as an error if you use the ``--strict`` option. Later versions of py.test are probably going to treat non-registered markers as an error. .. note:: 次ã®ããã«æ示çã«ãã¼ã«ã¼ãç»é²ãããã¨ãæ¨å¥¨ãã¾ã: * ãã¹ãã¹ã¤ã¼ãã®ä¸ç®æã§ãã¼ã«ã¼ãå®ç¾©ãã * ``py.test --markers`` ã§æ¢åã®ãã¼ã«ã¼ã«é¢ããåããããã説æã表示ãã * ``--strict`` ãªãã·ã§ã³ã使ãã¨ãé¢æ°ãã¼ã«ã¼å ã®èª¤åãã¨ã©ã¼ã«ãã¾ããæè¿ã® py.test ãã¼ã¸ã§ã³ã§ã¯ãæªç»é²ãã¼ã«ã¼ãã¨ã©ã¼ã¨ãã¦æ±ãããã«ãã¦ãã¾ã .. _`scoped-marking`: ã¯ã©ã¹ã¾ãã¯ã¢ã¸ã¥ã¼ã«å ¨ä½ããã¼ãã³ã° -------------------------------------- .. Marking whole classes or modules ---------------------------------------------------- .. If you are programming with Python 2.6 or later you may use ``pytest.mark`` decorators with classes to apply markers to all of its test methods:: Python 2.6 ãããã以ä¸ã®ãã¼ã¸ã§ã³ã§ã³ã¼ãã£ã³ã°ãã¦ãããªããã¯ã©ã¹ã®ãã¹ãã¡ã½ããå ¨ã¦ã«ãã¼ã«ã¼ãé©ç¨ããããã« ``pytest.mark`` ãã¯ã©ã¹ãã³ã¬ã¼ã¿ã¼ã¨ãã¦ä½¿ãã¾ã:: # test_mark_classlevel.py ã®å 容 import pytest @pytest.mark.webtest class TestClass: def test_startup(self): pass def test_startup_and_more(self): pass .. This is equivalent to directly applying the decorator to the two test functions. ããã¯2ã¤ã®ãã¹ãé¢æ°ã«ç´æ¥ãã³ã¬ã¼ã¿ã¼ãé©ç¨ããã®ã¨åãã§ãã .. To remain backward-compatible with Python 2.4 you can also set a ``pytestmark`` attribute on a TestClass like this:: Pythn 2.4 ã¨ã®å¾æ¹äºææ§ãç¶æããã«ã¯ã次ã®ããã« TestClass ã« ``pytestmark`` å±æ§ãè¨å®ã§ãã¾ã:: import pytest class TestClass: pytestmark = pytest.mark.webtest .. or if you need to use multiple markers you can use a list:: ãããã¯ãè¤æ°ã®ãã¼ã«ã¼ã使ãå¿ è¦ãããå ´åã¯ãªã¹ãã使ãã¾ã:: import pytest class TestClass: pytestmark = [pytest.mark.webtest, pytest.mark.slowtest] .. You can also set a module level marker:: ã¢ã¸ã¥ã¼ã«ã¬ãã«ã®ãã¼ã«ã¼ãè¨å®ã§ãã¾ã:: import pytest pytestmark = pytest.mark.webtest .. in which case it will be applied to all functions and methods defined in the module. ãã®å ´åããã®ã¢ã¸ã¥ã¼ã«å ã§å®ç¾©ããã¦ããå ¨ã¦ã®é¢æ°ã¨ã¡ã½ããã«é©ç¨ããã¾ãã .. Using ``-k TEXT`` to select tests ---------------------------------------------------- ``-k TEXT`` ã使ã£ããã¹ãã®é¸æ -------------------------------- .. You can use the ``-k`` command line option to only run tests with names matching the given argument:: æå®ããå¼æ°ã«ä¸è´ããååã®ãã¹ããå®è¡ããã«ã¯ ``-k`` ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã使ãã¾ã:: $ py.test -k send_http # åç¯ã§å®ç¾©ãããµã³ãã«ãå®è¡ =========================== test session starts ============================ platform linux2 -- Python 2.7.1 -- pytest-2.2.4 collecting ... collected 4 items test_server.py . =================== 3 tests deselected by '-ksend_http' ==================== ================== 1 passed, 3 deselected in 0.01 seconds ================== .. And you can also run all tests except the ones that match the keyword:: ã¾ãããã®ãã¼ã¯ã¼ãã«ä¸è´ãããã®ãé¤ãå ¨ã¦ã®ãã¹ããå®è¡ãããã¨ãã§ãã¾ã:: $ py.test -k-send_http =========================== test session starts ============================ platform linux2 -- Python 2.7.1 -- pytest-2.2.4 collecting ... collected 4 items test_mark_classlevel.py .. test_server.py . =================== 1 tests deselected by '-k-send_http' =================== ================== 3 passed, 1 deselected in 0.01 seconds ================== .. Or to only select the class:: ãããã¯ãã¯ã©ã¹ã®ã¿ãé¸æããã«ã¯æ¬¡ã®ããã«ãã¾ã:: $ py.test -kTestClass =========================== test session starts ============================ platform linux2 -- Python 2.7.1 -- pytest-2.2.4 collecting ... collected 4 items test_mark_classlevel.py .. =================== 2 tests deselected by '-kTestClass' ==================== ================== 2 passed, 2 deselected in 0.01 seconds ================== .. _`adding a custom marker from a plugin`: ã«ã¹ã¿ã ãã¼ã«ã¼ã¨ã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã«ãããã¹ãã®å®è¡å¶å¾¡ ---------------------------------------------------------------- .. Custom marker and command line option to control test runs ---------------------------------------------------------- .. regendoc:wipe .. Plugins can provide custom markers and implement specific behaviour based on it. This is a self-contained example which adds a command line option and a parametrized test function marker to run tests specifies via named environments:: ãã©ã°ã¤ã³ã¯ãã«ã¹ã¿ã ãã¼ã«ã¼ãæä¾ãã¦ããã®ãã¼ã«ã¼ã«åºã¥ãç¹å¥ãªæ¯ãèããå®è£ ãã¾ããããã¯ãã³ãã³ãã©ã¤ã³ãªãã·ã§ã³ã¨ãååä»ãã®ç°å¢ã®å¤ã«ç¹åãããã¹ããå®è¡ããããã®ãã©ã¡ã¼ã¿ã¼åããããã¹ãé¢æ°ãã¼ã«ã¼ã追å ããèªå·±å®çµåã®ãµã³ãã«ã§ã:: # conftest.py ã®å 容 import pytest def pytest_addoption(parser): parser.addoption("-E", dest="env", action="store", metavar="NAME", help="only run tests matching the environment NAME.") def pytest_configure(config): # 追å ã®ãã¼ã«ã¼ãç»é² config.addinivalue_line("markers", "env(name): mark test to run only on named environment") def pytest_runtest_setup(item): if not isinstance(item, item.Function): return if hasattr(item.obj, 'env'): envmarker = getattr(item.obj, 'env') envname = envmarker.args[0] if envname != item.config.option.env: pytest.skip("test requires env %r" % envname) .. A test file using this local plugin:: ãã® local ãã©ã°ã¤ã³ã使ããã¹ããã¡ã¤ã«ã§ã:: # test_someenv.py ã®å 容 import pytest @pytest.mark.env("stage1") def test_basic_db_operation(): pass .. and an example invocations specifying a different environment than what the test needs:: ãã®ãã¹ããå¿ è¦ã¨ãããã®ã§ã¯ãªãå¥ã®ç°å¢ãæå®ãã¦å®è¡ããä¾ã§ã:: $ py.test -E stage2 =========================== test session starts ============================ platform linux2 -- Python 2.7.1 -- pytest-2.2.4 collecting ... collected 1 items test_someenv.py s ======================== 1 skipped in 0.00 seconds ========================= .. and here is one that specifies exactly the environment needed:: ä»åº¦ã¯æ£ããå¿ è¦ã¨ããç°å¢ãæå®ãã¦å®è¡ãã¾ã:: $ py.test -E stage1 =========================== test session starts ============================ platform linux2 -- Python 2.7.1 -- pytest-2.2.4 collecting ... collected 1 items test_someenv.py . ========================= 1 passed in 0.00 seconds ========================= .. The ``--markers`` option always gives you a list of available markers:: ``--markers`` ãªãã·ã§ã³ã¯å©ç¨ã§ãããã¼ã«ã¼ã®ä¸è¦§ã表示ãã¾ã:: $ py.test --markers @pytest.mark.env(name): mark test to run only on named environment @pytest.mark.skipif(*conditions): skip the given test function if evaluation of all conditions has a True value. Evaluation happens within the module global context. Example: skipif('sys.platform == "win32"') skips the test if we are on the win32 platform. @pytest.mark.xfail(*conditions, reason=None, run=True): mark the the test function as an expected failure. Optionally specify a reason and run=False if you don't even want to execute the test function. Any positional condition strings will be evaluated (like with skipif) and if one is False the marker will not be applied. @pytest.mark.parametrize(argnames, argvalues): call a test function multiple times passing in multiple different argument value sets. Example: @parametrize('arg1', [1,2]) would lead to two calls of the decorated test function, one with arg1=1 and another with arg1=2. @pytest.mark.tryfirst: mark a hook implementation function such that the plugin machinery will try to call it first/as early as possible. @pytest.mark.trylast: mark a hook implementation function such that the plugin machinery will try to call it last/as late as possible. .. Reading markers which were set from multiple places ---------------------------------------------------- è¤æ°ã®å ´æããè¨å®ããããã¼ã«ã¼ãèªã¿è¾¼ã ------------------------------------------ .. versionadded: 2.2.2 .. If you are heavily using markers in your test suite you may encounter the case where a marker is applied several times to a test function. From plugin code you can read over all such settings. Example:: ãã¹ãã¹ã¤ã¼ãå ã§ãã¼ã«ã¼ããããã使ãã¨ããã¹ãé¢æ°ã«å¯¾ãã¦æ°åãã¼ã«ã¼ãé©ç¨ãããå ´åãããã¾ãããã©ã°ã¤ã³ã³ã¼ãããããããã£ãå ¨ã¦ã®è¨å®ãèªã¿è¾¼ãã¾ãããµã³ãã«ãç´¹ä»ãã¾ã:: # test_mark_three_times.py ã®å 容 import pytest pytestmark = pytest.mark.glob("module", x=1) @pytest.mark.glob("class", x=2) class TestClass: @pytest.mark.glob("function", x=3) def test_something(self): pass .. Here we have the marker "glob" applied three times to the same test function. From a conftest file we can read it like this:: ããã§ã¯ãåããã¹ãé¢æ°ã«å¯¾ãã¦3åé©ç¨ããã "glob" ãã¼ã«ã¼ãããã¾ããconftest ãã¡ã¤ã«ãã次ã®ããã«ãã¦ããã調ã¹ã¾ã:: # conftest.py ã®å 容 def pytest_runtest_setup(item): g = getattr(item.obj, 'glob', None) if g is not None: for info in g: print ("glob args=%s kwargs=%s" %(info.args, info.kwargs)) .. Let's run this without capturing output and see what we get:: æ¨æºåºåãåå¾ããã«ãã®ãã¹ããå®è¡ãã¦ãä½ã表示ãããããè¦ã¦ã¿ã¾ããã:: $ py.test -q -s collecting ... collected 2 items .. 2 passed in 0.01 seconds glob args=('function',) kwargs={'x': 3} glob args=('class',) kwargs={'x': 2} glob args=('module',) kwargs={'x': 1}