Python?????????????
???????????? ???????[ 2017/2/24 14:03:10 ] ????????Python
??????????ù???
????????????????????С???????????????????????
?????????????????????????????????????С???????ζ???????????????????????????????????????Щ???????????????setUp()??setDown()?????????
??д??п?????????????Щ????£????????????????????????????????е????????????????????????л????????????????????£?????????????????????????????С?
???ò??????????????????????
???д?????????????????????????д????????????????Ρ????????????????д???????????κ????飻
??????????????????????
?????????????????????д????????????????????????????????????????????????????????????????????????
????????????ó????????????????????????????д????У????????square()??sqr()??????????????????У?????????test_square_of_number_2()??test_square_negativer_number()??Щ???????Щ?????????????????
????????????п?????
???????????????????????????????
???????????????
2.1 Unittest
unittest??Python??????????????API??Java??JUnit??.net??NUnit??C++??CppUnit???????
??????unittest.TestCase???????????????????
??????ο? ??? ??
???????
import unittest
def fun(x):
return x + 1
class MyTest(unittest.TestCase):
def test(self):
self.assertEqual(fun(3)?? 4)
??к?????
?????????????????????5??????е?????????????
2.2 Doctest
doctest ??????????Щ??????????????? Python ??????Σ????????в???????.??????????? doctest????????????????????п??????????“??????????????????????????docstring?????” ???????????????????????????
????????
def square(x):
"""Squares x.
>>> square(2)
4
>>> square(-2)
4
>>> square(5)
25
"""
return x * x
if __name__ == '__main__':
import doctest
doctest.testmod()
????и???????????????>>> ???????????????????е??????б?????е??????£?
???????????????????£?square(2)???????5????????????£?
def square(x):
"""Squares x.
>>> square(2)
5
>>> square(-2)
4
>>> square(5)
25
"""
return x * x
if __name__ == '__main__':
import doctest
doctest.testmod()
??е???????????????
2.3 py.test
py.test??unittest??????????
????????????????????????????????????????????????????????????д??????????????unittest?????????????????py.test????????????
py.test??? ??? ??
????????py.test
pip install pytest
?????????д???????
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
??????????в??????
???pytest???·????
????????????????python.exe?????test.py ???????????磺
??е?????????????
2.4 Nose
Nose???unittest??????????python???????????nose???????????????У?nose?????????????????????????xUnitcompatible????????????
nose?????????? https://nose.readthedocs.org/en/latest/
???nose?????????python3?????????????????????
2.5 tox
??????????????????????????????????????????y??в????
tox?????????? http://testrun.org/tox/latest/
2.6 Unittest2
??unitest???????檔??API????????????????????????
unittest2?????????? https://pypi.python.org/pypi/unittest2
????????
pip install unittest2
??????????unittest??unittest2???????л??????????д??????£?
import unittest2 as unittest
class MyTest(unittest.TestCase):
...
2.7 mock unittest.mock??????????python?????python3.3?汾???????????????? ????汾????????pip install mock ???а????
mock??????????????????????????????????????????????????????????????????
mock??????????http://www.voidspace.org.uk/python/mock/
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11