最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Django+Pytest搭建在線自動化測試平臺

 更新時間:2022年07月13日 09:18:54   作者:qq闕繼婷  
最近由于公司的發(fā)展安排本人實現(xiàn)公司項目的自動化測試,下面這篇文章主要給大家介紹了關于如何Django?+?Pytest搭建在線自動化測試平臺的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

一、測試平臺:

解決分散用例執(zhí)行方式,提供統(tǒng)一測試用例執(zhí)行過程、用例管理、測試報告

主要是基于:

    fastapi+vue.js
    django+vue.js
    django

二、搭建過程

2.1 使用django搭建一個web系統(tǒng)

1. 創(chuàng)建項目

  django-admin startproject TestPlatform

創(chuàng)建python的包,測試平臺的配置和功能

2. 創(chuàng)建app

django-admin startapp web

創(chuàng)建python的包,具體的功能代碼

(1)在TestPlatform/Web/apps.py中注冊app

from django.apps import AppConfig
class WebConfig(AppConfig):
    # default_auto_field = 'django.db.models.BigAutoField'
    name = 'Web'
    verbose_name = "自動化測試"

(2) 在TestPlatform/TestPlatform/settings.py中進行相關設置

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'Web.apps.WebConfig'
]

LANGUAGE_CODE = 'zh-hans'# 設置中文
TIME_ZONE = 'Asia/Shanghai'# 設置時區(qū)

USE_I18N = True

USE_L10N = True

USE_TZ = False

MEDIA_ROOT = 'uploads/'
MEDIA_URL = 'uploads/'

(3)編輯TestPlatform/TestPlatform/urls.py文件

from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path

# 定義http路由,是web系統(tǒng)的入口點
urlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + [
    path('', admin.site.urls),
]

(4)啟動測試平臺

     python manage.py runserver

2.2 搭建測試框架

1. 定義model

model驅動開發(fā)

import pathlib
from django.db import models

# 定義model
# 抽象化
from django.utils import html


class Task(models.Model):
    name = models.CharField("用例名稱", max_length=20)
    case = models.FileField("用例文件", upload_to='tests/%Y_%m_%d_%H_%M_%S/')
    status = models.IntegerField(
        "測試狀態(tài)", default=-1, choices=[
            (-1, '初始化'),
            (0, '馬上執(zhí)行'),
            (1, '正在執(zhí)行測試用例'),
            (2, '正在生成測試報告'),
            (3, '執(zhí)行完畢'),
        ]
    )
    run_datatime = models.DateTimeField(
        "最近執(zhí)行時間", null=True, blank=True
    )

    class Meta:
        verbose_name_plural = verbose_name = "測試任務"

    def __str__(self):
        return self.name

    def get_url(self, _type):
        """生成報告或者測試日志的Url"""
        if self.case and self.status == 3:  # 執(zhí)行完畢
            case_path = pathlib.PurePosixPath(str(self.case))
            root_path = pathlib.PurePosixPath('/uploads')

            if _type == 'report':  # 報告的url
                report_path = root_path / case_path.parent / "report/index.html"
            elif _type == 'log':  # 日志的url
                report_path = root_path / case_path.parent / "pytest.txt"
            else:
                report_path = '_'

            return html.format_html(f"<a href='{report_path}' target='_blank'> 點擊查看</a>")
        else:
            return "-"

2. 定義界面

from django.contrib import admin
from .models import Task


# Register your models here.

@admin.register(Task)
class TaskAdmin(admin.ModelAdmin):
    # 決定了model 怎么顯示
    list_display = ("id", "name", "status", "run_datatime",
                    "report_url", "log_url",
                    )
    # 要顯示的字段

    readonly_fields = ('run_datatime',)

    def report_url(self, obj):
        return obj.get_url('report')

    report_url.short_description = '測試報告'

    def log_url(self, obj):
        return obj.get_url('url')

    log_url.short_description = '執(zhí)行日志'

3. 執(zhí)行數(shù)據(jù)庫遷移

   python manage.py makemigrations
   python manage.py migrate

三、平臺如何管理

1. 創(chuàng)建管理員賬號

   python manage.py createsuperuser

  用戶名:admin

  郵箱:admin@qq.com

  密碼:admin

2. 調整頁面顯示

3. 執(zhí)行測試用例

在models.py文件中調用pytest,pytest執(zhí)行yaml文件(關鍵字驅動)

import pathlib
from datetime import datetime

from django.db import models

# 定義model
# 抽象化
from django.utils import html


