濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 自動(dòng)化測(cè)試Pytest單元測(cè)試框架的基本介紹

自動(dòng)化測(cè)試Pytest單元測(cè)試框架的基本介紹

熱門(mén)標(biāo)簽:宿遷星美防封電銷卡 湛江智能外呼系統(tǒng)廠家 長(zhǎng)沙高頻外呼系統(tǒng)原理是什么 百度地圖標(biāo)注沒(méi)有了 外呼并發(fā)線路 西藏房產(chǎn)智能外呼系統(tǒng)要多少錢(qián) 地圖標(biāo)注審核表 ai電話機(jī)器人哪里好 ai電銷機(jī)器人源碼

一、Pytest概念

Pytest 是 Python 的一種單元測(cè)試框架,與 Python 自帶的 unittest 測(cè)試框架類似,但是比 unittest 框架使用起來(lái)更簡(jiǎn)潔,效率更高。

二、Pytest特點(diǎn)

Pytest是一個(gè)非常成熟的Python測(cè)試框架,主要特點(diǎn)有以下幾點(diǎn):

  • 非常容易上手,入門(mén)簡(jiǎn)單,文檔豐富,文檔中有很多實(shí)例可以參考;
  • 能夠支持簡(jiǎn)單的單元測(cè)試和復(fù)雜的功能測(cè)試;
  • 支持參數(shù)化;
  • 執(zhí)行測(cè)試過(guò)程中可以將某些測(cè)試用例跳過(guò)(skip),或者對(duì)某些預(yù)期失敗的case標(biāo)記成失??;
  • 支持重復(fù)執(zhí)行(rerun)失敗的 case;
  • 支持運(yùn)行由 nose, unittest 編寫(xiě)的測(cè)試 case;
  • 可生成html 報(bào)告;
  • 方便jenkins持續(xù)集成;
  • 可支持執(zhí)行部分用例;
  • 具有很多第三方插件,并且可以自定義擴(kuò)展。

三、Pytest安裝

安裝pytest命令:

pip install pytest

查看pytest版本:

pytest --version

安裝生成測(cè)試結(jié)果的HTML報(bào)告pytest-html

pip install pytest-html

這里已經(jīng)安裝過(guò),所以輸出信息和第一次安裝不一樣。

四、Pycharm配置Pytest

pycharm依次選擇

File->Settings->Tools->Python Integrated Tools

配置用例腳本運(yùn)行模式。

菜單欄

點(diǎn)擊Edit Configurations。

依次點(diǎn)擊"+" --》 Python tests --》pytest

配置項(xiàng)目路徑

如下:

五、Pytest用例運(yùn)行規(guī)則

用Pytest寫(xiě)用例時(shí)候,一定要按照下面的規(guī)則去寫(xiě),否則不符合規(guī)則的測(cè)試用例是不會(huì)執(zhí)行的。

文件名以 test_*.py 文件或*_test.py;

以 test_ 開(kāi)頭的函數(shù);

以 Test 開(kāi)頭的類,不能包含 __init__ 方法;

以 test_ 開(kāi)頭的類里面的方法;

所有的包(package)必項(xiàng)要有__init__.py 文件。

六、 Pytest簡(jiǎn)單使用

環(huán)境都準(zhǔn)備好了,嘗試下使用pytest運(yùn)行用例。

新建py文件

寫(xiě)兩條測(cè)試用例

import pytest
def test_demo1():
    assert 3 == 3
def test_demo2():
    assert 3 == 5
if __name__ == '__main__':
    pytest.main()

運(yùn)行之后,結(jié)果如下:

Testing started at 12:37 ...
C:\Users\96984\Desktop\code\learn_pytest\venv\Scripts\python.exe "C:\ruanjian\pycharm2019.3\PyCharm 2019.3.1\plugins\python\helpers\pycharm\_jb_pytest_runner.py" --path C:/Users/96984/Desktop/code/learn_pytest/demo/demo_pytest.py
Launching pytest with arguments C:/Users/96984/Desktop/code/learn_pytest/demo/demo_pytest.py in C:\Users\96984\Desktop\code\learn_pytest\demo
============================= test session starts =============================
platform win32 -- Python 3.6.8, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 -- C:\Users\96984\Desktop\code\learn_pytest\venv\Scripts\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.8', 'Platform': 'Windows-10-10.0.18362-SP0', 'Packages': {'pytest': '5.4.3', 'py': '1.9.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '2.1.1', 'metadata': '1.10.0'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_77'}
rootdir: C:\Users\96984\Desktop\code\learn_pytest\demo
plugins: html-2.1.1, metadata-1.10.0
collecting ... collected 2 items
demo_pytest.py::test_demo1 PASSED                                        [ 50%]
demo_pytest.py::test_demo2 FAILED                                        [100%]
demo_pytest.py:8 (test_demo2)
def test_demo2():
>       assert 3 == 5
E       AssertionError
demo_pytest.py:10: AssertionError
================================== FAILURES ===================================
_________________________________ test_demo2 __________________________________
    def test_demo2():
>       assert 3 == 5
E       AssertionError
demo_pytest.py:10: AssertionError
=========================== short test summary info ===========================
FAILED demo_pytest.py::test_demo2 - AssertionError
========================= 1 failed, 1 passed in 0.05s =========================
Process finished with exit code 0
您可能感興趣的文章:
  • Python 測(cè)試框架unittest和pytest的優(yōu)劣
  • 詳解如何使用Pytest進(jìn)行自動(dòng)化測(cè)試
  • 詳解Pytest測(cè)試用例的執(zhí)行方法
  • pytest基本用法簡(jiǎn)介
  • Pytest 使用簡(jiǎn)介
  • Python自動(dòng)化測(cè)試框架pytest的詳解安裝與運(yùn)行

標(biāo)簽:海南 南平 漯河 林芝 盤(pán)錦 大同 普洱 寧夏

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《自動(dòng)化測(cè)試Pytest單元測(cè)試框架的基本介紹》,本文關(guān)鍵詞  自動(dòng)化,測(cè)試,Pytest,單元,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《自動(dòng)化測(cè)試Pytest單元測(cè)試框架的基本介紹》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于自動(dòng)化測(cè)試Pytest單元測(cè)試框架的基本介紹的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    百色市| 文水县| 丰台区| 嘉禾县| 湟中县| 容城县| 阳城县| 郁南县| 屏山县| 松潘县| 巴南区| 河南省| 板桥市| 石屏县| 彭水| 平昌县| 北宁市| 达州市| 鹤岗市| 宜阳县| 澄城县| 资源县| 道孚县| 贡嘎县| 思茅市| 彰武县| 阳曲县| 城步| 惠水县| 元谋县| 太和县| 法库县| 东丽区| 延庆县| 宾阳县| 邵阳县| 迁安市| 纳雍县| 南京市| 黔江区| 崇左市|