生成pytest用例
install
python3 -m venv .myenv
source .myenv/bin/activate
#deactivate 退出
1. 初始化
Swagger2Tests --init --target='./'
初始化项目时会在指定的target目录,初始化项目,并且生成一些基础文件。
- constants 可配置一些公共参数 比如 domain等
- helpers 一些通用方法
- templates 生成模板,可进行自定义修改
生成测试文件
Swagger2Tests --swagger='https://swagger.json' --target='./'
- swagger 参数支持文件和url
当前会按请求参数类型,一次性生成固定值,如果需要每次请求都动态更改,则可以在文件里进行修改,使用Helper类的方法
def test_chargingUsingPOST(self):
# 计算费用
requestBody = {
"appid": REQUEST_APPID,
"bizContent": {
'amount': Helper.get_random_float(),
'gasId': 'string',
'gunNo': Helper.get_random_int(),
'oilNo': Helper.get_random_int()
},
"sign": "string",
"timestamp": Helper.get_random_datetime()
}
用pytest 进行接口测试
- 在当前目录创建test_suits目录
- 在该目录内创建符合pytest扫描规则的测试文件,下面是个示例
test_detail.py.
from apps.testCases.goods.items.DetailCase import *
class TestDetail:
def test_detail(self):
DetailCase.detailUsingGET()
以上只是一个示例,描述了对单个接口的测试。实际上,你可以处理接口返回,进行串联,模拟某个功能场景的测试集合。
- 编写pytest执行文件
import pytest
if __name__ == '__main__':
pytest.main(['-s'])
执行单接口或场景接口集合测试
python3 pytest_main.py
- 使用pytest_main.py 执行的意义在于,避免有使用自定义包,因目录层级而存在的包导入问题
结合locust进行压力测试
- 编写locust用例文件
from locust import HttpUser,task
from locust.clients import LocustResponse
from apps.testCases.goods.items.DetailCase import *
from Swagger2Tests.helpers.helper import RequestInfo
class TestUser(HttpUser):
min_wait = 5 * 1000
max_wait = 10 * 1000
@task
def getGoodsDetail(self):
r:RequestInfo = DetailCase.detailUsingGET(self.host)
response:LocustResponse
with self.client.request(r.request_method,r.request_path,
data=r.request_body,params=r.request_params,
catch_response =True
) as response:
if response.status_code == 200:
response.success()
else:
response.failure("Request failed with status code: %s" % response.status_code)
development
- test
python3 -m tests.test_run --swagger='./tests/oil_detail.json'
- compile and deploy
python3 setup.py sdist bdist_wheel
twine upload dist/*
Metadata
Release files for Swagger2Tests 1.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| Swagger2Tests-1.2.2.tar.gz | 12.6 kB | Without provenance | Details for Swagger2Tests-1.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| Swagger2Tests-1.2.2-py3-none-any.whl | Python 3 | none | any | Details for Swagger2Tests-1.2.2-py3-none-any.whl |
Total release size: 27.0 kB
Release files / Swagger2Tests-1.2.2.tar.gz
| Download URL | Swagger2Tests-1.2.2.tar.gz |
|---|---|
| Size | 12.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ade2cb6cb36485d6021eaa399d5d403a6252506c1c5745619e1daaacfa2d172b
|
|
BLAKE2b-256 checksum How to use checksums |
61d6813167be8029d24c6976e0e64c3b846c86ded9ad6428ac5a921f6addbb04
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.7
|
Release files / Swagger2Tests-1.2.2-py3-none-any.whl
| Download URL | Swagger2Tests-1.2.2-py3-none-any.whl |
|---|---|
| Size | 14.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6b74b2a156a3f9f73a291d4f52034c12f7711a2785f4f82abece3d9dca09dc98
|
|
BLAKE2b-256 checksum How to use checksums |
1392b91f22d2e5579a65a7b71481db1f22bbce4914d58f0d437c0ebe4df40e1f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.7
|
Security for Swagger2Tests 1.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.