码迷,mamicode.com
首页 > 编程语言 > 详细

python--unittest

时间:2018-01-28 00:01:49      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:bsp   utf-8   erro   equal   __init__   cep   setattr   python   att   

 

#https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143191629979802b566644aa84656b50cd484ec4a7838000

mydict.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 27 16:32:34 2018

@author: k
"""

class Dict(dict):
    def __init__(self,**kw):
        super().__init__(**kw)
        
    def __getattr__(self,key):
        try:
            return self[key]
        except KeyError:
            raise AttributeError(r"‘Dict‘ object has no attribute ‘%s‘"%key)
    
    def __setattr__(self,key,value):
        self[key] = value

        

 

test.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 27 22:51:23 2018

@author: k
"""

import unittest
from mydict import Dict

class TestDict(unittest.TestCase):
    def test_init(self):
        d = Dict(a=1,b=test)
        self.assertEqual(d.a,1)
        self.assertEqual(d.b,test)
        self.assertTrue(isinstance(d,dict))
    
    def test_key(self):
        d = Dict()
        d[key] = value
        self.assertEqual(d.key,value)
        
    def test_attr(self):
        d = Dict()
        d.key = value
        self.assertTrue(key in d)
        self.assertEqual(d[key],value)
        
    def test_keyerror(self):
        d = Dict()
        with self.assertRaises(KeyError):
            value = d[empty]
    def test_attrerror(self):
        d = Dict()
        with self.assertRaises(AttributeError):
            value = d.empty

if __name__==__main__:
    unittest.main()
    
            

 

python  test.py

 

python--unittest

标签:bsp   utf-8   erro   equal   __init__   cep   setattr   python   att   

原文地址:https://www.cnblogs.com/kprac/p/8367592.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!