pytest基本使用

setup/teardown函數

函數級別:

import pytest

class Test002:
    def setup(self):
        print("---------start")
        pass

    def test_001(self):
        print("----------test001")

    def test_002(self):
        print("----------test002")


    def teardown(self):
        print("----------stop")
        pass

if __name__ == '__main__':
    pytest.main(['-s', 'test002.py'])

結果:
---------start
----------test001
.----------stop
---------start
----------test002
.----------stop
每執行一個函數就會執行一次setup和teardown

類級別:

import pytest

class Test002:
    def setup_class(self):
        print("---------start")
        pass

    def test_001(self):
        print("----------test001")

    def test_002(self):
        print("----------test002")


    def teardown_class(self):
        print("----------stop")
        pass

if __name__ == '__main__':
    pytest.main(['-s', 'test002.py'])

結果:
---------start
----------test001
.----------test002
.----------stop
只執行一次setup和teardown

配置文件

主配置文件:pytest.ini
格式爲:

[pytest]

addopts = -s --html=./report/report.html --reruns 3
testpaths = ./scripts_test
python_files = test_*.py
python_classes = Test_*
python_functions = test_*

addopts = :命令行參數,使用命令行執行文件時自動添加
testpaths =:測試路徑
python_files =:搜索文件名
python_classes =:搜索測試類名
python_functions =:搜索測試方法名

addopts = -rsxX
–rsxX 表示pytest報告所有測試用例被跳過、預計失敗、預計失敗但實際被通過的原因

使用pytest --help指令可以查看pytest.ini的設置選項
[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:

  markers (linelist)       markers for test functions
  empty_parameter_set_mark (string) default marker for empty parametersets
  norecursedirs (args)     directory patterns to avoid for recursion
  testpaths (args)         directories to search for tests when no files or dire
 
  console_output_style (string) console output: classic or with additional progr
 
  usefixtures (args)       list of default fixtures to be used with this project
 
  python_files (args)      glob-style file patterns for Python test module disco
 
  python_classes (args)    prefixes or glob names for Python test class discover
 
  python_functions (args)  prefixes or glob names for Python test function and m
 
  xfail_strict (bool)      default for the strict parameter of 
  addopts (args)           extra command line options
  minversion (string)      minimally required pytest version

此處運行時遇到報錯:UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xb0 in position 26: illegal multibyte sequence
解決:將pytest.ini文件編碼格式改爲GBK,且文件中不能有中文,否則編譯不通過

其他配置文件:
conftest.py :測試用例的一些fixture配置
init.py :識別該文件夾爲python的package包
tox.ini :與pytest.ini類似,用tox工具時候纔有用
setup.cfg :也是ini格式文件,影響setup.py的行爲

插件

生成測試報告:pytest-html
安裝該插件後,直接在pytest.ini文件中addopts = 添加參數“- -html=./report/report.html”
失敗重試:pytest-rerunfailures
安裝該插件後,直接在pytest.ini文件中addopts = 添加參數–reruns n (n:爲重試的次數)
控制被測試函數執行的順序:pytest-ordering
安裝該插件後,通過修飾符寫在函數上一行@pytest.mark.run(order=3)
使用方法:

import pytest

@pytest.mark.run(order=3)
def test_01():
    print("--------test01")
    assert 1

@pytest.mark.run(order=-1)
def test02():
    print("---------test02")
    assert 0

@pytest.mark.run(order=1)
def test_03():
    print("----------test03")

@pytest.mark.run(order=2)
def test04():
    print("------------test04")

if __name__ == '__main__':
    pytest.main(['-s', 'test001.py'])

1、標記於被測試函數, @pytest.mark.run(order=x)
2、根據order傳入的參數來解決運行順序
3、order值全爲正數或負數時,值越小優先級越高
4、正負數同時存在時,正數優先極高
5、已標記和未標記的函數,已標記的函數優先極高

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章