Unnitest测试框架总结

时间:2021-04-29 21:07:04

Unnitest总结

第一点,setUp和tearDown方法

l  每次执行test开头的用例都会执行setUp和tearDown方法

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  结果:Unnitest测试框架总结

第二点: setUpClass和tearDownClass方法

l  如:

     import unittest
class Mydemo(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("调用setUpClass\n")
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  结果:Unnitest测试框架总结

第三点:定义全局的属性,可以放到setUpClass中testCase中均可去调用

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()

第四点: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\n")
    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  结果:

第三点:定义全局的属性,可以放到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()

第四点:testCase之间的执行顺序按照默认是以首字母排序的

l  执行顺序test1,test10,....test18,test2,..........test9