Unittest: How to ensure equality of types, and not just equality of equality?

In python 2.7 unittest framework,

self.assertEquals(u"a","a")

does not work.

How to compare strings in unittest so that it is strnot equal unicode?

I do not want to replace every

self.assertEquals(foo(...),"a")

with

v = foo(...)
self.assertEquals(type(v),str)
self.assertEquals(v,"a")

I can’t use addTypeEqualityFuncit because my function will be called only if two objects have exactly the same typeobj, called exactly when I want it to be called.

+4
source share
1 answer

, ( , , ). , , :

def assertEqualsValType(self,obj1,obj2,msg=None):
    self.assertEquals(type(obj1),type(obj2),msg)
    self.assertEquals(obj1,obj2,msg)

, , :

def assertEqualsValType(self,obj1,obj2,msg=None): #alternative
    self.assertEquals((type(obj1),obj1),(type(obj2),obj2),msg)

, . ( , , , , ). , , , .

self.assertEqualsValType('a',u'a'), . , , : , self.assertEqualsValType(2,2.0) .

, ..

, , . :

class BaseUnitTest(unittest.TestCase):

    def assertEqualsValType(self,obj1,obj2,msg=None):
        self.assertEquals(type(obj1),type(obj2),msg)
        self.assertEquals(obj1,obj2,msg)

    def assertBetweenEqual(self, value, min_value, max_value):
        self.assertGreaterEqual(value, min_value)
        self.assertLessEqual(value, max_value)

    def assertFoo(self,para,meters):
        #...
        pass

.

+4

Source: https://habr.com/ru/post/1667072/


All Articles