測試開發進階(三十)

來呀~

歡迎關注我的公衆號「測試遊記」

生成API文檔

  • coreapi

  • Pygments

  • Markdown

安裝

  1. $ pip install coreapi

  2. $ pip install Pygments

  3. $ pip install Markdown

使用coreapi

  • DRF框架(>3.10)需要添加

指定用於支持coreapi的shcema

  1. REST_FRAMEWORK = {

  2. 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema'

  3. }

LearnDjango/urls.py添加

  1. from django.contrib import admin

  2. from django.urls import path, include, re_path

  3. from rest_framework.documentation import include_docs_urls

  4. urlpatterns = [

  5. path('admin/', admin.site.urls),

  6. path('', include('interfaces.urls')),

  7. path('', include('projects.urls')),

  8. path('docs/', include_docs_urls(title='測試平臺接口文檔',

  9. description='這是一個接口文檔平臺'))

  10. ]

查看效果

添加註釋

  • 單一方法的視圖

直接給視圖類添加註釋

  • 多個方法的視圖

  1. class ProjectsListCreateViewSet(ListCreateAPIView):

  2. """

  3. get:

  4. 返回所有項目信息

  5. post:

  6. 新建項目

  7. """

  • 視圖集

  1. class ProjectsViewSet(viewsets.ModelViewSet):

  2. """

  3. create:

  4. 創建項目

  5. retrieve:

  6. 獲取項目詳情數據

  7. update:

  8. 完整更新項目

  9. partial_update:

  10. 部分更新項目

  11. destroy:

  12. 刪除項目

  13. list:

  14. 獲取項目列表數據

  15. names:

  16. 獲取所有項目名稱

  17. interfaces:

  18. 獲取指定項目的所有接口數據

  19. """

使用drf-yasg

支持swagger

  1. $ pip install drf-yasg

添加到 INSTALLED_APPS

  1. INSTALLED_APPS = [

  2. ...

  3. 'drf_yasg'

  4. ...

  5. ]

LearnDjango/urls.py添加以下部分

  1. from django.contrib import admin

  2. from django.urls import path, include, re_path

  3. from rest_framework.documentation import include_docs_urls

  4. from drf_yasg.views import get_schema_view

  5. from drf_yasg import openapi

  6. schema_view = get_schema_view(

  7. openapi.Info(

  8. title='API接口文檔',

  9. default_version='v1',

  10. description='這是一個接口文檔平臺',

  11. # terms_of_service='http://api.xxx.com',

  12. contact=openapi.Contact(email='[email protected]'),

  13. license=openapi.License(name='BSD License')

  14. ),

  15. public=True

  16. )

  17. urlpatterns = [

  18. re_path(r'^swagger(?P<format>\.json|\.yaml)$',

  19. schema_view.without_ui(cache_timeout=0), name='schema-json'),

  20. path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger'),

  21. path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc')

  22. ]

訪問:

  1. http://127.0.0.1:8000/swagger.json

返回json格式數據

訪問

  1. http://127.0.0.1:8000/swagger.yaml

會自動下載一份yaml文件

訪問

  1. http://127.0.0.1:8000/swagger/

訪問

  1. http://127.0.0.1:8000/redoc/

開始項目

創建項目

添加日誌器

ApiTest/settings.py中添加

  1. LOGGING = {

  2. 'version': 1,

  3. 'disable_exising_loggers': False,

  4. 'formatters': {

  5. 'verbose': {

  6. 'format': '%(asctime)s - [%(levelname)s] - [msg]%(message)s'

  7. },

  8. 'simple': {

  9. 'format': '%(asctime)s - [%(levelname)s] - %(name)s - [msg]%(message)s - [%(filename)s:%(lineno)d ]'

  10. },

  11. },

  12. 'filters': {

  13. 'require_debug_true': {

  14. '()': 'django.utils.log.RequireDebugTrue',

  15. },

  16. },

  17. 'handlers': {

  18. 'console': {

  19. 'level': 'DEBUG',

  20. 'filters': ['require_debug_true'],

  21. 'class': 'logging.StreamHandler',

  22. 'formatter': 'simple',

  23. },

  24. 'file': {

  25. 'level': 'INFO',

  26. 'class': 'logging.handlers.RotatingFileHandler',

  27. 'filename': os.path.join(BASE_DIR, 'logs/mytest.log'), # 日誌文件的位置

  28. 'maxBytes': 100 * 1024 * 1024,

  29. 'backupCount': 10,

  30. 'formatter': 'verbose'

  31. },

  32. },

  33. 'loggers': {

  34. 'mytest': { # 定義了一個名爲mytest的日誌器

  35. 'handlers': ['console', 'file'],

  36. 'propagate': True,

  37. 'level': 'DEBUG' # 日誌器接收的最低日誌級別

  38. }

  39. }

  40. }

用戶模塊

創建user子應用

  1. manage.py@ApiTest > startapp user

添加INSTALLED_APPS

  1. INSTALLED_APPS = [

  2. 'django.contrib.admin',

  3. 'django.contrib.auth',

  4. 'django.contrib.contenttypes',

  5. 'django.contrib.sessions',

  6. 'django.contrib.messages',

  7. 'django.contrib.staticfiles',

  8. 'user.apps.UserConfig',

  9. ]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章