unittest中的setUpClass()与tearDownClass()中对象的运用

注意:setUpClass(cls)与tearDownClass(cls): 在写的时候后边括号内时cls 而不是self。如果想要函数内的变量在 类中使用的话 前边需加cls

import unittest
from selenium import webdriver
from time import sleep
class MyTestCase(unittest.TestCase):
    @classmethod	
    def setUpClass(cls):
        # 创建web服务
        cls.driver = webdriver.Chrome()  # 注意  这里前边加cls. 使driver可以在类的其他方法中使用
        # 打开浏览器
        cls.driver.get('https://www.baidu.com')
        cls.title = None

    @classmethod
    def tearDownClass(cls):
    	#关闭浏览器
        cls.driver.quit()      #这里使用driver 前边是使用cls.

    def test_01(self):
    
        input_ = self.driver.find_element('id',"kw") #这里使用driver 前边是使用self.
        input_.clear()
        input_.send_keys('自动化学习')
        self.driver.find_element('id', "su").click()
        sleep(3)
        print (self.title)
        #如果要修改cls.title的值,在全局生效的话,使用 MyTestCase.title (类名.对象)
        MyTestCase.title =self.driver.title
        print (self.title)
        
	def test_02(self):
	
        input_ = self.driver.find_element('id',"kw") #这里使用driver 前边是使用self.
        input_.clear()
        input_.send_keys('自动化学习2')
        self.driver.find_element('id', "su").click()
        sleep(3)
        print (self.title)
        #如果要修改cls.title的值,在全局生效的话,使用 MyTestCase.title  (类名.对象)
        MyTestCase.title =self.driver.title
        print (self.title)




if __name__ == '__main__':
    unittest.main()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值