在执行测试用例时,有时候有些用例是不需要执行的,那我们怎么办呢?难道删除这些用例?那下次执行时如果又需要执行这些用例时,又把它补回来?这样操作就太麻烦了。
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)
执行结果:
"C:\Program Files\Python36\python.exe" D:/Git/Test_Framework/utils/cmslogin.py
test_login1 (__main__.CmsLoginTest)
用户名、密码为空 ... skipped '用户名密码都为空用例不执行'
test_login2 (__main__.CmsLoginTest)
用户名正确,密码错误 ... skipped '2没有大于3,此用例不执行'
test_login3 (__main__.CmsLoginTest)
用户名、密码正确 ... skipped '3大于2,此用例不执行'
test_login4 (__main__.CmsLoginTest)
用户名不存在 ... expected failure
test_login5 (__main__.CmsLoginTest)
用户名为空 ... FAIL
test_login6 (__main__.CmsLoginTest)
密码为空 ... ok ======================================================================
FAIL: test_login5 (__main__.CmsLoginTest)
用户名为空
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:/Git/Test_Framework/utils/cmslogin.py", line 71, in test_login5
self.assertEqual(error_message, '用户不存在!')
AssertionError: '用户名不能为空' != '用户不存在!'
- 用户名不能为空
+ 用户不存在! ----------------------------------------------------------------------
Ran 6 tests in 32.663s FAILED (failures=1, skipped=3, expected failures=1) Process finished with exit code 1