mydict.py
The code is as follows:
1 classDict (Dict):2 3 def __init__(Self, * *kw):4Super ().__init__(**kw)5 6 def __getattr__(self, key):7 Try:8 returnSelf[key]9 exceptKeyerror:Ten RaiseAttributeerror ( OneR"' Dict ' object has no attribute '%s '"%key) A - def __setattr__(self, Key, value): -Self[key] = value
Write the mydict_test.py
following:
1 ImportUnitTest2 3 fromDictImportDict4 5 6 classtestdict (unittest. TestCase):7 8 defsetUp (self):9 Print('setUp ...')Ten One defTearDown (self): A Print('TearDown ...') - - defTest_init (self): theD = Dict (A=1, b='Test') -Self.assertequal (D.A, 1) -Self.assertequal (D.B,'Test') - self.asserttrue (isinstance (d, dict)) + - defTest_key (self): +D =Dict () Ad['Key'] ='value' atSelf.assertequal (D.key,'value') - - deftest_attr (self): -D =Dict () -D.key ='value' -Self.asserttrue ('Key' inchd) inSelf.assertequal (d['Key'],'value') - to defTest_keyerror (self): +D =Dict () - With self.assertraises (keyerror): theValue = d['Empty'] * Print(value) $ Panax Notoginseng defTest_attrerror (self): -D =Dict () the With self.assertraises (attributeerror): +Value =D.empty A Print(value) the + - if __name__=='__main__': $Unittest.main ()
Python test Cases