1.概念

1.fixture的使用场景

  • 返回值
  • 初始化、清理【yield】
  • conftest.py【1.fixture的共享;2.使用pytest预设钩子,来补充脚本的能力】
  • fixture的参数化【@pytest.fixture(params=xx)】

    2.fixture函数放置的位置

  • 单独的test_xx.py文件中

  • 【推荐】conftest.py文件中

    3.pytest检测fixture的顺序

  1. 优先搜索该测试所在的module(test_xx.py)
  2. 搜索conftest.py
  3. 在testcase运行前执行该fixture

    2.fixture详细示例

    1.fixture_返回值

    ```python import pytest

fixture的返回值(场景)

@pytest.fixture()

使用pytest.fixture()来装饰helloData()函数

def helloData(): return “hello world”

def test_hello1(helloData): # test_hello1()的形参是“被装饰的helloData()函数” assert “hello” in helloData

  1. ![f1.png](https://cdn.nlark.com/yuque/0/2021/png/247346/1615723080573-a46394b1-bd29-4605-8eb0-e069165040e9.png#align=left&display=inline&height=806&margin=%5Bobject%20Object%5D&name=f1.png&originHeight=806&originWidth=1315&size=62862&status=done&style=none&width=1315)
  2. <a name="DtvRj"></a>
  3. ## 2.fixture_初始化、清理
  4. <a name="KlMyq"></a>
  5. ### 1.fixture执行的步骤
  6. 说明 pytest --setup-show # 查看fixture的运行步骤<br />![setup.png](https://cdn.nlark.com/yuque/0/2021/png/247346/1615800061501-24e6d103-b9a1-4f8a-8c8c-e77f33cd6273.png#align=left&display=inline&height=892&margin=%5Bobject%20Object%5D&name=setup.png&originHeight=892&originWidth=1657&size=122951&status=done&style=none&width=1657)
  7. <a name="BDnLG"></a>
  8. ### 2.结合selenium
  9. ```python
  10. import pytest
  11. from selenium import webdriver
  12. @pytest.fixture
  13. def driverOpt(selenium):# 选装pytest-selenium插件
  14. selenium.maximize_window()
  15. yield
  16. selenium.quit()
  17. def test_ui(driverOpt,selenium):
  18. selenium.get("https://www.baidu.com")
  19. assert selenium.title=="百度一下,你就知道"
  20. # 运行命令:pytest -sv --driver=Chrome test_demo2.py::test_ui [chromedriver需配置在系统Path中]

3.fixture_conftest.py

1.conftest.py的主要功能

  • 实现fixture函数的共享(多个testcase可引用conftest.py中的fixture函数)
  • 使用pytest预设钩子,来补充脚本的能力

    2.conftest.py存放的位置

  • testcase的同级目录

  • 【强烈推荐】项目根目录【在不同的目录,【区别】其作用域不同】

    3.testcase引用N个fixture函数

    testcase :可以引用N(N>=0)个fixture函数 ```python

    test_demo3.py

    def test_hello1(some_data): assert some_data == “hello wolrd”

testcase可以引用N(N>=0)个fixture函数

def test_hello2(some_data,some_data1): assert some_data == “hello wolrd” assert some_data1 == 2021

——————————————————————————-

conftest.py [此conftest.py可以会于testcase的同级目录或项目根目录]

def some_data(): return “hello wolrd”

@pytest.fixture() def some_data1(): return 2021

  1. ![conftest.png](https://cdn.nlark.com/yuque/0/2021/png/247346/1615803889580-b48f8044-c88b-4715-9d18-3e2c5f8fad13.png#align=left&display=inline&height=856&margin=%5Bobject%20Object%5D&name=conftest.png&originHeight=856&originWidth=1901&size=82503&status=done&style=none&width=1901)
  2. <a name="mbIcb"></a>
  3. ## 4.fixture_参数化
  4. 格式 @pytest.fixture(**params**=**[ xxx ]**)<br /> def init(**request**):<br />return** request.param**<br />**说明 paramas的值必须为list类型**
  5. ```python
  6. import pytest
  7. from testCase.fixtureDemo.readFileData import myJsonData
  8. def add(a,b):
  9. return a+b
  10. myList=[[1,1,2],[2,3,4]]
  11. @pytest.fixture(params=myList)
  12. def initData(request):
  13. return request.param
  14. def test_hello1(initData):
  15. assert add(initData[0],initData[1])==initData[2]
  16. JsonData=myJsonData(filePath="../fixtureDemo/resource/my.json") # 数据与脚本分离
  17. @pytest.fixture(params=JsonData,name="jsondata") # name为别名,testcase可以引用别名
  18. def getJsonData(request):
  19. return request.param
  20. def test_hello2(jsondata):
  21. assert add(jsondata["a"], jsondata["b"]) == jsondata["result"]

image.png