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

Python教程之pytest命令行方式運(yùn)行用例

 更新時(shí)間:2021年12月26日 11:27:27   作者:df0128  
pytest有豐富的命令行選項(xiàng),以滿足不同的需要,下面這篇文章主要給大家介紹了關(guān)于Python教程之pytest命令行方式運(yùn)行的相關(guān)資料,需要的朋友可以參考下

前言

用命令行方式調(diào)用用例是我們最常用的方式,這方面確實(shí)比java的TestNG框架要好用許多,至少不用寫xml文件,為了提供定制化運(yùn)行用例的方式,pytest提供了許多運(yùn)行命令以供定制化運(yùn)行某一類測(cè)試用例或者某個(gè)測(cè)試用例等;

pycharm里命令行運(yùn)行用例

在pycharm里寫好了測(cè)試用例后如何運(yùn)行呢?pycharm里好像并沒有像eclipse里提供TestNG用的插件一樣可以一鍵執(zhí)行的方式,那么我們可以使用命令行的方式來進(jìn)行,如下圖所示為一個(gè)用例文件:

代碼如下:

#-*- coding: utf-8 -*-
import pytest


class Test_simple():

    @pytest.mark.test
    def test_case1(self):
        print("testCase1")
        tof = True
        assert tof

    @pytest.mark.normal
    @pytest.mark.test
    def test_case2(self):
        print("testCase2")
        tof = False
        assert tof

    def test_case3(self):
        print("testCase3")
        assert True

    @pytest.mark.test
    def setup_class(self):
        print("用于test組")

    @pytest.mark.normal
    def setup_class(self):
        print("用于normal組")

如上所示添加了一個(gè)名為testSimple的工程,內(nèi)添加了一些測(cè)試用例即Test_simple;

想要運(yùn)行用例時(shí)可以打開下方的Terminal窗口:

會(huì)自動(dòng)切換到當(dāng)前工程目錄下,而后即可使用pytest的命令了,如下對(duì)運(yùn)行結(jié)果簡(jiǎn)單做下說明:

終端中使用pytest

在終端中使用pytest也是和在pycharm中類似,如下以windows系統(tǒng)為例:

先切換到用例所在工程或者目錄而后運(yùn)行pytest即可,如下:

linux系統(tǒng)中也是同樣的使用方法,只是如果沒有為pytest添加軟連接,則需要在pytest前面加上python命令;

用例全部運(yùn)行

全部運(yùn)行時(shí)不需要添加任何后綴,只需要添加命令pytest即可,此時(shí)打印的信息比較簡(jiǎn)單:

E:\pyspace\testSimple>pytest
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items                                                                                                                                                                                                                                                       

testcase\Test_simple.py .F.                                                                                                                                                                                                                                       [100%]

=============================================================================================================================== FAILURES ===============================================================================================================================
________________________________________________________________________________________________________________________ Test_simple.test_case2 ________________________________________________________________________________________________________________________

self = <testcase.Test_simple.Test_simple object at 0x00000000038508D0>

    @pytest.mark.normal
    @pytest.mark.test
    def test_case2(self):
        print("testCase2")
        tof = False
>       assert tof
E       assert False

testcase\Test_simple.py:18: AssertionError
------------------------------------------------------------------------------------------------------------------------- Captured stdout call -------------------------------------------------------------------------------------------------------------------------
testCase2
================================================================================================================== 1 failed, 2 passed in 0.08 seconds ==================================================================================================================

E:\pyspace\testSimple>

打印詳情-v

如上圖所示,只顯示了用例時(shí)成功還是失敗,至于里邊的log則沒有打印,那么如果我們想要看運(yùn)行詳細(xì)信息怎么辦呢?可以加上-v標(biāo)簽,如下:

E:\pyspace\testSimple>pytest -v
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- e:\software\python\python3.7\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.1', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items                                                                                                                                                                                                                                                       

testcase/Test_simple.py::Test_simple::test_case1 PASSED                                                                                                                                                                                                           [ 33%]
testcase/Test_simple.py::Test_simple::test_case2 FAILED                                                                                                                                                                                                           [ 66%]
testcase/Test_simple.py::Test_simple::test_case3 PASSED                                                                                                                                                                                                           [100%]

=============================================================================================================================== FAILURES ===============================================================================================================================
________________________________________________________________________________________________________________________ Test_simple.test_case2 ________________________________________________________________________________________________________________________

self = <testcase.Test_simple.Test_simple object at 0x000000000382EDA0>

    @pytest.mark.normal
    @pytest.mark.test
    def test_case2(self):
        print("testCase2")
        tof = False
>       assert tof
E       assert False

