python - 属性错误 : object has no attribute '_type_equality_funcs'

标签 python python-2.7

我的程序的 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'  

当我尝试创建一个公共(public)类并尝试通过公共(public)类实用程序测试函数执行时出现上述错误,但使用正常的 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 测试用例(这里例如测试用例没有删除行),以避免重复代码 我已经创建了公共(public)类 Utility,包括 test4_refactoring 函数,服务于所有测试用例,如 salesgrowth_DevInt。

下面是通用实用类代码

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.

最佳答案

self.assertEqual 将仅对继承 unittest.TestCase 类的类可用,而您的 Utility 类不会这样做。

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

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

关于python - 属性错误 : object has no attribute '_type_equality_funcs' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41978079/

相关文章:

python - 如何将 None 插入列表?

python - 从嵌套列表的字典理解返回字典

python - numpy,用数组替换列

python - 获取存储在python字符串中的数据类型

python - Visual Studio Code - Python 中的输入函数

python - Kafka-python 获取主题的分区数

python - python dict 的 len() 相对于 GIL 是原子的吗?

python - 如何在 Pyspark 数据帧中使用列表理解变量名称

python - 如何通过继承在两个不同的类中拆分函数?初学者

python - Python 中导入的类函数中的全局变量