This file is indexed.

/usr/share/doc/python-pytest-doc/html/_sources/example/markers.txt is in python-pytest-doc 2.6.3-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
.. _`mark examples`:

Working with custom markers
=================================================

Here are some example using the :ref:`mark` mechanism.

Marking test functions and selecting them for a run
----------------------------------------------------

You can "mark" a test function with custom metadata like this::

    # content of test_server.py

    import pytest
    @pytest.mark.webtest
    def test_send_http():
        pass # perform some webtest test for your app
    def test_something_quick():
        pass
    def test_another():
        pass
    class TestClass:
        def test_method(self):
            pass

.. versionadded:: 2.2

You can then restrict a test run to only run tests marked with ``webtest``::

    $ py.test -v -m webtest
    =========================== test session starts ============================
    platform linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3 -- /home/hpk/p/pytest/.tox/regen/bin/python3.4
    collecting ... collected 4 items
    
    test_server.py::test_send_http PASSED
    
    =================== 3 tests deselected by "-m 'webtest'" ===================
    ================== 1 passed, 3 deselected in 0.01 seconds ==================

Or the inverse, running all tests except the webtest ones::

    $ py.test -v -m "not webtest"
    =========================== test session starts ============================
    platform linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3 -- /home/hpk/p/pytest/.tox/regen/bin/python3.4
    collecting ... collected 4 items
    
    test_server.py::test_something_quick PASSED
    test_server.py::test_another PASSED
    test_server.py::TestClass::test_method PASSED
    
    ================= 1 tests deselected by "-m 'not webtest'" =================
    ================== 3 passed, 1 deselected in 0.01 seconds ==================

Selecing tests based on their node ID
-------------------------------------

You can provide one or more :ref:`node IDs <node-id>` as positional
arguments to select only specified tests. This makes it easy to select
tests based on their module, class, method, or function name::

    $ py.test -v test_server.py::TestClass::test_method
    =========================== test session starts ============================
    platform linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3 -- /home/hpk/p/pytest/.tox/regen/bin/python3.4
    collecting ... collected 5 items
    
    test_server.py::TestClass::test_method PASSED
    
    ========================= 1 passed in 0.01 seconds =========================

You can also select on the class::

    $ py.test -v test_server.py::TestClass
    =========================== test session starts ============================
    platform linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3 -- /home/hpk/p/pytest/.tox/regen/bin/python3.4
    collecting ... collected 4 items
    
    test_server.py::TestClass::test_method PASSED
    
    ========================= 1 passed in 0.01 seconds =========================

Or select multiple nodes::

  $ py.test -v test_server.py::TestClass test_server.py::test_send_http
  =========================== test session starts ============================
  platform linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3 -- /home/hpk/p/pytest/.tox/regen/bin/python3.4
  collecting ... collected 8 items
  
  test_server.py::TestClass::test_method PASSED
  test_server.py::test_send_http PASSED
  
  ========================= 2 passed in 0.01 seconds =========================

.. _node-id:

.. note::

    Node IDs are of the form ``module.py::class::method`` or
    ``module.py::function``.  Node IDs control which tests are
    collected, so ``module.py::class`` will select all test methods
    on the class.  Nodes are also created for each parameter of a
    parametrized fixture or test, so selecting a parametrized test
    must include the parameter value, e.g.
    ``module.py::function[param]``.

    Node IDs for failing tests are displayed in the test summary info
    when running py.test with the ``-rf`` option.  You can also
    construct Node IDs from the output of ``py.test --collectonly``.

Using ``-k expr`` to select tests based on their name
-------------------------------------------------------

.. versionadded: 2.0/2.3.4

You can use the ``-k`` command line option to specify an expression
which implements a substring match on the test names instead of the
exact match on markers that ``-m`` provides.  This makes it easy to
select tests based on their names::

    $ py.test -v -k http  # running with the above defined example module
    =========================== test session starts ============================
    platform linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3 -- /home/hpk/p/pytest/.tox/regen/bin/python3.4
    collecting ... collected 4 items
    
    test_server.py::test_send_http PASSED
    
    ====================== 3 tests deselected by '-khttp' ======================
    ================== 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 "not send_http" -v
    =========================== test session starts ============================
    platform linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3 -- /home/hpk/p/pytest/.tox/regen/bin/python3.4
    collecting ... collected 4 items
    
    test_server.py::test_something_quick PASSED
    test_server.py::test_another PASSED
    test_server.py::TestClass::test_method PASSED
    
    ================= 1 tests deselected by '-knot send_http' ==================
    ================== 3 passed, 1 deselected in 0.02 seconds ==================

Or to select "http" and "quick" tests::

    $ py.test -k "http or quick" -v
    =========================== test session starts ============================
    platform linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3 -- /home/hpk/p/pytest/.tox/regen/bin/python3.4
    collecting ... collected 4 items
    
    test_server.py::test_send_http PASSED
    test_server.py::test_something_quick PASSED
    
    ================= 2 tests deselected by '-khttp or quick' ==================
    ================== 2 passed, 2 deselected in 0.01 seconds ==================

