nose tutorial documentation

nose のsetup/teardown

«  nose の基本的な使い方   ::   Contents   ::   nose.tools を使う  »

nose のsetup/teardown

はじめに

nose の基本的な使い方その2です。

setup/teardown の動作についてです。

役割

テストを行う上で、あらかじめデータを用意しておく必要あったり、 テスト後に作成したデータを削除するときに利用します。

詳しくは、他のブログなどをみましょう。

書き方

nose では、パッケージレベル、モジュールレベル、関数レベルで定義することができます。

パッケージレベルでは、 __init__.py に setup/teardown を定義します。

モジュールレベルでは、テスト用モジュールに setup/teardown を定義します。

関数レベルでは、 nose.tools.with_setup に関数ごとに呼び出したい、setup/teardown をデコレータに渡します。

呼び出し順

テストパッケージ内に上の3つのレベルで、 setup/teardown が定義されていると呼び出し順序は次のようになります。

  1. パッケージの setup
  2. モジュールの setup
  3. 個々の関数の setup/teardown
  4. モジュールの teardown
  5. パッケージの teardown

ソースコード

動作確認用のモジュール

例題2 setup/teardown 付きのテストケースを記述してあります。

ex2_test.a_test()[source]

テスト a の呼び出し確認

ex2_test.b_setup()[source]
ex2_test.b_teardown()[source]
ex2_test.b_test()[source]

テスト b の呼び出し確認

ex2_test.debug_write(message)[source]
ex2_test.setup()[source]
ex2_test.teardown()[source]

«  nose の基本的な使い方   ::   Contents   ::   nose.tools を使う  »