selenium3 web自动化测试框架 三:Unittest介绍及项目实战中的运用
unittest介绍及运用,可以参考之前写的文章,除了未结合web自动化演示,基础知识都有了
https://www.cnblogs.com/wuzhiming/p/8858305.html
unittest在自动化中的基础使用
下例演示用例的运行
# -*- coding:utf-8 -*- import unittest from selenium import webdriver from business.register_business import RegisterBusiness class TestLoginPage(unittest.TestCase): @classmethod def setUpClass(cls): print("所有步骤前置操作(测试计划):测试计划之前只执行一次") @classmethod def tearDownClass(cls): print("所有步骤后置操作(测试计划):测试计划结束后只执行一次") def setUp(self): print("每条用例执行的前置步骤") self.driver = webdriver.Chrome() url = "http://www.5itest.cn/register" self.driver.get(url) self.driver.maximize_window() self.register_business = RegisterBusiness(self.driver) def tearDown(self): print("每条测试用例的后置步骤") self.driver.close() def test_base_case(self): test_login = self.register_business.base_case("","admin","123456") def test_login_email_error(self): test_loginemailerror = self.register_business.login_email_error("1","admin","123456") @unittest.skip("就是不执行下面的用例") def test_must_run(self): print("必须执行我") if __name__ == "__main__": unittest.main()
如果想自定义用例的运行顺序可以参考上面链接中的用例操作即可
单次运行多个文件中的大量用例
演示用例1
# -*- coding:utf-8 -*- ‘‘‘ filename : unittest_test1.py ‘‘‘ import unittest from selenium import webdriver from business.register_business import RegisterBusiness class TestLoginPage(unittest.TestCase): @classmethod def setUpClass(cls): print("All 只执行一次") @classmethod def tearDownClass(cls): print("End 只执行一次") def setUp(self): print("start 每条用例执行的前置步骤") def tearDown(self): print("end 每条测试用例的后置步骤") def test_base_case1(self): print("第一条用例") def test_base_case2(self): print("第二条用例") @unittest.skip("就是不执行下面的用例") def test_must_run(self): print("必须执行我") if __name__ == "__main__": unittest.main()
演示用例2
# -*- coding:utf-8 -*- ‘‘‘ filename:unittest_test2.py ‘‘‘ import unittest class TestLoginPage(unittest.TestCase): @classmethod def setUpClass(cls): print("All 只执行一次") @classmethod def tearDownClass(cls): print("End 只执行一次") def setUp(self): print("start 每条用例执行的前置步骤") def tearDown(self): print("end 每条测试用例的后置步骤") def test_base_caseA(self): print("第一条用例") def test_base_caseB(self): print("第二条用例") def test_must_runC(self): print("必须执行我") if __name__ == "__main__": unittest.main()
单次运行上述两个文件中的用例代码
# -*- coding:utf-8 -*- import unittest import os class MyTestCase(unittest.TestCase): def test_allcase(self): #os.getcwd()获取当前路径 #os.path.join 连接两个或更多的路径名组件 case_path = os.path.join(os.getcwd()) suite = unittest.defaultTestLoader.discover(case_path,‘unittest_t*.py‘) unittest.TextTestRunner().run(suite) if __name__ == ‘__main__‘: unittest.main()
生成测试报告
# -*- coding:utf-8 -*- import unittest import os import datetime from HTMLTestRunner_cn import HTMLTestRunner class TestLoginPage(unittest.TestCase): @classmethod def setUpClass(cls): print("All 只执行一次") @classmethod def tearDownClass(cls): print("End 只执行一次") def setUp(self): print("start 每条用例执行的前置步骤") def tearDown(self): print("end 每条测试用例的后置步骤") def test_base_case1(self): print("第一条用例") def test_base_case2(self): print("第二条用例") @unittest.skip("就是不执行下面的用例") def test_must_run(self): print("必须执行我") if __name__ == "__main__": report = os.path.join(‘F:/python_stack/python_autotest/report/report.html‘) suite1 = unittest.TestSuite() suite1.addTests([TestLoginPage("test_base_case1"),TestLoginPage("test_base_case2"),TestLoginPage("test_must_run")]) with open(report,‘wb‘) as f: runner2 = HTMLTestRunner(stream=f,title=‘Test Result‘,description=‘operator:admin‘,verbosity=2) runner2.run(suite1) print(‘report is done‘) #os.path.dirname(__file__)的作用是 返回脚本的路径,即文件路径中所在的目录 report_path = os.path.dirname(report) print(report_path) #os.chdir() 方法用于改变当前工作目录到指定的路径。 os.chdir(report_path) stamp = datetime.datetime.strftime(datetime.datetime.now(),"%Y-%m-%d-%H-%M-%S") report_new_name = TestLoginPage.__name__ + ‘report‘+ stamp +‘.html‘ os.rename(‘report.html‘,report_new_name)
相关推荐
Henryztong 2019-12-21
abdstime 2020-07-29
sdwylry 2020-06-26
银角大王 2020-06-25
freerocker 2020-06-12
deadgrape 2020-05-30
goodby 2020-05-07
abdstime 2020-05-04
huanghong 2020-05-01
taiyangshenniao 2020-04-29
bcbeer 2020-04-23
加油奋斗吧 2020-04-07
jszy 2020-03-08
deadgrape 2020-03-06
zcabcd 2020-03-04
lucialee 2020-02-23
AnndyR 2020-02-22