• unittest测试套件


    测试套件就是测试集,测试集是测试用例的集合。

    a.按用例顺序执行(addtest)

    当addtest与unittest的测试规则冲突时,仍然按照ASCII码的顺序执行。

    import unittest
    from selenium import webdriver
    
    class Baidu_Test(unittest.TestCase):
        @classmethod
        def setUpClass(cls):
            cls.driver = webdriver.Chrome()
            cls.driver.maximize_window()
            cls.driver.implicitly_wait(30)
            cls.driver.get("http://www.baidu.com/")
    
        @classmethod
        def tearDownClass(cls):
            cls.driver.quit()
    
        def test_baidu_news(self):
            self.driver.find_element_by_link_text("新闻").click()
            print("aaa")
            self.driver.back()
    
        def test_baidu_map(self):
            self.driver.find_element_by_link_text("地图").click()
            print("bbb")
            self.driver.back()
    
    if __name__ == "__main__":
        suite = unittest.TestSuite()
        suite.addTest(Baidu_Test("test_baidu_news"))
        suite.addTest(Baidu_Test("test_baidu_map"))
        unittest.TextTestRunner().run(suite)

    我是在ubuntu16.04上安装的python3.5.2,运行上述代码后发现并不是先执行的 “test_baidu_news”这个测试用例,依然是先执行“test_baidu_map”再执行“test_baidu_news”

    执行结果如下:

    Ran 2 tests in 13.497s
    
    
    OK
    
    
    Process finished with exit code 0
    bbb
    aaa

    总结:在要按顺序执行时,可以将测试用例设置为test_001这样。

    b. 按测试类执行(makesuit)
    if __name__ == "__main__":
        suite = unittest.TestSuite(unittest.makeSuite(F2))
        unittest.TextTestRunner().run(suite)
    

    c.按测试模块来执行

    import unittest
    from selenium import webdriver
    
    class baidu(unittest.TestCase):
        def setUp(self):
            self.driver = webdriver.Chrome()
            self.driver.maximize_window()
            self.driver.implicitly_wait(30)
            self.driver.get("http://www.baidu.com/")
    
        def tearDown(self) -> None:
            self.driver.quit()
    
        def test_baidu_news(self):
            self.driver.find_element_by_link_text("新闻").click()
    
        def test_baidu_map(self):
            self.driver.find_element_by_link_text("地图").click()
    
    class baidu_search(unittest.TestCase):
        def setUp(self) -> None:
            self.driver = webdriver.Chrome()
            self.driver.maximize_window()
            self.driver.implicitly_wait(30)
            self.driver.get("http://www.baidu.com/")
    
        def tearDown(self) -> None:
            self.driver.quit()
    
        def test_badu_search(self):
            self.driver.find_element_by_id("kw").send_keys("english")
            self.driver.find_element_by_id("su").click()
    
    
    if __name__ == "__main__":
        suite = unittest.TestLoader().loadTestsFromModule("unittest_case4.py")
        unittest.TextTestRunner().run(suite)  
    d.优化测试套件

    第一种:

    import unittest
    from selenium import webdriver
    
    class baidu(unittest.TestCase):
        def setUp(self):
            self.driver = webdriver.Chrome()
            self.driver.maximize_window()
            self.driver.implicitly_wait(30)
            self.driver.get("http://www.baidu.com/")
    
        def tearDown(self) -> None:
            self.driver.quit()
    
        def test_baidu_news(self):
            self.driver.find_element_by_link_text("新闻").click()
    
        def test_baidu_map(self):
            self.driver.find_element_by_link_text("地图").click()
    
        @staticmethod
        def suite():
            suite = unittest.TestSuite(unittest.makeSuite(baidu))
            return suite
    
    if __name__ == "__main__":
         unittest.TextTestRunner().run(baidu.suite())
    

    第二种:把固定的语句分离出来

    测试用例模块:

    from test import * #倒入自己编写的模块
    import unittest
    
    class baidu(Info):   #继承
        def test_baidu_news(self):
            self.driver.find_element_by_link_text("新闻").click()
    
        def test_baidu_map(self):
            self.driver.find_element_by_link_text("地图").click()
    
    if __name__ == "__main__":
        unittest.main(verbosity=2)
    

     固定的套件模块(test.py):

    import unittest
    from selenium import webdriver
    class Info(unittest.TestCase):
        def setUp(self):
            self.driver = webdriver.Chrome()
            self.driver.maximize_window()
            self.driver.implicitly_wait(30)
            self.driver.get("http://www.baidu.com/")
        def tearDown(self) -> None:
            self.driver.quit()
    

      

  • 相关阅读:
    再谈HTTP通信
    【ZOJ】[1586]QS Network
    【ZOJ】[1586]QS Network
    【POJ】[2421]Constructing Roads
    【POJ】[2421]Constructing Roads
    【POJ】[2031]Building a Space Station
    【POJ】[2031]Building a Space Station
    【POJ】[1287]Networking
    【POJ】[1287]Networking
    【杭电】[1875]畅通工程再续
  • 原文地址:https://www.cnblogs.com/1510152012huang/p/10690300.html
Copyright © 2020-2023  润新知