9、conftest使用

114 阅读1分钟

1、conftest特点

  • 可以跨.py文件调用
  • conftest.py与运行的用例要在同一个pakage下
  • 不需要import导入 conftest.py,pytest用例会自动识别该文件,放到项目的根目录下就可以全局目录调用了,如果放到某个package下,那就在该package内有效,可有多个conftest.py
  • conftest.py配置脚本名称是固定的,不能改名称

2、conftest结合fixture的使用

conftest代码如下

import pytest


@pytest.fixture()
def login():
    print("\n登录\n")
    yield
    print("\n登出\n")

测试用例如下调用conftest文件

import pytest

class TestCase:


    def test_1(self,login):
        print("\ntest 1\n")

    @pytest.mark.usefixtures('login')
    def test_2(self):
        print("\ntest 2\n")

执行结果如下

image.png