Feeling Transfer Protocol

何か心が動いた時とかに衝動的に気持ちを発信するツール

とあるnosetestsコマンドのあれこれ1

pythonのnoseモジュールのお話

※ インストール

pip install nose


例えば下記のようなsample_test.pyを書いておいて、

#!/usr/local/bin/python
# -*- coding: utf-8 -*-

class TestSample(object):
    """This is sample test."""

    @classmethod
    def setup_class(cls):
        """set up for class"""
        print "this is setup_class."

    @classmethod
    def teardown_class(cls):
        """tear down for class"""
        print "this is teardown_class."

    def setup(self):
        """set up for test case"""
        print "this is setup."

    def teardown(self):
        """tear down for test case"""
        print "this is teardown"

    def test_A(self):
        """sample test case A"""
        print "this is test case A."

    def test_B(self):
        """sample test case B"""
        print "this is test case B."

    def test_C(self):
        """sample test case C"""
        print "this is test case C."
        assert False
nosetests sample_test.py

みたいな感じでサクッと実行すると、

/home/user% nosetests sample_test.py
..F
======================================================================
FAIL: sample test case C
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/user/sample_test.py", line 37, in test_C
    assert False
AssertionError:
-------------------- >> begin captured stdout << ---------------------
this is setup
this is test case C.

--------------------- >> end captured stdout << ----------------------

----------------------------------------------------------------------
Ran 3 tests in 0.002s

FAILED (failures=1)

ってな出力がなされて、無事テストが通ったことがわかります。
も少し分かりやすく表示したい場合は、nosetestsコマンドのオプション -vをつけて、
verboseモードで実行してやると良いでしょう。

/home/user% nosetests sample_test.py -v
sample test case A ... ok
sample test case B ... ok
sample test case C ... FAIL

======================================================================
FAIL: sample test case C
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/user/sample_test.py", line 37, in test_C
    assert False
AssertionError:
-------------------- >> begin captured stdout << ---------------------
this is setup
this is test case C.

--------------------- >> end captured stdout << ----------------------

----------------------------------------------------------------------
Ran 3 tests in 0.004s

FAILED (failures=1)

無愛想な「..F」という記号の代わりに、
今度はdocstringが出力され、その結果okだったのかFAILだったのかが出力されています。

さらに、これまでの例ではprint文が全く出力されていないことに気が付きます。
そこでつけるオプションが -sです。
これはnoseのキャプチャリングをキャンセルするオプションです。

/home/user% nosetests sample_test.py -s
this is setup_class.
this is setup
this is test case A.
this is teardown
.this is setup
this is test case B.
this is teardown
.this is setup
this is test case C.
Fthis is teardown
this is teardown_class.

======================================================================
FAIL: sample test case C
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/user/sample_test.py", line 37, in test_C
    assert False
AssertionError:
-------------------- >> begin captured stdout << ---------------------
this is setup
this is test case C.

--------------------- >> end captured stdout << ----------------------

----------------------------------------------------------------------
Ran 3 tests in 0.003s

FAILED (failures=1)

これで、各setupやteardownがどの順で呼ばれているのかが明確になりました。

では本日記の最終内容。

それは、あるテストケースのみをテスト実行したい時の方法

【方法1】

from nose.plugins.attrib import attr

# ... (間は省略します)

@attr(now=True)
def test_A(self):

# ... (以下省略)

実行したいテストケースの上にデコレータを書いておいて、

nosetests sample_test.py -a "now"

で実行。

/home/user% nosetests sample_test.py -a "now" -v
sample test case B ... ok

----------------------------------------------------------------------
Ran 1 test in 0.002s

OK

【方法2】

/home/user% nosetests sample_test.py:TestSample.test_B -v
sample test case B ... ok

----------------------------------------------------------------------
Ran 1 test in 0.001s

OK

もう一つの方法がこれ。
コロンつなぎでクラス名、テストケース名として指定するパターン。

中々色々あって、勉強し甲斐がありますなー。