属性错误:对象没有属性“_type_equality_funcs”。

3

我在使用Unittest模块测试我的程序时遇到了以下错误:

File "/usr/lib/python2.7/unittest/case.py", line 493, in _getAssertEqualityFunc
    asserter = self._type_equality_funcs.get(type(first))
AttributeError: 'Utility' object has no attribute '_type_equality_funcs'  

当我尝试创建一个通用类并尝试通过通用类实用程序测试函数执行时,会出现以上错误。但是使用普通的Unittest类实现时没有出现任何错误。

以下是程序的详细解释,它可以在没有任何错误的情况下执行。

class BaseTestCase(unittest.TestCase):

    def __init__(self, methodName='runTest', param=None):
        super(BaseTestCase, self).__init__(methodName)
        self.param = param

    @staticmethod
    def parametrize(testcase_klass, param=None):

        testloader = unittest.TestLoader()
        testnames = testloader.getTestCaseNames(testcase_klass)
        suite = unittest.TestSuite()
        for name in testnames:
            suite.addTest(testcase_klass(name, param=param))
        return suite

现在我正在继承BaseTestCase类并调用测试用例..

     class salesgrowth_DevInt(BaseTestCase):
          def setUp(self):
                print "constructor"
                pwd = os.getcwd()

     def test4_refactoring(self,log):
             if (STATUS.lower() == "completed" or STATUS == "Actor : SUCCESS"):`enter code here`
                  self.assertEqual(os.stat(OUTPUT + '/tes1.txt').st_size, 0,
                 'employee count  is not matching with master data . Different  entries are in test1.txt\n')

到目前为止,一切正常。

现在有许多其他测试用例,例如salesgrowth_DevInt测试用例,它们继承BaseTestCase并执行test4_refactoring测试用例(例如这里删除了几行代码),为避免重复的代码,我创建了一个通用类Utility,其中包括服务于所有测试用例(如salesgrowth_DevInt)的test4_refactoring函数。

以下是通用工具类代码:

import sys
import json, sys, os, argparse, commands, time, string, filecmp
import unittest

class Utility(object):
    ''' common utility class for common test cases  operations'''

    def __init__(self):
        print "constructor"
        pwd = os.getcwd()
        print "Current working directlry %s\n" % pwd
        global scriptpath
        scriptpath = os.path.join(pwd, "src/Runner/")
        maxDiff = int(80)


     def test4_refactoring(self,STATUS,BASE,ANALYSIS_DIR,OUTPUT,log):
            print "common function"
            log.write('\n')
             if (STATUS.lower() == "completed" or STATUS == "Actor : SUCCESS"):
                  self.assertEqual(os.stat(OUTPUT + '/tes1.txt').st_size, 0,
                 'employee count  is not matching with master data . Different  entries are in test1.txt\n')




     but using utility code when i try to execute below statment
     self.assertEqual(os.stat(OUTPUT + '/tes1.txt').st_size, 0,
                 'employee count  is not matching with master data . Different  entries are in test1.txt\n') 


    getting below errors


Traceback (most recent call last):
  File "/src/testCases/salesgrowth_DevInt.py", line 96, in test4_refactoring
    utils_obj.test4_refactoring(self.STATUS,self.BASE,self.ANALYSIS_DIR,self.OUTPUT,log)
  File "/src/common/Utils.py", line 436, in test4_refactoring
    'employee count  is not matching with master data. Different entries are in test1.txt\n')
  File "/usr/lib/python2.7/unittest/case.py", line 512, in assertEqual
    assertion_func = self._getAssertEqualityFunc(first, second)
  File "/usr/lib/python2.7/unittest/case.py", line 493, in _getAssertEqualityFunc
    asserter = self._type_equality_funcs.get(type(first))
AttributeError: 'Utility' object has no attribute '_type_equality_funcs'




 Please let me know if any one has any pointers or suggestion for above issue and what is wrong in above implementation.

很棒的问题,我面临同样的问题,你为我解决了,感谢你让Stackoverflow帮助我 :) - undefined
1个回答

5

self.assertEqual仅适用于继承了unittest.TestCase类的类,而您的Utility类没有继承该类。

我建议将您的Utility方法放在BaseTestCase类下。

给它起一个不以test_开头的名称,随后调用这个新函数来验证其他多个函数的断言。


很好地解释答案 - undefined
3
为什么 assertEqual 是唯一具有这种行为的断言?当其他 unittest TestCase 断言是继承 unittest 的类的子类时,它们都可以正常调用。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接