pytest-第一次学习梳理

内容全部参考:https://cloud.tencent.com/developer/search/article-pytest文档50

1-环境准备与入门

2-用例运行规则

文件名以test_.py文件和test.py
以test_开头的函数
以Test开头的类
以test_开头的方法
所有的包pakege必须要有__init
_.py文件

以下是测试功能的可能结果:
PASSED (.):测试成功。
FAILED (F):测试失败(或XPASS + strict)。
SKIPPED (s): 测试被跳过。 你可以使用@pytest.mark.skip()或 pytest.mark.skipif()修饰器告诉pytest跳过测试
xfail (x):预期测试失败。@pytest.mark.xfail()
XPASS (X):测试不应该通过。
ERROR (E):错误

3-pycharm运行pytest

pycharm设置pytest
新建一个工程后,左上角file->Setting->Tools->Python Integrated Tools->项目名称->Default test runner->选择py.test

4-测试用例setup和teardown

用例运行级别
模块级(setup_module/teardown_module)开始于模块始末,全局的
函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
方法级(setup_method/teardown_method)开始于方法始末(在类中)
类里面的(setup/teardown)运行在调用方法的前后

5-fixture之conftest.py

firture相对于setup和teardown来说应该有以下几点优势
命名方式灵活,不局限于setup和teardown这几个命名
conftest.py 配置里可以实现数据共享,不需要import就能自动找到一些配置
scope=”module” 可以实现多个.py跨文件共享前置
scope=”session” 以实现多个.py跨文件使用一个session来完成多个用例

6-fixture之yield实现teardown

7-生成html报告

安装:pip install pytest-html
执行:打开cmd,cd到需要执行pytest用例的目录,执行指令:pytest —html=report.html
执行完之后,在当前目录会生成一个report.html的报告文件,显示效果如下

8-html报告报错截图+失败重跑

9-参数化parametrize

  1. pytest.mark.parametrize装饰器可以实现测试用例参数化。
# content of test_expectation.py

# coding:utf-8
# ** 作者:上海-悠悠 QQ交流群:588402570**

import pytest
@pytest.mark.parametrize("test_input,expected",
                         [ ("3+5", 8),
                           ("2+4", 6),
                           ("6 * 9", 42),
                         ])
def test_eval(test_input, expected):
    assert eval(test_input) == expected

if __name__ == "__main__":
    pytest.main(["-s", "test_canshu1.py"])

运行结果

================================== FAILURES ===================================
_____________________________ test_eval[6 * 9-42] _____________________________

test_input = '6 * 9', expected = 42

    @pytest.mark.parametrize("test_input,expected",
                             [ ("3+5", 8),
                               ("2+4", 6),
                               ("6 * 9", 42),
                             ])
    def test_eval(test_input, expected):
>       assert eval(test_input) == expected
E       AssertionError: assert 54 == 42
E        +  where 54 = eval('6 * 9')

test_canshu1.py:11: AssertionError
===================== 1 failed, 2 passed in 1.98 seconds ======================
  1. 也可以标记单个测试实例在参数化,例如使用内置的mark.xfail
  2. 获得多个参数化参数的所有组合,可以堆叠参数化装饰器

10-命令行传参addoption

命令行参数是根据命令行选项将不同的值传递给测试函数,比如平常在cmd执行”pytest —html=report.html”,这里面的”—html=report.html“就是从命令行传入的参数
对应的参数名称是html,参数值是report.html

contetest配置参数

11-assert断言

pytest里面断言实际上就是python里面的assert断言方法,常用的有以下几种

assert xx 判断xx为真
assert not xx 判断xx不为真
assert a in b 判断b包含a
assert a == b 判断a等于b
assert a != b 判断a不等于b

12-skip跳过用例

pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能

@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown():
    ...
def test_function():
    if not valid_config():
        pytest.skip("unsupported configuration")

概要
1.无条件地跳过模块中的所有测试:
pytestmark = pytest.mark.skip(“all tests still WIP”)

2.根据某些条件跳过模块中的所有测试
pytestmark = pytest.mark.skipif(sys.platform == “win32”, “tests for linux˓→ only”

3.如果缺少某些导入,则跳过模块中的所有测试
pexpect = pytest.importorskip(“pexpect”)

16-标记失败xfail

pytest里面用xfail标记用例为失败的用例,可以直接跳过。实现基本思路
把登录写为前置操作
对登录的账户和密码参数化,参数用canshu = [{“user”:”amdin”, “psw”:”111”}]表示
多个用例放到一个Test_xx的class里
test_01,test_02, test_03全部调用fixture里面的login功能
test_01测试登录用例
test_02和test_03执行前用if判断登录的结果,登录失败就执行,pytest.xfail(“登录不成功, 标记为xfail”)

18-配置文件pytest.ini

pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行。
pytest里面有些文件是非test文件

pytest.ini pytest的主配置文件,可以改变pytest的默认行为
conftest.py 测试用例的一些fixture配置
init.py 识别该文件夹为python的package包
tox.ini 与pytest.ini类似,用tox工具时候才有用
setup.cfg 也是ini格式文件,影响setup.py的行为

19-doctest测试框架

前言

doctest从字面意思上看,那就是文档测试。doctest是python里面自带的一个模块,它实际上是单元测试的一种。
官方解释:doctest 模块会搜索那些看起来像交互式会话的 Python 代码片段,然后尝试执行并验证结果doctest测试用例可以放在两个地方
函数或者方法下的注释里面
模块的开头

>>> multiply(4, 3)
    12
>>> multiply('a', 3)
    'aaa'
def multiply(a, b):
    """
    fuction: 两个数相乘

# 22-pytest分布式执行(pytest-xdist)
    >>> multiply(4, 3)
    12
    >>> multiply('a', 3)
    'aaa'
    """
    return a * b
if __name__ == '__main__':
    import doctest
    doctest.testmod(verbose=True)

26-conftest.py作用范围

27-运行上次失败用例(--lf 和 --ff)

28-重复执行用例(pytest-repeat)

29-allure-pytest(报告形式)

30-功能用例与自动化用例完美对接(allure)

31-allure标记用例级别severity

32-allure描述用例详细讲解

33-Hooks函数获取用例执行结果(pytest_runtest_makereport)

34-Hooks函数改变用例执行顺序(pytest_collection_modifyitems)

35-Hooks函数之统计测试结果(pytest_terminal_summary)

36-断言失败后还能继续执行pytest-assume

37-自定义用例顺序(pytest-ordering)

38-allure.setp添加测试用例步骤

39-参数化(parametrize)结合allure.title()生成不同标题报告

40-pytest.ini配置用例查找规则(面试题)

41-参数化 ids 用例描述为中文时控制台输出unicode

42-fixture参数化params

43-元数据使用(pytest-metadata)

44-allure.dynamic动态生成用例标题

45-allure添加环境配置(environment)

46-关于https请求警告问题

47-allure报告添加用例失败截图

posted @ 2020-09-14 13:49  Alice1005  阅读(137)  评论(0编辑  收藏  举报