完整示例参见:https://github.com/jiaxiaolei/my_django_project django_test 分支
$ git clone https://github.com/jiaxiaolei/my_django_project -b django_test
一个示例代码:
# filename: test_1.py
import unittest
def division_funtion(x, y):
return x / y
class TestDivision(unittest.TestCase):
def test_int(self):
self.assertEqual(division_funtion(9, 3), 3)
def test_int2(self):
self.assertEqual(division_funtion(9, 4), 2.25)
def test_float(self):
self.assertEqual(division_funtion(4.2, 3), 1.4)
if __name__ == '__main__':
unittest.main()
$ python test_1.py
(py2.7.13cmdb2.0) [root@test-app-1 t_unittest]# python test_1.py
F.F
======================================================================
FAIL: test_float (__main__.TestDivision)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_1.py", line 17, in test_float
self.assertEqual(division_funtion(4.2, 3), 1.4)
AssertionError: 1.4000000000000001 != 1.4
======================================================================
FAIL: test_int2 (__main__.TestDivision)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_1.py", line 14, in test_int2
self.assertEqual(division_funtion(9, 4), 2.25)
AssertionError: 2 != 2.25
----------------------------------------------------------------------
Ran 3 tests in 0.001s
FAILED (failures=2)
nose
安装:
$ pip install nose
(py2.7.13cmdb2.0) [root@test-app-1 t_unittest]# nosetests
F.F
======================================================================
FAIL: test_float (test_1.TestDivision)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/opt/my_django_project_test/project_001/tests/t_unittest/test_1.py", line 17, in test_float
self.assertEqual(division_funtion(4.2, 3), 1.4)
AssertionError: 1.4000000000000001 != 1.4
======================================================================
FAIL: test_int2 (test_1.TestDivision)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/opt/my_django_project_test/project_001/tests/t_unittest/test_1.py", line 14, in test_int2
self.assertEqual(division_funtion(9, 4), 2.25)
AssertionError: 2 != 2.25
----------------------------------------------------------------------
Ran 3 tests in 0.003s
FAILED (failures=2)
pytest
安装:
$ pip install pytest
(py2.7.13cmdb2.0) [root@test-app-1 t_unittest]# pytest
================================================================ test session starts =================================================================
platform linux2 -- Python 2.7.13, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
rootdir: /opt/my_django_project_test/project_001/tests/t_unittest, inifile:
collected 3 items
test_1.py F.F
====================================================================== FAILURES ======================================================================
______________________________________________________________ TestDivision.test_float _______________________________________________________________
self = <test_1.TestDivision testMethod=test_float>
def test_float(self):
> self.assertEqual(division_funtion(4.2, 3), 1.4)
E AssertionError: 1.4000000000000001 != 1.4
test_1.py:17: AssertionError
_______________________________________________________________ TestDivision.test_int2 _______________________________________________________________
self = <test_1.TestDivision testMethod=test_int2>
def test_int2(self):
> self.assertEqual(division_funtion(9, 4), 2.25)
E AssertionError: 2 != 2.25
test_1.py:14: AssertionError
========================================================= 2 failed, 1 passed in 0.05 seconds =========================================================
(py2.7.13cmdb2.0) [root@test-app-1 t_unittest]#
coverage
安装:
$ pip install coverage
$ coverage run test_1.py
F.F
======================================================================
FAIL: test_float (__main__.TestDivision)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_1.py", line 18, in test_float
self.assertEqual(division_funtion(4.2, 3), 1.4)
AssertionError: 1.4000000000000001 != 1.4
======================================================================
FAIL: test_int2 (__main__.TestDivision)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_1.py", line 15, in test_int2
self.assertEqual(division_funtion(9, 4), 2.25)
AssertionError: 2 != 2.25
----------------------------------------------------------------------
Ran 3 tests in 0.001s
FAILED (failures=2)
# 查看测试报告
(py2.7.13cmdb2.0) [root@test-app-1 tests]# coverage report -m
Name Stmts Miss Cover Missing
-------------------------------------
a.py 12 0 100%
扩展阅读
python 单元测试官方文档:
Python 2 (https://docs.python.org/2/library/unittest.html)
Python 3 (https://docs.python.org/3/library/unittest.html)
网友评论