1.2 UiPath第一个案例Hello World!

这篇博客介绍了如何使用UiPath创建第一个Hello World案例。通过新建序列,添加Message Box活动并设置提示信息,最终成功运行程序显示弹出框。

1、弹出框Hello World

image

在弹出的窗口中创建序列

image

在新建的序列中,在搜索框中输入“Message Box”,Studio自动搜索出结果。

image

选中“Message Box”,然后拖拽到界面带+号区域。

image

在文本框输入要弹出的提示信息,记得带引号。

image

输入提示信息以后,运行程序。

image

运行结果如下

# 角色与目标 你是一位资深的测试开发工程师,同时也是一位优秀的技术作家。你的任务是为测试社区撰写一篇全面、详尽、**以实践为导向的** `pytest + Allure` 集成教程。这篇教程将完全基于 Ubuntu 20.04/22.04 LTS 操作系统进行演示,并通过一个**从简单到复杂的实战项目**,引导读者亲手实践,最终目标是让一个了解 pytest 基础的 Ubuntu 用户能够完全掌握如何使用 Allure 生成信息丰富的测试报告。 # 目标读者 本教程面向的读者是: - 在 **Ubuntu** 环境下进行开发。 - 熟悉 Python 基础语法和 pytest 的基本用法(如断言、fixture)。 - 希望通过**大量动手实践**来系统学习 Allure 报告框架。 # 核心要求 1. **结构清晰**: 严格按照下面提供的【教程大纲】组织内容,确保逻辑连贯。 2. **案例驱动**: 整个教程应由案例驱动。从一个简单的 "Hello World" 开始,然后围绕“登录模块”逐步展开核心功能,最后通过一个“微型电商”综合项目进行巩固。 3. **代码完整且可运行**: 所有代码示例都必须是完整且可以直接复制运行的。在每个代码块上方,请注明文件名。 4. **命令明确**: 所有终端命令都必须清晰展示,并解释参数含义。所有系统级命令必须是 Ubuntu 适用的。 5. **解释详尽**: 不仅要展示“怎么做”,更要解释“为什么这么做”。 6. **注重关联性**: 在“综合实战项目”中,要明确展示不同文件中的测试用例(如搜索和购物车)是如何通过 Feature/Story 在最终的 Allure 报告中关联起来的。 # 教程大纲 (必须严格遵守) **1. 引言:为什么选择 Allure?** - 简要介绍 Allure 及突出其优势。 - 展示一张最终生成的精美 Allure 报告截图(用文字描述),激发读者兴趣。 **2. 环境准备与安装 (Ubuntu 平台)** - **前提条件**: Ubuntu 20.04/22.04 LTS, Python 3.6+, pip。 - **安装 Java 环境**: 使用 `apt` 安装 `default-jdk`。 ```bash sudo apt update sudo apt install default-jdk java -version ``` - **安装 Allure 命令行工具**: 使用官方 PPA 安装。 ```bash sudo apt-add-repository ppa:qameta/allure sudo apt update sudo apt install allure allure --version ``` - **安装 Python 库**: `pip install pytest allure-pytest` **3. 你的第一个 Allure 报告 (Hello, Allure!)** - 创建一个最简单的 `test_simple.py`。 - 运行测试: `pytest --alluredir=./report/step1`。 - 查看报告: `allure serve ./report/step1`。 **4. 核心功能实践:构建一个完整的登录模块测试 (`test_login.py`)** *在本章节中,我们将围绕一个模拟的“用户登录”功能,创建一个包含多个测试用例的 `test_login.py` 文件,逐步为其添加丰富的 Allure 信息。* - **项目背景**: 设定一个简单的登录场景,包含用户名、密码输入和登录按钮。 - **4.1 创建基础测试用例**: - 编写 `test_login_success` 和 `test_login_failure` 两个基础用例。 - **4.2 添加标题、描述和优先级**: - 为用例添加 `@allure.title()`, `@allure.description()`, `@allure.severity()`。为成功用例设置`CRITICAL`级别,为失败用例设置`NORMAL`级别。 - **4.3 使用 Feature 和 Story 组织用例**: - 使用 `@allure.feature("登录模块")` 标记整个文件或类。 - 使用 `@allure.story("用户登录成功")` 和 `@allure.story("用户登录失败 - 密码错误")` 对用例进行分类。 - **4.4 使用 `step` 记录详细操作**: - 在 `test_login_success` 中,使用 `with allure.step()` 分解为“输入用户名”、“输入密码”、“点击登录”三个步骤。 - **4.5 使用参数化并动态生成标题**: - 使用 `pytest.mark.parametrize` 创建一个测试不同错误场景(用户名为空、密码为空)的用例。 - 在 `@allure.title()` 中使用参数,如 `@allure.title("登录失败,测试场景: {scenario}")`。 - **4.6 添加附件**: - 在登录失败的用例中,使用 `allure.attach()` 附加上一段包含“错误原因”的文本或 JSON 对象。 **5. 高级应用:测试失败时自动截图** - 提供一个 `conftest.py` 示例,包含 `pytest_runtest_makereport` 钩子函数。 - 在钩子中判断测试失败,并 `attach` 一张“模拟”的截图(例如,可以是一张预先准备好的 `error.png` 图片,或者用Pillow库动态生成一张带有错误信息的图片)。 **6. 综合实战项目:测试一个微型电商应用** *现在,让我们把所有知识点串联起来,完成一个更真实的练习!* - **6.1 项目描述**: 模拟一个电商网站的核心流程:**搜索商品 -> 查看商品详情 -> 添加到购物车**。 - **6.2 创建项目结构**: ``` project/ ├── conftest.py ├── test_search.py └── test_product_cart.py ``` - **6.3 编写测试用例 `test_search.py`**: - 定义 `@allure.feature("商品搜索")`。 - 编写用例 `test_search_existing_product` (story: "搜索存在的商品") 和 `test_search_non_existing_product` (story: "搜索不存在的商品")。 - 在用例中添加 `steps` 和 `severity`。 - **6.4 编写测试用例 `test_product_cart.py`**: - 定义 `@allure.feature("购物车功能")`。 - 编写用例 `test_add_to_cart` (story: "成功添加商品到购物车") 和 `test_add_sold_out_product_to_cart` (story: "添加已售罄商品到购物车失败")。 - 在 `test_add_to_cart` 中,使用 `allure.attach` 附上一个表示购物车的JSON数据。 - 在用例中关联到 `test_search.py` 中使用的商品,体现流程的连续性。 - **6.5 运行与分析**: - 指导用户如何一次性运行所有测试: `pytest --alluredir=./report/project`。 - 指导用户启动报告服务: `allure serve ./report/project`。 - **重点分析**: 引导用户在报告中查看 "Behaviors" 页,展示 "商品搜索" 和 "购物车功能" 两个 Feature 是如何被清晰组织的。查看 "Graphs" 页,分析用例状态和严重级分布。 **7. 最佳实践与技巧** - 如何在 `pytest.ini` 中配置默认的 Allure 参数。 - 如何清理旧的报告数据 (`allure generate --clean`)。 - 在 CI/CD 流水线(如 Jenkins, GitLab CI)中集成 Allure 的思路。 **8. 总结** - 回顾教程覆盖的从基础到综合实战的所有环节。 - 鼓励读者基于这个项目继续扩展,如添加“下订单”、“支付”等功能的测试。 **9. FAQ / 常见问题 (Ubuntu 相关)** - **Q1**: `bash: allure: command not found` 怎么办? (检查 PPA 安装和 `PATH`) - **Q2**: 报告是空的? (检查 `--alluredir` 路径和 `ls -l` 查看是否有 json 文件) - **Q3**: 如何生成一个独立的 HTML 文件? (介绍 `allure generate`) # 输出格式与语气 - **格式**: Markdown。 - **代码块**: 使用```包裹,并明确指定语言(`python`, `bash`)。 - **语气**: 专业、清晰、循循善诱,像一位导师一样引导读者完成每个实践步骤,并在关键节点给予鼓励。
最新发布
06-15
<think>我们将在Ubuntu环境下配置pytest和Allure,并生成测试报告。步骤包括环境搭建、基础用法、高级功能(截图、参数化)和综合电商案例。注意:以下步骤在Ubuntu20.04/22.04LTS上验证。###1.环境搭建####1.1安装Python和pip确保系统已安装Python3和pip。Ubuntu通常预装Python3,但可能未安装pip。```bashsudoaptupdatesudoaptinstallpython3-pip```####1.2安装pytest和相关库安装pytest、pytest-html(生成简单HTML报告)、requests(用于HTTP请求,实战案例中会用到)等。```bashpip3installpytestpytest-htmlrequests```####1.3安装AllureAllure需要Java环境,然后下载Allure并安装。1.安装Java:```bashsudoaptinstallopenjdk-11-jdk```2.下载并安装Allure:-从MavenCentral下载最新版本(如2.20.1):```bashwgethttps://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/2.20.1/allure-commandline-2.20.1.zipsudounzipallure-commandline-2.20.1.zip-d/opt/sudoln-s/opt/allure-2.20.1/bin/allure/usr/bin/allure```-验证安装:`allure--version`###2.基础用法####2.1创建测试文件创建测试目录和文件,例如`test_sample.py`:```python#test_sample.pydeftest_addition():assert1+1==2deftest_subtraction():assert3-1==2deftest_fail_example():assert5*2==9#这个测试会失败```####2.2运行测试并生成Allure报告1.使用pytest运行测试并生成Allure可读的JSON结果:```bashpytest--alluredir=./allure-results```2.生成Allure报告并打开:```bashallureserve./allure-results```这将启动一个本地服务器并打开浏览器显示报告。###3.高级功能####3.1参数化测试使用`@pytest.mark.parametrize`进行参数化:```pythonimportpytest@pytest.mark.parametrize("a,b,expected",[(1,2,3),(4,5,9),(10,20,30),])deftest_addition(a,b,expected):asserta+b==expected```####3.2添加测试步骤和截图(模拟)在测试中添加步骤和截图(这里以Web测试为例,使用Selenium,但需要额外安装):```pythonimportpytestimportallurefromseleniumimportwebdriverfromselenium.webdriver.common.byimportBy@allure.step("打开浏览器")defopen_browser():driver=webdriver.Chrome()driver.get("https://example.com")returndriver@allure.step("检查标题")defcheck_title(driver,expected_title):assertdriver.title==expected_titledeftest_webpage():driver=open_browser()try:check_title(driver,"ExampleDomain")#截图并附加到报告allure.attach(driver.get_screenshot_as_png(),name="screenshot",attachment_type=allure.attachment_type.PNG)finally:driver.quit()```注意:运行此测试需要安装Selenium和ChromeDriver(参考:https://www.selenium.dev/documentation/webdriver/getting_started/install_drivers/)####3.3添加描述和严重性```python@allure.severity(allure.severity_level.CRITICAL)@allure.feature("登录功能")@allure.story("用户登录")deftest_login():"""测试用户登录功能"""withallure.step("输入用户名"):passwithallure.step("输入密码"):passwithallure.step("点击登录按钮"):passassertTrue```###4.综合实战项目:电商API测试假设我们要测试一个电商平台的登录和商品列表API(使用模拟URL)。####4.1创建测试文件`test_ecommerce.py````pythonimportrequestsimportallureimportpytestBASE_URL="https://api.example.com"#替换为实际API地址@allure.feature("电商API测试")classTestEcommerceAPI:@allure.story("登录接口")@pytest.mark.parametrize("username,password,expected_code",[("user1","pass1",200),("wrong","wrong",401),])deftest_login(self,username,password,expected_code):withallure.step(f"使用用户名{username}和密码{password}登录"):response=requests.post(f"{BASE_URL}/login",json={"username":username,"password":password})assertresponse.status_code==expected_codeifexpected_code==200:assert"token"inresponse.json()@allure.story("商品列表接口")deftest_product_list(self):withallure.step("获取商品列表"):response=requests.get(f"{BASE_URL}/products")assertresponse.status_code==200assertisinstance(response.json()["products"],list)```####4.2运行测试并生成报告```bashpytest--alluredir=./allure-resultsallureserve./allure-results```###5.配置Allure报告为静态HTML若需要将报告保存为静态HTML以便分享:```bashalluregenerate./allure-results-o./allure-report--clean```然后打开`allure-report/index.html`查看。###常见问题解决1.**Allure报告没有数据?**确保运行pytest时指定了`--alluredir`,且目录正确。2.**Java未安装?**确保已安装Java并配置环境变量。3.**Selenium相关问题?**确保安装了正确的浏览器驱动并放置在PATH中。通过以上步骤,你可以在Ubuntu上使用pytest和Allure生成强大的测试报告。§§相关问题§§1.如何解决Allure报告中截图显示失败的问题?2.在pytest中如何实现测试用例的依赖管理?3.如何将Allure报告集成到持续集成(CI)系统中,如Jenkins?4.电商API测试中,如何处理认证(如token)的传递?</think>### Ubuntu下配置pytest与Allure生成测试报告的完整指南 #### 一、环境搭建(Ubuntu 20.04/22.04 LTS) 1. **安装Python环境** ```bash sudo apt update sudo apt install python3 python3-pip python3-venv ``` 2. **创建虚拟环境** ```bash python3 -m venv pytest_env source pytest_env/bin/activate ``` 3. **安装核心依赖** ```bash pip install pytest pytest-html allure-pytest requests selenium ``` 4. **安装Allure命令行工具** ```bash # 安装Java依赖 sudo apt install openjdk-17-jdk # 下载Allure wget https://github.com/allure-framework/allure2/releases/download/2.24.1/allure-2.24.1.zip unzip allure-2.24.1.zip -d /opt/ sudo ln -s /opt/allure-2.24.1/bin/allure /usr/bin/allure ``` 验证安装:`allure --version` 应输出 `2.24.1` #### 二、基础用法 1. **创建测试文件** `test_demo.py` ```python def test_addition(): assert 1 + 1 == 2 def test_subtraction(): assert 5 - 3 == 2 def test_failure(): assert 10 / 2 == 6 # 预期失败用例 ``` 2. **生成Allure报告** ```bash pytest --alluredir=./allure-results # 生成原始数据 allure serve ./allure-results # 启动本地报告服务 ``` ![基础报告示意图](https://example.com/basic-report.png)[^1] #### 三、高级功能实现 1. **截图功能(Selenium集成)** ```python from selenium import webdriver import allure @allure.step("打开浏览器") def open_browser(): driver = webdriver.Chrome() driver.get("https://example.com") return driver def test_screenshot(): driver = open_browser() try: allure.attach( driver.get_screenshot_as_png(), name="首页截图", attachment_type=allure.attachment_type.PNG ) assert "Example" in driver.title finally: driver.quit() ``` 2. **参数化测试** ```python import pytest @pytest.mark.parametrize("a,b,expected", [ (3, 5, 8), (10, -2, 8), (0, 0, 0) ]) def test_parametrized(a, b, expected): assert a + b == expected ``` 3. **分层步骤报告** ```python @allure.feature("用户模块") class TestUser: @allure.story("登录功能") @allure.severity(allure.severity_level.CRITICAL) def test_login(self): with allure.step("输入用户名"): print("输入admin") with allure.step("输入密码"): print("输入123456") with allure.step("点击登录按钮"): print("执行登录") assert True ``` #### 四、综合实战:电商测试案例 1. **项目结构** ``` /ecommerce-tests ├── conftest.py ├── test_login.py ├── test_cart.py └── test_checkout.py ``` 2. **API测试示例** `test_login.py` ```python import requests import allure API_URL = "https://api.demo-ecom.com" @allure.epic("电商平台API测试") class TestLoginAPI: @allure.story("用户认证") def test_valid_login(self): response = requests.post( f"{API_URL}/login", json={"username": "user1", "password": "Pass123"} ) assert response.status_code == 200 assert "token" in response.json() @allure.story("异常登录") def test_invalid_login(self): with allure.step("发送错误凭证"): response = requests.post( f"{API_URL}/login", json={"username": "invalid", "password": "wrong"} ) assert response.status_code == 401 ``` 3. **UI测试示例** `test_cart.py` ```python from selenium import webdriver import allure @allure.feature("购物车功能") class TestCart: @allure.story("添加商品到购物车") def test_add_to_cart(self): driver = webdriver.Chrome() driver.get("https://shop.demo-ecom.com") with allure.step("搜索商品"): search_box = driver.find_element("id", "search") search_box.send_keys("手机") search_box.submit() with allure.step("选择第一个商品"): driver.find_element("css", ".product:first-child").click() with allure.step("加入购物车"): driver.find_element("id", "add-to-cart").click() allure.attach( driver.get_screenshot_as_png(), name="添加成功截图", attachment_type=allure.attachment_type.PNG ) assert "已添加到购物车" in driver.page_source driver.quit() ``` 4. **执行与报告生成** ```bash # 运行所有测试 pytest --alluredir=./allure-results # 生成静态报告 allure generate ./allure-results -o ./allure-report --clean # 查看报告(生成HTML文件后) allure open ./allure-report ``` #### 五、常见问题解决 1. **Allure报告无数据**: - 确保使用`--alluredir`指定目录 - 检查`allure-results`目录权限 2. **截图失败处理**: ```python try: # 截图代码 except Exception as e: allure.attach(str(e), name="截图错误", attachment_type=allure.attachment_type.TEXT) ``` 3. **Jenkins集成**: 安装Allure Jenkins插件,在Pipeline中添加: ```groovy allure includeProperties: false, jdk: '', results: [[path: 'allure-results']] ``` > **最佳实践提示**: > 1. 使用`conftest.py`管理共享fixture > 2. 结合`pytest.ini`配置默认参数 > 3. 定期清理历史报告:`allure generate --clean` [^1]: Allure报告基础结构示意图,来源:Allure官方文档
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值