Unnitest总结
第一点,setUp和tearDown方法
l 每次执行test开头的用例都会执行setUp和tearDown方法
l 如:
1 import unittest 2 3 class Mydemo(unittest.TestCase): 4 def setUp(self): 5 print('调用setUp方法') 6 self.a = 1 7 def test1(self): 8 print("i am test1 the value of a is {}".format(self.a)) 9 def test2(self): 10 print("i am test2 the value of a is {}".format(self.a)) 11 def test3(self): 12 print("i am test3 the value of a is {}".format(self.a)) 13 def tearDown(self): 14 print("调用tearDown方法") 15 if __name__ == '__main__': 16 unittest.main()
n 结果:
第二点: setUpClass和tearDownClass方法
l 如:
1 import unittest 2 class Mydemo(unittest.TestCase): 3 @classmethod 4 def setUpClass(cls): 5 print("调用setUpClass ") 6 def test1(self): 7 print("i am test1") 8 def test2(self): 9 print("i am test2") 10 @classmethod 11 def tearDownClass(cls): 12 print("调用tearDownClass") 13 if __name__ == '__main__': 14 unittest.main()
l 结果:
第三点:定义全局的属性,可以放到setUpClass中testCase中均可去调用
l 如:
1 class NavigationTest(unittest.TestCase): 2 @classmethod 3 def setUpClass(cls): 4 cls.driver = webdriver.Chrome() 5 cls.driver.implicitly_wait(30) 6 cls.driver.maximize_window() 7 cls.driver.get('https://www.baidu.com/') 8 driver = cls.driver #定义在全局中 9 cls.search_field = driver.find_element_by_name('wd') #定义在全局中 10 11 def testBrowserNavigation(self): 12 self.search_field.clear() 13 print('执行test1') 14 15 self.search_field.send_keys('圣女果') 16 self.search_field.submit() 17 time.sleep(1) 18 19 self.assertEqual('圣女果_百度搜索',self.driver.title) 20 21 self.driver.back() 22 self.assertTrue(WebDriverWait(self.driver,30).until(expected_conditions.title_contains('百度一下'))) 23 def testBrowserNavigation2(self): 24 driver = self.driver 25 search_field = driver.find_element_by_name('wd') 26 search_field.clear() 27 print('执行test2') 28 29 search_field.send_keys('璎珞') 30 search_field.submit() 31 time.sleep(1) 32 @classmethod 33 def tearDownClass(cls): 34 driver = cls.driver 35 time.sleep(10) 36 driver.quit() 37 38 if __name__ == "__main__": 39 unittest.main() 40
第四点:testCase之间的执行顺序按照默认是以首字母排序的
l 执行顺序test1,test10,....test18,test2,..........test9
Unnitest总结
第一点,setUp和tearDown方法
l 每次执行test开头的用例都会执行setUp和tearDown方法
l 如:
l import unittest
class Mydemo(unittest.TestCase):
def setUp(self):
print('调用setUp方法')
self.a =
1
def test1(self):
print("i am test1 the value of a is {}".format(self.a))
def test2(self):
print("i am test2 the value of a is {}".format(self.a))
def test3(self):
print("i am test3 the value of a is {}".format(self.a))
def tearDown(self):
print("调用tearDown方法")
if __name__ == '__main__':
unittest.main()
n 结果:
第二点: setUpClass和tearDownClass方法
l 如:
l import unittest
class Mydemo(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("调用setUpClass ")
def test1(self):
print("i am test1")
def test2(self):
print("i am test2")
@classmethod
def tearDownClass(cls):
print("调用tearDownClass")
if __name__ == '__main__':
unittest.main()
l 结果:
n
第三点:定义全局的属性,可以放到setUpClass中testCase中均可去调用
l 如:
l class NavigationTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
cls.driver.implicitly_wait(30)
cls.driver.maximize_window()
cls.driver.get('https://www.baidu.com/')
driver = cls.driver #定义在全局中
cls.search_field = driver.find_element_by_name('wd') #定义在全局中
def testBrowserNavigation(self):
self.search_field.clear()
print('执行test1')
self.search_field.send_keys('圣女果')
self.search_field.submit()
time.sleep(1)
self.assertEqual('圣女果_百度搜索',self.driver.title)
self.driver.back()
self.assertTrue(WebDriverWait(self.driver,30).until(expected_conditions.title_contains('百度一下')))
def testBrowserNavigation2(self):
driver = self.driver
search_field = driver.find_element_by_name('wd')
search_field.clear()
print('执行test2')
search_field.send_keys('璎珞')
search_field.submit()
time.sleep(1)
@classmethod
def tearDownClass(cls):
driver = cls.driver
time.sleep(10)
driver.quit()
if __name__ == "__main__":
unittest.main()
n
第四点:testCase之间的执行顺序按照默认是以首字母排序的
l 执行顺序test1,test10,....test18,test2,..........test9