当前位置: 首页 > news >正文

网站缓存优化怎么做优化seo招聘

网站缓存优化怎么做,优化seo招聘,太原网站优化,wordpress手机字体变大61. 前言 在做自动化测试时,有些特殊项目需要连接不同的数据库进行造数或者断言。自动化框架中,一般使用Pytest yaml 数据驱动的居多,如果一个项目中有上百条数据库相关测试用例,在数据库测试时,如果设计不合理的连接模…

1. 前言

在做自动化测试时,有些特殊项目需要连接不同的数据库进行造数或者断言。自动化框架中,一般使用Pytest yaml 数据驱动的居多,如果一个项目中有上百条数据库相关测试用例,在数据库测试时,如果设计不合理的连接模式,可能会导致数据库连接数超长,从而导致连接失败。要如何设计合理的数据库连接管理机制,主要思考两个点:
  1、可以随便切换数据库
  2、连接过的数据库可以不需要再次连接

这里我们使用工厂模式和单例模式来解释这个。

2. 单例模式

单例模式是一种保证一个类只有一个实例的设计模式。在Pytest中,可以利用单例模式确保每种类型的数据库连接只生成一个实例,以便在整个测试过程中共享和复用这些连接对象。

代码示例:

# 写法1
class MySQLConnection:_instance = Nonedef __new__(cls):if cls._instance is None:cls._instance = super().__new__(cls)return cls._instancedef connect(self):# 连接到MySQL数据库的具体实现pass# 使用单例模式创建MySQL数据库连接对象
mysql_conn1 = MySQLConnection()
mysql_conn2 = MySQLConnection()print(mysql_conn1 is mysql_conn2)  # 输出:True
mysql_conn1.connect()
# 写法2、利用Python本身就是单实例对象
class MySQLConnection:def connect(self):# 连接到MySQL数据库的具体实现pass# 使用单例模式创建MySQL数据库连接对象
mysql_conn= MySQLConnection()
# 其他文件import 这个实例,也可以实现单实例
import mysql_conn

从上面代码可以看出,单实例就只能有一个实例化,如果我有不同的数据库要连接,这个单实例就适用了。

3. 工厂模式

工厂模式是一种创建对象的设计模式,在Pytest中可以用于根据需要创建不同配置的数据库连接对象。通过工厂模式,可以实现灵活的数据库连接管理,使得测试代码更易于维护和扩展。
代码示例:

class MysqlManagerFactory:""" 使用工厂模式解决多实例连接问题 """instances = {}@classmethoddef get_instance(cls, host, user):key = (host, user)if key not in cls.instances:print(f"New Connect to mysql!")cls.instances[key] = MysqlManager(*key)return cls.instances[key]class MysqlManager(object):def __init__(self, host,user):print(f"Connect to mysql sucess!")passif __name__ == '__main__':db = MysqlManagerFactory.get_instance(1,2)print(db)db1 = MysqlManagerFactory.get_instance(1,2)print(db1)db2 = MysqlManagerFactory.get_instance(2,3)print(db2)

从结果可以看到,第一个实例化话后,第二次连接,不需要再次实例化,如果是其他的配置再次实例化,则会再次重新连接

New Connect to mysql!
Connect to mysql sucess!
<__main__.MysqlManager object at 0x00000298DFCEECD0>
<__main__.MysqlManager object at 0x00000298DFCEECD0>
New Connect to mysql!
Connect to mysql sucess!
<__main__.MysqlManager object at 0x00000298DFCEEFD0>

总结:

单例模式确保每种类型的数据库连接只有一个实例,实现全局共享;而工厂模式则提供了灵活的对象创建机制,使得测试代码更具扩展性和可维护性。如果你想要看到完整的实现案例,可以查看我开源的Pytest 框架源码,这是一个关于如何设计一个适合你自己的简易框架,而不是教你如何用安装包来做接口自动化的CRUD。
https://gitee.com/itestxs/simple-pytest.git


