日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python selenium unittest_python+selenium+unittest单元测试框架

發(fā)布時間:2025/4/5 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python selenium unittest_python+selenium+unittest单元测试框架 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

unittest簡介

python自動化測試不得不提unittest,unittest原名為PyUnit是python自帶的單元測試框架,類似于java的JUnit是有JUnit衍生而來。

unittest官網(wǎng)地址:https://docs.python.org/2/library/unittest.html

unittest整體結(jié)構(gòu)

unittest庫包括:TestCase、TestSuite、TestLoder、TextRunner、TextTestResult、TestFixture

TestCase是測試的最小單元模塊。它檢查對特定輸入集合的特定響應(yīng)。unittest提供給一個基類Testcase,可以使用這個類創(chuàng)建一個新測試testTestCase 。

TestSuite是測試集,測試集是測試用例、測試集或者二者的一個集合。它被用于把測試整合在一起,批量運行測試。

TestLoder是用來加載 TestCase到TestSuite中,其中有幾個loadTestsFrom_()方法,就是從各個地方尋找TestCase,創(chuàng)建他們的實例,然后add到TestSuite中,再返回一個TestSuite實例

TextRunner是運行測試器,測試運行器是協(xié)調(diào)測試執(zhí)行并向用戶提供結(jié)果的組件。運行器可以使用圖形界面、文本界面、或返回特定值來指示執(zhí)行測試的結(jié)果。

TextTestResult測試結(jié)果會保存到TextTestResult實例中,包括運行了多少用例,成功與失敗多少等信息

TestFixture表示執(zhí)行一個或者多個測試前的準(zhǔn)備工作,確保每個測試之間的獨立性。這可能涉及到創(chuàng)建臨時或者代理數(shù)據(jù)庫、目錄、服務(wù)器情動進(jìn)程等相關(guān)準(zhǔn)備工作

實例

from selenium import webdriver

import unittest

import time

class login1Test(unittest.TestCase):

def setUp(self):

self.url='http://test1.xgs.xiaoshushidai.com'

self.driver=webdriver.Firefox()

self.driver.maximize_window()

self.driver.implicitly_wait(30)#隱性等待30秒

self.driver.get(self.url)

def login(self,username,passwrod,txtVerify):

self.driver.find_element_by_id('txtUserName').send_keys(username)

self.driver.find_element_by_id('txtPassword').send_keys(passwrod)

self.driver.find_element_by_id('txtVerify').send_keys(txtVerify)

self.driver.find_element_by_id('btnSubmit').click()

def tearDown(self):

self.driver.quit()

def test_loginsSuccess(self):

'''登錄成功'''

self.login("T_mac",'Aa654321','123')

tip=self.driver.find_element_by_class_name('info').text

print(tip)

self.assertEqual(tip,'您好,T_mac''\n''測試刪除')

def test_nulluser(self):

'''用戶名為空'''

self.login('','','')

self.driver.switch_to.alert().accept()

nullusererror=self.driver.switch_to.alert().text

self.assertEqual(nullusererror,'請輸入管理員賬號')

def test_nullpwd(self):

'''密碼為空'''

self.login('13620180611','','')

self.driver.switch_to.alert().accept()

nullpassword=self.driver.switch_to.alert().text

self.assertEqual(nullpassword,'請輸入管理員密碼')

def test_nulltxtVerify(self):

self.login('13620180611','Aa654321','')

self.driver.switch_to.alert().accept()

nulltxtVerify=self.driver.switch_to.alert().text

self.assertEqual(nulltxtVerify,'請輸入驗證碼')

if __name__=='__main__':

unittest.main()

某網(wǎng)站的登錄setUp是初始化函數(shù)表示運行每個用例時首先需要初始化用例為test_xxx。teraDown為清除數(shù)據(jù)

test_xxx命名的函數(shù)為測試用例unittest.main()識別類中的以test_xxx命名的函數(shù)(執(zhí)行測試用例)

構(gòu)造測試用例集

# coding=utf-8

import unittest

from xiaoshutest.login import login1Test

import HTMLTestRunnerCN

#構(gòu)造測試集

suite=unittest.TestSuite()

suite.addTest(login1Test('test_loginsSuccess'))

suite.addTest(login1Test('test_loginfail'))

suite.addTest(login1Test('test_loginusernamefail'))

suite.addTest(login1Test('test_nulluser'))

suite.addTest(login1Test('test_nullpwd'))

表示在suite中添加上面例子中測試類的login1Test中的測試用例

if __name__=='__main__':

#執(zhí)行測試

runner=unittest.TestSuite()

with open('HTMLReport.html','wb+') as f:

runner=HTMLTestRunnerCN.HTMLTestReportCN(stream=f,title='測試報告',description='測試報告詳情',verbosity=2,tester='miss→麥')

runner.run(suite)#運行suite把報告寫成HTML格式

f.close()

用例的運行報告

總結(jié)

以上是生活随笔為你收集整理的python selenium unittest_python+selenium+unittest单元测试框架的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。