seleinum+mongodb+allure+日志打印封装

seleinum+mongodb+allure+日志打印封装

  1. 创建一个初始化的方法,来连接mongodb数据库
    def __init__(self, database, collection):
        """
        初始化方法,MongoDB的连接配置
        :param database: 指定要连接的数据库
        :param collection: 指定要连接的集合
        """
        with allure.step("连接mongodb数据库,host:{},username:{},password:{}".format("数据库地址", "用户名", "密码")):
            self.client = pymongo.MongoClient(host=数据库地址, port=27017, username=用户名, password=密码)
            logger.info("连接数据库成功")
        with allure.step("获取对应的数据库:{}".format(database)):
            self.db = self.client[database]
            logger.info("获取对应的数据库 %s" % database)
        # 检查数据库是否存在
        db_list = self.client.list_database_names()
        if database in db_list:
            logger.info("数据库:%s 存在" % database)
        else:
            logger.error("数据库:%s 不存在" % database)
        with allure.step("获取对应的表:{}".format(collection)):
            self.col = self.db[collection]
            logger.info("获取对应的表 %s" % collection)
  1. 新增数据的公共方法封装
@allure.step("新增数据")
    def mo_insert(self, date, onlyOne=True):
        """
        新增数据
        :param date: 新增数据
        :param onlyOne: 是否只插入一条,默认为True,只插入一条
        :return:None
        """
        try:
            if not isinstance(onlyOne, bool):
                logger.info("onlyOne=(%s),若为True则新增一条数据,若为False则新增多条数据" % onlyOne)
                raise TypeError
            with allure.step("新增数据{}".format(date)):
                self.col.insert_one(date) if onlyOne else self.col.insert_many(date)
                logger.info("新增数据(%s)成功" % date)
        except Exception as e:
            logger.error("执行函数:mo_insert失败,错误信息 %s" % e)
  1. 更新数据公共方法的封装
@allure.step("修改数据")
    def mo_update(self, date, new_date, onlyOne=True):
        """
        修改数据
        :param date: 原来的数据
        :param new_date: 新数据
        :param onlyOne: 是否只修改一条
        :return: None
        """
        if not isinstance(onlyOne, bool):
            raise TypeError
        with allure.step("将原数据{}修改为{}".format(date, new_date)):
            self.col.update_one(date, {'$set': new_date}) if onlyOne else self.col.update_one(date, {'$set': new_date})
            logger.info("将原数据-%s-修改为-%s-" % (date, new_date))
  1. 查询数据公共方法封装
@allure.step("查询数据")
    def mo_find(self, query=None, onlyOne=True):
        """
        查询数据
        :param query:查询条件,默认为None,查询所有
        :param onlyOne: 是否只查询一条
        :return: None
        """
        try:
            if not isinstance(onlyOne, bool):
                logger.info("onlyOne=(%s),若为True则查询第一条数据,若为False则查询所有数据" % onlyOne)
                raise TypeError
            with allure.step("按照条件 {} 查询(若为空并且onlyOne为True则查询全部)".format(query)):
                res = self.col.find_one(query) if onlyOne else list(self.col.find(query))
                logger.info("查询成功,查询数据为-%s-" % res)
                return res
        except Exception as e:
            logger.error("执行函数:mo_find失败,错误信息 %s" % e)
  1. 删除数据公共方法封装
@allure.step("删除数据")
    def mo_deflect(self, date, onlyOne=True):
        """
        删除数据
        :param date:要删除的数据
        :param onlyOne:是否只删除一条
        :return:None
        """
        try:
            if not isinstance(onlyOne, bool):
                logger.info("onlyOne=(%s),若为True则删除第一条数据,若为False则删除所有数据" % onlyOne)
                raise TypeError
            with allure.step("删除此数据{}".format(date)):
                self.col.delete_one(date) if onlyOne else self.col.delete_many(date)
                logger.info("删除数据-%s-成功" % date)
        except Exception as e:
            logger.error("执行函数:mo_deflect失败,错误信息 %s" % e)

