1. 程式人生 > >python--unittest

python--unittest

obj () pytho bsp style rtt att value except

#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