class Task(models.Model):
    name = models.CharField("用例名稱", max_length=20)
    case = models.FileField("用例文件", upload_to='tests/%Y_%m_%d_%H_%M_%S/')
    status = models.IntegerField(
        "測試狀態(tài)", default=-1, choices=[
            (-1, '初始化'),
            (0, '馬上執(zhí)行'),
            (1, '正在執(zhí)行測試用例'),
            (2, '正在生成測試報告'),
            (3, '執(zhí)行完畢'),
        ]
    )
    run_datatime = models.DateTimeField(
        "最近執(zhí)行時間", null=True, blank=True
    )

    class Meta:
        verbose_name_plural = verbose_name = "測試任務"

    def __str__(self):
        return self.name

    def get_url(self, _type):
        """生成報告或者測試日志的Url"""
        if self.case and self.status == 3:  # 執(zhí)行完畢
            case_path = pathlib.PurePosixPath(str(self.case))
            root_path = pathlib.PurePosixPath('/uploads')

            if _type == 'report':  # 報告的url
                report_path = root_path / case_path.parent / "report/index.html"
            elif _type == 'log':  # 日志的url
                report_path = root_path / case_path.parent / "pytest.txt"
            else:
                report_path = '_'

            return html.format_html(f"<a href='{report_path}' target='_blank'> 點擊查看</a>")
        else:
            return "-"

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

        # 判斷是否需要啟動測試框架,執(zhí)行測試用例

        if self.status == 0:
            self.status = 1  # 修改狀態(tài):正在執(zhí)行
            self.run_datatime = datetime.datetime.now()
            super().save()

            # 啟動測試框架
            import pytest

            pytest.main(self.case.path)  # 執(zhí)行指定的測試用例文件

            self.status = 3  # 修改狀態(tài):執(zhí)行完畢
            self.run_datatime = datetime.datetime.now()
            super().save()

總結

到此這篇關于使用Django+Pytest搭建在線自動化測試平臺的文章就介紹到這了,更多相關Django+Pytest在線自動化測試平臺內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 淺析Python條件語句中的解密邏輯與控制流

    淺析Python條件語句中的解密邏輯與控制流

    這篇文章主要想來和大家一起探索一下Python條件語句的奇妙世界——解密邏輯與控制流,文中的示例代碼講解詳細,感興趣的小伙伴可以學習一下
    2023-07-07
  • mac安裝pytorch及系統(tǒng)的numpy更新方法

    mac安裝pytorch及系統(tǒng)的numpy更新方法

    今天小編就為大家分享一篇mac安裝pytorch及系統(tǒng)的numpy更新方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • python實現(xiàn)自動生成SQL語句

    python實現(xiàn)自動生成SQL語句

    在數(shù)據(jù)處理和管理中,SQL(Structured?Query?Language)是一種非常重要的語言,本文主要介紹了如何使用python實現(xiàn)自動生成SQL語句,需要的可以參考下
    2024-04-04
  • Python類裝飾器實現(xiàn)方法詳解

    Python類裝飾器實現(xiàn)方法詳解

    這篇文章主要介紹了Python類裝飾器實現(xiàn)方法,結合實例形式較為詳細的分析了Python類裝飾器的相關概念、原理、實現(xiàn)方法與使用技巧,需要的朋友可以參考下
    2018-12-12
  • Python tempfile模塊生成臨時文件和臨時目錄

    Python tempfile模塊生成臨時文件和臨時目錄

    這篇文章主要介紹了Python tempfile模塊生成臨時文件和臨時目錄,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • Python3 用matplotlib繪制sigmoid函數(shù)的案例

    Python3 用matplotlib繪制sigmoid函數(shù)的案例

    這篇文章主要介紹了Python3 用matplotlib繪制sigmoid函數(shù)的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Python函數(shù)學習筆記

    Python函數(shù)學習筆記

    Python探測局部作用域的時候:是在python編譯代碼時檢測,而不是通過他們在運行時的賦值。
    2008-10-10
  • Python利用PaddleOCR制作個搜題小工具

    Python利用PaddleOCR制作個搜題小工具

    PaddleOCR是一個基于百度飛槳的OCR工具庫,單模型支持中英文數(shù)字組合識別、豎排文本識別、長文本識別。本文將利用PaddleOCR開發(fā)一個搜題小工具,感興趣的可以了解一下
    2022-06-06
  • python中使用正則表達式的連接符示例代碼

    python中使用正則表達式的連接符示例代碼

    在正則表達式中,匹配數(shù)字或者英文字母的書寫非常不方便。因此,正則表達式引入了連接符“-”來定義字符的范圍,下面這篇文章主要給大家介紹了關于python中如何使用正則表達式的連接符的相關資料,需要的朋友可以參考下。
    2017-10-10
  • python 測試實現(xiàn)方法

    python 測試實現(xiàn)方法

    使用python進行測試也足夠簡明了
    2008-12-12

最新評論

蓝田县| 利川市| 如东县| 交口县| 定陶县| 阳城县| 武宣县| 乳源| 峨山| 珠海市| 神池县| 阿克苏市| 三河市| 江北区| 肇州县| 桐梓县| 辰溪县| 浮山县| 湖州市| 博白县| 日土县| 辽阳市| 萍乡市| 赤壁市| 临桂县| 衡阳市| 麻栗坡县| 黑河市| 黑龙江省| 靖安县| 含山县| 沙洋县| 武平县| 昔阳县| 兴义市| 阿尔山市| 南城县| 弥勒县| 老河口市| 仙游县| 潮州市|