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("test@163.com","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)
用例执行失败后在指定目录生成错误截图
<pre> def tearDown(self): print("每条测试用例的后置步骤") #错误截图
for method_name,error in self._outcome.errors: if error:
case_name = self._testMethodName #保存文件的路径必须提前创建好,不然保存截图失败
image_path = os.path.join(os.getcwd() + '\\' + "image" + "\\"+ case_name + ".png")
self.driver.save_screenshot(image_path)
self.driver.close()</pre>
点赞关注~加入我们,642830685,免费领取最新软件测试大厂面试资料和Python自动化、接口、框架搭建学习资料!技术大牛解惑答疑,同行一起交流。
网友评论