【Pytest框架】marker标记

测试函数标记

pytest提供了标记机制,允许你使用marker对测试函数(测试用例)做标记,一个测试函数(测试用例)可以有多个marker,一个marker也可以用例标记多个测试函数(测试用例)。针对冒烟测试,我们可以使用marker标记,检查系统有没有重大缺陷,因为冒烟测试通常是不会包含全套测试的。

选定了两个测试函数(测试用例)来作为冒烟测试,首先我们在 test_run_pass() 、test_run_fail() 两个测试函数(测试用例)的头部添加装饰器 @pytest.mark.smoking; @pytest.mark.get_testing(注意:smoking;get_testing 标记并非pytest 内置的,是我自己在配置文件定义的)。

import pytest


@pytest.mark.smoking
def test_run_pass():
    expected = (1, 2, 3)
    assert expected == (1, 2, 3)

@pytest.mark.smoking
def test_run_fail():
    expected = (1, 2, 3)
    assert expected == (1, 2, 4)

运行展示结果如下:

osts\ReadPytest>pytest -v -m 'smoking' test_two.py
================================================================================== test session starts ===================================================================================
collected 2 items                                                                                                                                                                         

test_two.py::test_run_pass PASSED                                                                                                                                                   [ 50%]
test_two.py::test_run_fail FAILED                                                                                                                                                   [100%]

======================================================================================== FAILURES ========================================================================================
_____________________________________________________________________________________ test_run_fail ______________________________________________________________________________________

    @pytest.mark.smoking
    def test_run_fail():
        expected = (1, 2, 3)
>       assert expected == (1, 2, 4)
E       assert (1, 2, 3) == (1, 2, 4)
E         At index 2 diff: 3 != 4
E         Full diff:
E         - (1, 2, 4)
E         ?        ^
E         + (1, 2, 3)
E         ?        ^

test_two.py:20: AssertionError
================================================================================ short test summary info =================================================================================
FAILED test_two.py::test_run_fail - assert (1, 2, 3) == (1, 2, 4)
============================================================================== 1 failed, 1 passed in 0.09s ===============================================================================

上面展示的是每个测试函数上标记了一个装饰器,现在我们开始添加多个装饰器

import pytest


@pytest.mark.smoking
def test_run_pass():
    expected = (1, 2, 3)
    assert expected == (1, 2, 3)

@pytest.mark.get_tests
@pytest.mark.get_testing
@pytest.mark.smoking
def test_run_fail():
    expected = (1, 2, 3)
    assert expected == (1, 2, 3)

运行后展示的效果:

注意:在这需要注意一个细节,在输入命令时,使用单引号时运行失败,使用双引号时运行成功,至少目前我本地运行是要必须这样

F:\TESTING\BlogPosts\ReadPytest>pytest -v -m "get_testing"
================================================================================== test session starts ===================================================================================
collected 8 items / 7 deselected / 1 selected                                                                                                                                             

test_two.py::test_run_fail PASSED                                                                                                                                                   [100%]

============================================================================ 1 passed, 7 deselected in 0.06s =============================================================================

标记还可以结合逻辑表达式 :| and | or | not |

运行后展示的效果:

\TESTING\BlogPosts\ReadPytest>pytest -v -m "get_testing and smoking"
================================================================================== test session starts ===================================================================================
collected 8 items / 7 deselected / 1 selected                                                                                                                                             

test_two.py::test_run_fail PASSED                                                                                                                                                   [100%]

============================================================================ 1 passed, 7 deselected in 0.09s =============================================================================

F:\TESTING\BlogPosts\ReadPytest>pytest -v -m "get_testing or smoking"
================================================================================== test session starts ===================================================================================
collected 8 items / 6 deselected / 2 selected                                                                                                                                             

test_two.py::test_run_pass PASSED                                                                                                                                                   [ 50%]
test_two.py::test_run_fail PASSED                                                                                                                                                   [100%]

============================================================================ 2 passed, 6 deselected in 0.06s =============================================================================

" smoking and not get_testing " 的作用是筛选出有smoking标记,但没有get_testing的标记的测试

:\TESTING\BlogPosts\ReadPytest>pytest -v -m "smoking and not get_testing"
================================================================================== test session starts ===================================================================================
collected 8 items / 7 deselected / 1 selected                                                                                                                                             

test_two.py::test_run_pass PASSED                                                                                                                                                   [100%]

============================================================================ 1 passed, 7 deselected in 0.05s =============================================================================

带有相同标记的测试即使存放在不同的 . py文件下,也会一起被执行,运行后展示效果如下:

ESTING\BlogPosts\ReadPytest>pytest -v -m "get_testing"
================================================================================== test session starts ===================================================================================
collected 8 items / 5 deselected / 3 selected                                                                                                                                             

test_one.py::test_recursion_depth PASSED                                                                                                                                            [ 33%]
test_three.py::TestThree::test_01_three_pass PASSED                                                                                                                                 [ 66%]
test_two.py::test_run_fail PASSED                                                                                                                                                   [100%]

============================================================================ 3 passed, 5 deselected in 0.12s =============================================================================

给测试添加标记的场景大部分用于我们冒烟测试,当然我们上面所举的都是简单例子,还不能算是合理且完整的冒烟测试,合理完成的冒烟测试肯定会涉及业务逻辑和数据库的改动,但是标记的应用基本就是这样的,后期在编写好的测试用例上添加即可。


以上就是pytest中marker标记,如总结有不当之处,还请多多赐教,始终相信你的努力,终会在某一天得到回报!!!

【Pytest框架】marker标记_第1张图片

你可能感兴趣的:(pytest,python)