前言
Pytest 的 fixtrue 是存在作用域的,比如作用域为函数级别,那么没执行一条用例,就会重新执行一次 fixtrue,如果是类级别,那么多个类执行时会在去执行 fixture。fixture 的作用域有 5 个,分别是: function,class,module,package,session。有了这些作用域我们可以不用重复的去执行 fixture,从而节省时间。下面我们通过代码示例分别对这五种作用域来实验。
function 作用域
function: 默认范围,fixture 在测试结束时被销毁,示例代码如下:
import pytest
@pytest.fixture(scope="function")def login(): print("正在登录")
class TestDemo: def test_demo1(self, login): print("测试用例1")
def test_demo2(self, login): print("测试用例2")
------------运行结果如下:============================= test session starts =============================collecting ... collected 2 items
test_a.py::TestDemo::test_demo1 正在登录PASSED [ 50%]测试用例1
test_a.py::TestDemo::test_demo2 正在登录PASSED [100%]测试用例2
============================== 2 passed in 0.03s ==============================
复制代码
我们可以看到正在登录执行了两次,也就是说我们的函数级别的 fixture 在每个用例执行前都会执行内部代码。
class 作用域
class: fixture 在类中的最后一个测试执行结束后销毁,示例代码如下:
import pytest
@pytest.fixture(scope="class")def login(): print("正在登录")
class TestDemo: def test_demo1(self, login): print("测试用例1")
def test_demo2(self, login): print("测试用例2")
-------------------运行结果如下:============================= test session starts =============================collecting ... collected 2 items
test_a.py::TestDemo::test_demo1 正在登陆PASSED [ 50%]测试用例1
test_a.py::TestDemo::test_demo2 PASSED [100%]测试用例2
============================== 2 passed in 0.03s ==============================
复制代码
我们可以看到与上面的方法级别相比,类级别只执行了一次正在登录。
module 作用域
module: fixture 在模块中的最后一个测试执行结束后销毁,示例代码如下:
import pytest
@pytest.fixture(scope="module")def login(): print("正在登录")
class TestDemo: def test_demo1(self, login): print("测试用例1")
def test_demo2(self, login): print("测试用例2")
def test_demo3(login): print("测试用例3")
def test_demo4(login): print("测试用例4")
--------------------============================= test session starts =============================collecting ... collected 4 items
test_a.py::TestDemo::test_demo1 正在登录PASSED [ 25%]测试用例1
test_a.py::TestDemo::test_demo2 PASSED [ 50%]测试用例2
test_a.py::test_demo3 PASSED [ 75%]测试用例3
test_a.py::test_demo4 PASSED [100%]测试用例4
============================== 4 passed in 0.05s ==============================
复制代码
我们可以看到正在登录执行了一次,我们在这个模块中有 4 条用例,有类也有函数,但就只执行了一次,说明我们定义的模块作用域生效了。
package 作用域
package: fixture在包中的最后一个测试执行结束后销毁,整体的目录结构如下图:
根目录:mytest
二级目录: scripts
三级目录:test_demo01.py
三级目录: test_demo02.py
二级目录: tests
三级目录:test_demo01.py
三级目录: test_demo02.py
二级目录:conftest.py
二级目录:test_demo.py
test_demo01.py内容如下:
def test_demo01(login): print("scripts包内的第一个测试用例")
复制代码
其他的测试文件内容依次类推。
conftest.py内容如下:
import pytest
@pytest.fixture(scope="package")# @pytest.fixture(scope="module")def login(): print("正在登陆") return "login"
复制代码
执行结果如下:
======================================================================= test session starts =======================================================================platform win32 -- Python 3.7.7, pytest-7.4.4, pluggy-1.2.0 -- C:\Users\89703\PycharmProjects\mytest\venv\Scripts\python.execachedir: .pytest_cacherootdir: C:\Users\89703\PycharmProjectsmytest/mytest/scripts/test_demo02.py::test_demo02 scripts包内的第二个测试用例PASSEDmytest/tests/test_demo01.py::test_demo01 tests包内的第一个测试用例PASSEDmytest/tests/test_demo02.py::test_demo02 tests包内的第二个测试用例PASSED
======================================================================== 5 passed in 0.04s ========================================================================
复制代码
session 作用域
session: fixture 在整个测试周期执行结束后销毁,我们只需要修改conftest.py的内容,示例如下:
conftest.py内容:
import pytest
@pytest.fixture(scope="session")# @pytest.fixture(scope="module")def login(): print("正在登陆") return "login"
复制代码
执行结果如下:
======================================================================= test session starts =======================================================================platform win32 -- Python 3.7.7, pytest-7.4.4, pluggy-1.2.0 -- C:\Users\89703\PycharmProjects\mytest\venv\Scripts\python.execachedir: .pytest_cacherootdir: C:\Users\89703\PycharmProjectscollected 5 items
mytest/test_demo.py::test_demo 正在登陆包外的测试用例PASSEDmytest/mytest/scripts/test_demo01.py::test_demo01 scripts包内的第一个测试用例PASSEDmytest/mytest/scripts/test_demo02.py::test_demo02 scripts包内的第二个测试用例PASSEDmytest/tests/test_demo01.py::test_demo01 tests包内的第一个测试用例PASSEDmytest/tests/test_demo02.py::test_demo02 tests包内的第二个测试用例PASSED
======================================================================== 5 passed in 0.03s ========================================================================
复制代码
总结
fixtrue 作用范围:
function: 默认范围,fixture 在测试结束时被销毁
class: fixture 在类中的最后一个测试执行结束后销毁
module: fixture 在模块中的最后一个测试执行结束后销毁
package: fixture 在包中的最后一个测试执行结束后销毁
session: fixture 在整个测试周期执行结束后销毁
希望本文能够帮到大家!
评论