一、fix的调用方式

@pytest.fixture(scope = “function”,params=None,autouse=False,ids=None,name=None)

参数详解:
1、scope

  • 用于控制Fixture的作用范围
  • 作用类似于Pytest的setup/teardown
  • 默认取值为function(函数级别),控制范围的的排列顺序为:session>module>class>function
取值 范围 说明
function 函数级 每一个函数方法都会调用
class 类级别 每个测试类只运行一次
module 模块级 每一个.py文件调用一次
session 会话级 每次会话只需要运行一次,会话内说有方法及类,模块都共享这个方法

二、function

作用范围举例:
scope = “function”
语法

  1. @pytest.fixture()
  2. # 或者
  3. @pytest.fixture(score='function')

场景一:作为参数传入

  1. import pytest
  2. # fixture函数(类中) 作为多个参数传入
  3. @pytest.fixture()
  4. def login():
  5. print("打开浏览器")
  6. a = "account"
  7. return a
  8. @pytest.fixture()
  9. def logout():
  10. print("关闭浏览器")
  11. class TestLogin:
  12. #传入lonin fixture
  13. def test_001(self, login):
  14. print("001传入了loging fixture")
  15. assert login == "account"
  16. #传入logout fixture
  17. def test_002(self, logout):
  18. print("002传入了logout fixture")
  19. def test_003(self, login, logout):
  20. print("003传入了两个fixture")
  21. def test_004(self):
  22. print("004未传入仍何fixture哦")
  23. if __name__ == '__main__':
  24. pytest.main()

image.png

场景二、Fixture的互相调用

  1. # -*- coding:utf-8 -*-
  2. '''
  3. @Author : 测试工程师Jane
  4. @FileName : e.py
  5. @Description:
  6. '''
  7. import pytest
  8. # fixtrue作为参数,互相调用传入
  9. @pytest.fixture()
  10. def account():
  11. a = "account"
  12. print("第一层fixture")
  13. return a
  14. #Fixture的相互调用一定是要在测试类里调用这层fixture才会生次,普通函数单独调用是不生效的
  15. @pytest.fixture()
  16. def login(account):
  17. print("第二层fixture")
  18. class TestLogin:
  19. def test_1(self, login):
  20. print("直接使用第二层fixture,返回值为{}".format(login))
  21. def test_2(self, account):
  22. print("只调用account fixture,返回值为{}".format(account))
  23. if __name__ == '__main__':
  24. pytest.main()

image.png

注:

  • 即使fixture之间支持相互调用,但普通函数直接使用fixture是不支持的,一定是在测试函数内调用才会逐级调用生效
  • 有多层fixture调用时,最先执行的是最后一层fixture,而不是先执行传入测试函数的fixture
  • 上层fixture的值不会自动return,这里就类似函数相互调用的一样的逻辑

二、class

  • 当测试类内的每一个测试方法都调用了fixture,fixture只在该class下所有测试用例执行前执行一次
  • 测试类下面只有一些测试方法使用了fixture函数名,这样的话,fixture只在该class下第一个使用fixture函数的测试用例位置开始算,后面所有的测试用例执行前只执行一次,而该位置之前的测试用例就不管

语法

@pytest.fixture(scope=’class’)

场景一:

  1. # -*- coding:utf-8 -*-
  2. '''
  3. @Time : 2020/12/28 19:56
  4. @Author : 测试工程师Jane
  5. @FileName : g.py
  6. @Description:
  7. '''
  8. import pytest
  9. # fixture作用域 scope = 'class'
  10. @pytest.fixture(scope='class')
  11. def login():
  12. print("scope为class")
  13. class TestLogin:
  14. def test_1(self, login):
  15. print("用例1")
  16. def test_2(self, login):
  17. print("用例2")
  18. def test_3(self, login):
  19. print("用例3")
  20. if __name__ == '__main__':
  21. pytest.main()

1.png

场景二:

  1. # -*- coding:utf-8 -*-
  2. '''
  3. @Author : 测试工程师Jane
  4. @FileName : 1.py
  5. @Description:
  6. '''
  7. import pytest
  8. @pytest.fixture(scope='class')
  9. def login():
  10. a = '123'
  11. print("输入账号密码登陆")
  12. class TestLogin:
  13. def test_1(self):
  14. print("用例1")
  15. def test_2(self, login):
  16. print("用例2")
  17. def test_3(self, login):
  18. print("用例3")
  19. def test_4(self):
  20. print("用例4")
  21. if __name__ == '__main__':
  22. pytest.main()

image.png

三、module

scope = “module”:与class相同,只从.py文件开始引用fixture的位置生效

  1. # -*- coding:utf-8 -*-
  2. '''
  3. @Author : 测试工程师Jane
  4. @FileName : 2.py
  5. @Description:
  6. '''
  7. import pytest
  8. # fixture scope = 'module'
  9. @pytest.fixture(scope='module')
  10. def login():
  11. print("fixture范围为module")
  12. def test_01():
  13. print("用例01")
  14. def test_02(login):
  15. print("用例02")
  16. class TestLogin():
  17. def test_1(self):
  18. print("用例1")
  19. def test_2(self):
  20. print("用例2")
  21. def test_3(self):
  22. print("用例3")
  23. if __name__ == '__main__':
  24. pytest.main()

image.png

四、session

  • session的作用范围是针对.py级别的,module是对当前.py生效,session是对多个.py文件生效
  • session只作用一个.py文件时,作用相当于module
  • 所以session多数与contest.py文件一起使用,作为全局Fixture