环境
python 3.7 由于3.0-3.5以下部分pytest可能有部分兼容问题安装建议2.7-2.9,3.5-最新
pip install pytest专属 pytest框架包
pip install allure-pytest 专属allure的报告包后面会用到可以不安装这里
pip install pytest-html pytest自己专属报告包
pip install pytest-rerunfailures 失败重跑包也是pytest专属
并发的也可以安下,利用多cpu运行调高用例执行速度
python -m pip install xdist
配置
file>>setting>>tools>>Diff>>python integrated tools,选取pytest框架
2.重新封装webdriver定位方法
# coding=utf-8
"""
@author: ChenQuan 2019/1/20;
@:param: rewrite old find ways and return ele or elements ;
@:param: Usage: ele/elements = GetEle().ele_by_id(locator)
@:param: driver is given by setup/fixture by yield
"""
class GetEle(object):
def ele_by_id(self,driver,id):
return driver.find_element_by_id(id)
def ele_by_xpath(self,driver,xpath):
return driver.find_element_by_xpath(xpath)
def ele_by_css(self,driver,css_selector):
return driver.find_element_by_css_selector(css_selector)
def ele_by_class_name(self,driver,className):
return driver.find_element_by_class_name(className)
def ele_by_link(self,driver,link_text):
return driver.find_element_by_link_text(link_text)
def ele_by_part_link(self,driver,part_link_text):
return driver.find_element_by_partial_link_text(part_link_text)
def elements_by_id(self,driver,id):
return driver.find_element_by_id(id)
def elements_by_xpath(self,driver,xpath):
return driver.find_element_by_xpath(xpath)
def elements_by_css(self,driver,css_selector):
return driver.find_element_by_css_selector(css_selector)
def elements_by_class_name(self,driver,className):
return driver.find_element_by_class_name(className)
def elements_by_link(self,driver,link_text):
return driver.find_element_by_link_text(link_text)
def elements_by_part_link(self,driver,part_link_text):
return driver.find_element_by_partial_link_text(part_link_text)
3.编写pytest脚本
pytest命名规则:
包名test_开头或者以_test结尾
用例名称test_格式开头
类名称Test开头
4.脚本解释:
yield 作用是类似return ,在用例执行完成后会返回继续执行yield后面代码,yield将在python4.0以后删除详情见官网
# coding=utf-8
import pytest
from selenium import webdriver
from pyfoo.Utils.pageobj import GetEle
from pyfoo.Utils.pageobj import *
@pytest.fixture(scope="class")
def driver():
url="http://www.baidu.com"
driver= webdriver.Chrome()
driver.get(url)
yield driver
driver.close()
print("teardown...")
@pytest.mark.usefixtures("driver")
class Test_fix_module(object):
def test_open_bd(self,driver):
GetEle().ele_by_id(driver,'kw').send_keys("pytest")
GetEle().ele_by_id(driver,'su').click()
print("搜索资源成功")
def test_open_tb(self,driver):
GetEle().ele_by_xpath(driver,'//*[@id="s_tab"]/div/a[1]').click()
print("进入资讯系列")
if __name__ == "__main__":
pytest.main(['-v','-s','-q'])