nose tutorial 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

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

ソースコード

動作確認用のモジュール

例題3 nose.tools を使う。

ex3_test.eq_test()[source]

nose.tools.eq_ : a と b が等しいかどうかを評価します。

ex3_test.ex3_setup()[source]
ex3_test.ex3_teardown()[source]
ex3_test.ex3_test()[source]

nose.tools.with_setup : テストごとに、setup/teardown を実行します。

ex3_test.except_test(*arg, **kw)[source]

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

ex3_test.ok_test()[source]

nose.tools.ok_ : expr が True かどうかを評価します。

ex3_test.run_test()[source]

nose.tools.nottest : 通常は実行されますが、テストしません。 ** run_test は、表示されません。**

ex3_test.skip()[source]

nose.tools.istest : 通常はスキップされますが、テストとして見なされます。

ex3_test.time_test(*arg, **kw)[source]

nose.tools.timed : テスト実行の制限時間を設定します。 このテストは失敗します。

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