1. 程式人生 > >python測試unittest/doctest/nose

python測試unittest/doctest/nose

使用pylint/pyflakes/pep8檢查程式碼風格

使用unittest測試python

cap.py

def just_do_it(text):
    return text.capitalize()


test_cap.py 

import unittest
from python_test import cap

class Test(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_one_word(self)
:
text = 'duck' result = cap.just_do_it(text) self.assertEqual(result, 'Duck') if __name__ == '__main__': unittest.main()

使用doctest測試python

def just_do_it(text):
    """
    >>> just_do_it('duck')
    'Duck'
    >>> just_do_it('a veriable flock of ducks')
    'A Veriable Flock Of s Ducks'
    """
from string import capwords return capwords(text) if __name__ =='__main__': import doctest doctest.testmod()

使用nose測試python

nose安裝pip install nose
用法與unittest類似

cap.py

def just_do_it(text):
    return text.capitalize()


test.py 

from python_test import cap
from nose.tools import
eq_ def test_one_word(): text = 'duck' result = cap.just_do_it(text) eq_(result, 'Duck', ) 在終端輸入 nosetests test.py 測試