python--unittest

Posted kprac

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python--unittest相关的知识,希望对你有一定的参考价值。

 

#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的主要内容,如果未能解决你的问题,请参考以下文章

python——unittest(单元测试)

Python Unittest Discover即使失败也返回退出代码0

Python unittest模块心得

接口自动化1.0 python+unittest

python unittest框架装饰器

Python Unittest与数据驱动