pytest安装并且生成好看的HTML报告

第一步:
(1)单独建立一个python环境:
mkvirtualenv pytest --python=python3
(2)执行命令,切换python工作环境为pytest
第二步:直接安装pythest模块到新环境中:
pip3 install pytest
第三步:安装allure:
brew install allure
第四步:安装allure-pytest
pip install allure-pytest
第五步:写测试用例:
(1)创建一个test目录
(2)在test目录下新建conftest.py和test_shopping_trolley.py
其中 conftest.py内容为:

#!/usr/bin/env python
# coding=utf-8

import pytest
import allure
import yaml

@pytest.fixture(scope="session", autouse=True)
def env(request):
    """
    Parse env config info
    """
    root_dir = request.config.rootdir
    config_path = '{0}/config/env_config.yml'.format(root_dir)
    with open(config_path) as f:
        env_config = yaml.load(f) # 读取配置文件

    allure.environment(host=env_config['host']['domain']) # 测试报告中展示host
    allure.environment(browser=env_config['host']['browser']) # 测试报告中展示browser

    return env_config

test_shopping_trolley.py

#!/usr/bin/env python
# coding=utf-8

import pytest
import allure


@allure.feature('购物车功能')  # feature定义功能
class TestShoppingTrolley(object):
    @allure.story('加入购物车')  # story定义用户场景
    def test_add_shopping_trolley(self):
        login('刘春明', '密码')  # 调用“步骤函数”
        with allure.step("浏览商品"):  # 将一个测试用例分成几个步骤,将步骤打印到测试报告中,步骤2
            allure.attach('商品1', '刘春明')  # attach可以打印一些附加信息
            allure.attach('商品2', 'liuchunming')
        with allure.step("点击商品"):  # 将一个测试用例分成几个步骤,将步骤打印到测试报告中,步骤3
            pass
        with allure.step("校验结果"):
            allure.attach('期望结果', '添加购物车成功')
            allure.attach('实际结果', '添加购物车失败')
            assert 'success' == 'failed'

    @allure.story('修

第六步:运行测试用例生成结果: 将结过保存到result目录下
py.test test/ --alluredir ./result/
第七步:将result结果生成HTML报表并且在web界面展示:
(1)生成HTML报告
allure generate ./result/ -o ./report/ --clean
(2)启动web服务展示报告
allure open -h 127.0.0.1 -p 8083 ./report/
在http://localhost:8083/index.html#页面可以查看到用例情况
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值