当前位置: 移动技术网 > IT编程>脚本编程>Python > Python unittest装饰器实现原理及代码

Python unittest装饰器实现原理及代码

2020年09月09日  | 移动技术网IT编程  | 我要评论
1、前言前面讲到unittest里面setup可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例时候都会重新打开,这样就会浪费很多时间。于是就想是不是可以

1、前言

前面讲到unittest里面setup可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例时候都会重新打开,这样就会浪费很多时间。

于是就想是不是可以只打开一次浏览器,执行完用例再关闭呢?这就需要用到装饰器(@classmethod)来解决了。

2、@classmethod 装饰器中的 setupclass() 和 teardownclass() 与 setup() 和 teardown() 的区别

  • setup():每个测试 case 运行之前运行
  • teardown():每个测试 case 运行完之后执行
  • setupclass():必须使用 @classmethod 装饰器, 所有 case 运行之前只运行一次
    • @是修饰符,classmethod 是 python 里的类方法
  • teardownclass():必须使用 @classmethod 装饰器, 所有 case 运行完之后只运行一次
import unittest

class test(unittest.testcase):

  @classmethod
  def setupclass(cls) -> none:
    print("-----开始执行用例-----")

  def setup(self) -> none:
    print("执行用例准备动作...")
  def test01(self):
    print("第一条用例正在执行...")
  def test02(self):
    print("第二条用例正在执行...")
  def teardown(self) -> none:
    print("用例执行完结动作...")
    
  @classmethod
  def teardownclass(cls) -> none:
    print("-----用例执行结束-----")
    
if __name__ == '__main__':
  unittest.main()

#执行结果如下:
"""
-----开始执行用例-----
执行用例准备动作...
第一条用例正在执行...
用例执行完结动作...
执行用例准备动作...
第二条用例正在执行...
用例执行完结动作...
-----用例执行结束-----
"""

from selenium import webdriver
import unittest
from time import sleep

class dymlogin(unittest.testcase):

  @classmethod
  def setupclass(cls) -> none:
    """
    所有的用例只需要执行一次打开浏览器操作
    """
    print("test start...")
    cls.dr = webdriver.chrome()
    cls.dr.get("http://www.duoyoumi.com/")
    cls.dr.implicitly_wait(10)
    cls.dr.maximize_window()
    cls.dr.find_element_by_class_name("login").click()
    sleep(1)

  @classmethod
  def teardownclass(cls) -> none:
    sleep(2)
    cls.dr.quit()
    print("test end...")

  def test_login01(self):
    """
    账号密码都为空
    """
    self.dr.find_element_by_name("loginname").clear()
    self.dr.find_element_by_name("loginpwd").clear()
    self.dr.find_element_by_xpath('//*[@id="loginform"]/div/div/div/div[2]/div[1]/div[2]').click()   #登录操作
    tipsmsg = self.dr.find_element_by_class_name("promptlogin").text
    self.assertequal(tipsmsg,"-请输入登录账号!",msg="test_login01 login exception")

  def test_login02(self):
    """
    账号正确,密码为空
    """
    self.dr.find_element_by_name("loginname").clear()
    self.dr.find_element_by_name("loginpwd").clear()
    self.dr.find_element_by_name("loginname").send_keys("17779828887")
    self.dr.find_element_by_xpath('//*[@id="loginform"]/div/div/div/div[2]/div[1]/div[2]').click() # 登录操作
    tipsmsg = self.dr.find_element_by_class_name("promptlogin").text
    self.assertequal(tipsmsg, "-请输入密码!", msg="test_login02 login exception")

  def test_login03(self):
    """
    账号密码都正确
    """
    self.dr.find_element_by_name("loginname").clear()
    self.dr.find_element_by_name("loginpwd").clear()
    self.dr.find_element_by_name("loginname").send_keys("17779828887")
    self.dr.find_element_by_name("loginpwd").send_keys("zy295240???")
    self.dr.find_element_by_xpath('//*[@id="loginform"]/div/div/div/div[2]/div[1]/div[2]').click()   #登录操作
    sleep(2)
    tipsmsg = self.dr.find_element_by_css_selector("#w_nav_temp_1000 > div.fr.dl > a:nth-child(4)").text
    self.assertequal(tipsmsg,"[退出]",msg="test_login03 login exception")

if __name__ == '__main__':
  unittest.main()
#执行结果如下
"""
test start...
...
test end...
----------------------------------------------------------------------
ran 3 tests in 25.253s

ok
"""

3、通过装饰器跳过某条用例

添加装饰器(@unittest.skip(""))

import unittest

class test(unittest.testcase):

  def setup(self) -> none:
    print("---start---")

  def teardown(self) -> none:
    print("---end---")

  def testbbb(self):
    print("case testbbb")

  @unittest.skip("testaaa 被跳过")
  def testaaa(self):
    print("case testaaa")

if __name__ == '__main__':
  unittest.main()

# 结果如下
s.
----------------------------------------------------------------------
ran 2 tests in 0.000s
ok (skipped=1)

---start---
case testbbb
---end---

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网