Python自动化log日志学习与ddt数据驱动

本文详细介绍使用Selenium和Python进行自动化测试的方法,包括设置Firefox浏览器驱动,利用unittest框架组织测试用例,以及通过ddt模块实现数据驱动测试。通过具体示例展示了如何对百度搜索引擎进行关键字搜索的自动化测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from selenium import webdriver
import unittest,time
import logging,traceback
import ddt
from selenium.common.exceptions import NoSuchElementException
from testtoken01 import common0
logging.basicConfig(
    # 日志级别
    level= 10,
    #日志格式
    #时间、代码所在文件名、代码行号、日志级别名字、日志信息
    format= '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
    #打印日志时间
    datefmt = '%a ,%d %b %Y %H:%M:%S',
    filename = 'w'
)
@ddt.ddt
class TestDemo(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        time.sleep(5)
    @ddt.data(
            [u"神奇动物在哪里",u"耶茨"],
            [u"疯狂动物城",u"古德温"],
            [u"大话西游",u"周星驰"])
    @ddt.unpack
    def test_dataDrivenByObj(self,testdata,expectdata):
        url ='http://www.baidu.com'
        self.driver.get(url)
        time.sleep(10)
        find = common0.Basefunc(self.driver)
        try:
            loc01 = ('id','kw')
            find.findele_send(loc01,testdata)
            loc02 =('id','su')
            find.findele_click(loc02)
            time.sleep(10)
            self.assertTrue(expectdata in self.driver.page_source)
        except NoSuchElementException:
            logging.error(u'查找到的页面元素不存在,异常堆栈信息:'+str(traceback.format_exc()))
        except AssertionError :
            logging.info(u'搜索 %s ,期望%s,失败'  %(testdata,expectdata))
        except AssertionError :
            logging.error(u'未知错误,错误信息:' +str (traceback.format_exc()))
        else :
            logging.info(u'搜索%s,期望%s 通过'  %(testdata,expectdata))
    def tearDown(self):
            self.driver.quit()
if __name__ == '__main__':
    unittest.main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值