【pytest】(九)conftest.py是什么?

如果第三方插件满足不了自己的需求,那么就需要你自己定制化开发自己的插件了。

正因如此,pytest的插件生态非常的丰富,一般的常规性需求基本上都可以找到现成的第三方插件。

说到开发插件的话,还是要引入一个新的名词hook functions ,对于钩子函数是要另起一篇介绍的,那也是真正开发插件的开始。

一个插件里包含了一个或者多个钩子函数,编写钩子函数可以实现功能丰富的插件,所以pytest框架从配置、收集用例到运行用例,再到最后出报告,都是通过调用各种插件来实现的。

目前所说插件就是以下这3种了:

  • 内置插件,就是内部_pytest这个目录下的模块(.Python37Libsite-packages_pytest)
  • 外部插件,就是你安装的第三方插件
  • conftest.py ,这个可以理解成框架的固定写法,把hook或者fixture写在这个文件里,就会自动去调用。

一、conftest.py

内置插件、外部插件都还是很好理解的,那conftest文件是什么?
其实,它是一个python文件。

开个玩笑,其实conftest.py通常可以用来做2个事情:

  1. 存放你的fixture函数
  2. 在里面写自己的本地插件

另外,可以根据conftest.py的文件存放位置,来决定它的适用范围。

二、conftest.py放在哪

在之前的文章【pytest】(六) pytest中fixture的使用 中,我们用到的fixture函数都是写在了测试case的文件中。
比如:fixture函数logout_after_login_success()是在测试case test_login_success 执行结束后执行,用来退出登录。

@pytest.fixture()
def logout_after_login_success(browser):
    yield 
    HP = HomePage(browser)
    HP.logout()

@pytest.mark.critical
def test_login_success(browser, logout_after_login_success, login_temp):
    '''合法登录'''
    
    HP = HomePage(browser)
    assert HP.greetings().text == "Hi, test1"

那如果你发现你的一个fixture函数,在很多case模块中都要用到,这时候你要怎么引用呢?

没关系,我们可以把这个fixture函数放到conftest.py中去。
比如说,我现在有一个项目结构是这样的:

-demo_project
    -test_case
	-test_module_01
	    /* test_case1.py
            /* test_case2.py
	-test_module_02
	    /* test_case3.py
    -utils

test_module_01模块下,有2个case用例文件,分别是 test_case1.pytest_case2.py

test_module_02模块下,有一个case用例文件test_case3.py

现在如果想要test_module_01模块下的2个测试用例都用上同一个fixture函数,那么就可以在test_module_01模块下,新建一个与2个case文件同级的conftest.py ,文件结构就变成了这样:

 -demo_project
    -test_case
	-test_module_01
	    /* test_case1.py
            /* test_case2.py
            /* conftest.py     -----> 在这
	-test_module_02
	    /* test_case3.py
    -utils

test_case1.pytest_case2.pyconftest.py代码分别为:

# test_case1.py

import pytest

def test_01(demo_fixture):
    print("执行了test_01")


if __name__ == '__main__':
    pytest.main(["-s", "test_case1.py"])
# test_case2.py

import pytest

def test_02(demo_fixture):
    print("执行了test_02")
    

if __name__ == '__main__':
    pytest.main(["-s", "test_case2.py"])
# /test_module_01/conftest.py

import pytest

@pytest.fixture
def demo_fixture():
    print("这是fixture函数的输出")

分别执行test_case1.pytest_case2.py , 都输出了demo_fixture()函数的内容。

============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: D:练习demo_project	est_case	est_module_01
collected 1 item

test_case1.py
这是fixture函数的输出
执行了test_01
.

============================== 1 passed in 0.01s ==============================
[Finished in 0.4s]
============================= test session starts =============================
platform win32 -- Python 3.8.5, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: D:练习demo_project	est_case	est_module_01
collected 1 item

test_case2.py
这是fixture函数的输出
执行了test_02
.

============================== 1 passed in 0.02s ==============================
[Finished in 0.4s]

再继续,如果test_module_02模块下的也想用,怎么办?

那就把conftest.py再往外提一层,与 test_module_01test_module_02保持同级即可。

-demo_project
    -test_case
	-test_module_01
	    /* test_case1.py
            /* test_case2.py
	-test_module_02
	    /* test_case3.py
        /* conftest.py
    -utils

大家可以试一下,这里就不继续代码演示了。

总结来说,conftest.py文件可以作用于同级以及 以下的模块。
但是,当以下层级中也存在了另一个conftest.py,那么以下层级将由另一个conftest.py文件接管。

放个图示辅助说明一下:


2张图能说的事,水了这么多,哈哈。

原文地址:https://www.cnblogs.com/pingguo-softwaretesting/p/13657744.html