pytest | URL |
---|---|
xdistの-nオプションについて | https://qiita.com/cielavenir/items/3f3319f3fa153c16c703 |
デコレータの作り方 | https://qiita.com/cielavenir/items/542f7eaa612c6bb9ed90 |
デコレータに引数を渡す | https://qiita.com/cielavenir/items/2c971ca4bc138c375d17 |
部分テストパラメータの値を使ってテストパラメータを生成する | https://qiita.com/cielavenir/items/ce328313a9f0f498cbcb |
pytestのfixtureを別の箇所でライブラリとして読み込みたい (当該環境にはpytestが存在しない可能性がある) |
https://qiita.com/cielavenir/items/8144774a6ac2eca2e2ce |
fixtureを、pytestのデコレータを無視して普通に関数として呼んでも、シグネチャが合っていれば問題なく動作します。
が、pytest自体が存在しない場合には迂回が必要になります。たとえば以下のようにできます。
try:
import pytest
except ImportError:
# provide fake pytest function to keep the script still runnable as standalone script
def null_decorator_or_decomaker(f=None, **decomaker_kwargs):
if f:
# decorator
return lambda *args, **kwargs: f(*args, **kwargs)
else:
# decomaker
def _(f):
# decomaker_kwargs can be handled
return lambda *args, **kwargs: f(*args, **kwargs)
return _
def null_decomaker(*decomaker_args, **decomaker_kwargs):
def _(f):
return lambda *args, **kwargs: f(*args, **kwargs)
return _
def null_decorator(f):
return lambda *args, **kwargs: f(*args, **kwargs)
class pytest:
fixture = staticmethod(null_decorator_or_decomaker)
class mark:
parametrize = staticmethod(null_decomaker)
xfail = staticmethod(null_decorator)
# other function declarations follow if required.
try:
from _pytest.compat import get_real_func
except ImportError:
get_real_func = lambda x:x
##### below program runs "without pytest" #####
@pytest.fixture
def fixture1():
return 1
def fixture2():
return 2
@pytest.fixture(name='fixture2')
def fixture2_fixture():
return fixture2()
@pytest.mark.xfail
def test_a(fixture1,fixture2):
assert True
get_real_func(fixture1)()
fixture2()
test_a(1,2)
# もはやpytestと関係あるかどうか怪しいですが一応タグは付けますか。。
210107
fixtureを直接呼ぶことはPytest 4.x以降できなくなったのを今更知りました。。
get_real_funcを使うか、関数に別名をつけるのが良いです(別名をつけるのが公式がおすすめしている方法です。たぶんget_real_funcはpytest自体をテストするためのモック)。