LoginSignup
3
0

More than 1 year has passed since last update.

pytestのfixtureを引数付きで実行して戻り値を返却して後処理をする方法

Posted at

pytestのfixtureを引数付きで実行と戻り値の返却をして後処理をする際に少しつまづいたので、備忘録として残しておく。

はじめに

以下のやり方は、分かっていたが、戻り値を返却した後、テスト後の処理を追加する方法がわからなかった

  • 後処理を実行する方法
@pytest.fixture
def setUp():

  print('start')

  yield

  print('end')
  • 戻り値の返却する方法
@pytest.fixture
def setUp():

  return 'test'
  • pytestのfixtureを引数付きで実行する方法
@pytest.fixture
def setUp():

  def _setUp(param):
    return param

  return _setUp

やりたかったこと

引数を渡して戻り値を返却した後にテスト後の処理を実行したい

@pytest.fixture
def setUp():

  def _setUp(param):
    # 前処理、戻り値を返却
    return param

  return _setUp

  # この後に後処理を書きたい

やったこと

yield でメソッドを返却してその後に後処理を行うだけでした。

@pytest.fixture
def setUp():

  def _setUp(param):
    # 前処理、戻り値を返却
    return param

  yield _setUp

  # 後処理
  print('end')
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0