Skip to main content

📖Use TestCase auto generate API document.

Project description

Rimuru

Build Status codecov

📖用单元测试自动生成接口文档

已适配的客户端

  • [x] requests
  • [x] 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"
}
​```

Project details


Release history Release notifications

Download files

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

Files for rimuru, version 0.0.12
Filename, size File type Python version Upload date Hashes
Filename, size rimuru-0.0.12-py3-none-any.whl (13.5 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size rimuru-0.0.12.tar.gz (7.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page