动态改变unittest的setUpClass里的值

class Test(unittest.TestCase):

    @classmethod
    def  setUpClass(cls):
        cls.a = 1
    def test_01(self):
        Test.a = self.a + 1
        print(Test.a)
        print(self.a)

    def test_02(self):
        self.a = Test().a + 2
        print(self.a)

    def test_03(self):
        print(self.a)


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

打印:

2
2
4
2

还可以这样(setUpClass如果想要动态的改变某个值一定要使用python的可变的对象比如list,dict):

#coding=utf-8

import unittest

from  selenium import webdriver

class Mydemo(unittest.TestCase):

    @classmethod

    def setUpClass(cls):

        cls.a=[0]

    def test1(self):

        print "before update the a in test1 is:{}".format(self.a[0])

        self.a[0]=self.a[0]+1

        print "after update the a in test1 is:{}".format(self.a[0])

    def test2(self):

        print "the value in test2 is:{}".format(self.a[0])

    @classmethod

    def tearDownClass(cls):

        print "I am tearDownClass"

if __name__ == '__main__':

    unittest.main()

打印:

before update the a in test1 is:1

..

after update the a in test1 is:2

----------------------------------------------------------------------

the value in test2 is:1

I am tearDownClass

Ran 2 tests in 0.001s

如果是int,str,touple

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值