Chapter 1 Installation and Getting Started

安裝 pytest

  1. 在命令行中運行以下命令:
    pip install -U pytest
  2. 檢查當(dāng)前安裝的版本:
    pytest --version

創(chuàng)建你的第一個測試

創(chuàng)建一個簡單的只有四行的測試方法

# content of test_sample.py
def func(x):
  return x + 1

def test_answer():
  assert func(3) == 5

就是這樣. 現(xiàn)在你可以執(zhí)行這個測試方法了.

$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
rootdir: $REGENDOC_TMPDIR, inifile:
collected 1 item
test_sample.py F [100%]
================================= FAILURES =================================
_______________________________ test_answer ________________________________
def test_answer():
> assert func(3) == 5
E assert 4 == 5
E + where 4 = func(3)
test_sample.py:5: AssertionError
========================= 1 failed in 0.12 seconds =========================

這里測試返回了一個錯誤報告, 因為func(3) 不能返回 5.

Note: 你可以使用`assert`聲明驗證測試預(yù)期. pytest的高級斷言內(nèi)省將會

智能地報告assert表達(dá)式的中間值,這樣您就可以避免JUnit遺留的許多名稱
方法

運行多個測試

pytest將會運行當(dāng)前文件夾及其子文件夾中所有 test_.py* 及*_test.py格式的測試. 更普遍的, 請參閱 standard test discovery rules.

斷言引發(fā)了某個異常

斷言一些代碼引發(fā)異常通過使用raise這一助手

# content of test_sysexit.py
import pytest

def f():
  raise SystemExit(1)

def test_mytest():
  with pytest.raises(SystemExit):
    f()

通過安靜報告模式執(zhí)行測試功能

$ pytest -q test_sysexit.py
.                                                                           [100%]
1 passed in 0.12 seconds

在一個類中運行多個測試用例

你也許想要將一次開發(fā)的多個測試用例放到一個類里. pytest使創(chuàng)建一個類來控制超過一個測試用例變得很簡單:

# content of test_class.py
class TestClass(object):
  def test_one(self):
    x = 'this'
    assert 'h' in x

  def test_two(self):
    x = 'hello'
    assert hasattr(x, 'check')

pytest 遵循Python測試發(fā)現(xiàn)約定來發(fā)現(xiàn)所有測試用例, 因此它會找到所有test_開頭的方法. 這里不需要子類化任何東西. 我們能夠簡單的通過傳遞模塊文件名來運行模塊:

$ pytest -q test_class.py
.F [100%]
================================= FAILURES =================================
____________________________ TestClass.test_two ____________________________
self = <test_class.TestClass object at 0xdeadbeef>
def test_two(self):
x = "hello"
> assert hasattr(x, 'check')
E AssertionError: assert False
E + where False = hasattr('hello', 'check')
test_class.py:8: AssertionError
1 failed, 1 passed in 0.12 seconds

第一個用例是'Passed', 第二個是'Failed'. 你能夠很容易的看到斷言中的中間值來幫助你理解失敗的原因.

為功能測試請求一個唯一的臨時目錄

# content of test_tmpdir.py
def test_needsfiles(tmpdir):
  print(tmpdir)
  assert 0

在測試函數(shù)簽名中列出名稱tmpdir, pytest將查找并調(diào)用一個fixture工廠來創(chuàng)建
執(zhí)行測試函數(shù)調(diào)用之前的資源.Pytest 創(chuàng)建一個唯一的每個測試調(diào)用的臨時目錄:

$ pytest -q test_tmpdir.py
F [100%]
================================= FAILURES =================================
_____________________________ test_needsfiles ______________________________
tmpdir = local('PYTEST_TMPDIR/test_needsfiles0')
def test_needsfiles(tmpdir):
print (tmpdir)
> assert 0
E assert 0
test_tmpdir.py:3: AssertionError
--------------------------- Captured stdout call ---------------------------
PYTEST_TMPDIR/test_needsfiles0
1 failed in 0.12 seconds

更多關(guān)于臨時文件的處理請參閱Temporary directories and files

pytest --fixtures # 顯示內(nèi)置和自定義裝置
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評論 19 139
  • feisky云計算、虛擬化與Linux技術(shù)筆記posts - 1014, comments - 298, trac...
    不排版閱讀 4,354評論 0 5
  • 1.特殊字符過濾校驗 判斷是否有特殊字符(排除不需要校驗的字符),如果有則返回true。 2.判斷元素在一個數(shù)組中...
    丁白一閱讀 575評論 0 0
  • 在職場上,不少人都會遇到以前就特別鐵的哥們作為上司,那么如何與鐵哥們的上司相處?平時應(yīng)付都認(rèn)識的客戶時應(yīng)該如何拿捏...
    一段金子閱讀 742評論 0 0
  • 總是擔(dān)心自己考不上,因為真的很想讀研。但是還是要和親戚好友表現(xiàn)的我能接受各種結(jié)果。誒,真心折磨啊,陜師大,我就想讀...
    酷到天荒地老閱讀 279評論 0 0

友情鏈接更多精彩內(nèi)容