前言
用命令行方式調用用例是我們最常用的方式,這方面確實比java的TestNG框架要好用許多,至少不用寫xml文件,爲了提供定製化運行用例的方式,pytest提供了許多運行命令以供定製化運行某一類測試用例或者某個測試用例等;
pycharm裏命令行運行用例
在pycharm裏寫好了測試用例後如何運行呢?pycharm裏好像並沒有像eclipse裏提供TestNG用的插件一樣可以一鍵執行的方式,那麼我們可以使用命令行的方式來進行,如下圖所示爲一個用例文件:
代碼如下:
#-*- 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組")
如上所示添加了一個名爲testSimple
的工程,內添加了一些測試用例即Test_simple
;
想要運行用例時可以打開下方的Terminal窗口:
會自動切換到當前工程目錄下,而後即可使用pytest的命令了,如下對運行結果簡單做下說明:
終端中使用pytest
在終端中使用pytest也是和在pycharm中類似,如下以windows系統爲例:
先切換到用例所在工程或者目錄而後運行pytest即可,如下:
linux系統中也是同樣的使用方法,只是如果沒有爲pytest添加軟連接,則需要在pytest前面加上python
命令;
用例全部運行
全部運行時不需要添加任何後綴,只需要添加命令pytest
即可,此時打印的信息比較簡單:
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
如上圖所示,只顯示了用例時成功還是失敗,至於裏邊的log則沒有打印,那麼如果我們想要看運行詳細信息怎麼辦呢?可以加上-v
標籤,如下:
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>
如上圖會把詳細信息都打印出來
指定組別
如果用例中包含多個分組,想要只運行其中一個組,則使用-m "組名"
的方式,依然使用如上代碼,運行命令和結果如下:
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>
使用表達式指定某些用例-k
-k
選項允許我們設置表達式來運行某些用例,如下傳參就只運行了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%]
表達式的寫法有許多,可以用全稱如test_case1
這樣也可以去掉test_
,除了or
外也可以使用not
來指定那些用例不跑;
遇到失敗即停止運行-x
pytest的原本運行規則是每條用例均執行,不管是否有失敗,如果我們想在用例運行時遇到失敗即停止,則可以使用-x
,如下所示,第二條用例失敗後則不再運行第三條用例:
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>
指定運行某個測試py文件
指定運行某個py文件,只需要接上文件相對路徑即可:
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>
指定運行某個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>
指定運行某個方法:
寫法爲:py文件路徑::class名稱::method名稱
,範例如下:
E:\pyspace\testSimple>pytest -v testcase/Test_example.py::Test_example2::test_bbb
========================================================================================================================= 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.02 seconds =======================================================================================================================
E:\pyspace\testSimple>
如上幾種也可以組合使用;
其他
pytest還包含許多其他用法,具體用法可以使用pytest --help
來查看,如下: