欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pytest測(cè)試框架基本使用方法詳解

 更新時(shí)間:2020年11月25日 11:22:20   作者:-零  
這篇文章主要介紹了Pytest測(cè)試框架基本使用方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

pytest介紹

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

1、簡(jiǎn)單靈活,容易上手,文檔豐富;

2、支持參數(shù)化,可以細(xì)粒度地控制要測(cè)試的測(cè)試用例;

3、能夠支持簡(jiǎn)單的單元測(cè)試和復(fù)雜的功能測(cè)試,還可以用來(lái)做selenium/appnium等自動(dòng)化測(cè)試、接口自動(dòng)化測(cè)試(pytest+requests);

4、pytest具有很多第三方插件,并且可以自定義擴(kuò)展

  • 如pytest-selenium(集成selenium)、
  • pytest-html(完美html測(cè)試報(bào)告生成)、
  • pytest-rerunfailures(失敗case重復(fù)執(zhí)行)、
  • pytest-xdist(多CPU分發(fā))、
  • pytest--ordering(控制測(cè)試運(yùn)行的順序)

5、測(cè)試用例的skip和xfail處理;

6、可以很好的和CI工具結(jié)合,例如jenkins

編寫規(guī)則:

  • 測(cè)試文件以test_開(kāi)頭(以_test結(jié)尾也可以)
  • 測(cè)試類以Test開(kāi)頭,并且不能帶有 init 方法
  • 測(cè)試函數(shù)以test_開(kāi)頭

斷言使用基本的assert即可

快速示例

test_pyexample.py

import pytest

class TestClass:
    def test_one(self):
      x = "this"
      assert 'h' in x

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

    def test_three(self):
      a = "hello"
      b = "hello world"
      assert a in b

通過(guò)命令行運(yùn)行:

1、cd 到代碼所在的目錄,執(zhí)行命令:py.test test_pyexample.py

2、安裝pytest-sugar插件可以看到進(jìn)度條

Pycharm配置運(yùn)行:

1.file->Setting->Tools->Python Integrated Tools->項(xiàng)目名稱->Default test runner->選擇py.test

import pytest

class TestClass:
    def test_one(self):
      x = "this"
      assert 'h' in x

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

    def test_three(self):
      a = "hello"
      b = "hello world"
      assert a in b

if __name__ == "__main__":
  pytest.main('-q test_class.py')

Console常用參數(shù)介紹:

  • -v 用于顯示每個(gè)測(cè)試函數(shù)的執(zhí)行結(jié)果
  • -q 只顯示整體測(cè)試結(jié)果
  • -s 用于顯示測(cè)試函數(shù)中print()函數(shù)輸出
  • -x, --exitfirst, exit instantly on first error or failed test
  • -m 只運(yùn)行帶有裝飾器配置的測(cè)試用例
  • -h 幫助
py.test # run all tests below current dir
py.test test_mod.py # run tests in module file test_mod.py
py.test somepath # run all tests below somepath like ./tests/
py.test -k stringexpr # only run tests with names that match the
# the "string expression", e.g. "MyClass and not method"
# will select TestMyClass.test_something
# but not TestMyClass.test_method_simple
py.test test_mod.py::test_func # only run tests that match the "node ID",
# e.g "test_mod.py::test_func" will be selected
# only run test_func in test_mod.py

pytest參數(shù)化

使用裝飾器:@pytest.mark.parametrize()

單個(gè)參數(shù):

import pytest
import random
@pytest.mark.parametrize('x',[(1),(2),(6)])
def test_add(x):
  print(x)
  assert x==random.randrange(1,7)

多個(gè)參數(shù):

import pytest
@pytest.mark.parametrize('x,y',[
  (1+2,3),
  (2-0,1),
  (6*2,12),
  (10*2,3),
  ("test","test"),
])
def test_add(x,y):  #必須與上面保持一致,只能用x,y不能用其他字母
  assert x==y

控制測(cè)試運(yùn)行順序

安裝pytest-ordering

pip install pytest-ordering

借助于裝飾器@pytest.mark.run(order=1)控制測(cè)試運(yùn)行的順序

import pytest
import time
value=0
@pytest.mark.run(order=2) #后執(zhí)行order=2
def test_add2():
  print("I am 2")
  time.sleep(2)
  assert value==10
@pytest.mark.run(order=1)  #先執(zhí)行order=1
def test_add():
  print("I am add")
  global value
  value=10
  assert value==10

運(yùn)行后生成測(cè)試報(bào)告(htmlReport)

安裝pytest-html:

pip install -U pytest-html

如何使用:

py.test test_pyexample.py --html=report.html

更詳細(xì)的測(cè)試報(bào)告

安裝 pytest-cov:

pip install pytest-cov

如何使用

py.test --cov-report=html --cov=./ test_code_target_dir
Console參數(shù)介紹
--cov=[path], measure coverage for filesystem path (multi-allowed), 指定被測(cè)試對(duì)象,用于計(jì)算測(cè)試覆蓋率
--cov-report=type, type of report to generate: term, term-missing, annotate, html, xml (multi-allowed), 測(cè)試報(bào)告的類型
--cov-config=path, config file for coverage, default: .coveragerc, coverage配置文件
--no-cov-on-fail, do not report coverage if test run fails, default: False,如果測(cè)試失敗,不生成測(cè)試報(bào)告
--cov-fail-under=MIN, Fail if the total coverage is less than MIN. 如果測(cè)試覆蓋率低于MIN,則認(rèn)為失敗

多進(jìn)程運(yùn)行

安裝pytest-xdist:

pip install -U pytest-xdist

如何使用:

py.test test_pyexample.py -n NUM

其中NUM填寫并發(fā)的進(jìn)程數(shù)。

重新運(yùn)行失敗的用例

安裝pytest- rerunfailures:

import random
def add(x,y):
  return x+y
def test_add():
  random_value=random.randint(2,7)
  print('random_value:'+str(random_value))
  assert add(1,3)==random_value

如何使用:

命令:pytest --reruns 重試次數(shù)

  比如:pytest --reruns 3  表示:運(yùn)行失敗的用例可以重新運(yùn)行3次

命令:pytest --reruns 重試次數(shù) --reruns-delay 次數(shù)之間的延時(shí)設(shè)置(單位:秒)

  比如:pytest --reruns 3 --reruns-delay 5  表示:(譯:瑞軟四、地類)運(yùn)行失敗的用例可以重新運(yùn)行3次,第一次和第二次的間隔時(shí)間為5秒鐘

另外也可以通過(guò)裝飾器的方式配置:

@pytest.mark.flaky(reruns=3, reruns_delay=5)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論