• 【pytest学习5】fixture中conftest.py文件


    不同的用例使用到登录的场景,如果不同文件夹的用例使用到相同的fixture应该怎么办呢?如果再写一个fixture就会比较麻烦,那么强大的pytest肯定不会让我们这样麻烦的,只会让我们更加方便。这里就要引入新的知识点conftest.py文件

     

    conftest.py

    conftest.py文件属于fixture中的共享配置文件,有以下几个特点:

    • conftest.py文件必须命名这个,不能随便改名
    • conftest.py文件虽然属于pytest文件,但是不用导入即可使用
    • conftest.py文件可以存放多个fixture方法
    • conftest.py和执行的用例要在同一个pakage下,并且有__init__.py文件
    • 不同目录可以有单独的 conftest.py,一个项目中可以有多个 conftest.py

    比如:有一个测试场景,存在两个用例目录,这两个目录下的用例,有的使用到了登录方法和清除数据的方法,有的没有用到。这里就可以通过把两个方法都放到conftest.py文件中

    复制代码
    # conftest.py
    import pytest
    
    
    @pytest.fixture()
    def login():
        print('输入用户名,密码,完成登陆')
    
    
    @pytest.fixture()
    def clear():
        print('清除用例数据内容!!')
    复制代码

    创建好两个fixture内容后,然后直接在我们的用例中调用就行了

    先看下目录内容,这样更加清楚conftest.py执行内容

    复制代码
    auto_test                         # 项目名称
        --- test_01                   # 用例文件夹
            --- test__01.py           # 用例文件
            --- __init__.py     
                       
        ---test_02                    # 用例文件夹
            --- test__02.py           # 用例文件
            --- __init__.py  
        conftest.py                   # conftest.py文件
    复制代码

    然后分别在两个用例文件下编写测试用例

    复制代码
    # test__01.py
    import pytest
    
    class Test_01:
    
        def test_01(self,login):
            print('需要用到登录功能')
            print('----用例01---')
    
        def test_02(self,clear):
            print('不需要登录功能')
            print('----用例02---')
    
        def test_03(self, login):
            print('需要用到登录功能')
            print('----用例03---')
    
    
    if __name__ == '__main__':
        pytest.main(['-s','test__01.py'])
    复制代码
    复制代码
    # test__02.py
    
    import pytest
    
    
    class Test_02:
    
        def test_01(self, login):
            print('需要用到登录功能')
            print('----用例01---')
    
        def test_02(self, clear):
            print('不需要登录功能')
            print('----用例02---')
    
        def test_03(self, login):
            print('需要用到登录功能')
            print('----用例03---')
    
    
    if __name__ == '__main__':
        pytest.main(['-s','test__02.py'])
    复制代码

    然后进入到auto目录下,直接执行代码pytest -s 查看执行结果。

    发现配置conftest.py中fixture已经全部都生效了,我们也可以通过--setup--show来查看详细的执行过程

    无论是单独执行用例还是全部执行用例,都可以进行执行fixture内容

    声明 欢迎转载,但请保留文章原始出处:) 博客园:https://www.cnblogs.com/chenxiaomeng/ 如出现转载未声明 将追究法律责任~谢谢合作
  • 相关阅读:
    ThinkPHP第二十天(getField用法、常用管理员表结构、树形结构前小图标CSS)
    ThinkPHP第十九天(Ueditor高亮插件、扩展函数载入load、静态缓存)
    Bootstrap第一天
    ThinkPHP第十八天(Widget类的使用,连贯操作where IN用法,缓存S函数使用)
    ThinkPHP第十七天(隐藏index.php和简短路径配置)
    ThinkPHP第十六天(redirect、join、视图模型)
    ThinkPHP第十五天(setField、setInc、setDec、关联模型)
    ThinkPHP第十四天(显示TRACE界面配置,关联模型详解定义)
    ThinkPHP第十三天(CONF_PATH、APP_PATH,UEditor用法)
    ThinkPHP常量参考
  • 原文地址:https://www.cnblogs.com/chenxiaomeng/p/14814856.html
Copyright © 2020-2023  润新知