首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >使用unittest和ddt进行数据驱动

使用unittest和ddt进行数据驱动

作者头像
py3study
发布2020-01-19 16:34:07
发布2020-01-19 16:34:07
8420
举报
文章被收录于专栏:python3python3

1、安装ddt

代码语言:javascript
复制
#pip install ddt

2、卸载ddt

代码语言:javascript
复制
# coding = utf-8
# encoding = utf-8
import ddt
import time
import unittest
import logging
import traceback
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
    datefmt='%a %d %b %Y %H: %M: %S',
    filename='D://pytest//test//report.log',
    filemode='w'
)


@ddt.ddt
class TestDemo(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()

    @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)
        self.driver.implicitly_wait(10)
        try:
            self.driver.find_element_by_id("kw").send_keys(testdata)
            self.driver.find_element_by_id("su").click()
            time.sleep(3)
            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))
        else:
            logging.info(u"搜索:%s,期望:%s,通过" % (testdata, expectdata))

    def tearDown(self):
        self.driver.quit()


if __name__ == '__main__':
    unittest.main()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/03/19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档