欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pytest測試框架+allure超詳細(xì)教程

 更新時間:2022年11月30日 10:02:58   作者:測試運維小猴子  
這篇文章主要介紹了pytest測試框架+allure超詳細(xì)教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

1、測試識別和運行

文件識別:

  • 在給定的目錄中,搜索所有test_.py或者_(dá)test.py文件

用例識別:

  • Test*類包含的所有test_*的方法(測試類不能有__init__方法)
  • 不在類中的所有test_*方法
  • pytest也能執(zhí)行unit test寫的用例和方法

運行方式
1、pycharm頁面修改默認(rèn)的測試運行方式
settings頁面,輸入pytest,修改Default test runner

在這里插入圖片描述

2、右鍵執(zhí)行python文件
3、命令行界面執(zhí)行,點擊pycharm下方的terminal,打開命令行界面,執(zhí)行pytest 命令

在這里插入圖片描述

4、pycharm代碼邊界界面,左側(cè)單條用例運行按鈕

在這里插入圖片描述

5、主函數(shù)運行方式
在運行文件中編寫主函數(shù),主函數(shù)啟動需要導(dǎo)入pytest包,不然找不到pytest方法

import pytest
 
class TestClass:
    def test_one(self):
        x = "this"
        assert "h" in x
 
    def test_two(self):
        x = "hello"
        assert hasattr(x, "check")
 
if __name__ == '__main__':
#    pytest.main()
    pytest.main("test_study.py")

pytest.main()會自動讀取當(dāng)前目錄下的所有test開頭的.py文件,運行test方法或者類

可以傳入不同的參數(shù),讓運行更加定制化

pytest.main(['./'])               # 運行./目錄下所有(test_*.py  和 *_test.py)
pytest.main (['./subpath1'])    # 運行./subpath1 目錄下用例
pytest.main (['./subpath1/test_module1.py'])    # 運行指定模塊
pytest.main (['./subpath1/test_module1.py::test_m1_1'])  # 運行模塊中的指定用例
pytest.main (['./subpath2/test_module2.py::TestM2::test_m2_02'])  # 運行類中的指定用例
pytest.main (['-k','pp'])         # 匹配包含pp的用例(匹配目錄名、模塊名、類名、用例名)
pytest.main(['-k','spec','./subpath1/test_module1.py'])     # 匹配test_module1.py模塊下包含spec的用例
pytest.main(['-k','pp','./subpath2/test_module2.py::TestM2'])   # 匹配TestM2類中包含pp的用例

2、參數(shù)化

@pytest.mark.parametrize(argnames,argvalues)

  • argnames:要參數(shù)化的變量,可以是string(用逗號分割),list,tuple
  • argvalues:參數(shù)化的值,list[tuple],以列表形式傳入元組,每個元組都是一條測試數(shù)據(jù)
  • ids,:默認(rèn)為none,用來重新定義測試用例的名稱
# 使用string分割參數(shù)化的變量
@pytest.mark.parametrize('a,b',[(10,20),(30,40)])
def test_param(a, b):
	print(a,b)

# 使用list分割參數(shù)化的變量
@pytest.mark.parametrize(['a', 'b'],[(10,20),(30,40)])
def test_param(a, b):
	print(a,b)

# 使用tuple分割參數(shù)化的變量
@pytest.mark.parametrize(('a', 'b'),[(10,20),(30,40)])
def test_param(a, b):
	print(a,b)

# 使用ids重新定義用例名稱
@pytest.mark.parametrize('a,b',[(10,20),(30,40)], ids=['case1', 'case2'])
def test_param(a, b):
	print(a,b)

yaml參數(shù)化
pip install PyYAML
yaml實現(xiàn)list

- 10
- 20
- 30

yaml實現(xiàn)字典

by: id
locator: name
action: click

yaml二維數(shù)組

companies:
  - id: 1
    name: company1
    price: 200w
  - id: 2
    name: company2
    price: 500w
fruites:
  - name: 蘋果
    price: 8.6
  - name: 香蕉
    price: 2.6

讀取yaml文件
yaml.safe_load(open(‘./data.yaml’))

3、測試報告美化-allure

1、操作系統(tǒng)內(nèi)部先安裝allure
2、安裝allure-pytest插件

pip install allure-pytest

