PyUnit提供的动态方法,只编写一个测试类来完成对整个软件模块的测试,这样对象的初始化工作可以在setUp()方法中完成,而资源的释放则可以在tearDown()方法中完成。

使用PyUnit可以像xunit那样设置一个个函数,分别测试不同的数据和不同的用例。

例如,构建下面的测试类,测试远程API服务

  class Request:
def __init__(self):
pass
def request(self,RequestMethod,RequestURL,param,requestHeaders):
return Request.__dict__[RequestMethod](self,RequestURL,param,requestHeaders)
def get(self,RequestURL,param,requestHeaders):
RequestURL=RequestURL+"?"+urllib.parse.urlencode(param)
req = url.Request(RequestURL)
result = url.urlopen(req).read()
return result
def post(self,RequestURL,param,requestHeaders):
postdata= json.dumps(param)
req = url.Request(
url = RequestURL,
data = postdata.encode("utf-8"),
headers = requestHeaders
)
result = url.urlopen(req).read()
return result

想要知道其中执行结果,构造下面的测试类

  class APITest(unittest.TestCase):
def setUp(self):
self.r=Request()
def test_UserPermisionQueryPost(self):
RequestURL="http://10.125.198.137/permission/table/listUserTablePermissions.json"
RequestMethod="post"
requestHeaders={"Accept":"*/*",}
param={
"tablePermissionParam": {
"currentPage": 1,
"pageSize": 50,
"tablename": "s_auction_auctions",
"usernames": [
""
]
}
}
expectResult=b'{"returnCode":"0","returnValue":[{"id":2067,"username":"067765","tablename":"s_auction_auctions","packagename":"yunti1.taobao","readlevelpermission":2,"permissiontime":
"2014-03-18 06:02:10","distabletime":"2015-03-18 06:02:08","permissionstatus":1,"gmtCreate":"2014-03-18 06:02:10","gmtModify":"2014-03-18 06:02:10","columns":["id","title"]}]}'
self.assertEqual(self.r.request(RequestMethod,RequestURL,param,requestHeaders),expectResult)
def test_UserPermisionQueryGet(self):
RequestMethod="get"
RequestURL="http://10.125.198.137/permission/table/listUserTablePermissions"
param={"tablename":"s_auction_auctions"}
expectResult=b'{"returnCode":"0","returnValue":[{"id":2067,"username":"067765","tablename":"s_auction_auctions","packagename":"yunti1.taobao","readlevelpermission":2,"permissiontime":
"2014-03-18 06:02:10","distabletime":"2015-03-18 06:02:08","permissionstatus":1,"gmtCreate":"2014-03-18 06:02:10","gmtModify":"2014-03-18 06:02:10","columns":["id","title"]},{"id":2066,
"username":"","tablename":"s_auction_auctions","packagename":"yunti1.taobao","readlevelpermission":2,"permissiontime":"2014-03-18 06:02:10","distabletime":"2015-03-18 06:02:08",
"permissionstatus":1,"gmtCreate":"2014-03-18 06:02:10","gmtModify":"2014-03-18 06:02:10","columns":["id","title"]}],"count":2}'
self.assertEqual(self.r.request(RequestMethod,RequestURL,param,""),expectResult)

PyUnit模块中定义了一个名为main的全局方法,使用它可以很方便地将一个单元测试模块变成可以直接运行的测试脚本,main()方法使用TestLoader类来搜索所有包含在该模块中的测试方法,并自动执行它们。如果Python程序员能够按照约定(以test开头)来命名所有的测试方法,那就只需要在测试模块的最后加入如下几行代码即可:

if __name__=='__main__':
unittest.main()

或者通过自己添加的方式决定哪些用例需要真正的被执行

 def suite():
suite = unittest.TestSuite()
suite.addTest(APITest("test_UserPermisionQueryGet"))
suite.addTest(APITest("test_UserPermisionQueryPost"))
runner = unittest.TextTestRunner()
runner.run(suite)

当选择main函数作为入口的时候,还可以获取到帮助信息

python3 ./Request.py  -h
Usage: Request.py [options] Options:
-h, --help show this help message and exit
-v, --verbose Verbose output
-q, --quiet Quiet output
-f, --failfast Stop on first fail or error
-c, --catch Catch ctrl-C and display results so far
-b, --buffer Buffer stdout and stderr during tests

直接在命令行中指定testSuite.testcase

python3 ./Request.py APITest.test_UserPermisionQueryGet
.
----------------------------------------------------------------------
Ran 1 test in 0.179s OK