selenium3 web自動化測試框架 三:Unittest介紹及項目實戰中的運用

unittest介紹及運用,能夠參考以前寫的文章,除了未結合web自動化演示,基礎知識都有了html

http://www.javashuo.com/article/p-plbyqxqe-d.htmlpython

 

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()

 

若是想自定義用例的運行順序能夠參考上面連接中的用例操做便可web

 

單次運行多個文件中的大量用例 


 演示用例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)

 

用例執行失敗後在指定目錄生成錯誤截圖


    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()
相關文章
相關標籤/搜索