pytest中所谓的夹具:
fixture scope参数详解:
scope: 表示fixture共享夹具的范围,有以下五个范围
function 功能: 在函数或方法运行时执行fixture函数的功能 默认作用域
class 功能:在类运行时调用一次。
module 功能:在一个模块(一个py文件为一个模块)被运行时,执行一次fixture
package 功能:当前被使用fixture的文件所在的包,执行一次fixture
session 功能:当前被使用fixture的文件所在的整个项目目录,执行一次fixture
函数作为参数或使用@pytest.mark.usefixtures("login") 引用函数来操作:
- import pytest
-
- @pytest.fixture(scope="class")
- def login():
- print("登录成功")
- return "token"
-
-
- # 第一种引用方式:直接将函数名当成参数传递
- def test_a(login):
-
- print("test_a execute! ", login)
-
- # 第二种引用方式:使用pytest.mark.usefixtures
- @pytest.mark.usefixtures("login")
- def test_b():
- print(login)
conftest.py实现数据共享
场景: 你与其他测试⼯程师合作⼀起开发时,公共的模块要在不同⽂件中,要在⼤家都访问到的地⽅。
解决: 使⽤ conftest.py 这个⽂件进⾏数据共享,并且他可以放在不同位置起着不同的范围共享作⽤。
前提:
conftest ⽂件名是不能换的
放在项⽬下是全局的数据共享的地⽅
执⾏:
系统执⾏到参数 login 时先从本模块中查找是否有这个名字的变量什么的,
之后在 conftest.py 中找是否有。
步骤: 将登陆模块带@pytest.fixture 写在 conftest.py
conftest.py 文件
- import pytest
-
- '''
- scope : function /class / module /package /session
- '''
-
- @pytest.fixture(scope="class")
- def login():
- print("登录")
- yield "token : kjl;kj;jk;1232131"
- print("登出")
-
-
- @pytest.fixture(scope="class")
- def db_connect():
- print("连接数据库")
- yield
- print("断开数据库")
使用夹具,作为参数传入函数:
- class TestClass():
-
- def test_01(self,login,db_connect):
- print("test -001")
-
- def test_02(self,login,db_connect):
- print("test -002")

跳过的测试方法
gogoabc.py
- class gloa(object):
- def max(values):
-
- _max = values[0]
-
- for val in values:
- if val > _max:
- _max = val
-
- return _max
-
- def min(values):
-
- _min = values[0]
-
- for val in values:
- if val < _min:
- _min = val
-
- return _min
跳过使用 @pytest.mark.skip 使用测试方法跳过
- import pytest
- from two.gogoabc import gloa
-
- #跳过这个方法不执行,装饰器,在不改变原有方法的情况下,给予方法额外功能
- @pytest.mark.skip
- def test_min():
- values = (2, 3, 1, 4, 6)
-
- val = gloa.min(values)
- assert val == 1
-
- def test_max():
- values = (2, 3, 1, 4, 6)
-
- val = gloa.max(values)
- assert val == 6
-
-
- if __name__ == '__main__':
- pytest.main()
分模块进行执行可以使用 @pytest.mark.a进行标记
marking.py
- import pytest
-
- @pytest.mark.a
- def test_a1():
-
- assert (1) == (1)
-
- @pytest.mark.a
- def test_a2():
-
- assert (1, 2) == (1, 2)
-
- @pytest.mark.a
- def test_a3():
-
- assert (1, 2, 3) == (1, 2, 3)
-
- @pytest.mark.b
- def test_b1():
-
- assert "falcon" == "fal" + "con"
-
- @pytest.mark.b
- def test_b2():
-
- assert "falcon" == f"fal{'con'}"
执行方式如下 pytest -m a marking.py 指定执行 marking中 a 的模块。
参数化的方法使用 @pytest.mark.parametrize()进行操作
parametrized.py
- import pytest
- from two.gogoabc import gloa
-
-
- @pytest.mark.parametrize("data, expected", [((2, 3, 1, 4, 6), 1),
- ((5, -2, 0, 9, 12), -2), ((200, 100, 0, 300, 400), 0)])
- def test_min(data, expected):
-
- val = gloa.min(data)
- assert val == expected
-
- @pytest.mark.parametrize("data, expected", [((2, 3, 1, 4, 6), 6),
- ((5, -2, 0, 9, 12), 12), ((200, 100, 0, 300, 400), 400),((200, 100, 0, 300, 400,650), 650)])
- def test_max(data, expected):
-
- val = gloa.max(data)
- assert val == expected
命令行执行命令: pytest -v -s .\test_params.py
- rootdir: C:\pyworks\interfaceTest\one
- plugins: html-3.2.0, metadata-1.11.0
- collected 3 items
-
- test_params.py::test_demo[name1] 登录
- 用户名 tom
- PASSED退出
-
- test_params.py::test_demo[name2] 登录
- 用户名 jack
- PASSED退出
-
- test_params.py::test_demo[name3] 登录
- 用户名 tony
- PASSED退出