自動化測試平台的搭建(一)


工具:pycharm

django+mysql

1.創建django項目autotest:

django-admin startproject autotest 

2.修改數據庫信息

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', #mysql數據庫
        'NAME': 'api_test', # 數據庫名
        'USER': 'dagnkai',# 用戶
        'PASSWORD': 'dangkai', # 密碼
        'HOST': '127.0.0.1', # 本地
        'PORT': '3306',
        # 'OPTIONS': {
        #     'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        #     'sql_mode': 'traditional',
        #     },
    }
}

3. 啟動服務:

Python manage.py runserver  默認端口8000,  也可以修改端口啟動 Python manage.py runserver 127.0.0.1:80

4. 創建相應的表:

  4.1 根據model.py中的類創建:

    python manage.py makemigrations  這個命令是記錄我們對models.py的所有改動,並且將這個改動遷移到migrations這個文件下生成一個文件

python manage.py makemigrations

 如下所示:

E:\Python_Web\autotest>python manage.py makemigrations
No changes detected

4.2 根據模型類創建表   

python manage.py migrate

如果要想精確到某一個遷移文件則可以使用:

python manage.py migrate appname

E:\Python_Web\autotest>python  manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying sessions.0001_initial... OK

4.3 創建超級用戶:

python manage.py createsuperuser
E:\Python_Web\autotest>python manage.py createsuperuser
Username (leave blank to use 'xjy'): dangkai
Email address: 1370465454@qq.com
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
Password:
Password (again):
Superuser created successfully.

4.4 瀏覽器中輸入127.0.0.1:8000/admin  輸入剛創建的超級用戶

5 漢化中文界面:

# LANGUAGE_CODE = 'en-us'
#
# TIME_ZONE = 'UTC'
LANGUAGE_CODE = 'zh-Hans'

TIME_ZONE = 'Asia/Shanghai'

6 創建應用

    python manage.py startapp apitest

   將app注冊到django中 setting中加入apitest

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'apitest'
]

 

7 創建視圖:   

  在views中加入test函數

from django.shortcuts import render
from django.http import HttpResponse #加入引用
# Create your views here.
def test(request):
    return HttpResponse("hello test") #返回響應函數

8 創建映射:

在autotest/urls.py中加入:

from django.contrib import admin
from django.urls import path
from apitest import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('test/',views.test),# 加入關聯路徑和函數
]

瀏覽器中輸入:127.0.0.1:8000/test

 9 創建模板:

在apitest下創建templates文件夾,然后創建login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>
<body>
<h1>login</h1>
<form method="post" action="/login/">
    {% csrf_token %}
    <br> <input name="username" type="text" placeholder="username">
    <br> <input name="password" type="password" placeholder="password">
    {{ error }}<br>
    <br> <button id="submit" type="submit">submit</button>
</form>

</body>
</html>

 在autotest/urls.py中創建關聯映射

from django.contrib import admin
from django.urls import path
from apitest import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('test/',views.test),# 加入關聯路徑和函數
    path('login/',views.login)
]

在apitest/views.py創建login函數

from django.shortcuts import render
from django.http import HttpResponse #加入引用
# Create your views here.
def test(request):
    return HttpResponse("hello test") #返回響應函數

def login(request):
    return render(request,'login.html')

然后輸入127.0.0.1:8000/login,可以看到登錄頁面

使用mysql數據庫

在autotest/__init__.py文件中加入

import pymysql
pymysql.install_as_MySQLdb()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM