首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError:对象没有属性“”_type_equality_funcs“”

AttributeError:对象没有属性“”_type_equality_funcs“”
EN

Stack Overflow用户
提问于 2017-02-01 19:00:55
回答 1查看 4.1K关注 0票数 2

我在为我的程序实现Unittest模块时遇到了以下错误

代码语言:javascript
复制
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类实现时,没有得到错误。

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

代码语言:javascript
复制
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类并调用了测试用例。

代码语言:javascript
复制
     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测试用例(例如,测试用例没有删除任何行),为了避免代码重复,我创建了公共类实用工具,它包含了test4_refactoring函数来服务于所有的测试用例,比如salesgrowth_DevInt。

下面是常用的实用程序类代码

代码语言:javascript
复制
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.
EN

回答 1

Stack Overflow用户

发布于 2017-02-01 21:23:20

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

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

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

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41978079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档