2012年10月19日 星期五

nose unittest extender

nose 一個好用的 unittest manager, 讓每隻的 testsuits 都可以連結或者個是分開個別來測, 測完後有個coverage report. 很適合拿來做 regression 的驗證. 底下就是個非常簡單的例子

dut.py 我們所寫好的 method
""" dut """

__all__ = ['frame']

class frame(object):

    def __init__(self):
     self.name = "frame"

    def double(self,w):
     return w * 2

    def triple(self,w):
     return w * 3
test_dut.py 測試寫的 method
 
import os
import unittest

import nose

from dut import *
import gc

class TestDouble(unittest.TestCase):

    def setUp(self):
        self.frame = frame()

    def teardown(self):
     self.frame = None
     gc.collect()


    @unittest.skip("calling test skip test_double_word")
    def test_double_word(self):
     """ test double word """

     expect  = "hihi"
     results = self.frame.double("hi")
     self.assertTrue(expect == results)


    def test_double_dec(self):
     """ test double dec """

     expect = 4.0
     results= self.frame.double(2.0)
     self.assertTrue(expect == results)


if __name__ == '__main__':
    # unittest.main()
    import nose
    # nose.runmodule(argv=[__file__,'-vvs','-x', '--ipdb-failure'],
    #                exit=False)
    nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
                   exit=False)
__init__.py path include

runtest.sh top test run manager

#!/bin/sh
coverage erase
nosetests -w ./ --with-coverage --cover-package=dut $*

how to run it
>>> ./runtest.sh 

test results
>>> results .S ---------------------------------------------------------------------- Ran 2 tests in 0.001s OK (SKIP=1)

1 則留言:

  1. 怎格式都跑掉了....真是麻煩.就先頂一下的用吧

    回覆刪除