python 讀取yaml文件的兩種方法(在unittest中使用)
作者:做夢(mèng)的人(小姐姐)出處:https://www.cnblogs.com/chongyou/
python讀取yaml文件使用,有兩種方式:
1.使用ddt讀取
2,使用方法讀取ddt的內(nèi)容,在使用方法中進(jìn)行調(diào)用
1.使用ddt讀取
@ddt.ddtclass loginTestPage(unittest.TestCase): @ddt.file_data(path) @ddt.unpack def testlogin(self,**kwargs):u’’’ '輸入郵件賬號(hào)、用戶名、密碼符合要求 勾選同意協(xié)議' 1、注冊(cè)成功,跳轉(zhuǎn)到注冊(cè)成功頁面 '1、驗(yàn)證URL,https://www.XX.com/site/register-success.html2、郵箱收到注冊(cè)成功郵件3、數(shù)據(jù)庫(kù)中user表中有成功添加注冊(cè)賬號(hào)數(shù)據(jù)' :return:’’’ self.loginPage = CBLogin(self.driver)log.info(kwargs)self.page = Page(self.driver,kwargs.get(’login_url’)) self.page.send_text(self.loginPage.login_sendkes_username(),kwargs.get(’username’))self.page.send_text(self.loginPage.login_sendkes_password(),kwargs.get(’password’))self.page.click(self.loginPage.login_click_btn())# 斷言登錄是否成功self.assertIsNotNone(self.loginPage.is_success(),'元素沒有查找到,登錄失敗')
2.使用已有的方法進(jìn)行調(diào)用
class HandleYmal: ''' 獲取測(cè)試環(huán)境的配置 ''' def __init__(self,file_path=None):if file_path: self.file_path=file_pathelse: #獲取path root_dir=os.path.dirname(os.path.abspath(’.’)) print(root_dir) self.file_path=root_dir+'/config/base.yaml' def get_data(self):fp=open(self.file_path,encoding='utf-8')data=yaml.load(fp)return data @ddt.ddtclass loginTestPage(unittest.TestCase): @classmethod def setUpClass(cls):'''前置應(yīng)該是讀取所有內(nèi)容''' yaml=HandleYmal()cls.kwargs=yaml.get_data()[’testenvironment’]cls.driver = webdriver.Chrome() def testlogin(self):u’’’ '輸入郵件賬號(hào)、用戶名、密碼符合要求 勾選同意協(xié)議' 1、注冊(cè)成功,跳轉(zhuǎn)到注冊(cè)成功頁面 '1、驗(yàn)證URL,https://www.chinabrands.com/site/register-success.html2、郵箱收到注冊(cè)成功郵件3、數(shù)據(jù)庫(kù)中user表中有成功添加注冊(cè)賬號(hào)數(shù)據(jù)' :return:’’’ self.loginPage = CBLogin(self.driver)log.info(self.kwargs)self.page = Page(self.driver,self.kwargs.get(’login_url’))self.page.send_text(self.loginPage.login_sendkes_username(),self.kwargs.get(’username’))self.page.send_text(self.loginPage.login_sendkes_password(),self.kwargs.get(’password’))self.page.click(self.loginPage.login_click_btn())# 斷言登錄是否成功self.assertIsNotNone(self.loginPage.is_success(),'元素沒有查找到,登錄失敗')
以上就是python 讀取yaml文件的兩種方法(在unittest中使用)的詳細(xì)內(nèi)容,更多關(guān)于python 讀取yaml文件的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. JAMon(Java Application Monitor)備忘記2. Python 的 __str__ 和 __repr__ 方法對(duì)比3. Java8內(nèi)存模型PermGen Metaspace實(shí)例解析4. IntelliJ IDEA設(shè)置背景圖片的方法步驟5. Spring security 自定義過濾器實(shí)現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實(shí)例代碼)6. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法7. 學(xué)python最電腦配置有要求么8. 基于python實(shí)現(xiàn)操作git過程代碼解析9. Python OpenCV去除字母后面的雜線操作10. 增大python字體的方法步驟
