pytest文档2--firture之conftest.py

conftest.py配置

1.上面一个案例是在同一个.py文件中,多个用例调用一个登陆功能,如果有多个.py的文件都需要调用这个登陆功能的话,那就不能把登陆写到用例里面去了。
此时应该要有一个配置文件,单独管理一些预置的操作场景,pytest里面默认读取conftest.py里面的配置

conftest.py配置需要注意以下点:

  • conftest.py配置脚本名称是固定的,不能改名称
  • conftest.py与运行的用例要在同一个pakage下,并且有__init__.py文件
  • 不需要import导入 conftest.py,pytest用例会自动查找

示例

__init__.py

conftest.py
    # coding:utf-8
    import pytest

    @pytest.fixture()
    def login():
        print("输入账号,密码先登录")

test_fix1.py
    # coding:utf-8
    import pytest
    
    def test_s1(login):
        print("用例1:登录之后其它动作111")
    
    def test_s2():  # 不传login
        print("用例2:不需要登录,操作222")
    
    def test_s3(login):
        print("用例3:登录之后其它动作333")
    
    if __name__ == "__main__":
        pytest.main(["-s", "test_fix1.py"])

test_fix2.py
    # coding:utf-8
    import pytest
    
    def test_s4(login):
        print("用例4:登录之后其它动作111")
    
    def test_s5():  # 不传login
        print("用例5:不需要登录,操作222")
    
    if __name__ == "__main__":
        pytest.main(["-s", "test_fix2.py"])

 

posted @ 2020-05-13 11:32  云深不知处!  阅读(112)  评论(0编辑  收藏  举报