您当前的位置: 首页 >  Python

网易测试开发猿

暂无认证

  • 3浏览

    0关注

    221博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Pytest 自动化测试 / [码尚教育]Python全栈自动化VIP课程对标大厂标准(挑战年薪40万)

网易测试开发猿 发布时间:2022-04-19 18:58:07 ,浏览量:3

目录:导读
    • 一、Pytest和Unittest区别
    • 二、Pytest 安装
    • 三、Pytest 示例
    • 四、标记
    • 五、固件(Fixture)
      • 1、预处理和后处理
      • 2、作用域
      • 3、自动执行
      • 4、参数化
    • 六、总结

一、Pytest和Unittest区别

如何区分这两者,很简单unittest作为官方的测试框架,在测试方面更加基础,并且可以再次基础上进行二次开发,同时在用法上格式会更加复杂;而pytest框架作为第三方框架,方便的地方就在于使用更加灵活,并且能够对原有unittest风格的测试用例有很好的兼容性,同时在扩展上更加丰富,可通过扩展的插件增加使用的场景,比如一些并发测试等;

二、Pytest 安装

pip安装:

pip install pytest

测试安装成功:

pytest --help

py.test --help

复制代码 检查安装版本:

pytest --version
三、Pytest 示例

Pytest编写规则:

  • 测试文件以test_开头(以_test为结尾)
  • 测试的类以Test开头;
  • 测试的方法以test_开头
  • 断言使用基本的assert

test_example.py

def count_num(a: list) -> int:
    return len(a)


def test_count():
    assert count_num([1, 2, 3]) != 3

执行测试:

pytest test_example.py

执行结果:

C:\Users\libuliduobuqiuqiu\Desktop\GitProjects\PythonDemo\pytest>pytest test_example.py -v
================================================================= test session starts =================================================================
platform win32 -- Python 3.6.8, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- d:\coding\python3.6\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\libuliduobuqiuqiu\Desktop\GitProjects\PythonDemo\pytest
plugins: Faker-8.11.0
collected 1 item                                                                                                                                       

test_example.py::test_count FAILED                                                                                                               [100%]

====================================================================== FAILURES =======================================================================
_____________________________________________________________________ test_count ______________________________________________________________________

    def test_count():
>       assert count_num([1, 2, 3]) != 3
E       assert 3 != 3
E        +  where 3 = count_num([1, 2, 3])

test_example.py:11: AssertionError
=============================================================== short test summary info ===============================================================
FAILED test_example.py::test_count - assert 3 != 3
================================================================== 1 failed in 0.16s ==================================================================

备注:

  • .代表测试通过,F代表测试失败;
  • -v显示详细的测试信息, -h显示pytest命令详细的帮助信息;
四、标记

默认情况下,pytest会在当前目录下寻找以test_为开头(以_test结尾)的测试文件,并且执行文件内所有以test_为开头(以_test为结尾)的所有函数和方法;

1.指定运行测试用例,可以通过::显示标记(文件名::类名::方法名)(文件名::函数名)

pytest test_example3.py::test_odd

2.指定一些测试用例测试运行,可以使用-k模糊匹配

pytest -k example

3.通过pytest.mark.skip()或者pytest.makr.skipif()条件表达式,跳过指定的测试用例

import pytest

test_flag = False

@pytest.mark.skip()
def test_odd():
    num = random.randint(0, 100)
    assert num % 2 == 1


@pytest.mark.skipif(test_flag is False, reason="test_flag is False")
def test_even():
    num = random.randint(0, 1000)
    assert num % 2 == 0

4.通过pytest.raises()捕获测试用例可能抛出的异常

def test_zero():
    num = 0
    with pytest.raises(ZeroDivisionError) as e:
        num = 1/0
    exc_msg = e.value.args[0]
    print(exc_msg)
    assert num == 0

5.预先知道测试用例会失败,但是不想跳过,需要显示提示信息,使用pytest.mark.xfail()

@pytest.mark.xfail()
def test_sum():
    random_list = [random.randint(0, 100)  for x in range(10)]
    num = sum(random_list)
    assert num             
关注
打赏
1665054478
查看更多评论
0.0383s