【PYTEST】第二章编写测试函数

知识点:

  • assert
  • 测试函数标记
  • 跳过测试
  • 标记预期失败的测试用例

1. asseet

返回的都是布尔值,等于False(F) 就是失败, assert 有很多

  • assert something
  • assert a == b
  • assert a <=b

2. 测试函数标记

  pytest中提供了标记机制,使用marker做标记,一个测试函数可以有多个标记,当然一个marker可以标记多个测试函数,实战中,例如我们想对某些测试模块中的测试函数需要进行冒烟测试,某些测试函数不需要进行冒烟测试,,我们可以对需要进行冒烟测试的测试用例加上@pytest.mark.smoke装饰器,注意这边的smoke是我自定义的,并非内置,你可以随便命令成任意你喜欢的

pytest/ch2/test_smoke.py

import pytest

@pytest.mark.smoke
def test_1():
    assert (1, 2, 3) == (1, 2, 3)

def test_2():
    assert 1 == 2


def test_3():
    assert 2 == 2

@pytest.mark.smoke
def test_4():
    assert 2 != 5

$ cd pytest/ch2
$ pytest -v -m smoke test_smoke.py


分析:

-v : 为了详细看清结果,所以加了一个-v

-m smoke: 命名中指定了 smoke这个标记

我们可以从这个结果中得出,这个测试模块中有4个测试用例,但是只运行了指定标记的测试函数

3. 跳过测试

  如果你不想运行这个测试用例,你可以给他添加内置的标记,skip. skipif

 pytest/ch2/test_skip.py

import pytest

@pytest.mark.skip(reason="这是错误的")
def test_1():
    assert (1, 2, 3) == (1, 2, 3)

def test_2():
    assert 1 == 2


def test_3():
    assert 2 == 2

@pytest.mark.smoke
def test_4():
    assert 2 != 5

$ cd pytest/ch2
$ pytest -v test_skip.py

  

4. 标记预期失败的测试用例

用xfail 替代skip即可,这边就不做多余的演示了,自己可以试试

原文地址:https://www.cnblogs.com/totoro-cat/p/13372483.html