3、運行測試用例
查看pytest中allure相關(guān)的命令行參數(shù)

C:\Users\Administrator>pytest --help | findstr allure  <== linux 使用grep過濾
  --allure-severities=SEVERITIES_SET     <==  根據(jù)用例級別過濾需要執(zhí)行的用例
  --allure-epics=EPICS_SET               
  --allure-features=FEATURES_SET         <== 根據(jù)用例設(shè)置的feature名稱過濾需要執(zhí)行的用例
  --allure-stories=STORIES_SET           <== 根據(jù)用例設(shè)置的story名稱過濾需要執(zhí)行的用例
  --allure-ids=IDS_SET  Comma-separated list of IDs.
  --allure-link-pattern=LINK_TYPE:LINK_PATTERN
  --alluredir=DIR       <== 指定存放用例執(zhí)行結(jié)果的目錄
  --clean-alluredir     清除alluredir文件夾(如果存在)
  --allure-no-capture   Do not attach pytest captured logging/stdout/stderr to

4、執(zhí)行測試命令

# --alluredir: 用于指定存儲測試結(jié)果的路徑
pytest [測試文件] -vs --alluredir=./result/ --clean-alluredir

5、查看測試報告

在線查看報告,直接打開默認(rèn)瀏覽器展示當(dāng)前報告

# 注意這里的serve書寫,后面接用例執(zhí)行結(jié)果(./result/:就是存放執(zhí)行結(jié)果的目錄路徑)
allure serve ./result/

從結(jié)果生成報告
1.生成報告

# 注意:覆蓋路徑加--clean
allure generate ./result/ -o ./report/ --clean

2.打開報告

allure open -h 127.0.0.1 -p 8883 ./report/

allure常用特性
支持在報告中查看測試功能、子功能或場景、測試步驟和測試附加信息等,可以通過@feature、@story、@step和@attach等裝飾器實現(xiàn)

實現(xiàn)的步驟

  • import allure
  • 功能上加@allure.feature(“功能名稱”)
  • 子功能上加@allure.story(“子功能名稱”)
  • 用例標(biāo)題@allure.title(“用例名稱”)
  • 用例描述@allure.description(“用例描述”)
  • 步驟上加@allure.step(“步驟細(xì)節(jié)”)
  • @allure.attach(“具體文本信息”),需要附加的信息,可以是數(shù)據(jù)、文本、圖片、視頻和網(wǎng)頁
  • 用例級別@allure.severity(級別)
  • 如果只測試登錄功能運行的時候,可以加限制過濾
# 注意這里--allure-features中間是-中線, 需要使用雙引號
#  不能過濾--allure-features下的--allure-stories
pytest 文件名 --allure-features="登錄模塊"
  • feature相當(dāng)于一個功能,一個大模塊,將case分類到某個feature中,報告中Behaviors(功能中展示),相當(dāng)于testsuite
  • story相當(dāng)于這個功能或者模塊下的不能場景,分支功能,屬于feature之下的結(jié)構(gòu),報告中features中展示,詳單與tescase
  • feature與story類似于父子關(guān)系
  • 2、allure特性-step
  • 測試過程中每個步驟,一般放在具體邏輯方法中
  • 可以放在關(guān)步驟中,在報告中顯示
  • 在app、web自動化測試中,建議每切換到一個新頁面就做一個step
  • 用法:
  • @allure.step():只能以裝飾器的形式放在類或者方法上面
  • with allure.step():可以放在測試用例方法里面,但是測試步驟代碼需要被該語句包含

3、allure特性-testcase

關(guān)聯(lián)測試用例,可以直接給測試用例的地址鏈接,一般用于關(guān)聯(lián)手工測試用例
實力代碼:

import pytest
import allure

