1.fixture_作用范围

@pytest.fixture(scoop=”session”/“module”/“class”/“function”)中的scoop参数可以控制fixture的作用范围。

  • 作用范围 **session > module > class > function**

    1.function级别

    每个testcase(N个fixture)都会调用执行对应的fixture函数【N个fixture则进行N次“setup…teardown过程”】
    简记 【function级别】每1个testcase,都会对引入的每个fixture函数执行一次【setup…teardown过程】
    1. # function级别:每个testcase执行的时候,fixture都会被调用
    2. import pytest
    3. @pytest.fixture()
    4. def getUsername():
    5. print("获取username")
    6. a = "admin"
    7. return a
    8. @pytest.fixture(scope="function")
    9. def getPassword():
    10. print("获取password")
    11. b = "123456"
    12. return b
    13. def test_username(getUsername):
    14. print("当前的username:{username}".format(username=getUsername))
    15. assert getUsername == "admin"
    16. def test_password(getUsername, getPassword):
    17. print("当前的password:{password}".format(password=getPassword))
    18. assert getPassword == "123456"
    fun1.png

    2.class级别

    1个“测试Class”(一个类可能有N个testcase)不管testcase使用(N个fixture),最后“每个fixture只会执行一次【“setup…teardown过程”】”
    简记 【Class级别】每1个类,只会对每个fixture函数执行一次【setup…teardown过程】
    1. #此脚本的主要功能 :fixture(scoop="class")
    2. import pytest
    3. @pytest.fixture(scope="class")
    4. def getUsername():
    5. print("获取username")
    6. a = "admin"
    7. return a
    8. @pytest.fixture(scope="class")
    9. def getPassword():
    10. print("获取password")
    11. b = "123456"
    12. return b
    13. class Test01():# 1个类,只会对每个fixture函数执行一次【setup...teardown过程】
    14. def test_username1(self,getUsername):
    15. print("当前的username:{username}".format(username=getUsername))
    16. assert getUsername == "admin"
    17. def test_username2(self,getUsername):
    18. print("当前的username:{username}".format(username=getUsername))
    19. assert getUsername == "admin"
    20. def test_password(self,getUsername, getPassword):
    21. print("当前的password:{password}".format(password=getPassword))
    22. assert getPassword == "123456"
    23. class Test02():每1个类,只会对每个fixture函数执行一次【setup...teardown过程】
    24. def test_username1(self,getUsername):
    25. print("当前的username:{username}".format(username=getUsername))
    26. assert getUsername == "admin"
    27. # def test_username2(self,getUsername):
    28. # print("当前的username:{username}".format(username=getUsername))
    29. # assert getUsername == "admin"
    30. def test_password(self,getUsername, getPassword):
    31. print("当前的password:{password}".format(password=getPassword))
    32. assert getPassword == "123456"
    class1.png

    3.module级别

    每1个测试文件(test_xx.py),只会对每个fixture函数执行一次【setup…teardown过程】
    简记 【Module级别】每1个测试文件(test_xx.py),只会对每个fixture函数执行一次【setup…teardown过程】
    1. #此脚本的主要功能 :fixture(scoop="module")
    2. import pytest
    3. @pytest.fixture(scope="module")
    4. def getUsername():
    5. print("获取username")
    6. a = "admin"
    7. return a
    8. @pytest.fixture(scope="module")
    9. def getPassword():
    10. print("获取password")
    11. b = "123456"
    12. return b
    13. class Test01():# 1个测试文件(test_xx.py),只会对每个fixture函数执行一次【setup...teardown过程】
    14. def test_username1(self,getUsername):
    15. print("当前的username:{username}".format(username=getUsername))
    16. assert getUsername == "admin"
    17. # def test_username2(self,getUsername):
    18. # print("当前的username:{username}".format(username=getUsername))
    19. # assert getUsername == "admin"
    20. def test_password(self,getUsername, getPassword):
    21. print("当前的password:{password}".format(password=getPassword))
    22. assert getPassword == "123456"
    23. class Test02(): # 每1个测试文件(test_xx.py),只会对每个fixture函数执行一次【setup...teardown过程】
    24. def test_username1(self,getUsername):
    25. print("当前的username:{username}".format(username=getUsername))
    26. assert getUsername == "admin"
    27. # def test_username2(self,getUsername):
    28. # print("当前的username:{username}".format(username=getUsername))
    29. # assert getUsername == "admin"
    30. def test_password(self,getUsername, getPassword):
    31. print("当前的password:{password}".format(password=getPassword))
    32. assert getPassword == "123456"
    mod1.png

    4.session级别

    session是运行多个测试文件(test_xx.py),其中的fixture函数只会执行一次“setup…teardwon过程”
    1. # conftest.py
    2. @pytest.fixture(scope="session")
    3. def getUsername():
    4. print("获取username")
    5. a = "admin"
    6. return a
    7. #----------------
    8. # test_session1.py
    9. def test_s1(getUsername):
    10. assert getUsername=="admin"
    11. # test_session2.py
    12. def test_s1(getUsername):
    13. assert getUsername=="admin"
    14. # 执行命令:pytest -sv --setup-show test_session1.py test_session2.py
    sessi.png