Rimuru
📖用单元测试自动生成接口文档
已适配的客户端
- requests
- django.test.client
- flask.testing.FlaskClient
使用方法
用requests模块进行测试
import unittest
import requests as requests_module
from rimuru.core import doc_client, APIDocument
class APITestCase(unittest.TestCase):
def setUp(self):
self.api_document = APIDocument()
self.client = doc_client(self.api_document, requests_module)
def test_api(self):
url = 'http://127.0.0.1:5000/api/books'
method = 'GET'
name = '书列表接口'
self.api_document.set_api_name(method=method, url=url, name=name)
response = self.client.get(url)
"""
你的测试逻辑
self.assertEqual(response.status_code, 200)
...
"""
self.client.get(
url, params={'name': 'A'},
headers={'TEST_HEADER': 'test'},
requires={'name': False},
add_response=False
)
url = 'http://127.0.0.1:5000/api/books/<int:id>'
method = 'GET'
name = '书详情接口'
self.api_document.set_api_name(method=method, url=url, name=name)
self.client.get('http://127.0.0.1:5000/api/books/2')
response = self.client.get('http://127.0.0.1:5000/api/books/4')
self.assertEqual(response.status_code, 404)
self.api_document.save(file_path='tests/')
用django.test.client模块进行测试
import django.test import TestCase
class APITestCase(unittest.TestCase):
def setUp(self):
self.api_document = APIDocument()
self.client = doc_client(self.api_document, self.client)
def test_api(self):
url = 'http://127.0.0.1:5000/api/books'
method = 'GET'
name = '书列表接口'
self.api_document.set_api_name(method=method, url=url, name=name)
response = self.client.get(url)
"""
你的测试逻辑
self.assertEqual(response.status_code, 200)
...
"""
self.client.get(
url, data={'name': 'A'},
requires={'name': False},
add_response=False,
**{'HTTP_TEST_HEADER': 'test'}
)
url = 'http://127.0.0.1:5000/api/books/<int:id>'
method = 'GET'
name = '书详情接口'
self.api_document.set_api_name(method=method, url=url, name=name)
self.client.get('http://127.0.0.1:5000/api/books/2')
response = self.client.get('http://127.0.0.1:5000/api/books/4')
self.assertEqual(response.status_code, 404)
self.api_document.save(file_path='tests/')
生成文档
书籍列表接口.md
## 书列表接口
### 请求地址
`GET` `http://127.0.0.1:5000/api/books`
### 请求头
| 参数名 | 必填 | 参考值 |
| ------- | ---- | -------------------------------- |
|TEST_HEADER|True|test|
### 请求参数
| 参数名 | 类型 | 必填 | 描述 | 默认值 | 参考值 |
| -------- | ------ | ---- | ---- | ------ | ------ |
|name|String|False|||A|
### 返回正确
状态码 `200`
```json
[
{
"id": 1,
"name": "A"
},
{
"id": 2,
"name": "B"
},
{
"id": 3,
"name": "C"
}
]
```
书详情接口.md
## 书详情接口
### 请求地址
`GET` `http://127.0.0.1:5000/api/books/<int:id>`
### 返回正确
状态码 `200`
```json
{
"id": 2,
"name": "B"
}
```
### 返回错误
状态码 `404`
```json
{
"msg": "Not Found"
}
```
Metadata
Release files for rimuru 0.0.12
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 |
|---|---|---|---|---|
| rimuru-0.0.12.tar.gz | 7.7 kB | Without provenance | Details for rimuru-0.0.12.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| rimuru-0.0.12-py3-none-any.whl | Python 3 | none | any | Details for rimuru-0.0.12-py3-none-any.whl |
Total release size: 21.2 kB
Release files / rimuru-0.0.12.tar.gz
| Download URL | rimuru-0.0.12.tar.gz |
|---|---|
| Size | 7.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
664a2c6dc61544aa67949892a551ef07d3578348c75292a3b22cfdea74c2601e
|
|
BLAKE2b-256 checksum How to use checksums |
b55120548f01f1bab2413979d81693408b850c73345c0e973a381eb878ac1293
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.11.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5
|
Release files / rimuru-0.0.12-py3-none-any.whl
| Download URL | rimuru-0.0.12-py3-none-any.whl |
|---|---|
| Size | 13.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
28338c1c2fecf494ba960c6dce2f3488cb3f31b203b63635f56280988ecffd63
|
|
BLAKE2b-256 checksum How to use checksums |
899fbc39968268074d33ddd9383b2dff5860f88d794e2f6a24e781fcf054063a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.11.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5
|
Security for rimuru 0.0.12
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.