unittest例項

藕丝鲜芋發表於2024-08-17

例項1:執行所有的用例

import  unittest  #匯入unittest框架
from   selenium import  webdriver  #匯入selenium
from  time import  *
class  Cms(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        pass
    @classmethod
    def tearDownClass(cls) -> None:
        pass
    def setUp(self) -> None:
        self.dx=webdriver.Chrome()
        self.dx.get("http://cms.duoceshi.cn/manage/login.do")
        self.dx.maximize_window()
        self.dx.implicitly_wait(2)
        self.dx.find_element_by_id("userAccount").send_keys("admin")  # 輸入賬號
        sleep(2)
        self.dx.find_element_by_id("loginPwd").send_keys("123456")  # 輸入密碼
        sleep(2)
        self.dx.find_element_by_id("loginBtn").click()  # 點選登入
    def  tearDown(self) -> None:
         sleep(10)
         self.dx.close()
    def  test1_tc(self):
        self.dx.find_element_by_xpath("/html/body/header/span[2]/a").click()
    def   test2_yhgl(self):
        self.dx.find_element_by_xpath('//*[@id="menu-user"]/dt/i').click() #使用者中心
        sleep(2)
        self.dx.find_element_by_link_text("使用者管理").click()
    def  testA_xtgl(self):
        self.dx.find_element_by_xpath('//*[@id="menu-system"]/dt/i').click()
    def  wzgl(self):
        self.dx.find_element_by_xpath('//*[@id="menu-system"]/dt/i').click()
        sleep(2)
        self.dx.find_element_by_link_text("文章管理").click()
if __name__ == '__main__':
    unittest.main()

二、執行單個用例或部分用例

import  unittest  #匯入unittest框架
from   selenium import  webdriver  #匯入selenium
from  time import  *
class  Cms(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        pass
    @classmethod
    def tearDownClass(cls) -> None:
        pass
    def setUp(self) -> None:
        self.dx=webdriver.Chrome()
        self.dx.get("http://cms.duoceshi.cn/manage/login.do")
        self.dx.maximize_window()
        self.dx.implicitly_wait(2)
        self.dx.find_element_by_id("userAccount").send_keys("admin")  # 輸入賬號
        sleep(2)
        self.dx.find_element_by_id("loginPwd").send_keys("123456")  # 輸入密碼
        sleep(2)
        self.dx.find_element_by_id("loginBtn").click()  # 點選登入
    def  tearDown(self) -> None:
         sleep(10)
         self.dx.close()
    def  test1_tc(self):
        self.dx.find_element_by_xpath("/html/body/header/span[2]/a").click()
    def   test2_yhgl(self):
        self.dx.find_element_by_xpath('//*[@id="menu-user"]/dt/i').click() #使用者中心
        sleep(2)
        self.dx.find_element_by_link_text("使用者管理").click()
    def  testA_xtgl(self):
        self.dx.find_element_by_xpath('//*[@id="menu-system"]/dt/i').click()
    def  wzgl(self):
        self.dx.find_element_by_xpath('//*[@id="menu-system"]/dt/i').click()
        sleep(2)
        self.dx.find_element_by_link_text("文章管理").click()
if __name__ == '__main__':
    # unittest.main()
    tj=unittest.TestSuite()
    tj.addTest(Cms("test2_yhgl"))
    unittest.TextTestRunner().run(tj)

三、執行一個路徑下的用例

import  unittest  #匯入unittest框架
from   selenium import  webdriver  #匯入selenium
from  time import  *
class  Cms(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        pass
    @classmethod
    def tearDownClass(cls) -> None:
        pass
    def setUp(self) -> None:
        self.dx=webdriver.Chrome()
        self.dx.get("http://cms.duoceshi.cn/manage/login.do")
        self.dx.maximize_window()
        self.dx.implicitly_wait(2)
        self.dx.find_element_by_id("userAccount").send_keys("admin")  # 輸入賬號
        sleep(2)
        self.dx.find_element_by_id("loginPwd").send_keys("123456")  # 輸入密碼
        sleep(2)
        self.dx.find_element_by_id("loginBtn").click()  # 點選登入
    def  tearDown(self) -> None:
         sleep(10)
         self.dx.close()
    def  test1_tc(self):
        self.dx.find_element_by_xpath("/html/body/header/span[2]/a").click()
    def   test2_yhgl(self):
        self.dx.find_element_by_xpath('//*[@id="menu-user"]/dt/i').click() #使用者中心
        sleep(2)
        self.dx.find_element_by_link_text("使用者管理").click()
    def  testA_xtgl(self):
        self.dx.find_element_by_xpath('//*[@id="menu-system"]/dt/i').click()
    def  wzgl(self):
        self.dx.find_element_by_xpath('//*[@id="menu-system"]/dt/i').click()
        sleep(2)
        self.dx.find_element_by_link_text("文章管理").click()
if __name__ == '__main__':
    # unittest.main()
    # tj=unittest.TestSuite()
    # tj.addTest(Cms("test2_yhgl"))
    # unittest.TextTestRunner().run(tj)
    lj=r"C:\Users\Administrator.USER-20231114RR\PycharmProjects\untitled8\bao"
    d=unittest.TestLoader().discover(start_dir=lj,pattern="kj3*.py")
    unittest.TextTestRunner().run(d)

第四執行並生成自動化測試報告模板:

import  unittest  #匯入unittest框架
from   selenium import  webdriver  #匯入selenium
from  time import  *
from  bao.HTMLTestRunnerNew import HTMLTestRunner
class  Cms(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        pass
    @classmethod
    def tearDownClass(cls) -> None:
        pass
    def setUp(self) -> None:
        self.dx=webdriver.Chrome()
        self.dx.get("http://cms.duoceshi.cn/manage/login.do")
        self.dx.maximize_window()
        self.dx.implicitly_wait(2)
        self.dx.find_element_by_id("userAccount").send_keys("admin")  # 輸入賬號
        sleep(2)
        self.dx.find_element_by_id("loginPwd").send_keys("123456")  # 輸入密碼
        sleep(2)
        self.dx.find_element_by_id("loginBtn").click()  # 點選登入
    def  tearDown(self) -> None:
         sleep(10)
         self.dx.close()
    def  test1_tc(self):
        self.dx.find_element_by_xpath("/html/body/header/span[2]/a").click()
    def   test2_yhgl(self):
        self.dx.find_element_by_xpath('//*[@id="menu-user"]/dt/i').click() #使用者中心
        sleep(2)
        self.dx.find_element_by_link_text("使用者管理").click()
    def  testA_xtgl(self):
        self.dx.find_element_by_xpath('//*[@id="menu-system"]/dt/i').click()
    def  wzgl(self):
        self.dx.find_element_by_xpath('//*[@id="menu-system"]/dt/i').click()
        sleep(2)
        self.dx.find_element_by_link_text("文章管理").click()
if __name__ == '__main__':
    # unittest.main()
    # tj=unittest.TestSuite()
    # tj.addTest(Cms("test2_yhgl"))
    # unittest.TextTestRunner().run(tj)
    # lj=r"C:\Users\Administrator.USER-20231114RR\PycharmProjects\untitled8\bao"
    # d=unittest.TestLoader().discover(start_dir=lj,pattern="kj3*.py")
    # unittest.TextTestRunner().run(d)
    lj=r"C:\Users\Administrator.USER-20231114RR\PycharmProjects\untitled8\bao"
    bglj = r"C:\Users\Administrator.USER-20231114RR\PycharmProjects\untitled8\bao"
    d=unittest.TestLoader().discover(start_dir=lj,pattern="kj3*.py")
    new=strftime("%y-%m-%d %H-%M-%S")
    file=bglj+"/"+str(new)+"__ui.html"
    f=open(file,"bw")
    r=HTMLTestRunner(
        stream=f,title="ui自動化測試報告",description="執行情況",tester="dcs"
    )
    r.run(d)

相關文章