Dive into python 实例学python (1) —— 函数和测试
2014-02-12 20:04
639 查看
odbchelper.py
1、'''...'''是docstring
2、join()函数是连接字符串,这里用";"分号来连接,参数是字符串列表list。
测试代码:
odbchelpertest.py
def buildConnectionString(params): """Build a connection string from a dictionary Returns string. """ return ";".join(["%s=%s" % (k, v) for k, v in params.items()]) if __name__ == "__main__": myParams = {"server":"mpilgrim", \ "database":"master", \ "uid":"sa", \ "pwd":"secret" } print buildConnectionString(myParams)
1、'''...'''是docstring
2、join()函数是连接字符串,这里用";"分号来连接,参数是字符串列表list。
测试代码:
odbchelpertest.py
import unittest import odbchelper class GoodInput(unittest.TestCase): def testBlank(self): """buildConnectionString handles empty dictionary""" self.assertEqual("", odbchelper.buildConnectionString({})) def testKnownValue(self): """buildConnectionString returns known result with known input""" params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"} knownItems = params.items() knownItems.sort() knownString = repr(knownItems) result = odbchelper.buildConnectionString(params) resultItems = [tuple(e.split("=")) for e in result.split(";")] resultItems.sort() resultString = repr(resultItems) self.assertEqual(knownString, resultString) class BadInput(unittest.TestCase): def testString(self): """buildConnectionString should fail with string input""" self.assertRaises(AttributeError, odbchelper.buildConnectionString, "") def testList(self): """buildConnectionString should fail with list input""" self.assertRaises(AttributeError, odbchelper.buildConnectionString, []) def testTuple(self): """buildConnectionString should fail with tuple input""" self.assertRaises(AttributeError, odbchelper.buildConnectionString, ()) if __name__ == "__main__": unittest.main()
相关文章推荐
- Dive into python 实例学python (2) —— 自省,apihelper
- Dive Into Python 学习记录3-getattr 介绍/过滤列表/and or/lambda 函数
- Dive Into Python 学习记录2-自省/info 函数 /str / type /callable 函数
- Dive Into Python 学习记录1-函数/模块导入/字典/列表/元组/字符串分割、连接、格式化/映射list/
- Python——类、实例和其他对象的内建函数
- Python可变参数函数用法实例
- Python: 测试函数是否被调用
- Python列表类型的内建函数使用实例(insert、remove、index、pop等)
- appium+python实例(1)原生应用的appium测试步骤
- isupper字符串测试函数应用实例
- Python 研究(Dive Into Python)笔记2--第3章 内置数据类型
- Dive into python第三章内置数据类型总结
- 深入python3 (Dive Into Python 3) 在线阅读与下载
- Python中threading模块join函数用法实例分析
- Python3 对象实例函数
- 《Dive into Python》读书笔记之正则表达式
- Python函数返回值实例分析
- python知识点1之引用、拷贝、函数参数传递、实例调用了类变量
- Loadrunner测试json接口实例以及 web_custom_request 函数详解