django单元测试(一): nose, pytest, coverage

完整示例参见: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)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    aimaile阅读 26,717评论 6 427
  • GitHub 上有一个 Awesome - XXX 系列的资源整理,资源非常丰富,涉及面非常广。awesome-p...
    若与阅读 18,978评论 4 418
  • 环境管理管理Python版本和环境的工具。p–非常简单的交互式python版本管理工具。pyenv–简单的Pyth...
    MrHamster阅读 9,242评论 1 61
  • 寂静的办公室里只残存着我破马张飞般唰唰翻作业的声音和另一边噼里啪啦悲愤的键盘声。 门吱呀的开了,...
    lemonzx阅读 6,230评论 0 2
  • 你朱唇轻扫胭脂色,像涂色的黑白水墨,轻生细语谁家姑娘心事。当一曲笛声戛止,少年已白头。
    双笙书阅读 975评论 0 1