testcase\Test_simple.py:18: AssertionError
------------------------------------------------------------------------------------------------------------------------- Captured stdout call -------------------------------------------------------------------------------------------------------------------------
testCase2
================================================================================================================== 1 failed, 2 passed in 0.08 seconds ==================================================================================================================

E:\pyspace\testSimple>

如上圖會(huì)把詳細(xì)信息都打印出來

指定組別

如果用例中包含多個(gè)分組,想要只運(yùn)行其中一個(gè)組,則使用-m "組名"的方式,依然使用如上代碼,運(yùn)行命令和結(jié)果如下:

E:\pyspace\testSimple>pytest -s -m "normal"
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items / 2 deselected / 1 selected                                                                                                                                                                                                                           

testcase\Test_simple.py 用于normal組
testCase2
F

=============================================================================================================================== FAILURES ===============================================================================================================================
________________________________________________________________________________________________________________________ Test_simple.test_case2 ________________________________________________________________________________________________________________________

self = <testcase.Test_simple.Test_simple object at 0x00000000036D27F0>

    @pytest.mark.normal
    @pytest.mark.test
    def test_case2(self):
        print("testCase2")
        tof = False
>       assert tof
E       assert False

testcase\Test_simple.py:18: AssertionError
================================================================================================================ 1 failed, 2 deselected in 0.07 seconds ================================================================================================================

E:\pyspace\testSimple>

使用表達(dá)式指定某些用例-k

-k選項(xiàng)允許我們?cè)O(shè)置表達(dá)式來運(yùn)行某些用例,如下傳參就只運(yùn)行了test_case1和test_case2

E:\pyspace\testSimple>pytest -v -k "case1 or case2"
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- e:\software\python\python3.7\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.1', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items / 1 deselected / 2 selected                                                                                                                                                                                                                           

testcase/Test_simple.py::Test_simple::test_case1 PASSED                                                                                                                                                                                                           [ 50%]
testcase/Test_simple.py::Test_simple::test_case2 FAILED                                                                                                                                                                                                           [100%]

表達(dá)式的寫法有許多,可以用全稱如test_case1這樣也可以去掉test_,除了or外也可以使用not來指定那些用例不跑;

遇到失敗即停止運(yùn)行-x

pytest的原本運(yùn)行規(guī)則是每條用例均執(zhí)行,不管是否有失敗,如果我們想在用例運(yùn)行時(shí)遇到失敗即停止,則可以使用-x,如下所示,第二條用例失敗后則不再運(yùn)行第三條用例:

E:\pyspace\testSimple>pytest -v -x
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- e:\software\python\python3.7\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.1', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 3 items                                                                                                                                                                                                                                                       

testcase/Test_simple.py::Test_simple::test_case1 PASSED                                                                                                                                                                                                           [ 33%]
testcase/Test_simple.py::Test_simple::test_case2 FAILED                                                                                                                                                                                                           [ 66%]

=============================================================================================================================== FAILURES ===============================================================================================================================
________________________________________________________________________________________________________________________ Test_simple.test_case2 ________________________________________________________________________________________________________________________

self = <testcase.Test_simple.Test_simple object at 0x00000000037A9B00>

    @pytest.mark.normal
    @pytest.mark.test
    def test_case2(self):
        print("testCase2")
        tof = False
>       assert tof
E       assert False

testcase\Test_simple.py:18: AssertionError
------------------------------------------------------------------------------------------------------------------------- Captured stdout call -------------------------------------------------------------------------------------------------------------------------
testCase2
================================================================================================================== 1 failed, 1 passed in 0.08 seconds ==================================================================================================================

E:\pyspace\testSimple>

指定運(yùn)行某個(gè)測(cè)試py文件

指定運(yùn)行某個(gè)py文件,只需要接上文件相對(duì)路徑即可:

E:\pyspace\testSimple>pytest -v testcase/Test_example.py
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- e:\software\python\python3.7\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.1', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 1 item                                                                                                                                                                                                                                                        

testcase/Test_example.py::Test_example::test_aaa PASSED                                                                                                                                                                                                           [100%]

======================================================================================================================= 1 passed in 0.02 seconds =======================================================================================================================

E:\pyspace\testSimple>

指定運(yùn)行某個(gè)class

寫法為:py文件路徑::class名稱,范例如下:

E:\pyspace\testSimple>pytest -v testcase/Test_example.py::Test_example2
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- e:\software\python\python3.7\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.1', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 1 item                                                                                                                                                                                                                                                        

testcase/Test_example.py::Test_example2::test_bbb PASSED                                                                                                                                                                                                          [100%]

======================================================================================================================= 1 passed in 0.08 seconds =======================================================================================================================

E:\pyspace\testSimple>

