• 【HttpRunnerManager】搭建接口自动化测试平台实战


    一、需要准备的知识点

    1. linux: 安装 python3、nginx 安装和配置、mysql 安装和配置

    2. python: django 配置、uwsgi 配置

    二、我搭建的环境

    1. Centos7 (配置 rabbitmq、mysql 、Supervisord)

    2. python 3.6.8 (配置 django、uwsgi)

    3. git 1.8.3.1 (克隆代码)

    三、搭建过程

    1. 配置数据库安装 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 版本)

    1. [root@gitlab HttpRunnerManager]# cat requirements.txt
    2. Django == 2.0.3
    3. PyYAML == 3.12
    4. requests == 2.18.4
    5. eventlet == 0.22.1
    6. mysqlclient == 1.3.12
    7. django-celery == 3.2.2
    8. flower == 0.9.2
    9. dwebsocket == 0.4.2
    10. paramiko == 2.4.1
    11. HttpRunner == 1.5.8
    12. tornado>=4.2.0,<6.0.0

    5. 修改 setting.py 配置文件

    1. """
    2. Django settings for HttpRunnerManager project.
    3. Generated by 'django-admin startproject' using Django 1.11.7.
    4. For more information on this file, see
    5. https://docs.djangoproject.com/en/1.11/topics/settings/
    6. For the full list of settings and their values, see
    7. https://docs.djangoproject.com/en/1.11/ref/settings/
    8. """
    9. from __future__ import absolute_import, unicode_literals
    10. import os
    11. # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    12. import djcelery
    13. from django.conf.global_settings import SESSION_COOKIE_AGE
    14. BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    15. # Quick-start development settings - unsuitable for production
    16. # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
    17. # SECURITY WARNING: keep the secret key used in production secret!
    18. SECRET_KEY = '=w+1if4no=o&6!la#5j)3wsu%k@$)6bf+@3=i0h!5)h9h)$*s7'
    19. # SECURITY WARNING: don't run with debug turned on in production!
    20. DEBUG = False
    21. # DEBUG = True
    22. ALLOWED_HOSTS = ['*']
    23. # Application definition
    24. INSTALLED_APPS = [
    25. 'django.contrib.admin',
    26. 'django.contrib.auth',
    27. 'django.contrib.contenttypes',
    28. 'django.contrib.sessions',
    29. 'django.contrib.messages',
    30. 'django.contrib.staticfiles',
    31. 'ApiManager',
    32. 'djcelery',
    33. ]
    34. MIDDLEWARE = [
    35. 'django.middleware.security.SecurityMiddleware',
    36. 'django.contrib.sessions.middleware.SessionMiddleware',
    37. 'django.middleware.common.CommonMiddleware',
    38. # 'django.middleware.csrf.CsrfViewMiddleware',
    39. 'django.contrib.auth.middleware.AuthenticationMiddleware',
    40. 'django.contrib.messages.middleware.MessageMiddleware',
    41. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
    42. ]
    43. MIDDLEWARE_CLASSES = [
    44. 'dwebsocket.middleware.WebSocketMiddleware'
    45. ]
    46. ROOT_URLCONF = 'HttpRunnerManager.urls'
    47. TEMPLATES = [
    48. {
    49. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
    50. 'DIRS': [os.path.join(BASE_DIR, 'templates')],
    51. 'APP_DIRS': True,
    52. 'OPTIONS': {
    53. 'context_processors': [
    54. 'django.template.context_processors.debug',
    55. 'django.template.context_processors.request',
    56. 'django.contrib.auth.context_processors.auth',
    57. 'django.contrib.messages.context_processors.messages',
    58. ],
    59. },
    60. },
    61. ]
    62. WSGI_APPLICATION = 'HttpRunnerManager.wsgi.application'
    63. # Password validation
    64. # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators
    65. AUTH_PASSWORD_VALIDATORS = [
    66. {
    67. 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    68. },
    69. {
    70. 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    71. },
    72. {
    73. 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    74. },
    75. {
    76. 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    77. },
    78. ]
    79. # Internationalization
    80. # https://docs.djangoproject.com/en/1.11/topics/i18n/
    81. LANGUAGE_CODE = 'zh-Hans'
    82. TIME_ZONE = 'Asia/Shanghai'
    83. USE_I18N = True
    84. USE_L10N = True
    85. USE_TZ = False
    86. # Static files (CSS, JavaScript, Images)
    87. # https://docs.djangoproject.com/en/1.11/howto/static-files/
    88. if DEBUG:
    89. DATABASES = {
    90. 'default': {
    91. 'ENGINE': 'django.db.backends.mysql',
    92. 'NAME': 'HttpRunner', # 新建数据库名
    93. 'USER': 'root', # 数据库登录名
    94. 'PASSWORD': '123456', # 数据库登录密码
    95. 'HOST': '192.168.1.254', # 数据库所在服务器ip地址
    96. 'PORT': '3306', # 监听端口 默认3306即可
    97. }
    98. }
    99. STATICFILES_DIRS = (
    100. os.path.join(BASE_DIR, 'static'), # 静态文件额外目录
    101. )
    102. else:
    103. DATABASES = {
    104. 'default': {
    105. 'ENGINE': 'django.db.backends.mysql',
    106. 'NAME': 'HttpRunner', # 新建数据库名
    107. 'USER': 'root', # 数据库登录名
    108. 'PASSWORD': '123456', # 数据库登录密码
    109. 'HOST': '192.168.1.254', # 数据库所在服务器ip地址
    110. 'PORT': '3306', # 监听端口 默认3306即可
    111. }
    112. }
    113. STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    114. STATIC_URL = '/static/'
    115. STATICFILES_FINDERS = (
    116. 'django.contrib.staticfiles.finders.FileSystemFinder',
    117. 'django.contrib.staticfiles.finders.AppDirectoriesFinder'
    118. )
    119. SESSION_COOKIE_AGE = 300 * 60
    120. djcelery.setup_loader()
    121. CELERY_ENABLE_UTC = True
    122. CELERY_TIMEZONE = 'Asia/Shanghai'
    123. BROKER_URL = 'amqp://guest:guest3@192.168.91.45:5672//' if DEBUG else 'amqp://guest:guest@192.168.1.254:5672//'
    124. CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'
    125. CELERY_RESULT_BACKEND = 'djcelery.backends.database:DatabaseBackend'
    126. CELERY_ACCEPT_CONTENT = ['application/json']
    127. CELERY_TASK_SERIALIZER = 'json'
    128. CELERY_RESULT_SERIALIZER = 'json'
    129. CELERY_TASK_RESULT_EXPIRES = 7200 # celery任务执行结果的超时时间,
    130. CELERYD_CONCURRENCY = 1 if DEBUG else 10 # celery worker的并发数 也是命令行-c指定的数目 根据服务器配置实际更改 一般25即可
    131. CELERYD_MAX_TASKS_PER_CHILD = 100 # 每个worker执行了多少任务就会死掉,我建议数量可以大一些,比如200
    132. # 发送邮件
    133. EMAIL_HOST = 'smtp.exmail.qq.com'
    134. EMAIL_PORT = 465
    135. EMAIL_USR_SSL = True
    136. EMAIL_SUBJECT_PREFIX = '测试部'
    137. EMAIL_HOST_USER = 'notic@test.com' # 自己的邮箱
    138. EMAIL_HOST_PASSWORD = "fadfadfdn8hf7AXc" # 我的邮箱密码
    139. EMAIL_SEND_USERNAME = 'notic@test.com' # 定时任务报告发送邮箱,支持163,qq,sina,企业qq邮箱等,注意需要开通smtp服务
    140. EMAIL_SEND_PASSWORD = 'Frfadfadfhf7AXc' # 邮箱密码
    141. LOGGING = {
    142. 'version': 1,
    143. 'disable_existing_loggers': True,
    144. 'formatters': {
    145. 'standard': {
    146. 'format': '%(asctime)s [%(name)s:%(lineno)d] [%(module)s:%(funcName)s] [%(levelname)s]- %(message)s'}
    147. # 日志格式
    148. },
    149. 'filters': {
    150. },
    151. 'handlers': {
    152. 'mail_admins': {
    153. 'level': 'ERROR',
    154. 'class': 'django.utils.log.AdminEmailHandler',
    155. 'include_html': True,
    156. },
    157. 'default': {
    158. 'level': 'DEBUG',
    159. 'class': 'logging.handlers.RotatingFileHandler',
    160. 'filename': os.path.join(BASE_DIR, 'logs/all.log'),
    161. 'maxBytes': 1024 * 1024 * 100,
    162. 'backupCount': 5,
    163. 'formatter': 'standard',
    164. },
    165. 'console': {
    166. 'level': 'DEBUG',
    167. 'class': 'logging.StreamHandler',
    168. 'formatter': 'standard'
    169. },
    170. 'request_handler': {
    171. 'level': 'DEBUG',
    172. 'class': 'logging.handlers.RotatingFileHandler',
    173. 'filename': os.path.join(BASE_DIR, 'logs/script.log'),
    174. 'maxBytes': 1024 * 1024 * 100,
    175. 'backupCount': 5,
    176. 'formatter': 'standard',
    177. },
    178. 'scprits_handler': {
    179. 'level': 'DEBUG',
    180. 'class': 'logging.handlers.RotatingFileHandler',
    181. 'filename': os.path.join(BASE_DIR, 'logs/script.log'),
    182. 'maxBytes': 1024 * 1024 * 100,
    183. 'backupCount': 5,
    184. 'formatter': 'standard',
    185. },
    186. },
    187. 'loggers': {
    188. 'django': {
    189. 'handlers': ['default', 'console'],
    190. 'level': 'INFO',
    191. 'propagate': True
    192. },
    193. 'HttpRunnerManager.app': {
    194. 'handlers': ['default', 'console'],
    195. 'level': 'DEBUG',
    196. 'propagate': True
    197. },
    198. 'django.request': {
    199. 'handlers': ['request_handler'],
    200. 'level': 'DEBUG',
    201. 'propagate': True
    202. },
    203. 'HttpRunnerManager': {
    204. 'handlers': ['scprits_handler', 'console'],
    205. 'level': 'DEBUG',
    206. 'propagate': True
    207. },
    208. 'scripts': {
    209. 'handlers': ['scprits_handler', 'console'],
    210. 'level': 'DEBUG',
    211. 'propagate': True
    212. },
    213. }
    214. }

    6. django 生成数据库表和字段(在项目路径下执行)

    1. #生成数据迁移脚本
    2. python manage.py makemigrations ApiManager
    3. #应用到db生成数据表
    4. python manage.py migrate

    7. 创建 django 管理后台的超级账户

    python manage.py createsuperuser

    8. 安装 和 配置 uwsgi 

      安装:pip install uwsgi

        配置:ini_hrm.ini

    1. [uwsgi]
    2. socket = 127.0.0.1:9092
    3. #socket = 127.0.0.1:3031
    4. stats = 127.0.0.1:9193
    5. chdir = /root/TestProject/HttpRunnerManager
    6. wsgi-file = /root/TestProject/HttpRunnerManager/HttpRunnerManager/wsgi.py
    7. # module = Joyo.settings
    8. virtualenv = /root/.envs/hrm
    9. pidfile = /root/TestProject/running/uwsgi_hrm.pid
    10. touch-reload = /root/TestProject/running/uwsgi_hrm.pid
    11. # py-auto-reload = 1
    12. buffer-size = 32768
    13. processes = 1
    14. workers=2
    15. # threads = 2
    16. 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 任务监控后台

        url: http://192.168.1.254:5555/dashboard

    五、两个问题点

      1.  启动 django 项目时,ImportError: No module named 'MySQLdb':

        安装 pymysql: pip install pymysql

        在 Django 项目中的 HttpRunnerManager/__init__.py 中引用:

    1. import pymysql

    2. pymysql.install_as_MySQLdb()

    2. 打开 HttpRunnerManager 后台时:百度未授权使用地图API 

    ### 来几张图

    7天练完接口自动化测试30个实战项目,28K入职字节测试岗。【自动化测试/接口测试/软件测试/性能测试/Jmeter】

    最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

    在这里插入图片描述

    这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!   

  • 相关阅读:
    【UGUI】给美术的备忘录
    redis的原理和源码-主从复制的原理介绍
    二叉树的顺序存储——堆——初识堆排序
    操作系统浅谈(二)
    YOLOv8环境搭建
    使用setInterval定时器实现文字一个一个的打印在页面中
    2022年测试岗最新自动化测试面试题整理,干货满满
    【-前端攻城师之JS基础】01JS基础语法
    ssh远程连接脚本
    分类预测 | Matlab实现基于PSO-SDAE粒子群优化算法优化堆叠去噪自编码器的数据分类预测
  • 原文地址:https://blog.csdn.net/2301_78843735/article/details/133208148