@allure.feature('用戶登錄')
class TestLogin:

	@allure.story('登錄成功')
	def test_login_success(self):
		with allure.step('步驟1:打開應(yīng)用'):
			print('打開應(yīng)用')
		with allure.step('步驟2:進(jìn)入登錄頁面'):
			print('進(jìn)入登錄頁面')
		with allure.step('步驟3:輸入用戶名和密碼'):
			print('輸入用戶名和密碼')
		print('這是登錄成功測試用例')

	@allure.story('登錄失敗')
	def test_login_fail(self):
		print('這是登錄失敗測試用例')

	@allure.story('登錄失敗')
	@allure.title('用戶名缺失')
	def test_login_fail_a(self):
		print('這是登錄失敗測試用例')

	@allure.story('登錄失敗')
	@allure.testcase('https://www.baidu.com/', '關(guān)聯(lián)測試用例地址')
	@allure.title('密碼缺失')
	@allure.description('這是一個用例描述信息')
	def test_login_fail_b(self):
		with allure.step('點擊用戶名'):
			print('輸入用戶名')
		with allure.step('點擊密碼'):
			print('輸入密碼')
		print('點擊登錄')
		with allure.step('點擊登錄之后登錄失敗'):
			assert '1' == 1

按重要性級別 進(jìn)行一定范圍測試
通常測試用PO、冒煙測試、驗證上線測試。按照重要性級別來分別執(zhí)行
缺陷嚴(yán)重級別

1. Blocker級別——中斷缺陷
    客戶端程序無響應(yīng),無法執(zhí)行下一步操作。
2. Critical級別――臨界缺陷,包括:
    功能點缺失,客戶端爆頁。
3. Major級別——較嚴(yán)重缺陷,包括:
    功能點沒有滿足需求。
4. Normal級別――普通缺陷,包括:
    1. 數(shù)值計算錯誤
    2. JavaScript錯誤。
5. Minor級別———次要缺陷,包括:
    1. 界面錯誤與UI需求不符。
    2. 打印內(nèi)容、格式錯誤
    3. 程序不健壯,操作未給出明確提示。
6. Trivial級別——輕微缺陷,包括:
    1. 輔助說明描述不清楚
    2. 顯示格式不規(guī)范,數(shù)字,日期等格式。 
    3. 長時間操作未給用戶進(jìn)度提示
    4. 提示窗口文字未采用行業(yè)術(shù)語
    5. 可輸入?yún)^(qū)域和只讀區(qū)域沒有明顯的區(qū)分標(biāo)志
    6. 必輸項無提示,或者提示不規(guī)范。
7. Enhancement級別——測試建議、其他(非缺陷)
   1. 以客戶角度的易用性測試建議。
   2. 通過測試挖掘出來的潛在需求。

解決方法:

  • 通過附加pytest.mark標(biāo)記
  • 通過allure.feature,allure.story
  • 也可以通過allure.servity來附加標(biāo)記

步驟:
在方法,函數(shù)和類上面加:@allure.severity(allure.severity_level.TRIVIAL)
執(zhí)行時過濾:pytest -vs [文件名] --allure-severities normal, critical

前端自動化測試-截圖
前端自動化測試經(jīng)常需要附加圖片或html,在適當(dāng)?shù)牡胤?,適當(dāng)時機截圖
@allure.attach 實現(xiàn)不同類型附件,可以補充測試步驟或測試結(jié)果
使用方式:

  • 在測試報告附加網(wǎng)頁
allure.attach(body(內(nèi)容), name, attachment_type, extension)
allure.attach('<body>這是一段html</body>', 'html測試', attachment_type=allure.attachment_type.HTML)

在測試報告附加圖片

allure.attach.file(source, name, attachment_type, extension)
allure.attach.file('./123.jpg', name='這是一個圖片', attachment_type=allure.attachment_type.JPG)

示例代碼:

import allure

def test_attach_text():
	allure.attach('這是一個純文本', attachment_type=allure.attachment_type.TEXT)

def test_attach_html():
	allure.attach('<body>這是一段html</body>', 'html測試', attachment_type=allure.attachment_type.HTML)

def test_attach_phote():
	allure.attach.file('./123.jpg', name='這是一個圖片', attachment_type=allure.attachment_type.JPG)

def test_attach_video():
	allure.attach.file('./123.mp4',name='這是一個視頻',attachment_type=allure.attachment_type.MP4)
import allure
from selenium import webdriver
import time
import pytest


@allure.testcase('https://www.baidu.com/', '百度搜索功能')
@pytest.mark.parametrize('data',
                         ['allure', 'pytest', 'unittest'],
                         ids=['search allure', 'search pytest', 'search unittest']
                         )
