微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python测试框架pytest23插件 - pytest-picked、pytest-lazy-fixture

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

运行结果:

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