一、
安装Python的时候,就默认安装了Unittest,所以直接import使用就可以
1. Unittest初体验
# -*-coding:UTF:8-*- import unittest class TestMethod(unittest.TestCase): #每次方法之前执行 def setUp(self): print('test-->setup') #每次方法之后执行 def tearDown(self): print('test---->tearDown') def test_001(self): # unittest中的用例必须以test开头 print('这个是第一个测试方法') def test_002(self): print('这个是第二个测试方法') if __name__ == '__main__': unittest.main()
运行结果:
2. 加入类方法
类方法是整个测试只执行一遍
# -*-coding:UTF:8-*- import unittest class TestMethod(unittest.TestCase): @classmethod def setUpClass(cls): #类方法,整个测试只会执行一次 print("类执行之前的方法") @classmethod def tearDownClass(cls): print("类执行之后的方法") #每次方法之前执行 def setUp(self): print('test-->setup') #每次方法之后执行 def tearDown(self): print('test---->tearDown') def test_001(self): print('这个是第一个测试方法') def test_002(self): print('这个是第二个测试方法') if __name__ == '__main__': unittest.main()
运行结果:
二、实际应用
1. 把Setup()运用在实际测试中
#coding:utf-8 import unittest from TestDemo_No_init import RunMain import json class TestMethod(unittest.TestCase): def setUp(self): self.run = RunMain() def test01(self): url = 'http://127.0.0.1:8000/login/' data = { 'username': 'Jessie', 'password': '123456' } r = self.run.run_main(url, 'POST', data) print(r) re = json.dumps(r, indent=2, sort_keys=False, ensure_ascii=False) print(re) def test02(self): url = 'http://fanyi.youdao.com/translate' data = { 'doctype': 'json', 'type': 'AUTO', 'i': 'weather' } r = self.run.run_main(url, 'GET', data) print(r) re = json.dumps(r, indent=2, sort_keys=False, ensure_ascii=False) print(re) @unittest.skip('test03') # 跳过test03 def test03(self): url = 'http://fanyi.youdao.com/translate' data = { 'doctype': 'json', 'type': 'AUTO', 'i': '天气' } r = self.run.run_main(url, 'GET', data) print(r) re = json.dumps(r, indent=2, sort_keys=False, ensure_ascii=False) if __name__ == '__main__': unittest.main()
运行结果:
可以对比下有json.loads()转化后,输出的格式,更容易查看了
2. 加入结果比对验证
assertEqual() 来检查预期的输出。具体为:比较第一个参数值和第二个参数值,如果相等则通过。如果不相等,则输出第三个参数的信息
assertTrue() 或 assertFalse() 来验证一个条件
assertRaises() 来验证抛出了一个特定的异常
#coding:utf-8 import unittest from TestDemo_No_init import RunMain import json class TestMethod(unittest.TestCase): def setUp(self): self.run = RunMain() def test01(self): """这是一个POST的接口自动化测试""" url = 'http://127.0.0.1:8000/login/' data = { 'username': 'Jessie', 'password': '123456' } r = self.run.run_main(url, 'POST', data) # print(r) re = json.dumps(r, indent=2, sort_keys=False, ensure_ascii=False) print(re) re_new = json.loads(re) self.assertEqual(re_new['user'], 'Jessie', '测试失败') # globals()['userid'] = 66666 def test02(self): """查询翻译词的接口""" # print(userid) url = 'http://fanyi.youdao.com/translate' data = { 'doctype': 'json', 'type': 'AUTO', 'i': 'weather' } r = self.run.run_main(url, 'GET', data) # print(r) # print(type(r)) re = json.dumps(r, indent=2, sort_keys=False, ensure_ascii=False) print(re) # print(type(re)) re_new = json.loads(re) # print(type(re_new)) self.assertEqual(re_new['errorCode'], 0, '测试失败') #检查预期结果 @unittest.skip('test03') # 跳过test03 def test03(self): url = 'http://fanyi.youdao.com/translate' data = { 'doctype': 'json', 'type': 'AUTO', 'i': '天气' } r = self.run.run_main(url, 'GET', data) print(r) re = json.dumps(r, indent=2, sort_keys=False, ensure_ascii=False) re_new = json.loads(re) self.assertEqual(re_new['errorCode'], 0, '测试失败') #检查预期结果 if __name__ == '__main__': unittest.main()
运行结果:
如果把test02的这个地方改一下,故意制造个错误
看下运行结果:
打印的提示信息非常明显了
三、 case的管理运用
1.
#coding:utf-8 import unittest from TestDemo_No_init import RunMain import json class TestMethod(unittest.TestCase): def setUp(self): self.run = RunMain() def test01(self): url = 'http://127.0.0.1:8000/login/' data = { 'username': 'Jessie', 'password': '123456' } r = self.run.run_main(url, 'POST', data) print(r) re = json.dumps(r, indent=2, sort_keys=False, ensure_ascii=False) re_new = json.loads(re) self.assertEqual(re_new['user'], 'Jessie', '测试失败') # globals()['userid'] = 66666 #全局变量 def test02(self): # print(userid) url = 'http://fanyi.youdao.com/translate' data = { 'doctype': 'json', 'type': 'AUTO', 'i': 'weather' } r = self.run.run_main(url, 'GET', data) print(r) re = json.dumps(r, indent=2, sort_keys=False, ensure_ascii=False) re_new = json.loads(re) self.assertEqual(re_new['errorCode'], 0, '测试失败') @unittest.skip('test03') # 跳过test03 def test03(self): url = 'http://fanyi.youdao.com/translate' data = { 'doctype': 'json', 'type': 'AUTO', 'i': '天气' } r = self.run.run_main(url, 'GET', data) print(r) re = json.dumps(r, indent=2, sort_keys=False, ensure_ascii=False) re_new = json.loads(re) self.assertEqual(re_new['errorCode'], 0, '测试失败') if __name__ == '__main__': suite = unittest.TestSuite() #创建放case的容器 suite.addTest(TestMethod('test01')) #添加case suite.addTest(TestMethod('test02')) suite.addTest(TestMethod('test03')) runner = unittest.TextTestRunner() runner.run(suite)
运行结果: