pytest之fixture结合conftest.py文件使用+断言实战

马肤
这是懒羊羊

pytest之fixture结合conftest.py文件使用

  • conftest.py--存放固件
  • 固件的优先级
  • pytest执行流程
  • pytest之断言实战
  • pytest结合allure-pytest插件生成美观的报告

    conftest.py–存放固件

    在一个项目的测试中,大多数情况下会有多个类、模块、或者包要使用相同的测试夹具。这种情况下如果我们把测试夹具定义在某一个模块中则无法实现共享,针对这种情况,我们可以把需要共享的测试夹具放入一个单独的 conftest.py 文件中 ,这样多个可以实现多个测试模块共享了

    #读取数据的方式
    import pytest
    def read_yaml():
        return ['chenglong','zhenzidan','caiyilin']
    @pytest.fixture(scope="function",autouse=False,params=read_yaml(),ids=["c","z","l"],name='db')
    def exe_database_sql(request):
        print(request.param)
        print("执行sql查询")
        yield request.param
        print("关闭数据库连接")
    

    固件的优先级

    夹具定义可以通过参数 scope 指定夹具的级别,如果不指定夹具级别,scope 默认值为 function(用例级别)

    用例级别:scope = function

    测试类型:scope = class

    模块级别:scope = module

    包级别: scope = package

    会话级别:scope = session

    fixture里面有个scope参数可以控制fixture的作用范围:session > module > class > function

    pytest执行流程

    1.查间当前目录下的conftest py.文件.

    2查间当前目录下的pytest.ini文件.

    3查闻用例目录下的conftest.py文件.

    4查i间py文件中是含有setup.teardown.setup_class.teardown_cass

    5.再根据pytest.ini文件的测试用例的规则去查找用例并执行,

    pytest之断言实战

        def test_baili(self):
            print("测试百利老师")
            assert 1==1
        def test_tom(self):
            print("测试汤姆老师")
            assert 'a' in 'abc'
    

    pytest结合allure-pytest插件生成美观的报告

    • dos窗口检查

      pytest之fixture结合conftest.py文件使用+断言实战,在这里插入图片描述,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,os,终端,第1张

      • pycharm终端检查

        pytest之fixture结合conftest.py文件使用+断言实战,在这里插入图片描述,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,os,终端,第2张

      • 生成临时报告

        pytest之fixture结合conftest.py文件使用+断言实战,在这里插入图片描述,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,os,终端,第3张

        • 输出报告

          pytest之fixture结合conftest.py文件使用+断言实战,在这里插入图片描述,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,os,终端,第4张

          pytest之fixture结合conftest.py文件使用+断言实战,在这里插入图片描述,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,os,终端,第5张

          pytest之fixture结合conftest.py文件使用+断言实战,在这里插入图片描述,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,os,终端,第6张

          pytest之fixture结合conftest.py文件使用+断言实战,在这里插入图片描述,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,os,终端,第7张


文章版权声明:除非注明,否则均为VPS857原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复:表情:
评论列表 (暂无评论,0人围观)

还没有评论,来说两句吧...

目录[+]

取消
微信二维码
微信二维码
支付宝二维码