allure生成测试报告(pycharm+python+pytest)

以下是使用简单的add方法以及一个失败的测试用例和成功的用例说明如何简单的使用allure生成美观的测试 报告

【环境配置】
1)前置条件:pytest
2)下载allure:http://allure.qatools.ru/
3)解压alllure并配置环境变量,allure的bin目录
vim .bash_profile(编辑环境变量)
source .bash_profile(使环境变量生效)
4)检查环境变量是否配置成功

allure --version

5)安装allure-pytest :pip3 install allure-pytest
注意:allure-pytest 与 pytest-allure-adaptor不能同时使用
在这里插入图片描述
【初次生成测试报告,但不是html文件格式】
pytest -s -q --alluredir=/Users/you/Documents/PycharmProject/addMethod/src/report /Users/you/Documents/PycharmProject/addMethod/src/testcase/add3
(测试case)在这里插入图片描述

【美化报告-生成html文件格式的测试报告】
allure generate --clean /Users/you/Documents/PycharmProject/addMethod/src/report -o /Users/you/Documents/PycharmProject/addMethod/src/report/html
在这里插入图片描述
【测试报告】
在这里插入图片描述
在这里插入图片描述
【进一步美化,使用allure的特性-feature】

#导入allure
import allure

def add(x,y):
    return x+y

#定义被测试的功能
@allure.feature("测试成功的用例")
def test_add3():
    assert add(2,3)==5

在这里插入图片描述

<think>我们正在回答用户关于如何在PyCharm中使用Python、Selenium和pytest进行测试自动化的问题。根据引用,我们了解到需要搭建一个测试框架,其中:-编程语言:Python3-框架:pytest-方法:Selenium(用于Web自动化)步骤:1.环境准备:安装PythonPyCharm、Selenium和pytest。2.创建项目并配置环境。3.编写测试用例。4.运行测试并生成报告。详细步骤:1.环境准备:-安装Python3:从官网下载并安装。-安装PyCharm:下载并安装。-在PyCharm中创建新项目,并配置Python解释器。-安装必要的包:在PyCharm的终端中执行以下命令:```bashpipinstallseleniumpytestpytest-html```其中,pytest-html用于生成HTML测试报告。2.配置项目:-在项目中创建测试目录(例如:tests)。-在测试目录中创建测试文件,文件名以`test_`开头或`_test.py`结尾(例如:test_login.py)。3.编写测试用例:-使用pytest的测试函数格式,函数名以`test_`开头。-在测试函数中,使用Selenium编写Web自动化脚本。示例测试用例(test_login.py):```pythonfromseleniumimportwebdriverimportpytest@pytest.fixture(scope="function")defbrowser():#初始化浏览器driver=webdriver.Chrome()yielddriver#测试结束后关闭浏览器driver.quit()deftest_login(browser):browser.get("https://example.com/login")username=browser.find_element_by_id("username")password=browser.find_element_by_id("password")submit=browser.find_element_by_id("submit")username.send_keys("testuser")password.send_keys("password")submit.click()#验证登录成功welcome_message=browser.find_element_by_id("welcome-message")assert"Welcome"inwelcome_message.text```4.运行测试:-在PyCharm中,可以直接右键点击测试文件选择运行(选择以pytest方式运行)。-或者,在终端中使用命令:```bashpytesttests/test_login.py--html=report.html```这将运行测试并生成一个名为report.html的测试报告。5.持续集成(可选):-可以配置CI工具(如Jenkins、GitHubActions)来定期运行测试。注意事项:-确保安装了对应浏览器的WebDriver(如ChromeDriver),并将其放在系统路径中,或者在代码中指定路径。-使用pytest的fixture(如上面的browser)可以方便地管理测试资源。根据引用[3],pytest会默认搜寻所有文件名为`test_*.py`或者`*_test.py`的文件,因此按照此规则命名文件即可被自动发现。引用[4]提到可以使用Allure生成更美观的测试报告,安装Allure并配置:-安装Allure:https://docs.qameta.io/allure/-安装pytestAllure插件:`pipinstallallure-pytest`-运行测试并生成Allure报告:```bashpytest--alluredir=allure-resultsallureserveallure-results```以上就是在PyCharm中使用Python、Selenium和pytest进行测试自动化的基本步骤。生成相关问题:1.如何配置ChromeDriver以与Selenium一起使用?2.在pytest中如何使用参数化测试?3.如何将自动化测试集成到持续集成(CI)流程中?</think>### 在PyCharm中使用Python+Selenium+pytest实现测试自动化 #### 环境配置步骤: 1. **安装必要组件** - 在PyCharm终端执行: ```bash pip install selenium pytest pytest-html webdriver-manager ``` - `webdriver-manager`自动管理浏览器驱动[^3] 2. **项目结构配置** ```markdown project/ ├── tests/ │ ├── test_login.py # 测试用例文件 │ └── conftest.py # 全局fixture ├── pages/ │ └── login_page.py # 页面对象模型 └── requirements.txt # 依赖清单 ``` 3. **创建基础框架** **conftest.py** (浏览器初始化): ```python import pytest from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager @pytest.fixture(scope="function") def browser(): driver = webdriver.Chrome(ChromeDriverManager().install()) driver.implicitly_wait(10) yield driver driver.quit() ``` **pages/login_page.py** (页面对象): ```python class LoginPage: def __init__(self, driver): self.driver = driver self.username_field = ("id", "username") self.password_field = ("id", "password") def login(self, username, password): self.driver.find_element(*self.username_field).send_keys(username) self.driver.find_element(*self.password_field).send_keys(password) self.driver.find_element("id", "submit").click() ``` **tests/test_login.py** (测试用例): ```python from pages.login_page import LoginPage def test_valid_login(browser): browser.get("https://example.com/login") login_page = LoginPage(browser) login_page.login("admin", "securepass") assert "Dashboard" in browser.title ``` #### 执行测试 1. **PyCharm运行配置** - 右键测试目录 → `Run pytest in tests` - 或终端执行: ```bash pytest tests/ --html=report.html ``` 2. **增强功能** - 参数化测试: ```python @pytest.mark.parametrize("user,pwd", [("user1","pwd1"), ("user2","pwd2")]) def test_login_combinations(browser, user, pwd): # 测试逻辑 ``` - Allure报告: ```bash pytest --alluredir=allure-results allure serve allure-results ``` #### 常见问题解决 1. **浏览器驱动问题** - 使用`webdriver-manager`自动下载驱动[^3] - 或手动指定路径: ```python driver = webdriver.Chrome(executable_path="path/to/chromedriver") ``` 2. **元素定位失败** - 添加显式等待: ```python from selenium.webdriver.support.ui import WebDriverWait WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.ID, "element_id")) ) ``` 3. **测试报告优化** - 安装`pytest-html`生成HTML报告 - 使用`allure-pytest`生成可视化报告[^4] > **最佳实践**:遵循页面对象模式(POM)分离测试逻辑与页面操作,使用`conftest.py`管理共享fixture,保持测试原子性[^2]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值