White scenery @showyou, hatena

If you have any comments, you may also send twitter @shsub or @showyou.

nose

下の続き。doctestじゃなくてunittestで書く。
ここではunittestの補助にnoseを使う。

ubuntu 9.04にはnoseがaptで入れられるっぽいのでaptitude install python-noseとかやって入れる。


test_compare.py

def test_compare():
    assert 1==1

if __name__ == '__main__':
    import nose
    nose.main()

test_heapqnose.py

from heapq import heappush, heappop
def test_heapq():
    heap = []
    data = [8, 3, 5, 9, 7, 1, 4]

    for item in data:
        heappush(heap,item)
    assert heap == [1, 7, 3, 9, 8, 5, 4]

    sorted = []
    while heap:
        sorted.append(heappop(heap))
    assert sorted == [1, 3, 4, 5, 7, 8, 9]

    data.sort()
    assert data == sorted

if __name__ == '__main__':
    import nose
    nose.main()

って感じに書いてみる。

$ nosetests -w ./ -v

test_compare.test_compare ... ok
test_heapqnose.test_heapq ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.175s

OK

これはわかる。

$ python test_heapqnose.py -v

test_compare.test_compare ... ok
test_heapqnose.test_heapq ... ok

----------------------------------------------------------------------
Ran 2 tests in 3.080s

OK

てっきりtest_heapqnose.pyだけ実行すると思ったら、そのディレクトリ以下のテストを全部実行するっぽい?(-vvで確認)

nose
http://makunouchi.jp/zope3/7396323127