自动化测试框架系列-pytest

1.什么是pytest?

pytest是一个python的一个自动化测试框架,主要用于单元测试和功能测试。

java: junit 或者 testng

python: unittest 或者 pytest

2.pytest有哪些特点?

pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点:

1.免费

2.简单灵活,容易上手,文档丰富

3.插件丰富,目前已有600+

  • pytest-selenium(集成selenium)、
  • pytest-html(完美html测试报告生成)、
  • pytest-rerunfailures(失败case重复执行)、
  • pytest-xdist(测试用例分布式执行,多cpu分发)
  • allure-pytest(用于生成美观的测试报告)
  • pytest-ordering (改变测试用例的执行顺序)

4. 支持参数化,可以有效的控制测试用例。

5. 能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests。

6、可以很好的和CI工具结合,例如jenkins

7.Pytest可用于在API,数据库等上测试各种应用程序

8.Pytest能够在测试执行期间从所有测试方法中跳过一些测试方法

9.Pytest可以根据条件选择运行特定的测试方法或特定测试文件的所有测试方法

3. 如何安装pytest

首先用命令行查看是否已经安装:

pip list

如果没有安装用命令行进行安装:

pip install pytest

 检查是否安装了正确的版本:

pytest --version

 查看有哪些命令参数:

pytest --help

4. 创建测试用例

4.1 第一个测试用例-简单测试

创建一个.py 文件,写入如下代码:



def add(x,y):
    return x+y

def test1():
    assert 2==add(1,1)

def test2():
    assert 1!=add(1,1)

打开终端用pytest执行文件代码:

pytest -vv D:\pythonProject1\test_func.py 

4.2  第二个测试用例-错误测试



def add(x,y):
    return x+y

def test1():
    assert 3==add(1,1)

def test2():
    assert 1!=add(1,1)

 用pytest再次执行:

pytest -vv D:\pythonProject1\test_func.py

4.3  第三个测试用例-传入参数的异常测试

import pytest

def add(x,y):
    return x+y

def test1():
    assert 2==add(1,1)

def test2():
    assert 1!=add(1,1)

def func(x):
    if x==0:
      raise ValueError("value error")
    else:
        pass

def test_mytest1():
    with pytest.raises(ValueError):
        func(0)

def test_mytest2():
    assert func(1)==None

用pytest执行代码:

 4.4  第四个测试用例-不同参数传递测试

import pytest

def add(x,y):
    return x+y

def test1():
    assert 2==add(1,1)

def test2():
    assert 1!=add(1,1)

def func(x):
    if x==0:
      raise ValueError("value error")
    else:
        pass

def test_mytest1():
    with pytest.raises(ValueError):
        func(0)

def test_mytest2():
    assert func(1)==None

@pytest.mark.parametrize(
    "x,y,expected",
    [
        (1,1,2),
        (2,2,4),
        (10,10,20),
    ]
)

def test_add(x,y,expected):
    assert add(x,y) == expected

用pytest执行代码:

  4.5  第五个测试用例-分组测试

  如果测试文件中有100个函数,但是我仅仅想测试其中几个函数,如何去做?这就需要对这些函数进行分组,用到的命令如下:

pytest --markers

import pytest

def add(x,y):
    return x+y

@pytest.mark.a
def test1():
    assert 2==add(1,1)

@pytest.mark.a
def test2():
    assert 1!=add(1,1)

def func(x):
    if x==0:
      raise ValueError("value error")
    else:
        pass

def test_mytest1():
    with pytest.raises(ValueError):
        func(0)

def test_mytest2():
    assert func(1)==None

@pytest.mark.parametrize(
    "x,y,expected",
    [
        (1,1,2),
        (2,2,4),
        (10,10,20),
    ]
)

def test_add(x,y,expected):
    assert add(x,y) == expected

执行代码:

pytest -vv -m "a"

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

全栈开发与测试

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

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

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

打赏作者

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

抵扣说明:

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

余额充值