完整代码!!!

# mongodb数据库
import allure
import pymongo
from Utils.log import Log

logger = Log()


class MyMongoDB:
    def __init__(self, database, collection):
        """
        初始化方法,MongoDB的连接配置
        :param database: 指定要连接的数据库
        :param collection: 指定要连接的集合
        """
        with allure.step("连接mongodb数据库,host:{},username:{},password:{}".format("", "", "")):
            self.client = pymongo.MongoClient(host=, port=27017, username=, password=)
            logger.info("连接数据库成功")
        with allure.step("获取对应的数据库:{}".format(database)):
            self.db = self.client[database]
            logger.info("获取对应的数据库 %s" % database)
        # 检查数据库是否存在
        db_list = self.client.list_database_names()
        if database in db_list:
            logger.info("数据库:%s 存在" % database)
        else:
            logger.error("数据库:%s 不存在" % database)
        with allure.step("获取对应的表:{}".format(collection)):
            self.col = self.db[collection]
            logger.info("获取对应的表 %s" % collection)

    @allure.step("新增数据")
    def mo_insert(self, date, onlyOne=True):
        """
        新增数据
        :param date: 新增数据
        :param onlyOne: 是否只插入一条,默认为True,只插入一条
        :return:None
        """
        try:
            if not isinstance(onlyOne, bool):
                logger.info("onlyOne=(%s),若为True则新增一条数据,若为False则新增多条数据" % onlyOne)
                raise TypeError
            with allure.step("新增数据{}".format(date)):
                self.col.insert_one(date) if onlyOne else self.col.insert_many(date)
                logger.info("新增数据(%s)成功" % date)
        except Exception as e:
            logger.error("执行函数:mo_insert失败,错误信息 %s" % e)

    @allure.step("修改数据")
    def mo_update(self, date, new_date, onlyOne=True):
        """
        修改数据
        :param date: 原来的数据
        :param new_date: 新数据
        :param onlyOne: 是否只修改一条
        :return: None
        """
        if not isinstance(onlyOne, bool):
            raise TypeError
        with allure.step("将原数据{}修改为{}".format(date, new_date)):
            self.col.update_one(date, {'$set': new_date}) if onlyOne else self.col.update_one(date, {'$set': new_date})
            logger.info("将原数据-%s-修改为-%s-" % (date, new_date))

    @allure.step("查询数据")
    def mo_find(self, query=None, onlyOne=True):
        """
        查询数据
        :param query:查询条件,默认为None,查询所有
        :param onlyOne: 是否只查询一条
        :return: None
        """
        try:
            if not isinstance(onlyOne, bool):
                logger.info("onlyOne=(%s),若为True则查询第一条数据,若为False则查询所有数据" % onlyOne)
                raise TypeError
            with allure.step("按照条件 {} 查询(若为空并且onlyOne为False则查询全部)".format(query)):
                res = self.col.find_one(query) if onlyOne else list(self.col.find(query))
                logger.info("查询成功,查询数据为-%s-" % res)
                return res
        except Exception as e:
            logger.error("执行函数:mo_find失败,错误信息 %s" % e)

    @allure.step("删除数据")
    def mo_deflect(self, date, onlyOne=True):
        """
        删除数据
        :param date:要删除的数据
        :param onlyOne:是否只删除一条
        :return:None
        """
        try:
            if not isinstance(onlyOne, bool):
                logger.info("onlyOne=(%s),若为True则删除第一条数据,若为False则删除所有数据" % onlyOne)
                raise TypeError
            with allure.step("删除此数据{}".format(date)):
                self.col.delete_one(date) if onlyOne else self.col.delete_many(date)
                logger.info("删除数据-%s-成功" % date)
        except Exception as e:
            logger.error("执行函数:mo_deflect失败,错误信息 %s" % e)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值