Pytest集成自动化平台插件
Project description
pytest-platform-adapter
Pytest集成自动化平台插件
功能
- 根据用例
allure.title中的 ID 筛选执行的用例 - 跳过所有用例的执行,快速生成 Allure 报告
- 周期性的通过 RESTApi 上报执行用例的整体进度
环境依赖
- Python >= 3.6
- pytest >= 7.1.2
- allure-pytest >= 2.9.45
安装
执行以下命令即可完成安装
pip install pytest-platform-adapter
使用方法
根据 Allure title 中的 ID 筛选用例
假设有以下测试用例
import allure
@allure.title('19936-测试用例1')
def test_case1():
assert True
@allure.title('19930-测试用例2')
def test_case2():
assert True
@allure.title('19939-测试用例3')
def test_case3():
assert True
该插件提供两种方式来筛选测试用例:
- 通过命令行参数指定测试ID:
pytest tests/ -v --case-ids="19936,19930"
- 通过文件指定测试ID:
pytest tests/ -v --case-ids-file="test_ids.txt"
其中 test_ids.txt 的内容格式如下:
19936
19930
跳过所有用例
使用 --scan 参数,启用扫描模式。在扫描模式下,所有用例都会被 skip 不会执行,可以用于快速生成 Allure 报告。使用方法如下:
pytest --scan --alluredir allure-results
将执行进展通过RESTApi回报
在 pytest.ini 中配置以下内容
[pytest]
platform_ip = 127.0.0.1
platform_port = 8080
platform_path = /api/autoplatform/task/refresh_data_count
platform_use_https = False
执行用例的过程中(前 10 个用例的时候执行完就立刻回报,之后就是每隔 10 个用例才回报一次) 会将测试用例的整体进展以 POST 的方式回报给 http://127.0.0.1:8080/api/autoplatform/task/refresh_data_count 请求体如下:
{
"pipeline": "JOB_NAME",
"build_number": "BUILD_NUMBER",
"passed_case_count": 29,
"skipped_case_count": 1,
"failed_case_count": 0,
"selected_case": 100
}
其中 JOB_NAME 和 BUILD_NUMBER 是通过环境变量获取的。
调试方法
部署虚拟环境
新建一个虚拟环境,建议使用 Python 3.11.9 。
安装依赖
pip install -r requirements.txt
在项目根目录(与pyproject.toml同一路径)执行以下命令安装本插件
pip install -e .
执行 pytest --help 检查返回的内容是否包含以下文本,如果有的话就说明安装成功了
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
positional arguments:
file_or_dir
general:
..... 省略中间
自动化平台插件:
--case_ids=CASE_IDS 要执行的测试用例ID列表,使用逗号分隔,例如:19936,19930
--case_ids_file=CASE_IDS_FILE
包含测试用例ID的文件路径,文件中每行一个ID
--scan 扫描模式:快速生成 Allure 报告而不实际执行测试
[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg|pyproject.toml file found:
..... 省略中间
platform_ip (string): 自动化平台IP
platform_port (string):
自动化平台端口
创建测试用例
在项目根目录新建 tests 目录,然后在 tests 目录中新建以 test_ 开头的 py 文件,作为测试用例。
例如新建 test_aaa.py,在文件中写入以下内容
import allure
class TestAbc:
@allure.title('19936-用例1')
def test_abc(self):
print('test_abc 用例执行中')
# @pytest.mark.xfail(reason='跳过')
class TestAbd:
@allure.title('19930-用例2')
def test_abd(self):
with allure.step('步骤1'):
pass
# @pytest.mark.xfail(reason='跳过')
class TestAbe:
@allure.title('19932-用例3')
def test_abe(self):
pass
# raise AssertionError()
# pytest.fail('用例失败')
配置执行入口
然后再项目根目录新建 main.py 作为执行的入口,写入以下内容。
import pytest
import os
import shutil
if __name__ == '__main__':
pytest_options = [
'tests/',
# '--case_ids', '456456',
# '--scan',
'-v',
'--alluredir', 'allure-results']
# shutil.rmtree('allure-results') # 如果需要清空 allure-results 目录就解除本行注释
pytest.main(pytest_options)
os.system('allure generate allure-results -o ./report --clean')
通过为 main.py 中的 pytest_options 列表,添加 --case_ids 参数可以排除指定的用例,添加或删除 --scan 参数可以启用或禁用扫描模式。
注意:如果发现 pytest_runtest_logreport 中的逻辑没有被执行。那可能是没有用例被选中(也就是本次没有执行任何用例)
修改 plugin.py 的话不用重新 pip install 安装本插件,它是实时生效的。因为 pip install -e 的本质是在 Python 解释器的 site-packages 里面做了一个软链接到了本项目。
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_platform_adapter-1.0.0.tar.gz.
File metadata
- Download URL: pytest_platform_adapter-1.0.0.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7de215763ff48a05bc60b7822b6acc30ee977ef4ddbdbdad29178dff293fdc3c
|
|
| MD5 |
33da0fbfea142c78756bb1a9611ba206
|
|
| BLAKE2b-256 |
55c2d85c930d864d7114f26c876f5f555c209ad940d88e5dd82b3cbd5c2f43fe
|
Provenance
The following attestation bundles were made for pytest_platform_adapter-1.0.0.tar.gz:
Publisher:
deploy.yml on blackyau/pytest-platform-adapter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_platform_adapter-1.0.0.tar.gz -
Subject digest:
7de215763ff48a05bc60b7822b6acc30ee977ef4ddbdbdad29178dff293fdc3c - Sigstore transparency entry: 172193847
- Sigstore integration time:
-
Permalink:
blackyau/pytest-platform-adapter@37cd8055f8a44436568511791d52991fad2347f7 -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/blackyau
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
deploy.yml@37cd8055f8a44436568511791d52991fad2347f7 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pytest_platform_adapter-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pytest_platform_adapter-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8dfbfcddb75f034183c449811fe41f09b8939cd98341b786707cb0795ac976e3
|
|
| MD5 |
2248dca538a653a093d44f8b23c4c145
|
|
| BLAKE2b-256 |
caeb078b64ba5934c6f6dbb7fad1f66265723104eb19adc96eaaf1796ed562f5
|
Provenance
The following attestation bundles were made for pytest_platform_adapter-1.0.0-py3-none-any.whl:
Publisher:
deploy.yml on blackyau/pytest-platform-adapter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_platform_adapter-1.0.0-py3-none-any.whl -
Subject digest:
8dfbfcddb75f034183c449811fe41f09b8939cd98341b786707cb0795ac976e3 - Sigstore transparency entry: 172193849
- Sigstore integration time:
-
Permalink:
blackyau/pytest-platform-adapter@37cd8055f8a44436568511791d52991fad2347f7 -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/blackyau
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
deploy.yml@37cd8055f8a44436568511791d52991fad2347f7 -
Trigger Event:
release
-
Statement type: