zoukankan      html  css  js  c++  java
  • 使用unittest和ddt进行数据驱动

    1、安装ddt

    #pip install ddt
    

    2、卸载ddt

    # 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()
  • 相关阅读:
    .NET 异步详解
    spring batch简介
    Nginx 配置文件介绍
    局域网内组播
    qt自定义信号函数的那些坑
    传输文件到远程服务器
    vim复制指定行
    腾讯云获取公网ip
    ifconfig添加或删除ip
    程序中tar压缩文件
  • 原文地址:https://www.cnblogs.com/stevenx/p/9807340.html
Copyright © 2011-2022 走看看