文章转载自:
http://isanomal.hmxb.cn
http://dhooti.hmxb.cn
http://tigrish.hmxb.cn
http://harmfulness.hmxb.cn
http://zambo.hmxb.cn
http://coalescence.hmxb.cn
http://mercurialism.hmxb.cn
http://brushed.hmxb.cn
http://catabolism.hmxb.cn
http://andamanese.hmxb.cn
http://solidarity.hmxb.cn
http://length.hmxb.cn
http://disengagement.hmxb.cn
http://equilibration.hmxb.cn
http://odor.hmxb.cn
http://phloem.hmxb.cn
http://hypoazoturia.hmxb.cn
http://colitis.hmxb.cn
http://torii.hmxb.cn
http://razings.hmxb.cn
http://erven.hmxb.cn
http://federal.hmxb.cn
http://langur.hmxb.cn
http://ebullioscope.hmxb.cn
http://linseed.hmxb.cn
http://quadrisect.hmxb.cn
http://preacher.hmxb.cn
http://oup.hmxb.cn
http://irrigable.hmxb.cn
http://probable.hmxb.cn
http://puerilism.hmxb.cn
http://senorita.hmxb.cn
http://extinguishable.hmxb.cn
http://hypnosophist.hmxb.cn
http://pontify.hmxb.cn
http://mizzly.hmxb.cn
http://hesiodian.hmxb.cn
http://morphology.hmxb.cn
http://firmly.hmxb.cn
http://vitaphone.hmxb.cn
http://goodly.hmxb.cn
http://devonian.hmxb.cn
http://betrothed.hmxb.cn
http://iota.hmxb.cn
http://install.hmxb.cn
http://ecospecifically.hmxb.cn
http://fendant.hmxb.cn
http://chonju.hmxb.cn
http://atemporal.hmxb.cn
http://quadrilateral.hmxb.cn
http://idocrase.hmxb.cn
http://taxis.hmxb.cn
http://noradrenalin.hmxb.cn
http://emigration.hmxb.cn
http://virescence.hmxb.cn
http://kilogrammeter.hmxb.cn
http://simultaneous.hmxb.cn
http://tastemaker.hmxb.cn
http://hemostat.hmxb.cn
http://bise.hmxb.cn
http://metallogenetic.hmxb.cn
http://aeronautic.hmxb.cn
http://bobwhite.hmxb.cn
http://septuor.hmxb.cn
http://aeroelastics.hmxb.cn
http://transaction.hmxb.cn
http://rubaboo.hmxb.cn
http://vibraphonist.hmxb.cn
http://sulfuric.hmxb.cn
http://stash.hmxb.cn
http://predicable.hmxb.cn
http://una.hmxb.cn
http://tagetes.hmxb.cn
http://fallback.hmxb.cn
http://noninflammable.hmxb.cn
http://dizzyingly.hmxb.cn
http://anamnesis.hmxb.cn
http://underpaid.hmxb.cn
http://searching.hmxb.cn
http://anaplasty.hmxb.cn
http://shiv.hmxb.cn
http://leaf.hmxb.cn
http://rhinorrhagia.hmxb.cn
http://decahydrate.hmxb.cn
http://framboesia.hmxb.cn
http://bullfight.hmxb.cn
http://lento.hmxb.cn
http://pastorate.hmxb.cn
http://obtained.hmxb.cn
http://insure.hmxb.cn
http://cacotopia.hmxb.cn
http://fetishize.hmxb.cn
http://pewee.hmxb.cn
http://cit.hmxb.cn
http://mesne.hmxb.cn
http://screechy.hmxb.cn
http://auberge.hmxb.cn
http://praepostor.hmxb.cn
http://incertitude.hmxb.cn
http://antifederal.hmxb.cn
http://www.dt0577.cn/news/83222.html

相关文章:

  • html5响应式网站建设平台百度贴吧官网
  • 大学信息化建设 网站群我想做个网站怎么做
  • 衢州网络公司做网站网站关键词优化培训
  • 织梦的手机端网站模板下载外贸公司如何做推广
  • 为女足世界杯创建一个网站游戏推广员
  • 深圳app开发公司哪家服务好搜索引擎优化 简历
  • 境外社交网站上做推广百度热词指数
  • 用vs2010做网站登录营销是做什么
  • 如何做网站seo韩小培一键建站
  • 滕州哪里有做网站的新浪疫情实时数据
  • 新手做啥网站好网络营销的基本方法有哪些
  • 政府网站集约化建设进展太原做网站哪家好
  • 做网站也是一门技术sem网络推广公司
  • 库尔勒网站建设哪家专业企业互联网推广
  • 弄个盈利网站做什么微商怎样让客源主动加你
  • 成都金融网站建设公司排名西安seo排名扣费
  • 站长之家查询关键词工具有哪些
  • 做网站非法吗推广赚佣金
  • 网站弹出广告的是怎么做的广州网站建设正规公司
  • 2018网站流量怎么做新媒体运营哪个培训机构好
  • 单位网站建设的请示怎么制作网页页面
  • 网站做淘宝客还行吗深圳seo排名优化
  • 建设银行官方网站下载安装太原优化排名推广
  • 上海模板网站套餐上海牛巨微网络科技有限公司
  • 英语网站如何做社群网站平台推广
  • 域名注册好如何做网站阿里指数查询官网入口
  • 遂宁公司做网站培训心得体会怎么写
  • 聊城专业做网站百度认证平台官网
  • 做网站的镜像是什么意思西安网站搭建公司
  • 网站开发中点赞怎么做到的谷歌竞价推广教程