c++ case 里使用||_内置fixture——record_property,为你的case添加额外的信息

a879317d9d1c7dabe1591e64e4a54993.png

最近在写测试代码时,遇到了一个需求,就是在每一个case跑出failure时去抓取产品的现场log,其实这个实现并不难,在小蛛佩奇:Hook函数pytest_runtest_protocol——让你在执行代码时获取用例的结果 就讲过了,可是这里又遇到了另外一个问题,测试代码针对了多种平台,而且每个平台里又有很多不一样的参数,那在pytest_runtest_protocl中你如何感知case所运行环境的所有信息参数去做不同的log处理呢?

看了API文档,发现TestReport里的字段user_properties是可以让我们利用的,它存放了测试用例的信息,可是打印出来是个空列表。于是又扫了下API,发现内置fixture record_property可以为测试方法配置这个字段。

conftest.py

import sys
from _pytest.runner import runtestprotocol
import pytest

def pytest_addoption(parser):
    parser.addoption("--ip_type", action="store", default="loopback",
                     help="ip type includes loopback, domain and local_network")

def pytest_runtest_protocol(item, nextitem):
    reports = runtestprotocol(item=item, nextitem=nextitem)
    for report in reports:
        if report.when == "call":
            if report.outcome == "failed":
                print("failure in call step...capture log")
                print(report.user_properties)
        elif report.when == "setup":
            if report.outcome == "failed":
                print("failure in setup step...capture log")
        elif report.when == "teardown":
            if report.outcome == "failed":
                print("failure in teardown step...capture log")
    return True

@pytest.fixture(autouse=True)
def add_info_for_case(record_property):
    record_property("platform", "pf_1")
    record_property("information", "this is a test")-

在原来基础上,我们又新加了一个fixture add_info_for_case,里面使用了record_property增加了两个信息项。当case在running时出现失败时,我们加了一条对report.user_properties的打印

test_ping.py

import os
import re
import pytest
import time

def get_ping_response(ip_addr):
    pid = os.popen("ping " + ip_addr)
    prompt = pid.read()
    m = re.search(r"Sent = (d+), Received = (d+), Lost = (d+) ((d+)% loss)", prompt)
    sent_num = int(m.group(1))
    recv_num = int(m.group(2))
    lost_num = int(m.group(3))
    lost_rate = int(m.group(4))
    return sent_num, recv_num, lost_num, lost_rate


def test_case(request):
    ip_type = request.config.getoption("ip_type")
    if ip_type == "loopback":
        assert get_ping_response("127.0.0.1")[3] == 0
    elif ip_type == "local":
        assert get_ping_response("192.168.1.1")[3] == 0
    else:
        raise Exception("failure")

运行结果:

C:UsersthinkPycharmProjectslearnpytestping_test>pytest -s --ip_type unknown test_ping.py::test_
case
Test session starts (platform: win32, Python 3.7.0, pytest 5.1.3, pytest-sugar 0.9.2)
rootdir: C:UsersthinkPycharmProjectslearnpytestping_test, inifile: pytest.ini
plugins: allure-pytest-2.8.11, assume-2.2.0, html-2.0.0, metadata-1.8.0, sugar-0.9.2
collecting ...

???????????????????????????????????????????? test_case ????????????????????????????????????????????

request = <FixtureRequest for <Function test_case>>

    def test_case(request):
        ip_type = request.config.getoption("ip_type")
        if ip_type == "loopback":
            assert get_ping_response("127.0.0.1")[3] == 0
        elif ip_type == "local":
            assert get_ping_response("192.168.1.1")[3] == 0
        else:
>           raise Exception("failure")
E           Exception: failure

test_ping.py:24: Exception

 test_ping.py ?                                                                      100% ██████████
failure in call step...capture log
[('platform', 'pf_1'), ('information', 'this is a test')]


Results (0.10s):
       1 failed
         - test_ping.py:17 test_case

我们看到了用record_property加入的两条信息,既然得到了case的额外环境信息,我们就可以为所欲为做特殊处理了。


版权声明:本文为weixin_33314040原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。