Python测试框架pytest(23)插件 - pytest-picked、pytest-lazy-fixture

29 篇文章 8 订阅
28 篇文章 13 订阅

目录

1、pytest-picked(运行未提交的git用例)

1.1、安装

1.2、参数

1.3、用法

1.4、示例

2、pytest-lazy-fixture(在pytest.mark.parametrize中使用fixture) 

2.1、安装 

2.2、示例


1、pytest-picked(运行未提交的git用例)

自动化测试用例一般编写完后且又执行通过,都会提交到 git 仓库里。但是每次新增用例后,希望只执行未提交到 git 仓库里的用例。

pytest-picked 插件可以实现只执行未提交到 git 仓库里的测试用例。

1.1、安装

在命令行中运行以下命令进行安装:

pip install pytest-picked

或者(使用国内的豆瓣源,数据会定期同步国外官网,速度快。)

pip install pytest-picked -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

1.2、参数

  • --picked=[{only,first}] 单独或首先运行与更改的文件相关的测试

  • --mode=PICKED_MODE    Options: unstaged, branch

  • --parent-branch=PARENT_BRANCH 回购的主要分支(master、main、trunk等)

1.3、用法

pytest --picked
 
pytest --picked=first
 
pytest --picked --mode=branch
 
pytest --picked --mode=unstaged # 默认
 
pytest --picked --mode=branch --parent-branch=main # 如果你的父分支与主分支"master"不同

1.4、示例

以gitlab为例,首先要创建gitlab账号

访问官网并注册账号

https://gitlab.com/

使用git前,要先安装git

访问官网下载并安装即可

https://git-scm.com/

创建项目,项目目录结构:

创建test_case1.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

class TestDemo():
    def test_case1(self):
        print("执行用例1")

    def test_case2(self):
        print("执行用例2")

    def test_case3(self):
        print("执行用例3")

创建test_case2.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

class TestClass():
    def test_case4(self):
        print("执行用例4")

    def test_case5(self):
        print("执行用例5")

    def test_case6(self):
        print("执行用例6")

在gitlab上创建相关项目,例如My_Demo

PyCharm上创建本地git仓库

配置gitlab上所创建的项目(My_Demo)地址

提交

push到远程仓库里

已将项目同步到gitlab上

之后再新增2个文件,添加用例

创建test_case3.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

class TestAdd1():
    def test_case7(self):
        print("执行用例7")

    def test_case8(self):
        print("执行用例8")

    def test_case9(self):
        print("执行用例9")

创建test_case4.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

class TestAdd2():
    def test_case10(self):
        print("执行用例10")

    def test_case11(self):
        print("执行用例11")

    def test_case12(self):
        print("执行用例12")

创建完成后的目录结构

命令行跳转到项目的根目录,输入命令查看当前分支状态

git status

新增的2个文件没有提交到git仓库里

1、使用参数(--picked)

命令行输入执行命令

pytest --picked

运行结果:只运行新增的2个文件用例

2、使用参数(--picked=first)

命令行输入执行命令

pytest --picked=first

运行结果:首先运行修改后的测试文件,之后运行所有未修改的测试文件。

2、pytest-lazy-fixture(在pytest.mark.parametrize中使用fixture) 

pytest-lazy-fixture 插件,解决在测试用例中使用 @pytest.mark.parametrize 参数化时调用 fixture。

2.1、安装 

在命令行中运行以下命令进行安装: 

pip install pytest-lazy-fixture

或者(使用国内的豆瓣源,数据会定期同步国外官网,速度快。)

pip install pytest-lazy-fixture -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

2.2、示例

示例一:@pytest.mark.parametrize 参数化

创建test_lazy_fixture.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import pytest

@pytest.fixture(params=["admin", "123456"])
def my_fixture(request):
    return request.param

@pytest.mark.parametrize("param1, param2", [("login", pytest.lazy_fixture("my_fixture"))])
def test_case(param1, param2):
    print("\n参数param1:" + param1)
    print("\n参数param2:" + param2)
    assert param2 in ["admin", "123456"]

打开命令行,输入执行命令:

pytest -s -v test_lazy_fixture.py

运行结果:

示例二:@pytest.fixture 参数化

创建test_lazy_fixture2.py文件

脚本代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import pytest

@pytest.fixture
def my_fixture_1():
    one = "admin"
    return one

@pytest.fixture
def my_fixture_2():
    two = "123456"
    return two

@pytest.fixture(params=[pytest.lazy_fixture("my_fixture_1"), pytest.lazy_fixture("my_fixture_2")])
def my_fixture_all(request):
    return request.param

def test_case(my_fixture_all):
    print("\n参数:" + my_fixture_all)
    assert my_fixture_all in ["admin", "123456"]

打开命令行,输入执行命令:

pytest -s -v test_lazy_fixture2.py

运行结果:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangmcn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值