ReportPortal 在python下的应用介绍之----python各框架整合

接上一篇,装完之后,需要对接我们现有的自动化测试。就Python而言,有几种框架可以直接使用,这里介绍一下

首先,我们需要了解这是agent server模式,所以server上认证信息需要拿到。按这种方法拿:

登录进系统后,输入对应的地址:http://xxx/ui/#user-profile,有对应的token信息:

 

 

 

一、Nose

步骤:

先安装插件

pip install nose-reportportal

我们拿下面这段简单代码来尝试

# coding = utf-8

# author:huzq
import logging
log = logging.getLogger(__name__)
class Testclass:

    def __init__(self):

        pass

    def setup(self):

        print('start')

    def teardown(self):

        print('stop')

    def testfunc1(self):
        """dafdfdfd"""
        log.info("bbbbd")

        print('this is case1')

    def testfunc2(self):
        log.info("bbbbd")
        print('this is case2')

    def testfunc3(self):
        log.info("bbbbd")

        print('this is case3')
View Code

怎么运行呢?

两种方法:

1.文件形式

新建文件rp.ini,如下内容:

[base]
rp_uuid = fb586627-32be-47dd-93c1-678873458a5f
rp_endpoint = http://192.168.1.10:8080
rp_project = user_personal
rp_launch = AnyLaunchName
rp_launch_tags = Nose
rp_launch_description = Smoke test

不过对应的信息需要修改

运行:

nosetests xxx.py --with-reportportal --rp-config-file rp.ini

2.命令行形式

直接下面的命令:

nosetests xxx.py --with-reportportal --rp_uuid=xxxx rp_endpoint=http://xxx --rp_project=xxxx --rp_launch=xxx
--rp-config-file rp.ini

但这个时候,你可能会遇到下面这个问题:

 

 怎么办呢?直接给出解决方案吧:

找到对应python的site-packages中的nose_reportportal文件夹,修改service.py文件

修改第116行为下面这个样子:

"parameters": None,

再次运行,问题可以解决。

 

二、pytest

安装:

pip install pytest-reportportal

添加配置 文件config.cfg

[tool:pytest]
rp_uuid = 55466cae-8588-452c-9400-9ce4c5960e98
rp_endpoint = https://xxxx
rp_project = user
rp_launch = default_TEST_EXAMPLE
rp_launch_attributes = 'PyTest' 'Smoke'
rp_launch_description = 'Smoke test'
rp_ignore_errors = True
rp_ignore_attributes = 'xfail' 'usefixture'

对应的内容要修改

运行:

pytest xxxx.py --reportportal config.cfg

三、其它

其它的就不详细介绍,大同小异

比如RF

pip install robotframework-reportportal

 ------update 2022.4.8-------------

很多朋友问到pytest怎么搞截图附到reportportal上。这里放两种方法:

一。用原生的unittest继承的框架

def teardown(self):
    from pytest_reportportal import RPLogger
    import logging
    logger = logging.getLogger(__name__)
    logging.setLoggerClass(RPLogger)
    with open('xxx.png', "rb") as fh:
        image = fh.read()
    logger.info('xxx', attachment={"data": image,"mime": "image/png"})

二、用pytest特有的fixture方法:

第一步,在conftest.py中加入下面这段代码

@pytest.fixture(scope="session")
def rp_logger(request):
    from pytest_reportportal import RPLogger, RPLogHandler
    import logging, sys
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)
    # Create handler for Report Portal if the service has been
    # configured and started.
    if hasattr(request.node.config, 'py_test_service'):
        # Import Report Portal logger and handler to the test module.
        logging.setLoggerClass(RPLogger)
        rp_handler = RPLogHandler(request.node.config.py_test_service)
        # Add additional handlers if it is necessary
        console_handler = logging.StreamHandler(sys.stdout)
        console_handler.setLevel(logging.INFO)
        logger.addHandler(console_handler)
    else:
        rp_handler = logging.StreamHandler(sys.stdout)
    # Set INFO level for Report Portal handler.
    rp_handler.setLevel(logging.INFO)
    return logger

第二步, 在实际用例中使用
def test_1(self, rp_logger):
    rp_logger.info("save png")
    with open('xxx.png', "rb") as fh:
        image = fh.read()
    rp_logger.info(image_name,attachment={"data": image,"mime": "application/octet-stream"})

 

posted @ 2022-03-04 12:18  Believer007  阅读(315)  评论(0编辑  收藏  举报