‘‘‘1. 在suite.addTest时,可以把需要的用例先写入一个列表list中,list当做addTest的参数;2. 在unittest.main(verbosity=2)中,默认为1,设置为2,可以输出每个case的执行情况;3. 所有用例的开始结尾,只运行一遍setup 和 teardown,那么用setUpClass 和 tearDownClass,注意装饰器;4. 在不需要的case上写入@unittest.skip(‘"I don\‘t want to run this case."‘),就可以跳过它 5. skip装饰器一共有三个: unittest.skip(reason)、 unittest.skipIf(condition, reason)、 unittest.skipUnless(condition, reason), skip无条件跳过, skipIf当condition为True时跳过, skipUnless当condition为False时跳过。6. 文件日期命名:now = time.strftime(‘%Y-%m-%M %H_%M_%S‘) ‘‘‘ 注意:1. 未加入HTMLTestRunner时,如下不可以被注销:
2. https 报warnning,直接把相关文件中的函数暴力注销:
open -a pycharm /Users/vivi/Library/Python/3.6/lib/python/site-packages/requests/packages/urllib3/connectionpool.py
注销844-851
3. HTMLTestRunner 只支持python2.7,需要修改一些地方,这个可以google,到处都有,很全面;
from unittest import TestCase import requests from play import LoggIn import unittest from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) import HTMLTestRunner import sys,time class TestLoggIn(TestCase): @classmethod def setUpClass(self): self.url = LoggIn.logUrl print(‘do something before‘) @classmethod def tearDownClass(self): print(‘over‘) pass # def setUp(self): # self.url = LoggIn.logUrl # # def tearDown(self): # pass def test_account_empty(self): data = {‘abbr‘: ‘US‘, ‘clientType‘: ‘ios‘, ‘dBrand‘: ‘apple‘, ‘imagePHeight‘: ‘736‘, ‘imagePixels‘: ‘828‘, ‘osVersion‘: ‘iOS10.3‘, ‘password‘: ‘******‘, ‘rememberMe‘: ‘1‘,‘st‘: ‘1496557229892‘, ‘udid‘: ‘9fa81f8b42277b1e9567e‘} r= requests.post(self.url,data=data,verify=False) result = r.json() self.assertEqual(result[‘code‘],‘220120‘) self.assertEqual(result[‘message‘],‘is empty!‘) self.assertEqual(result[‘result‘],‘account or password is null‘) # @unittest.skip(‘"I don\‘t want to run this case."‘) def test_account_error(self): data = {‘abbr‘: ‘US‘, ‘clientType‘: ‘ios‘, ‘dBrand‘: ‘apple‘, ‘imagePHeight‘: ‘736‘, ‘imagePixels‘: ‘828‘, ‘loginAccount‘:‘name123‘,‘osVersion‘: ‘iOS10.3‘, ‘password‘: ‘******‘, ‘rememberMe‘: ‘1‘,‘st‘: ‘1496557229892‘, ‘udid‘: ‘9fac416e2d75e77b1e9567e‘} r= requests.post(self.url,data=data,verify=False) result = r.json() self.assertEqual(result[‘code‘],‘22012811‘) self.assertEqual(result[‘message‘],‘account or password error thrid!‘) self.assertEqual(result[‘result‘],‘account or password error third!‘) def test_account_pass(self): data = {‘abbr‘: ‘US‘, ‘clientType‘: ‘ios‘, ‘dBrand‘: ‘apple‘, ‘imagePHeight‘: ‘736‘, ‘imagePixels‘: ‘828‘, ‘loginAccount‘:‘name‘,‘osVersion‘: ‘iOS10.3‘, ‘password‘: ‘***‘, ‘rememberMe‘: ‘1‘,‘st‘: ‘1496557229892‘, ‘udid‘: ‘9fac416e2d75e42277b1e9567e‘} r= requests.post(self.url,data=data,verify=False) result = r.json() self.assertEqual(result[‘code‘],‘200‘) self.assertEqual(result[‘message‘],‘success!‘) self.assertEqual(result[‘result‘][‘loginAccount‘],‘name‘) self.assertEqual(result[‘result‘][‘memberPicture‘],‘pre/diyrelease/320187/150365518619893164.jpg‘) if __name__ == ‘__main__‘: # verbosity=2 详细输出每个case的执行结果 # verbosity=1 只输出对错:失败是 F,出错是 E,跳过是 S # verbosity=0 不输出任何结果 # unittest.main(verbosity=2) suite = unittest.TestSuite() # 添加到TestSuite中的case是会按照添加的顺序执行的 suite.addTest(TestLoggIn(‘test_account_empty‘)) suite.addTest(TestLoggIn(‘test_account_error‘)) suite.addTest(TestLoggIn(‘test_account_pass‘)) # 这样 用列表,和上面一个个添加一样的效果 # tests = [TestLoggIn(‘test_account_empty‘),TestLoggIn(‘test_account_error‘),TestLoggIn(‘test_account_pass‘)] # suite.addTest(tests) # 执行测试用例 # runner = unittest.TextTestRunner() # runner.run(suite) # 执行并写入文件 # suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestLoggIn)) # now = time.strftime(‘%Y-%m-%M %H_%M_%S‘,time.localtime(time.time())) now = time.strftime(‘%Y-%m-%M %H_%M_%S‘) filename =‘/Users/vivi/PycharmProjects/testreport_html/report/‘+ now + ‘_result.html‘ # filename = ‘/Users/vivi/PycharmProjects/a.html‘ fp = open(filename,‘wb‘) runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=‘Play LogIn Test‘, description=‘generated by HTMLTestRunner.‘, verbosity=2 ) runner.run(suite) fp.close()
执行成功:
执行失败:
时间: 2024-10-12 06:30:49