python 读取yaml文件的两种方法(在unittest中使用)

(编辑:jimmy 日期: 2025/2/27 浏览:2)

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

python读取yaml文件使用,有两种方式:

1.使用ddt读取

2,使用方法读取ddt的内容,在使用方法中进行调用

1.使用ddt读取

@ddt.ddt
class loginTestPage(unittest.TestCase):
    @ddt.file_data(path)
    @ddt.unpack
    def testlogin(self,**kwargs):
        u'''
       "输入邮件账号、用户名、密码符合要求
       勾选同意协议"  1、注册成功,跳转到注册成功页面    "
        1、验证URL,https://www.XX.com/site/register-success.html
        2、邮箱收到注册成功邮件
        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_path
        else:
            #获取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.ddt
class 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.html
        2、邮箱收到注册成功邮件
        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文件的资料请关注其它相关文章!

一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。