最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pytest測試框架+allure超詳細教程

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

1、測試識別和運行

文件識別:

  • 在給定的目錄中,搜索所有test_.py或者_test.py文件

用例識別:

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

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

在這里插入圖片描述

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

在這里插入圖片描述

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

在這里插入圖片描述

5、主函數(shù)運行方式
在運行文件中編寫主函數(shù),主函數(shù)啟動需要導入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()會自動讀取當前目錄下的所有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,:默認為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相關的命令行參數(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ù)用例設置的feature名稱過濾需要執(zhí)行的用例
  --allure-stories=STORIES_SET           <== 根據(jù)用例設置的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、查看測試報告

在線查看報告,直接打開默認瀏覽器展示當前報告

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

3、allure特性-testcase

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

import pytest
import allure

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

	@allure.story('登錄成功')
	def test_login_success(self):
		with allure.step('步驟1:打開應用'):
			print('打開應用')
		with allure.step('步驟2:進入登錄頁面'):
			print('進入登錄頁面')
		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/', '關聯(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

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

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

解決方法:

  • 通過附加pytest.mark標記
  • 通過allure.feature,allure.story
  • 也可以通過allure.servity來附加標記

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

前端自動化測試-截圖
前端自動化測試經(jīng)常需要附加圖片或html,在適當?shù)牡胤?,適當時機截圖
@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:關閉瀏覽器,退出'):
		driver.quit()

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

相關文章

  • Python單鏈表簡單實現(xiàn)代碼

    Python單鏈表簡單實現(xiàn)代碼

    這篇文章主要介紹了Python單鏈表簡單實現(xiàn)代碼,結(jié)合實例形式分析了Python單鏈表的具體定義與功能實現(xiàn)技巧,需要的朋友可以參考下
    2016-04-04
  • 使用PyQt4 設置TextEdit背景的方法

    使用PyQt4 設置TextEdit背景的方法

    今天小編就為大家分享一篇使用PyQt4 設置TextEdit背景的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • Pandas數(shù)據(jù)清洗函數(shù)總結(jié)

    Pandas數(shù)據(jù)清洗函數(shù)總結(jié)

    本文主要介紹了Pandas數(shù)據(jù)清洗函數(shù)總結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-01-01
  • Python中單引號、雙引號和三引號具體的用法及注意點

    Python中單引號、雙引號和三引號具體的用法及注意點

    這篇文章主要給大家介紹了關于Python中單引號、雙引號和三引號具體的用法及注意點的相關資料,Python中單引號、雙引號、三引號中使用常常困惑,想弄明白這三者相同點和不同點,需要的朋友可以參考下
    2023-07-07
  • JavaScript實現(xiàn)一維數(shù)組轉(zhuǎn)化為二維數(shù)組

    JavaScript實現(xiàn)一維數(shù)組轉(zhuǎn)化為二維數(shù)組

    下面小編就為大家分享一篇JavaScript實現(xiàn)一維數(shù)組轉(zhuǎn)化為二維數(shù)組,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • python的多元數(shù)據(jù)類型(上)

    python的多元數(shù)據(jù)類型(上)

    這篇文章主要為大家詳細介紹了python的多元數(shù)據(jù)類型,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Python鍵值互換的實現(xiàn)示例

    Python鍵值互換的實現(xiàn)示例

    Python鍵值互換是一種對Python字典類型中鍵值對進行反轉(zhuǎn)的技術,有時候,我們需要以值作為鍵,以鍵作為值來操作字典,這時候就需要用到鍵值互換的技術,本文主要介紹了Python鍵值互換的實現(xiàn)示例,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • Python?pygame項目實戰(zhàn)英雄動畫特效實現(xiàn)

    Python?pygame項目實戰(zhàn)英雄動畫特效實現(xiàn)

    這篇文章主要為大家介紹了Python?pygame項目實戰(zhàn)英雄動畫特效實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • Python實現(xiàn)一鍵整理百度云盤中重復無用文件

    Python實現(xiàn)一鍵整理百度云盤中重復無用文件

    有沒有頭疼過百度云盤都要塞滿了,可是又沒有工具能剔除大量重復無用的文件?這里教你一個用Python實現(xiàn)的簡單方法,通過整理目錄的方式來處理我們云盤中無用的文件吧
    2022-08-08
  • 教你在pycharm中使用tensorflow的方法

    教你在pycharm中使用tensorflow的方法

    當前使用的是anaconda的3.8版本,無法正常下載tensorflow包,需要構(gòu)建虛擬環(huán)境使用3.7及以下的解釋器才可以,如何解決這個問題呢,下面小編給大家?guī)砹巳绾卧趐ycharm中使用tensorflow,感興趣的朋友參考下吧
    2021-11-11

最新評論

胶南市| 靖宇县| 平果县| 铁岭市| 洞口县| 边坝县| 师宗县| 大理市| 灵台县| 友谊县| 方正县| 承德市| 洞口县| 深泽县| 岳阳县| 巴里| 高密市| 吉安县| 海晏县| 衡阳市| 荆门市| 渝中区| 綦江县| 高邑县| 固阳县| 济源市| 文昌市| 桂东县| 容城县| 齐齐哈尔市| 永修县| 获嘉县| 集贤县| 巴彦淖尔市| 龙口市| 县级市| 云南省| 长武县| 道孚县| 盖州市| 梅州市|