栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Pytest学习day2

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Pytest学习day2

1、调用fixture的三种方式:

  1. test函数参数中写上fixture名称
  2. 使用装饰器标记test函数@pytest.mark.usefixtures()括号中填写要调用的fixture名称
  3. fixture设置成自动调用auto=True
    2、命令行运行参数中–tb=style style可以替换成喜欢的风格,short等
    2、fixture传入多个参数的话,可以执行多次
    例如下面的params传入了两个参数,该fixture函数会执行两次
@pytest.fixture(scope="module", params={10,20})
def params_to_test_runs(request):
    a = request.param
    return a

调用该fixture函数的方法如下:会输出两次值,10和20

def test_param_to_runs(params_to_test_runs):
    print(params_to_test_runs)
    print("================")

3、 --collect-only的用法

  • --collect-only的用法以及ids关键字的用法需要继续看
    4、pytest.param()用法
import pytest

@pytest.fixture(params=[0, 1, pytest.param(2, marks=pytest.mark.skip)])
def data_set(request):
    return request.param

def test_data(data_set):
    print(data_set)
    pass

上述代码只会运行data_set这个fixture两次,返回值分别是0和1,第三个值2会被忽略,因为加了marks=pytest.mark.skip
5、fixture调用另一个fixture

  • fixture调用另一个fixture还需要进一步看下
    fixture只能调用比它范围更大的fixture,不能调用比它的scope范围小的 例如:scope为module的fixture可以调用scope为session(或同级的module)的fixture,反过来就不行
    6、fixture的scope使用:对于fixture1的scope为module,fixture2的scope为function,则每重新执行一个test都要执行一轮fixture2,而对于fixture1,如果test1和test2都调用fixture1(假设有2个参数),则先执行第一个参数对应的fixture1的setup然后执行test1->test2->第一个参数fixture1的teardown->第二个参数对应的fixture1的setup->test1->test2->第二个参数fixture1的teardown
    7、pytest.mark.usefixtures(“fixture_name”),fixture不能直接用pytest.mark.usefixtures(“fixture_name”)来调用别的fixture,test可以,可以直接在一个class前使用pytest.mark.usefixtures(“fixture_name”),这样整个类里的test都可以调用这个fixture;
    如果所有test都要调用这个fixture,可以直接在pytest.ini文件中配置usefixtures=fixture_name
    8、fixture调用的覆盖问题
    1)有多个conftest.py时,先执行外层的conftest.py中fixture的setup,再执行里层(test文件所在层)conftest.py中fixture的setup,再执行test,然后再执行里层conftest.py中fixture的teardown,最后再执行外层conftest.py中fixture的teardown
    2)当前目录的conftest.py文件有fixture1,当前.py文件也有fixture1,会先执行conftest.py文件中的fixture1,再执行当前.py文件的fixture1
  1. 多层fixture时,外层conftest.py->里层conftest.py->当前.py文件中的fixture
  2. @pytest.mark.parametrize()参数值会覆盖同名的fixture,例如:@pytest.mark.parametrize(‘username’, [‘directly-overridden-username-other’]) 中username的值为’directly-overridden-username-other’,如果有fixture名称为username,里面返回aa,username的值不会是aa,而是directly-overridden-username-other
  3. .py文件中的fixture里包含params会替换掉conftet.py文件中不带params的fixture;同样地, .py文件中的fixture里不包含params也会替换掉conftet.py文件中带params的fixture;
    例如test_somthing.py文件代码如下:
import pytest

@pytest.fixture
def parametrized_username():
    return 'overridden-username'


@pytest.fixture(params=['one', 'two', 'three'])
def non_parametrized_username(request):
    return request.param

def test_username(parametrized_username):
    assert parametrized_username == 'overridden-username'

def test_parametrized_username(non_parametrized_username):
    assert non_parametrized_username in ['one', 'two', 'three']

conftest.py文件代码如下:

import pytest

@pytest.fixture(params=['one', 'two', 'three'])
def parametrized_username(request):
    return request.param

@pytest.fixture
def non_parametrized_username(request):
    return 'username'

运行结果都是test_somthing.py中的fixture返回的结果
6).py文件中既有带params的fixture,又有不带params的fixture,执行哪个与带不带params无关,谁在下面执行谁

如果上面1和2换顺序,则执行1
9、pytest_plugins用法还需要进一步看下

  • List item
    10、marks只能用到test上,不能用到fixture上
    usefixtures - use fixtures on a test function or class
    • filterwarnings - filter certain warnings of a test function
    • skip - always skip a test function
    • skipif - skip a test function if a certain condition is met
    • xfail - produce an “expected failure” outcome if a certain condition is met
    • parametrize - perform multiple calls to the same test function
    11、登记marks的3种方式
  1. pytest.ini
[pytest]
markers =
slow: marks tests as slow (deselect with '-m "not slow"')
serial
  1. pyproject.toml
[tool.pytest.ini_options]
markers = [
"slow: marks tests as slow (deselect with '-m "not slow"')",
"serial",
]
  1. pytest_configure hook 有待进一步研究下
  • List item
def pytest_configure(config):
config.addinivalue_line(
"markers", "env(name): mark test to run only on named environment"
)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/918307.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号