.. note::

    If you are using expressions such as "X and Y" then both X and Y
    need to be simple non-keyword names.  For example, "pass" or "from"
    will result in SyntaxErrors because "-k" evaluates the expression.

    However, if the "-k" argument is a simple string, no such restrictions
    apply.  Also "-k 'not STRING'" has no restrictions.  You can also
    specify numbers like "-k 1.3" to match tests which are parametrized
    with the float "1.3".

Registering markers
-------------------------------------

.. versionadded:: 2.2

.. ini-syntax for custom markers:

Registering markers for your test suite is simple::

    # content of 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::

    $ py.test --markers
    @pytest.mark.webtest: mark a test as a webtest.
    
    @pytest.mark.skipif(condition): skip the given test function if eval(condition) results in 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. see http://pytest.org/latest/skipping.html
    
    @pytest.mark.xfail(condition, reason=None, run=True, raises=None): mark the the test function as an expected failure if eval(condition) has a True value. Optionally specify a reason for better reporting and run=False if you don't even want to execute the test function. If only specific exception(s) are expected, you can list them in raises, and if the test fails in other ways, it will be reported as a true failure. See http://pytest.org/latest/skipping.html
    
    @pytest.mark.parametrize(argnames, argvalues): call a test function multiple times passing in different arguments in turn. argvalues generally needs to be a list of values if argnames specifies only one name or a list of tuples of values if argnames specifies multiple names. Example: @parametrize('arg1', [1,2]) would lead to two calls of the decorated test function, one with arg1=1 and another with arg1=2.see http://pytest.org/latest/parametrize.html for more info and examples.
    
    @pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see http://pytest.org/latest/fixture.html#usefixtures 
    
    @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`.

.. note::

    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. Future versions of ``pytest`` are probably
      going to start treating non-registered markers as errors at some point.

.. _`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::

    # content of 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.

To remain backward-compatible with Python 2.4 you can also set a
``pytestmark`` attribute on a TestClass like this::

    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.

.. _`marking individual tests when using parametrize`:

Marking individual tests when using parametrize
-----------------------------------------------

When using parametrize, applying a mark will make it apply
to each individual test. However it is also possible to
apply a marker to an individual test instance::

    import pytest

    @pytest.mark.foo
    @pytest.mark.parametrize(("n", "expected"), [
        (1, 2),
        pytest.mark.bar((1, 3)),
        (2, 3),
    ])
    def test_increment(n, expected):
         assert n + 1 == expected

In this example the mark "foo" will apply to each of the three
tests, whereas the "bar" mark is only applied to the second test.
Skip and xfail marks can also be applied in this way, see :ref:`skip/xfail with parametrize`.


