AttributeError: 对象没有属性“_type_equality_funcs" [英] AttributeError: object has no attribute '_type_equality_funcs'

查看:100
本文介绍了AttributeError: 对象没有属性“_type_equality_funcs"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序的 Unittest 模块实现出现以下错误

I am getting following error with Unittest module implement for my program

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 类实现没有错误.

when i am trying to create a common class and try to execute through common class utility test functions getting above errors but with normal Unittest class implementation no error was getting.

下面是程序的详细说明,没有任何错误的执行

below is detail explanation of program which executing without any errors

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 类并调用测试用例..

Now i am inheriting BaseTestCase class and calling testcases..

     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 包括 test4_refactoring 函数,用于所有测试用例,如 salesgrowth_DevInt .

now like salesgrowth_DevInt testcases there is no of other testcases which inheriting BaseTestCase and executing test4_refactoring testcases(here for example testcases no of lines removed) , to avoid duplication of code i have created common class Utility includes test4_refactoring function serving to all the testcases like 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 类没有做.

self.assertEqual will be only available to classes which inherits unittest.TestCase class, which your Utility class not doing.

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

I suggest try putting your Utility methods under BaseTestCase class.

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

Give it a name not starting with test_, later on call this new function to validate your asserts for numerous other functions.

这篇关于AttributeError: 对象没有属性“_type_equality_funcs"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