unittest详解 跳过用例的执行(skip)

在执行测试用例时,有时候有些用例是不需要执行的,那我们怎么办呢?难道删除这些用例?那下次执行时如果又需要执行这些用例时,又把它补回来?这样操作就太麻烦了。

unittest提供了一些跳过指定用例的方法

  • @unittest.skip(reason):强制跳转。reason是跳转原因
  • @unittest.skipIf(condition, reason):condition为True的时候跳转
  • @unittest.skipUnless(condition, reason):condition为False的时候跳转
  • @unittest.expectedFailure:如果test失败了,这个test不计入失败的case数目
    # coding = utf-8
    import unittest
    import warnings
    from selenium import webdriver
    from time import sleep
    # 驱动文件路径
    driverfile_path = r'D:\coship\Test_Framework\drivers\IEDriverServer.exe' class CmsLoginTest(unittest.TestCase):
    def setUp(self):
    # 这行代码的作用是忽略一些告警打印
    warnings.simplefilter("ignore", ResourceWarning)
    self.driver = webdriver.Ie(executable_path=driverfile_path)
    self.driver.get("http://172.21.13.83:28080/") def tearDown(self):
    self.driver.quit() @unittest.skip("用户名密码都为空用例不执行")
    def test_login1(self):
    '''用户名、密码为空'''
    self.driver.find_element_by_css_selector("#imageField").click()
    error_message1 = self.driver.find_element_by_css_selector("[for='loginName']").text
    error_message2 = self.driver.find_element_by_css_selector("[for='textfield']").text
    self.assertEqual(error_message1, '用户名不能为空')
    self.assertEqual(error_message2, '密码不能为空') @unittest.skipIf(3 > 2, "3大于2,此用例不执行")
    def test_login3(self):
    '''用户名、密码正确'''
    self.driver.find_element_by_css_selector("[name='admin.loginName']").send_keys("autotest")
    self.driver.find_element_by_css_selector("[name='admin.password']").send_keys("")
    self.driver.find_element_by_css_selector("#imageField").click()
    sleep(1)
    self.driver.switch_to.frame("topFrame")
    username = self.driver.find_element_by_css_selector("#nav_top>ul>li>a").text
    self.assertEqual(username,"autotest") @unittest.skipUnless(3 < 2,"2没有大于3,此用例不执行")
    def test_login2(self):
    '''用户名正确,密码错误'''
    self.driver.find_element_by_css_selector("[name='admin.loginName']").send_keys("autotest")
    self.driver.find_element_by_css_selector("[name='admin.password']").send_keys("")
    self.driver.find_element_by_css_selector("#imageField").click()
    error_message = self.driver.find_element_by_css_selector(".errorMessage").text
    self.assertEqual(error_message, '密码错误,请重新输入!') @unittest.expectedFailure
    def test_login4(self):
    '''用户名不存在'''
    self.driver.find_element_by_css_selector("[name='admin.loginName']").send_keys("test007")
    self.driver.find_element_by_css_selector("[name='admin.password']").send_keys("")
    self.driver.find_element_by_css_selector("#imageField").click()
    error_message = self.driver.find_element_by_css_selector(".errorMessage").text
    self.assertEqual(error_message, '用户名不存在!') def test_login5(self):
    '''用户名为空'''
    self.driver.find_element_by_css_selector("[name='admin.password']").send_keys("")
    self.driver.find_element_by_css_selector("#imageField").click()
    error_message = self.driver.find_element_by_css_selector("[for='loginName']").text
    self.assertEqual(error_message, '用户不存在!') def test_login6(self):
    '''密码为空'''
    self.driver.find_element_by_css_selector("[name='admin.loginName']").send_keys("autotest")
    self.driver.find_element_by_css_selector("#imageField").click()
    error_message = self.driver.find_element_by_css_selector("[for='textfield']").text
    self.assertEqual(error_message, '密码不能为空') if __name__ == "__main__":
    unittest.main(verbosity=2)
上一篇:搞懂分布式技术6:Zookeeper典型应用场景及实践


下一篇:空间转换及案例