pytest自動化運用實戰

2021-09-10 01:24:03 字數 3311 閱讀 7164

環境

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執行調高用例執行速度

配置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=""

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("進入資訊系列")

web自動化 三 pytest 引數化

前言 環境 centons 7.6 python 3.6 chrome 80.0.3987.132 chromedriver 80.0.3987.16 selenium 3.14 自動化測試過程中,經常會出有些場景需要測試多種場景,例如 常見的登入頁面需要對賬號的型別,種類,長度等分別進行測試,引用...

python自動化測試框架 pytest

pip install u pytest 示例 import pytest def test a print test a assert 1 成功 def test b print test b assert 0 失敗 if name main pytest.main s test pytest.p...

介面自動化 pytest 環境搭建

出處 前言 python鄙視鏈 pytest 鄙視 unittest 鄙視 robotframework 鄙視 記流水賬 鄙視 hello world 小白 所以,讓我們一起學習pytest吧。pytest簡介 pytest是python的一種單元測試框架,與python自帶的unittest測試框...