Pytest使用

安装

pip install pytest

用例设计原则

  • 文件名 为test_*.py或*_test.py
  • test_* 为测试函数名
  • Test* 为测试类名
  • test_* 为测试方法名

用例执行

  • <font color=red>执行目录下的所有文件</font>
pytest dirname/
  • 执行某个测试文件
pytest test_mod.py
  • 执行某个测试方法
pytest test_mod.py::TestClass::test_method
  • 按照关键字执行(匹配模块名、类名、函数名)
pytest -k "MyClass"
  • <font color=red>按照标记执行</font>
@pytest.mark.slow
def test_slow():
    pass

@pytest.mark.fast
def test_fast():
    pass

>>>pytest -m "slow and fast"
  • 跳过测试
@pytest.mark.skip(reason="mark as skip")
def test_skip():
    pass

@pytest.mark.skipif(skip_flag=False, reason="skip determined by the flag")
def test_skip_if():
    pass

  • 第一个(N个)失败后停止测试
pytest -x

pytest --maxfail=n
  • <font color=red>脚本调试使用</font>
pytest.main()

pytest.main(['test_mod.py', '-v'])

用例编写

  • 断言 assert
assert a == b
assert a != b
assert a >= b
assert b <= b
assert a in b
assert a not in b
assert a is b
assert a is not b
assert True
assert False

  • setup和teardown
import pytest

class TestClass:
    
    def setup_class(self):
        print("setup_class:类中所有用例执行之前")

    def teardown_class(self):
        print("teardown_class:类中所有用例执行之前")

    def setup_method(self):
        print("setup_method:  每个用例开始前执行")

    def teardown_method(self):
        print("teardown_method:  每个用例结束后执行")

    def setup(self):
        print("setup: 每个用例开始前执行")

    def teardown(self):
        print("teardown: 每个用例结束后执行")

    def test_one(self):
        print("执行第一个用例")

    def test_two(self):
        print("执行第二个用例")

def setup_module():
    print("setup_module:整个.py模块只执行一次")

def teardown_module():
    print("teardown_module:整个.py模块只执行一次")

def setup_function():
    print("setup_function:每个方法用例开始前都会执行")

def teardown_function():
    print("teardown_function:每个方法用例结束前都会执行")

  • <font color=red>参数化 pytest.mark.parametrize(argnames, args)</font>
@pytest.mark.parametrize('text', ['test1','test2','test3'])
def test_one(text):
    print(text)

@pytest.mark.parametrize(['username', 'pwd'], [('a', 1), ('b', 2), ('c', 3)])
def test_two(username, pwd):
    print(username, pwd)

Fixture

  • 基本使用
import pytest

@pytest.fixture()
def get_url():
    return "http://www.baidu.com"


def test_web(get_url):
    print(get_url)


if __name__ == '__main__':
    pytest.main()

  • 作用域
    • session: 会话级,一次测试只执行一次,所有被找到的函数和方法都可用
    • module: 模块级,每个模块执行一次,模块内函数和方法都可使用
    • function: 函数级,每个测试函数都会执行一次固件
    • class: 类级别,每个测试类执行一次,所有方法都可以使用
#./conftest.py

@pytest.fixture(scope="class")
def text():
    print("开始执行")
    yield 'test'
    print("执行完毕")

#./test_sample.py

@pytest.mark.usefixtures('text')
class TestClass:

    def test_one(self):
        print(text)

  • 参数化
#./conftest.py

@pytest.fixture(scope='module', params=['test1', 'test2'])
def text(request):
    print('开始执行')
    yield request.param
    print('执行完毕')

#./ test_sample.py

def test_one(text):
    print(text)

  • 配置文件
    • 所有的fixture放在conftest.py文件中,自动发现,统一管理

生成报告

  • <font color=red>html报告、顺序执行、失败重试</font>
pip install pytest-html           # 生成html报告
pip install pytest-ordering       # 按照顺序执行用例
pip install pytest-rerunfailures  # 失败重试

pytest test_mod.py --html=./report.html

# pytest.mark.run(order=1)
# pytest.mark.run(order=2)

pytest test_mod.py # 按照mark标记的order顺序执行

pytest test_mod.py --reruns 3 # 失败重试3次

  • xml报告
pytest --junitxml=report.xml
  • allure report
# install java
# install allure

pip install allure-pytest

pytest casedir/ --alluredir ./report

allure serve report
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • pytest是第三方开发的一个python测试模块,可以轻松地编写小型测试,而且可以扩展以支持应用程序和库的复杂功...
    何小有阅读 9,885评论 0赞 3
  • 1. 概述 pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点: 1、简单灵活,容易上手...
    红薯爱帅阅读 263,387评论 6赞 81
  • 前言 pytest 是 python 的第三方单元测试框架,比自带 unittest 更简洁和高效,支持315种以...
    梵音11阅读 366评论 0赞 0
  • pytest使用 1、pytest简介 官档是最好的教程 pytest是一个非常成熟的全功能的Python测试框架...
    道无虚阅读 4,490评论 1赞 5
  • 使用介绍2.1. 安装pip install pytest 2.2. 示例代码编写规则编写pytest测试样例非常...
    天才亮阅读 245评论 0赞 0

友情链接更多精彩内容