目次

前のトピックへ

skip と xfail: 成功しないテストを扱う

次のトピックへ

非推奨の警告やその他の警告のアサート

属性をもつテスト関数のマーク

pytest.mark ヘルパーを使って、テスト関数にメタデータを簡単に設定できます。組み込みのマーカーを紹介します:

  • skipif: 特定の条件を満たした場合にテスト関数をスキップ
  • xfail: 特定の条件を満たした場合に “失敗を期待”
  • parametrize: 同じテスト関数に対して複数回の呼び出しを実行

カスタムマーカーを作成する、または全体のテストクラスやモジュールにマーカーを適用するのは簡単です。ドキュメントでもある カスタムマーカーを使う のサンプルを参照してください。

マーカー関連オブジェクトの API リファレンス

class _pytest.mark.MarkGenerator[ソース]

Factory for MarkDecorator objects - exposed as a pytest.mark singleton instance. Example:

import py
@pytest.mark.slowtest
def test_function():
   pass

will set a ‘slowtest’ MarkInfo object on the test_function object.

class _pytest.mark.MarkDecorator(name, args=None, kwargs=None)[ソース]

A decorator for test functions and test classes. When applied it will create MarkInfo objects which may be retrieved by hooks as item keywords. MarkDecorator instances are often created like this:

mark1 = pytest.mark.NAME              # simple MarkDecorator
mark2 = pytest.mark.NAME(name1=value) # parametrized MarkDecorator

and can then be applied as decorators to test functions:

@mark2
def test_function():
    pass
When a MarkDecorator instance is called it does the following:
  1. If called with a single class as its only positional argument and no additional keyword arguments, it attaches itself to the class so it gets applied automatically to all test cases found in that class.
  2. If called with a single function as its only positional argument and no additional keyword arguments, it attaches a MarkInfo object to the function, containing all the arguments already stored internally in the MarkDecorator.
  3. When called in any other case, it performs a ‘fake construction’ call, i.e. it returns a new MarkDecorator instance with the original MarkDecorator’s content updated with the arguments passed to this call.

Note: The rules above prevent MarkDecorator objects from storing only a single function or class reference as their positional argument with no additional keyword or positional arguments.

class _pytest.mark.MarkInfo(name, args, kwargs)[ソース]

Marking object created by MarkDecorator instances.

add(args, kwargs)[ソース]

add a MarkInfo with the given args and kwargs.

args = None

positional argument list, empty if none specified

kwargs = None

keyword argument dictionary, empty if nothing specified

name = None

name of attribute