Pipenv环境配置+Pytest运行

本文介绍了如何使用Pipenv管理Python项目的依赖,创建并激活虚拟环境,以及如何在PyCharm中配置默认测试运行器pytest,包括设置pytest.ini文件和运行测试用例。

环境配置

使用Pipenv进行虚拟环境管理,Pipfile为依赖模块管理文件。

  1. 安装pipenv:brew install pipenv
  2. 根项目根目录下执行命令创建虚拟环境: pipenv install
  3. 在Pycharm中指定项目运行的虚拟环境 :File->Settings->Project:->Project Interpreter

执行用例

  • 配置PyCharm的default test runner为pytest

  • 打开PyCharm的run/debug configurations窗口,指定运行文件

  • pytest.ini文件中指定配置的config文件

addopts = -rsxX --color=yes --env=test --config=folder/xx_config.yaml --alluredir allure-results --json-report

  • 运行

使用Python、Playwright、Pytest和BDD,结合有限状态机(FSM)打造高效测试框架可按以下步骤进行: ### 项目结构设计 构建良好的目录结构,这是高效开发和维护的基础。结合FSM思想,pytest - playwright框架项目结构可设计如下: ```plaintext pytest-playwright-demo/ ├── fsm/ │ └── user_fsm.py ├── pages/ │ ├── base_page.py │ ├── login_page.py │ ├── register_page.py │ └── dashboard_page.py ├── tests/ │ └── test_user_flow_fsm.py ├── conftest.py ├── requirements.txt └── README.md ``` 其中,`fsm` 目录存放与有限状态机相关的代码,如 `user_fsm.py`;`pages` 目录用于存放页面相关的类,像 `base_page.py` 作为基类,`login_page.py`、`register_page.py` 和 `dashboard_page.py` 分别对应不同页面的操作;`tests` 目录存放测试用例文件,如 `test_user_flow_fsm.py`;`conftest.py` 用于存放pytest的配置;`requirements.txt` 记录项目所需的依赖包;`README.md` 是项目说明文件 [^1]。 ### 准备依赖库 创建 `requirements.txt` 文件,包含以下依赖: ```plaintext pytest==7.3.1 pytest-bdd==7.3.0 playwright==1.48.0 pytest-html==4.1.1 ``` 可使用 `pip install -r requirements.txt` 命令来安装这些依赖 [^5]。 ### 编写有限状态机代码 在 `fsm/user_fsm.py` 中实现有限状态机逻辑,用于描述用户在系统中的状态转换,例如用户从登录状态到注册状态再到仪表盘状态等。 ### 编写页面类 在 `pages` 目录下编写各个页面的类,如 `base_page.py` 可定义一些通用的页面操作方法,其他页面类继承自 `base_page.py` 并实现各自页面特有的操作方法。 ### 编写测试用例 在 `tests/test_user_flow_fsm.py` 中编写测试用例,结合BDD的思想,使用 `pytest-bdd` 编写场景和步骤。例如: ```python # 示例代码,需根据实际情况完善 from pytest_bdd import scenario, given, when, then @scenario('user_flow.feature', 'User logs in and accesses dashboard') def test_user_flow(): pass @given("the user is on the login page") def user_on_login_page(page): # 实现跳转到登录页面的代码 pass @when("the user enters valid credentials and clicks login") def user_enters_credentials_and_clicks_login(page): # 实现输入用户名、密码并点击登录的代码 pass @then("the user should be redirected to the dashboard page") def user_is_on_dashboard_page(page): # 验证是否跳转到仪表盘页面的代码 pass ``` ### 运行自动化测试 可以使用 `pipenv` 管理虚拟环境来运行测试: 1. 激活已存在的虚拟环境(如果不存在会创建一个):`pipenv shell`(必须在项目根目录下执行) 2. 运行测试: - `python run.py`:默认在 `test` 环境运行测试用例,报告采用 `allure` - `python run.py -m demo`:在 `test` 环境仅运行打了标记 `demo` 的用例,默认报告采用 `allure` - `python run.py -env live`:在 `live` 环境运行测试用例 - `python run.py -env=test`:在 `test` 环境运行测试用例 - `python run.py -browser chromium`:使用 `chrome` 浏览器运行测试用例 - `python run.py -env test -report no -browser chromium -mode headless`:在 `test` 环境,使用谷歌无头浏览器运行用例,并且生成 `allure html report` [^3]。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值