nose tutorial v documentation

nose.tools を使う

«  nose のsetup/teardown   ::   Contents   ::   nose plugin の作成  »

nose.tools を使う

はじめに

nose には、簡単にテストするための機能が用意されています。

機能は、 nose.tools パッケージにまとまっています。

nose.tools を一通り使えば、ある程度のテストはこなせるようになります。

いくつかの機能は、前回までに出ていますが、あらためて書いてあります。

使い方

nose.tools.ok_

ok_(expr, msg=None)

expr が True かどうかを評価します。 msg がある場合は、テスト結果に出力します。

nose.tools.eq_

eq_(a, b, msg=None)

a と b が等しいかどうかを評価します。 msg がある場合は、テスト結果に出力します。

nose.tools.raises

@raises(TypeError)
def raise_test():
  raise TypeError("This test passes")

@raises(TypeError, ValueError)
def raise_test():
  pass

テスト関数に期待する値が、例外の場合に使用します。

raises デコレータは、値を複数持てます。

nose.tools.timed

@timed(.1)
def time_test():
  import time
  time.sleep(.5)

テスト実行に制限時間を持たせる場合に使用します。

timed で指定した時間を越えた場合は、テスト失敗になります。

nose.tools.with_setup

@with_setup(setup=x_setup, teardown=x_teardown)
def x_test():
  pass

テストごとに setup と teardown を実行させる場合に使用します。

パッケージレベル、モジュールレベルの setup/teardown は実行されます。

nose.tools.istest

@istest
def skip():
  pass

テストと見なされない関数をテストとして実行させる場合に使用します。

nose.tools.nottest

@nottest
def skip_test():
  pass

テストをスキップさせたい場合に使用します。

ソースコード

動作確認用のモジュール

«  nose のsetup/teardown   ::   Contents   ::   nose plugin の作成  »