Chinaunix首页 | 论坛 | 博客
  • 博客访问: 616966
  • 博文数量: 140
  • 博客积分: 2635
  • 博客等级: 少校
  • 技术积分: 1353
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-04 15:46
文章分类
文章存档

2015年(2)

2014年(12)

2013年(10)

2012年(10)

2011年(85)

2010年(21)

分类: Python/Ruby

2011-04-22 15:01:42

先学习下如何调试Python程序,然后再学习下如何做单元测试。Python的pdb模块同GDB非常相似,不过它只是GDB一个小小的子集,似乎不支持调试多线程程序。在网上找到一篇不错的文章:用PDB库调试Python程序。写了个小例子,练习下使用pdb调试和做单元测试吧。

下面是一个简单的数学库,实现了简单的加减函数。代码如下:

1def add(op1, op2):
2    return op1+op2;
3 
4def sub(op1, op2):
5    return op1-op2;

主程序会调用数学库中的函数,代码如下:

01#!/usr/bin/env python
02 
03import math_utils
04 
05a = 3;
06b = 4;
07 
08c = math_utils.add(a, b);
09 
10print c;

下面演示了几条最基本的调试命令的使用方法,包括”list”,”continue”,”break”,”run”,”backtrace”等等。打断点的时候不要打在函数的def处。如果这样做,import的时候会运行到这个位置,真正调用函数时却不会停住。impor的时候会生成Python内部的运行指令,因此会运行到每个函数定义的地方。

01henshao@henshao-desktop:~/source$ python -m pdb main.py
02> /home/henshao/source/main.py(3)()
03-> import math_utils
04(Pdb) h
05 
06Documented commands (type help ):
07========================================
08EOF    bt         cont      enable  jump  pp       run      unt
09a      c          continue  exit    l     q        s        until
10alias  cl         d         h       list  quit     step     up
11args   clear      debug     help    n     r        tbreak   w
12b      commands   disable   ignore  next  restart  u        whatis
13break  condition  down      j       p     return   unalias  where
14 
15Miscellaneous help topics:
16==========================
17exec  pdb
18 
19Undocumented commands:
20======================
21retval  rv
22 
23(Pdb) b math_utils.py:4
24Breakpoint 1 at /home/henshao/source/math_utils.py:4
25(Pdb) h cont
26c(ont(inue))
27Continue execution, only stop when a breakpoint is encountered.
28(Pdb) run
29Restarting main.py with arguments:
30 
31> /home/henshao/source/main.py(3)()
32-> import math_utils
33(Pdb) cont
34> /home/henshao/source/math_utils.py(4)add()
35-> return op1+op2;
36(Pdb) bt
37  /usr/lib/python2.6/bdb.py(368)run()
38-> exec cmd in globals, locals
39  (1)()
40  /home/henshao/source/main.py(8)()
41-> c = math_utils.add(a, b);
42> /home/henshao/source/math_utils.py(4)add()
43-> return op1+op2;
44(Pdb) args
45op1 = 3
46op2 = 4

Python的单元测试框架同JUnit差不多,因为它们的开发者都是Kent Beck,接口都是一样的。使用setUp和tearDown做准备和收尾工作。每个要测试的接口都以”test”开始。在math_utils.py添加下面这段代码便可。

01import unittest
02 
03class MathTestCase(unittest.TestCase):
04        def setUp(self):
05                pass;
06 
07        def tearDown(self):
08                pass;
09 
10        def testAdd(self):
11                self.assertEqual(add(4, 5), 9)
12 
13        def testSub(self):
14                self.assertEqual(sub(8, 5), 3);
15 
16        def suite():
17                suite = unittest.TestSuite()
18                suite.addTest(MathTestCase())
19                return suite
20 
21if __name__ == "__main__":
22        unittest.main()

直接运行程序,得到以下输出:

1henshao@henshao-desktop:~/source$ python math_utils.py
2..
3----------------------------------------------------------------------
4Ran 2 tests in 0.000s
5 
6OK

上面这种方式将测试代码同源代码混杂在一起,并不是一种很好的方式。可以将测试用例的代码放到一个单独的unittest目录中。对每个用例设计一个test case,然后使用下面这种方式添加测试用例。

1if __name__ == "__main__":
2        suite = unittest.TestSuite()
3 
4        suite.addTest(MathTestCase("testAdd"))
5        suite.addTest(MathTestCase("testSub"))
6 
7        runner = unittest.TextTestRunner()
8        runner.run(suite)

如果安装python-tk包,就可以使用图形界面来跑测试用例了。PyUnit源码包中有一个unittestgui.py文件,将该文件拷贝到测试用例代码目录下。输入如下命令,注意math_utils不要加”.py”后缀,否则会出错。

1./unittestgui.py math_utils

测试界面如下:

阅读(2563) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~