1. Installation of DDT
#pip Install DDT
2. Uninstall DDT
#coding = Utf-8#encoding = Utf-8ImportDDTImport TimeImportUnitTestImportLoggingImportTraceback fromSeleniumImportWebdriver fromSelenium.common.exceptionsImportNosuchelementexceptionlogging.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. DDTclassTestdemo (unittest. TestCase):defsetUp (self): Self.driver=Webdriver. Chrome () Self.driver.maximize_window () @ddt. Data ([u"where are the magical animals?", u"Yates"], [u"Crazy Animal City", u"Goodwin"], [u"The Moonlight Box of the West tour", u"Stephen Chow"]) @ddt. Unpackdeftest_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 (ExpectdatainchSelf.driver.page_source)exceptNoSuchElementException:logging.error (U"the page element found does not exist:"+Str (TRACEBACK.FORMAT_EXC ()))exceptAssertionError:logging.info (U"Search:%s, expected:%s, failed"%(TestData, expectdata))Else: Logging.info (U"Search:%s, expected:%s, by"%(TestData, expectdata))defTearDown (self): Self.driver.quit ()if __name__=='__main__': Unittest.main ()
Data-driven using unittest and DDT