.. _`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::

    # content of conftest.py

    import pytest
    def pytest_addoption(parser):
        parser.addoption("-E", action="store", metavar="NAME",
            help="only run tests matching the environment NAME.")

    def pytest_configure(config):
        # register an additional marker
        config.addinivalue_line("markers",
            "env(name): mark test to run only on named environment")

    def pytest_runtest_setup(item):
        envmarker = item.get_marker("env")
        if envmarker is not None:
            envname = envmarker.args[0]
            if envname != item.config.getoption("-E"):
                pytest.skip("test requires env %r" % envname)

A test file using this local plugin::

    # content of 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 linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3
    collected 1 items
    
    test_someenv.py s
    
    ======================== 1 skipped in 0.01 seconds =========================

and here is one that specifies exactly the environment needed::

    $ py.test -E stage1
    =========================== test session starts ============================
    platform linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3
    collected 1 items
    
    test_someenv.py .
    
    ========================= 1 passed in 0.01 seconds =========================

The ``--markers`` option always gives you a list of available markers::

    $ py.test --markers
    @pytest.mark.env(name): mark test to run only on named environment
    
    @pytest.mark.skipif(condition): skip the given test function if eval(condition) results in 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. see http://pytest.org/latest/skipping.html
    
    @pytest.mark.xfail(condition, reason=None, run=True, raises=None): mark the the test function as an expected failure if eval(condition) has a True value. Optionally specify a reason for better reporting and run=False if you don't even want to execute the test function. If only specific exception(s) are expected, you can list them in raises, and if the test fails in other ways, it will be reported as a true failure. See http://pytest.org/latest/skipping.html
    
    @pytest.mark.parametrize(argnames, argvalues): call a test function multiple times passing in different arguments in turn. argvalues generally needs to be a list of values if argnames specifies only one name or a list of tuples of values if argnames specifies multiple names. Example: @parametrize('arg1', [1,2]) would lead to two calls of the decorated test function, one with arg1=1 and another with arg1=2.see http://pytest.org/latest/parametrize.html for more info and examples.
    
    @pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see http://pytest.org/latest/fixture.html#usefixtures 
    
    @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

.. regendoc:wipe

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::

    # content of 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::

    # content of conftest.py
    import sys

    def pytest_runtest_setup(item):
        g = item.get_marker("glob")
        if g is not None:
            for info in g:
                print ("glob args=%s kwargs=%s" %(info.args, info.kwargs))
                sys.stdout.flush()

Let's run this without capturing output and see what we get::

    $ py.test -q -s
    glob args=('function',) kwargs={'x': 3}
    glob args=('class',) kwargs={'x': 2}
    glob args=('module',) kwargs={'x': 1}
    .
    1 passed in 0.01 seconds

marking platform specific tests with pytest
--------------------------------------------------------------

.. regendoc:wipe

Consider you have a test suite which marks tests for particular platforms,
namely ``pytest.mark.osx``, ``pytest.mark.win32`` etc. and you
also have tests that run on all platforms and have no specific
marker.  If you now want to have a way to only run the tests
for your particular platform, you could use the following plugin::

    # content of conftest.py
    #
    import sys
    import pytest

    ALL = set("osx linux2 win32".split())

    def pytest_runtest_setup(item):
        if isinstance(item, item.Function):
            plat = sys.platform
            if not item.get_marker(plat):
                if ALL.intersection(item.keywords):
                    pytest.skip("cannot run on platform %s" %(plat))

then tests will be skipped if they were specified for a different platform.
Let's do a little test file to show how this looks like::

    # content of test_plat.py

    import pytest

    @pytest.mark.osx
    def test_if_apple_is_evil():
        pass

    @pytest.mark.linux2
    def test_if_linux_works():
        pass

    @pytest.mark.win32
    def test_if_win32_crashes():
        pass

    def test_runs_everywhere():
        pass

then you will see two test skipped and two executed tests as expected::

    $ py.test -rs # this option reports skip reasons
    =========================== test session starts ============================
    platform linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3
    collected 4 items
    
    test_plat.py sss.
    ========================= short test summary info ==========================
    SKIP [3] /tmp/doc-exec-224/conftest.py:12: cannot run on platform linux
    
    =================== 1 passed, 3 skipped in 0.01 seconds ====================

Note that if you specify a platform via the marker-command line option like this::

    $ py.test -m linux2
    =========================== test session starts ============================
    platform linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3
    collected 4 items
    
    test_plat.py s
    
    =================== 3 tests deselected by "-m 'linux2'" ====================
    ================= 1 skipped, 3 deselected in 0.01 seconds ==================

then the unmarked-tests will not be run.  It is thus a way to restrict the run to the specific tests.

Automatically adding markers based on test names
--------------------------------------------------------

.. regendoc:wipe

If you a test suite where test function names indicate a certain
type of test, you can implement a hook that automatically defines
markers so that you can use the ``-m`` option with it. Let's look
at this test module::

    # content of test_module.py

    def test_interface_simple():
        assert 0

    def test_interface_complex():
        assert 0

    def test_event_simple():
        assert 0

    def test_something_else():
        assert 0

We want to dynamically define two markers and can do it in a
``conftest.py`` plugin::

    # content of conftest.py

    import pytest
    def pytest_collection_modifyitems(items):
        for item in items:
            if "interface" in item.nodeid:
                item.add_marker(pytest.mark.interface)
            elif "event" in item.nodeid:
                item.add_marker(pytest.mark.event)

We can now use the ``-m option`` to select one set::

  $ py.test -m interface --tb=short
  =========================== test session starts ============================
  platform linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3
  collected 4 items
  
  test_module.py FF
  
  ================================= FAILURES =================================
  __________________________ test_interface_simple ___________________________
  test_module.py:3: in test_interface_simple
      assert 0
  E   assert 0
  __________________________ test_interface_complex __________________________
  test_module.py:6: in test_interface_complex
      assert 0
  E   assert 0
  ================== 2 tests deselected by "-m 'interface'" ==================
  ================== 2 failed, 2 deselected in 0.01 seconds ==================

or to select both "event" and "interface" tests::

  $ py.test -m "interface or event" --tb=short
  =========================== test session starts ============================
  platform linux -- Python 3.4.0 -- py-1.4.25 -- pytest-2.6.3
  collected 4 items
  
  test_module.py FFF
  
  ================================= FAILURES =================================
  __________________________ test_interface_simple ___________________________
  test_module.py:3: in test_interface_simple
      assert 0
  E   assert 0
  __________________________ test_interface_complex __________________________
  test_module.py:6: in test_interface_complex
      assert 0
  E   assert 0
  ____________________________ test_event_simple _____________________________
  test_module.py:9: in test_event_simple
      assert 0
  E   assert 0
  ============= 1 tests deselected by "-m 'interface or event'" ==============
  ================== 3 failed, 1 deselected in 0.01 seconds ==================