指定運(yùn)行某個(gè)方法:

寫法為:py文件路徑::class名稱::method名稱,范例如下:

E:\pyspace\testSimple>pytest -v testcase/Test_example.py::Test_example2
========================================================================================================================= test session starts ==========================================================================================================================
platform win32 -- Python 3.7.1, pytest-4.4.1, py-1.8.0, pluggy-0.11.0 -- e:\software\python\python3.7\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.1', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'pytest': '4.4.1', 'py': '1.8.0', 'pluggy': '0.11.0'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-pytest': '2.6.3'}, 'JAVA_HOME': 'D:\\project\\jdk1.8'}
rootdir: E:\pyspace\testSimple
plugins: metadata-1.8.0, html-1.20.0, allure-pytest-2.6.3
collected 1 item                                                                                                                                                                                                                                                        

testcase/Test_example.py::Test_example2::test_bbb PASSED                                                                                                                                                                                                          [100%]

======================================================================================================================= 1 passed in 0.08 seconds =======================================================================================================================

E:\pyspace\testSimple>

如上幾種也可以組合使用;

其他

pytest還包含許多其他用法,具體用法可以使用pytest --help來查看,如下:

總結(jié)

到此這篇關(guān)于Python教程之pytest命令行方式運(yùn)行的文章就介紹到這了,更多相關(guān)pytest命令行方式運(yùn)行內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解決pip install psycopg2出錯(cuò)問題

    解決pip install psycopg2出錯(cuò)問題

    這篇文章主要介紹了解決pip install psycopg2出錯(cuò)問題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Python調(diào)用C語(yǔ)言的實(shí)現(xiàn)

    Python調(diào)用C語(yǔ)言的實(shí)現(xiàn)

    這篇文章主要介紹了Python調(diào)用C語(yǔ)言的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • python MysqlDb模塊安裝及其使用詳解

    python MysqlDb模塊安裝及其使用詳解

    本篇文章主要介紹了python MysqlDb模塊安裝及其使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-02-02
  • 關(guān)于Python中的空值問題及解決

    關(guān)于Python中的空值問題及解決

    這篇文章主要介紹了關(guān)于Python中的空值問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Python 實(shí)現(xiàn)文件打包、上傳與校驗(yàn)的方法

    Python 實(shí)現(xiàn)文件打包、上傳與校驗(yàn)的方法

    今天小編就為大家分享一篇Python 實(shí)現(xiàn)文件打包、上傳與校驗(yàn)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • Python 在函數(shù)上添加包裝器

    Python 在函數(shù)上添加包裝器

    這篇文章主要介紹了Python 如何在函數(shù)上添加包裝器,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • python中匿名函數(shù)的應(yīng)用方法

    python中匿名函數(shù)的應(yīng)用方法

    這篇文章主要介紹了python中匿名函數(shù)的應(yīng)用方法,匿名函數(shù)是無需使用def定義的函數(shù),只需使用關(guān)鍵字lambda進(jìn)行聲明,且只可使用一次,只有一個(gè)返回值,需要的朋友可以參考下
    2023-07-07
  • 深入理解Python中的元類(metaclass)

    深入理解Python中的元類(metaclass)

    這篇文章主要介紹了深入理解Python中的元類(metaclass),本文是一篇相當(dāng)精彩的外文翻譯,講解了類也是對(duì)象、動(dòng)態(tài)地創(chuàng)建類、到底什么是元類,需要的朋友可以參考下
    2015-02-02
  • Python圖像運(yùn)算之圖像灰度線性變換詳解

    Python圖像運(yùn)算之圖像灰度線性變換詳解

    這篇文章將詳細(xì)講解圖像灰度線性變換,包括灰度上移、對(duì)比度增強(qiáng)、對(duì)比度減弱和灰度反色變換。文中的示例代碼講解詳細(xì),需要的可以參考一下
    2022-03-03
  • 說說如何遍歷Python列表的方法示例

    說說如何遍歷Python列表的方法示例

    這篇文章主要介紹了如何遍歷Python列表的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02

最新評(píng)論

宜兰县| 共和县| 如东县| 红原县| 米泉市| 铅山县| 涡阳县| 富裕县| 宁化县| 安乡县| 于都县| 怀安县| 寻甸| 弥渡县| 会泽县| 兰坪| 焦作市| 新乡县| 大厂| 汉寿县| 巢湖市| 大埔区| 凤庆县| 安国市| 从化市| 枣强县| 漯河市| 盐边县| 娄底市| 安顺市| 黔江区| 老河口市| 卓资县| 葫芦岛市| 虹口区| 微博| 南平市| 克山县| 郸城县| 石狮市| 延庆县|