def test_search(data):
	with allure.step('步驟1:打開瀏覽器輸入百度地址'):
		driver = webdriver.Chrome()
		driver.implicitly_wait(5)
		driver.get('https://www.baidu.com/')

	with allure.step(f'步驟2:在搜索框中輸入{data}, 并點擊百度一下'):
		driver.find_element_by_id('kw').send_keys(data)
		driver.find_element_by_id('su').click()
		time.sleep(2)

	with allure.step('步驟3: 截圖保存到項目中'):
		driver.save_screenshot(f'./result/{data}.jpg')
		allure.attach.file(f'./result/{data}.jpg', name=f'搜索{data}的截圖', attachment_type=allure.attachment_type.JPG)
		allure.attach(driver.page_source, f'搜索{data}的網(wǎng)頁內(nèi)容', allure.attachment_type.HTML)

	with allure.step('步驟4:關(guān)閉瀏覽器,退出'):
		driver.quit()

到此這篇關(guān)于pytest測試框架+allure超詳細(xì)教程的文章就介紹到這了,更多相關(guān)pytest allure測試框架內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • pandas的apply函數(shù)用法詳解

    pandas的apply函數(shù)用法詳解

    本文主要介紹了pandas的apply函數(shù)用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • 一文詳解NumPy數(shù)組迭代與合并

    一文詳解NumPy數(shù)組迭代與合并

    NumPy?數(shù)組迭代是訪問和處理數(shù)組元素的重要方法,它允許您逐個或成組地遍歷數(shù)組元素,NumPy?提供了多種函數(shù)來合并數(shù)組,用于將多個數(shù)組的內(nèi)容連接成一個新數(shù)組,本文給大家詳細(xì)介紹了NumPy數(shù)組迭代與合并,需要的朋友可以參考下
    2024-05-05
  • python 如何調(diào)用 dubbo 接口

    python 如何調(diào)用 dubbo 接口

    這篇文章主要介紹了python 如何調(diào)用 dubbo 接口,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-09-09
  • Python中的flask框架詳解

    Python中的flask框架詳解

    flask是一個基于Python開發(fā)并且依賴jinja2模板和Werkzeug?WSGI服務(wù)的一個微型框架,對于Werkzeug本質(zhì)是Socket服務(wù)端,其用于接收http請求并對請求進(jìn)行預(yù)處理,然后觸發(fā)Flask框架,本文給大家介紹Python中的flask框架,感興趣的朋友跟隨小編一起看看吧
    2022-02-02
  • Anaconda中Python虛擬環(huán)境的創(chuàng)建使用與刪除方法詳解

    Anaconda中Python虛擬環(huán)境的創(chuàng)建使用與刪除方法詳解

    這篇文章主要為大家介紹了在Anaconda環(huán)境下,創(chuàng)建、使用與刪除Python虛擬環(huán)境的方法,具有一定的借鑒價值,需要的小伙伴可以跟隨小編一起了解一下
    2023-08-08
  • django foreignkey(外鍵)的實現(xiàn)

    django foreignkey(外鍵)的實現(xiàn)

    這篇文章主要介紹了django foreignkey(外鍵)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python繪制的二項分布概率圖示例

    Python繪制的二項分布概率圖示例

    這篇文章主要介紹了Python繪制的二項分布概率圖,涉及Python基于numpy、math的數(shù)值運算及matplotlib圖形繪制相關(guān)操作技巧,需要的朋友可以參考下
    2018-08-08
  • python控制臺顯示時鐘的示例

    python控制臺顯示時鐘的示例

    這篇文章主要介紹了python控制臺顯示時鐘的示例,需要的朋友可以參考下
    2014-02-02
  • 10款最好的Web開發(fā)的 Python 框架

    10款最好的Web開發(fā)的 Python 框架

    這篇文章主要介紹了10款最好的Web開發(fā)的 Python 框架,總結(jié)的都是非常常用的而且評價都非常不錯的框架,需要的朋友可以參考下
    2015-03-03
  • django 框架實現(xiàn)的用戶注冊、登錄、退出功能示例

    django 框架實現(xiàn)的用戶注冊、登錄、退出功能示例

    這篇文章主要介紹了django 框架實現(xiàn)的用戶注冊、登錄、退出功能,結(jié)合實例形式詳細(xì)分析了Django框架用戶注冊、登陸、退出等功能具體實現(xiàn)方法及操作注意事項,需要的朋友可以參考下
    2019-11-11

最新評論