Allure+pytest生成测试报告

Allure+pytest生成测试报告

pip install allure-pytest 
  • 使用时 import allure 运行时在pytest命令后面加上 --alluredir ./report/allure_raw 会自动在当前文件夹根目录生成report/allure_raw文件,里面是测试报告的JSON格式文件
import pytest
import allure

@pytest.mark.unit
@allure.feature("测试控制类")
class TestControl:
	def test_Equal(self):
        assert 2 == 2
    
    def test_Equal(self):
        assert True
        
    @allure.story("错误测试")
    def test_Equal(self):
        print("测试用例3")
        allure.attach("错误测试项")
        assert 1 == 2

    @allure.story("测试跳过项")
    def test_skip(self):
        """this test is skipped"""
        pytest.skip('for a reason!')

    @allure.story("测试故障项")
    def test_broken(self):
        raise Exception('oops')

  • 执行测试用例时使用方法如下,执行之后得到的是测试报告的JSON格式文件
pytest --alluredir ./report/allure_raw
  • 得到测试报告的JSON格式文件之后有两种处理方式
#在当前文件夹根目录启动allure服务
#不生成本地html文件,生成成功会自动打开浏览器
allure serve report/allure_raw
#生成本地html报告,需手动打开index.html文件,如下图
allure generate ./report/allure_raw  -o ./allure_reports/html/ --clean

成功生成结果如下
在这里插入图片描述
在这里插入图片描述生成html报告的目录格式

  • 报告结果,一些具体内容这里不给出详细介绍,如有需要可自行百度搜索。

在这里插入图片描述

生成的html页面浏览器打开时会提示出现跨域无法访问的问题,火狐浏览器设置如下(谷歌不支持)

1.进入火狐配置页进行设置 ,地址栏输入"about:config"
2.点击”我了解此风险”后进入页面
3.搜索”security.fileuri.strict_origin_policy”,并设置该项为false
4.重启浏览器,可以跨域访问
  • 到此使用allure生成一个美观的测试报告就介绍完毕了。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用 Appium、PytestAllure生成测试淘宝 App 的完整框架时,可以按照以下步骤进行: 1. 安装必要的软件和库: - 安装 Python:访问 Python 官方网站(https://www.python.org/),下载并安装最新版本的 Python。 - 安装 Pytest:在命令行中运行 `pip install pytest`。 - 安装 Appium-Python-Client:在命令行中运行 `pip install Appium-Python-Client`。 - 安装 Allure-Pytest:在命令行中运行 `pip install allure-pytest`。 2. 配置 Appium 环境: - 下载并安装 Appium Desktop(https://github.com/appium/appium-desktop)。 - 启动 Appium Desktop,并设置 Appium 服务器的相关配置,如设备连接信息、应用程序路径等。 3. 创建测试文件和目录结构: - 创建一个新的目录来保存你的测试代码和相关文件。 - 在该目录下创建一个名为 `conftest.py` 的文件,用于配置测试环境和共享的方法。 - 创建一个名为 `test_taobao.py` 的文件,用于编写测试用例和测试步骤。 4. 编写测试用例: - 在 `test_taobao.py` 文件中导入所需的库和模块,如 `pytest`、`Appium-Python-Client`、`allure` 等。 - 编写测试用例,可以使用 `pytest` 提供的装饰器来标记测试用例,如 `@pytest.mark.parametrize`、`@pytest.fixture` 等。 - 在测试用例中,使用 `Appium-Python-Client` 提供的方法来控制 Appium 服务器和执行 App 操作,如启动 App、查找元素、点击按钮等。 - 可以使用 `allure` 提供的装饰器和方法来添加测试步骤、生成测试报告、添加截图等。 5. 运行测试用例: - 在命令行中进入到测试代码所在的目录。 - 运行命令 `pytest --alluredir=./allure-results` 来执行测试用例,并生成 Allure 报告所需的数据。 6. 生成测试报告: - 在命令行中运行 `allure serve ./allure-results` 来生成并打开 Allure 报告。 以下是一个简单的示例代码,用于演示如何使用 Appium、PytestAllure 进行淘宝 App 的自动化测试: ```python import allure import pytest from appium import webdriver @pytest.fixture(scope='session') def driver(): desired_caps = { 'platformName': 'Android', 'deviceName': 'YourDeviceName', 'appPackage': 'com.taobao.taobao', 'appActivity': 'com.taobao.tao.homepage.MainActivity3', 'noReset': True } driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) yield driver driver.quit() @allure.feature('淘宝 App 测试') class TestTaobaoApp: @allure.story('搜索商品') def test_search_product(self, driver): with allure.step('启动淘宝 App'): # 启动淘宝 App with allure.step('搜索商品'): # 在搜索框中输入关键词 with allure.step('点击搜索按钮'): # 点击搜索按钮 with allure.step('验证搜索结果'): # 验证搜索结果是否符合预期 with allure.step('添加截图'): # 添加当前页面的截图到报告中 allure.attach(driver.get_screenshot_as_png(), name='搜索结果截图', attachment_type=allure.attachment_type.PNG) ``` 请根据你的具体测试需求和环境配置,修改和扩展上述示例代码。这只是一个简单的框架示例,具体的实现方式可能因项目要求和测试需求而有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值