一、需要準備的知識點
1. linux: 安裝 python3、nginx 安裝和配置、mysql 安裝和配置
2. python: django 配置、uwsgi 配置
二、我搭建的環(huán)境
1. Centos7 (配置 rabbitmq、mysql 、Supervisord)
2. python 3.6.8 (配置 django、uwsgi)
3. git 1.8.3.1 (克隆代碼)
三、搭建過程
1. 配置數(shù)據(jù)庫(安裝 mysql ,自行百度)
新建庫:httprunner (UI 工具直接新建)
2. 安裝 rabbitmq(消息中間件)
3. 克隆代碼
git clone https://github.com/HttpRunner/HttpRunnerManager.git
4. 安裝項目依賴庫
pip install -r requirements.txt
? ? ? ? ? ? ? # requirements.txt ?(celery 需要用到 tornado,建議安裝 5.1.1 版本)
[root@gitlab HttpRunnerManager]# cat requirements.txt
Django == 2.0.3
PyYAML == 3.12
requests == 2.18.4
eventlet == 0.22.1
mysqlclient == 1.3.12
django-celery == 3.2.2
flower == 0.9.2
dwebsocket == 0.4.2
paramiko == 2.4.1
HttpRunner == 1.5.8
tornado>=4.2.0,<6.0.0
5. 修改 setting.py 配置文件
"""
Django settings for HttpRunnerManager project.
Generated by 'django-admin startproject' using Django 1.11.7.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""
from __future__ import absolute_import, unicode_literals
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import djcelery
from django.conf.global_settings import SESSION_COOKIE_AGE
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '=w+1if4no=o&6!la#5j)3wsu%k@$)6bf+@3=i0h!5)h9h)$*s7'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
# DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'ApiManager',
'djcelery',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
MIDDLEWARE_CLASSES = [
'dwebsocket.middleware.WebSocketMiddleware'
]
ROOT_URLCONF = 'HttpRunnerManager.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'HttpRunnerManager.wsgi.application'
# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
if DEBUG:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'HttpRunner', # 新建數(shù)據(jù)庫名
'USER': 'root', # 數(shù)據(jù)庫登錄名
'PASSWORD': '123456', # 數(shù)據(jù)庫登錄密碼
'HOST': '192.168.1.254', # 數(shù)據(jù)庫所在服務(wù)器ip地址
'PORT': '3306', # 監(jiān)聽端口 默認3306即可
}
}
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'), # 靜態(tài)文件額外目錄
)
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'HttpRunner', # 新建數(shù)據(jù)庫名
'USER': 'root', # 數(shù)據(jù)庫登錄名
'PASSWORD': '123456', # 數(shù)據(jù)庫登錄密碼
'HOST': '192.168.1.254', # 數(shù)據(jù)庫所在服務(wù)器ip地址
'PORT': '3306', # 監(jiān)聽端口 默認3306即可
}
}
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder'
)
SESSION_COOKIE_AGE = 300 * 60
djcelery.setup_loader()
CELERY_ENABLE_UTC = True
CELERY_TIMEZONE = 'Asia/Shanghai'
BROKER_URL = 'amqp://guest:guest3@192.168.91.45:5672//' if DEBUG else 'amqp://guest:guest@192.168.1.254:5672//'
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'
CELERY_RESULT_BACKEND = 'djcelery.backends.database:DatabaseBackend'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_RESULT_EXPIRES = 7200 # celery任務(wù)執(zhí)行結(jié)果的超時時間,
CELERYD_CONCURRENCY = 1 if DEBUG else 10 # celery worker的并發(fā)數(shù) 也是命令行-c指定的數(shù)目 根據(jù)服務(wù)器配置實際更改 一般25即可
CELERYD_MAX_TASKS_PER_CHILD = 100 # 每個worker執(zhí)行了多少任務(wù)就會死掉,我建議數(shù)量可以大一些,比如200
# 發(fā)送郵件
EMAIL_HOST = 'smtp.exmail.qq.com'
EMAIL_PORT = 465
EMAIL_USR_SSL = True
EMAIL_SUBJECT_PREFIX = '測試部'
EMAIL_HOST_USER = 'notic@test.com' # 自己的郵箱
EMAIL_HOST_PASSWORD = "fadfadfdn8hf7AXc" # 我的郵箱密碼
EMAIL_SEND_USERNAME = 'notic@test.com' # 定時任務(wù)報告發(fā)送郵箱,支持163,qq,sina,企業(yè)qq郵箱等,注意需要開通smtp服務(wù)
EMAIL_SEND_PASSWORD = 'Frfadfadfhf7AXc' # 郵箱密碼
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format': '%(asctime)s [%(name)s:%(lineno)d] [%(module)s:%(funcName)s] [%(levelname)s]- %(message)s'}
# 日志格式
},
'filters': {
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'include_html': True,
},
'default': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': os.path.join(BASE_DIR, 'logs/all.log'),
'maxBytes': 1024 * 1024 * 100,
'backupCount': 5,
'formatter': 'standard',
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'standard'
},
'request_handler': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': os.path.join(BASE_DIR, 'logs/script.log'),
'maxBytes': 1024 * 1024 * 100,
'backupCount': 5,
'formatter': 'standard',
},
'scprits_handler': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': os.path.join(BASE_DIR, 'logs/script.log'),
'maxBytes': 1024 * 1024 * 100,
'backupCount': 5,
'formatter': 'standard',
},
},
'loggers': {
'django': {
'handlers': ['default', 'console'],
'level': 'INFO',
'propagate': True
},
'HttpRunnerManager.app': {
'handlers': ['default', 'console'],
'level': 'DEBUG',
'propagate': True
},
'django.request': {
'handlers': ['request_handler'],
'level': 'DEBUG',
'propagate': True
},
'HttpRunnerManager': {
'handlers': ['scprits_handler', 'console'],
'level': 'DEBUG',
'propagate': True
},
'scripts': {
'handlers': ['scprits_handler', 'console'],
'level': 'DEBUG',
'propagate': True
},
}
}
6. django 生成數(shù)據(jù)庫表和字段(在項目路徑下執(zhí)行)
#生成數(shù)據(jù)遷移腳本
python manage.py makemigrations ApiManager
#應(yīng)用到db生成數(shù)據(jù)表
python manage.py migrate
7. 創(chuàng)建 django 管理后臺的超級賬戶
python manage.py createsuperuser
8. 安裝 和 配置 uwsgi?
安裝:pip install uwsgi
配置:ini_hrm.ini
[uwsgi]
socket = 127.0.0.1:9092
#socket = 127.0.0.1:3031
stats = 127.0.0.1:9193
chdir = /root/TestProject/HttpRunnerManager
wsgi-file = /root/TestProject/HttpRunnerManager/HttpRunnerManager/wsgi.py
# module = Joyo.settings
virtualenv = /root/.envs/hrm
pidfile = /root/TestProject/running/uwsgi_hrm.pid
touch-reload = /root/TestProject/running/uwsgi_hrm.pid
# py-auto-reload = 1
buffer-size = 32768
processes = 1
workers=2
# threads = 2
daemonize = /root/TestProject/logs/uwsgi_hrm.log
9. 配置 Nginx
10. 啟動 Nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
11. 啟動 uwsgi
/root/.envs/hrm/bin/uwsgi --ini /root/TestProject/configs/ini_hrm.ini
12. Supervisor 守護 Celery worker、beat、flower
四、訪問鏈接
1. django 后臺
url: http://192.168.1.254/admin/login/?next=/admin/
id: root
pw: abc123456
2. rabbitmq
url: http://192.168.1.254:15672/#/ id: guest pw: guest
3. HttpRunnerManager
注冊:http://192.168.1.254:8000/api/register/ 登錄:http://192.168.1.254:8000/api/login/
4. Celery 任務(wù)監(jiān)控后臺
url: http://192.168.1.254:5555/dashboard
五、兩個問題點
1. ?啟動 django 項目時,ImportError: No module named 'MySQLdb':
安裝 pymysql: pip install pymysql
在 Django 項目中的 HttpRunnerManager/__init__.py 中引用:
import pymysql
pymysql.install_as_MySQLdb()
2. 打開 HttpRunnerManager 后臺時:百度未授權(quán)使用地圖API?
### 來幾張圖
?文章來源:http://www.zghlxwxcb.cn/news/detail-495052.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-495052.html
到了這里,關(guān)于【HttpRunnerManager】搭建接口自動化測試平臺操作流程的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!