1. 程式人生 > 其它 >Pytest相關元件、相關配置檔案介紹(二)

Pytest相關元件、相關配置檔案介紹(二)

pytest.ini配置檔案(作用:用來修改配置pytest預設的執行規則)

  1. pytest.ini放在專案根目錄,檔名字固定
  2. 常用配置介紹(⚠️不能出現中文註釋)
#coding=utf-8

[pytest]

filterwarnings =
   error
   ignore::UserWarning
   ignore:function ham\(\) is deprecated:DeprecationWarning

addopts = -v -s
          --strict
          -m "level0 or level1"
          --cache-clear

norecursedird = .svn -build tmp* .git .idea .pytest_cache

python_files = test_*.py
python_functions = test_run

markers =
    level0 : smoketest
    level1 : systemtest
  1. 配置引數含義
  • filterwarnings:消除pytest執行警告(從版本開始3.1,pytest現在會在測試執行期間自動捕獲警告並在會話結束時顯示它們)
  • addopts: 更改預設命令列選項,當我們用命令列執行時,需要輸入多個引數,可在這裡配置(比如:pytest -s test_001.py 配置後只需要 pytest test_001.py)
  • norecursedird: pytest收集用例排除的檔案
  • python_files: 用例收集規則
  • python_classes/python_functions: 類/函式收集規則
  • markers: 指定mark標籤

Demo(採用上面ini檔案)

import pytest


class TestCase001:
    def sum(self, x):
        return x + 3

    def test_001(self):
        assert self.sum(1) == 3

    def test_002(self):
        assert self.sum(2) == 5

    @pytest.mark.level0
    def test_run(self):
        assert sum(5) == 7

=================================== FAILURES ===================================
_____________________________ TestCase001.test_run _____________________________

self = <TestCase.Demo.test_demo_001.TestCase001 object at 0x13be5a3c8>

    @pytest.mark.level0
    def test_run(self):
>       assert sum(5) == 7
E       TypeError: 'int' object is not iterable

test_demo_001.py:16: TypeError
=========================== short test summary info ============================
FAILED test_demo_001.py::TestCase001::test_run - TypeError: 'int' object is n...
============================== 1 failed in 0.09s ===============================
Process finished with exit code 0

這裡只運行了test_run(在ini中我們指定了python_functions和執行的mark)