Skip to main content

Provide the pytest with the ability to collect use cases based on rules in text files

Project description

pytest-choose

English | 简体中文

为pytest提供基于文本文件收集用例的能力

v0.1.0升级说明:

  • v0.0.x版本升级至v0.1.0版本时,请将原--fc-path参数更改为--fc-allow-path
  • 升级后规则文件不强制要求完全包含class function块,按需编写即可

1.安装

pip install pytest-choose

2.参数说明

Parameter Description
--fc 默认'off',关闭文件选择。可选项'off', 'on'
--fc-coding 文件编码, 默认 'utf-8'
--fc-allow-path 白名单文件路径, 支持多次传入以应用多个规则文件
--fc-block-path 黑名单文件路径, 支持多次传入以应用多个规则文件

3.过滤文件模板

3.1.JSON

{
  "@说明": [
    "@注释以‘@’符开头声明"
  ],
  "class": [
    "@下方填写需要执行的测试类",
    "TestClassName"
  ],
  "function": [
    "@下方填写需要执行的测试方法",
    "test_function_name"
  ]
}

4.示例

创建测试文件:

# test_demo.py
class TestDemo:
    def test_demo_1(self):
        print(1)

    def test_demo_2(self):
        print(2)

def test_demo_3():
    print(3)

def test_demo_4():
    print(4)

def test_demo_5():
    print(5)

def test_demo_6():
    print(6)

def test_demo_7():
    print(7)

4.1.基本使用

创建文件choose.json,用于选择用例:

{
  "class": [
    "TestDemo"
  ],
  "function": [
    "test_demo_2",
    "test_demo_3",
    "test_demo_4",
    "test_demo_5"
  ]
}

执行测试:

pytest --fc="on" --fc-allow-path="./choose.json" --fc-coding="utf-8"

测试结果如下:

======================= test session starts =======================
platform win32 -- Python 3.9.6, pytest-7.4.3, pluggy-1.3.0
rootdir: D:\Project\PytestDev\pytest-choose
plugins: choose-0.1.0
collecting ... 

[pytest-choose] <Allow list>: choose.json -> Successful use of rules
[pytest-choose] Filter 5 cases and collect 2 cases
collected 7 items

cases\test_choose.py ..                            [100%] 
======================== 2 passed in 0.03s ======================== 

4.2.黑名单过滤

创建文件filter.json,用于过滤用例:

{
  "function": [
    "test_demo_3",
    "test_demo_4",
    "test_demo_5"
  ]
}

执行测试:

pytest --fc="on" --fc-block-path="./filter.json" --fc-coding="utf-8"

测试结果如下:

======================= test session starts =======================
platform win32 -- Python 3.9.6, pytest-7.4.3, pluggy-1.3.0
rootdir: D:\Project\PytestDev\pytest-choose
plugins: choose-0.1.0
collecting ... 

[pytest-choose] <Block list>: filter.json -> Successful use of rules
[pytest-choose] Filter 3 cases and collect 4 cases
collected 7 items

cases\test_choose.py ....                          [100%] 
======================== 4 passed in 0.05s ======================== 

4.3.黑白名单过滤

  • 当同时使用黑白名单时,过滤结果为两个规则的差集({x∣x∈白名单,且x∉黑名单})
  • 当黑白名单无重合项时,默认黑名单失效。

创建文件choose.json,用于选择用例:

{
  "function": [
    "test_demo_2",
    "test_demo_3"
  ]
}

创建文件filter.json,用于过滤用例:

{
  "function": [
    "test_demo_3",
    "test_demo_4",
    "test_demo_5"
  ]
}

执行测试:

pytest --fc="on" --fc-allow-path="./choose.json" --fc-block-path="./filter.json" --fc-coding="utf-8"

测试结果如下:

======================= test session starts =======================
platform win32 -- Python 3.9.6, pytest-7.4.3, pluggy-1.3.0
rootdir: D:\Project\PytestDev\pytest-choose
plugins: choose-0.1.0


[pytest-choose] <Allow list>: choose.json -> Successful use of rules
[pytest-choose] <Block list>: filter.json -> Successful use of rules
[pytest-choose] Filter 6 cases and collect 1 cases
collected 7 items

cases\test_choose.py .                             [100%] 
======================== 1 passed in 0.04s ======================== 

4.4.多规则文件过滤

  • 当传入规则文件不存在时,仅会抛出响应日志,不会中止测试
  • 当传入同类型的多个规则文件时,生效规则为该类型所有规则文件的并集

创建文件choose.jsonchoose_1.json,用于选择用例:

{
  "function": [
    "test_demo_2",
    "test_demo_3"
  ]
}
{
  "function": [
    "test_demo_4"
  ]
}

创建文件filter.json,用于过滤用例:

{
  "function": [
    "test_demo_3",
    "test_demo_4",
    "test_demo_5"
  ]
}

同时传入不存在的文件filter_1.json,执行测试:

pytest --fc="on" --fc-allow-path="./choose.json" --fc-allow-path="./choose_1.json" --fc-block-path="./filter.json" --fc-block-path="./filter_1.json" --fc-coding="utf-8"

测试结果如下:

======================= test session starts =======================
platform win32 -- Python 3.9.6, pytest-7.4.3, pluggy-1.3.0
rootdir: D:\Project\PytestDev\pytest-choose
plugins: choose-0.1.0


[pytest-choose] <Allow list>: choose.json -> Successful use of rules
[pytest-choose] <Allow list>: choose_1.json -> Successful use of rules
[pytest-choose] <Block list>: filter.json -> Successful use of rules
[pytest-choose] <Block list>: filter_1.json -> No such file or directory
[pytest-choose] Filter 6 cases and collect 1 cases
collected 7 items

cases\test_choose.py .                              [100%] 
======================== 1 passed in 0.04s ======================== 

许可证

pytest-choose使用 GPLv3 许可证

Copyright © 2023 by Azusa.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-choose-0.1.0.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

pytest_choose-0.1.0-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest-choose-0.1.0.tar.gz.

File metadata

  • Download URL: pytest-choose-0.1.0.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for pytest-choose-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1128dad563dd1366755c0de2ee625d6899d93ecf958ea5c218ceb267ade34617
MD5 e17f35c62902ab342abbcc3a136468da
BLAKE2b-256 ebd03a427ded94b92ab281a206eb24ea1b332d315e8f202fe2d5b6c84a125fd2

See more details on using hashes here.

File details

Details for the file pytest_choose-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_choose-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for pytest_choose-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d9ecd1dabe695d64c8eb1653cb9af089151bc859ab9072d6767b99590dba1d33
MD5 e88a05029f4631f79ad61197545f5984
BLAKE2b-256 0b5c94c2ebdbba4af054533fba5f1e421c398e110c1a0146843a7e40d5c69da9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page