使用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 测试