今天因为测试任务在新的环境又脑补了一下基于Python的Pytest环境配置与安装,加以分享,希望对大家有所帮助
前置:python安装已经安装
1.安装pytest
pip install -U pytest
2.检查pytest安装的版本
pip show pytest
3.基于pycharm设置pytest运行环境
create_project | File>>Setting>>Tools>>Python integrated Tools >> Default test runner >> 下拉选择pytest
注意:创建项目时在pycharm对应的运行环境需要选择正确,不然出现在pip安装的模块都无法正常使用
按照以下步骤进行设置pytest运行环境
设置完后创建一个以test开头的新py文件,代码区右键可以看到Run已经是pytest
注:unitest的脚本使用pytest运行模式同样兼容
3.pycharm环境试运行
** 运行之前需要给大家介绍下pytest框架文件的设计规则,这是pytest的游戏规则请遵守,否则在执行期间pytest找不到不按规则命名的脚本
文件名需以 test_*.py 文件和*_test.py 函数以 test_开头 类以 Test 开头 方法以 test_开头 所有的包 pakege 必项要有__init__.py 文件
以百度检索知乎为Demo进行演示
# coding:utf-8
# authou: shichao
# pytest试运行
import pytest
from selenium import webdriver
import time
def test_login():
driver = webdriver.Chrome()
driver.get("https://www.baidu.com") # 打开百度浏览器
driver.find_element_by_id("kw").send_keys("知乎") # 定位输⼊框并输⼊关键字
driver.find_element_by_id("su").click() # 点击[百度⼀下]搜索
time.sleep(3) # 等待3秒
driver.quit() # 关闭浏览器
if __name__ == '__main__':
pytest.main('-q test_auto_demo.py')
注:驱动以及浏览器对应版本:https://registry.npmmirror.com/binary.html?path=chromedriver/