上海扩展学习用

assertpy

功能强大,java里是有比较强大的异常处理的,但python也有这样的库
下午的时候抽空写着玩的(只写了很小一部分),真要用还需要高级封装class AssertNew(object):并让其继承与unittest.TestCase

# util/Assert_pyinfo.py
from assertpy import assert_that
import unittest

def except_trace(code):
    """
    保护处理的装饰器
    :param code: 需要保护业务代码
    :return:
    """
    def texcept(*args, **kwargs):
        try:
            return code(*args, **kwargs)
        except Exception as error:
\t\t#这里如果加截图()函数等于简单的截图装饰器
            print(format(error))
            return False
    return texcept


def mock_data(data):
\t"mock数据"
    if data.lower() =="d": #字典那边验证结果随机性
        data = [{"name": "chenziang","age":35},{ 'a':1,'b':2}]
        if isinstance(data, list): #扩展
            import random
            data=data[random.randint(0, 1)]

    elif data.lower() =="l":
        data = [123,123]
        if isinstance(data, list):
            pass
    elif data.lower() =="t":
        data = ((1, 2), (3, 4))
        if isinstance(data, tuple):
            pass
    elif data.lower() =="s":
        data = set('data_a')
        if isinstance(data, set): #变长
            data.add("data_b")
    else:
        data = ""
    return data
#print(mock_data("d"))


class AssertNew(object):
    "简单封装一下"

    def assertEqual_new(self,src_data,diss_data,msg=None):#class AssertNew
        try:
            assert_that(src_data).is_equal_to(diss_data)#class AssertNew
        except AssertionError as error:
            raise Exception('Expected <%s> to be not equal to <%s>, but was not.msg=<%s>' % (src_data,diss_data,msg))


    def assertIn_new(self,src_data,diss_data,msg=None):
        try:
            assert_that(diss_data).contains(src_data) #顺序需要颠倒
        except AssertionError as error:
            raise Exception('Expected <%s> to be contains to <%s>, but not in.msg=<%s>' % (src_data,diss_data,msg))

class TestDemo(unittest.TestCase):

    @except_trace
    def test_is_length(self):
        "判断复合数据类型的长度"
        assert_that(mock_data('l')).is_length(2)
        assert_that(mock_data('d')).is_length(2)
        assert_that(mock_data('t')).is_length(2)
        assert_that(mock_data('S')).is_length(5)
        #self.assertEqual()

    @except_trace
    def test_is_empty(self):
        assert_that(mock_data('1')).is_empty() #输出以外的

    @except_trace
    def test_is_not_empty(self):
        assert_that(mock_data('l')).is_not_empty()
        assert_that(mock_data('s')).is_not_empty()

    @except_trace
    def test_contains_key(self):
        "包含key"
        assert_that(mock_data('d')).contains_key("name")#随机成功失败

    @except_trace
    def test_contains_value(self):
        "包含value,还有不包含的"
        assert_that(mock_data('d')).contains_value(1, 2) #随机成功失败

    @except_trace
    def test_contains_not_value(self):
        "包含value,还有不包含的"
        assert_that(mock_data('d')).does_not_contain_value(35)  # 随机成功失败

    @except_trace
    def test_assertion_failure_str(self):
        fred = {'first_name': 'Fred', 'last_name': 'Smith', 'shoe_size': 12}
        #assert_that(fred).has_first_name('Fred')
        assert_that(mock_data("d")).has_first_name('name') # 随机成功失败

    @except_trace
    def test_assertEqual(self):
        "验证新封装的断言类相等性"
        AssertNew().assertEqual_new(1,1,msg="结果验证不相等")

    @except_trace
    def test_assertIn(self):
        "验证新封装的断言类包含性"
        AssertNew().assertIn_new(['a','b'],'a',msg="验证结果是否包含")

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