您的位置:首頁技術文章
文章詳情頁

python 讀取yaml文件的兩種方法(在unittest中使用)

瀏覽:38日期:2022-07-03 14:36:17

作者:做夢的人(小姐姐)出處:https://www.cnblogs.com/chongyou/

python讀取yaml文件使用,有兩種方式:

1.使用ddt讀取

2,使用方法讀取ddt的內容,在使用方法中進行調用

1.使用ddt讀取

@ddt.ddtclass loginTestPage(unittest.TestCase): @ddt.file_data(path) @ddt.unpack def testlogin(self,**kwargs):u’’’ '輸入郵件賬號、用戶名、密碼符合要求 勾選同意協議' 1、注冊成功,跳轉到注冊成功頁面 '1、驗證URL,https://www.XX.com/site/register-success.html2、郵箱收到注冊成功郵件3、數據庫中user表中有成功添加注冊賬號數據' :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.使用已有的方法進行調用

class HandleYmal: ''' 獲取測試環境的配置 ''' 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):'''前置應該是讀取所有內容''' yaml=HandleYmal()cls.kwargs=yaml.get_data()[’testenvironment’]cls.driver = webdriver.Chrome() def testlogin(self):u’’’ '輸入郵件賬號、用戶名、密碼符合要求 勾選同意協議' 1、注冊成功,跳轉到注冊成功頁面 '1、驗證URL,https://www.chinabrands.com/site/register-success.html2、郵箱收到注冊成功郵件3、數據庫中user表中有成功添加注冊賬號數據' :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中使用)的詳細內容,更多關于python 讀取yaml文件的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
国产综合久久一区二区三区