首页 > 解决方案 > “AttributeError: '_ErrorHolder' 对象没有属性 '_testMethodName'” 错误

问题描述

我在运行测试时遇到了这个问题,我无法缩小范围,因为我对此有点陌生,我似乎无法弄清楚或在其他地方寻求帮助。测试执行后,如果出现错误,我通常会得到以下输出:

Traceback (most recent call last):
    File "./src/automated_tests/tests_launcher.py", line 215, in <module>
        pprint(result.jsonify())
    File "./src/automated_tests/tests_launcher.py", line 110, in jsonify
        json_out = self.json_append(t, ERROR, json_out, k)
    File "./src/automated_tests/tests_launcher.py", line 93, in json_append
        {LOGS: logs, STATUS: result, TITLE: test._testMethodName})
AttributeError: '_ErrorHolder' object has no attribute '_testMethodName'

引用的相关行是:

class JsonTestResult(TextTestResult):

    def __init__(self, stream, descriptions, verbosity=2):
        super(JsonTestResult, self).__init__(stream, descriptions, verbosity)
        self.successes = []

    def addSuccess(self, test):
        super(JsonTestResult, self).addSuccess(test)
        self.successes.append(test)

    def json_append(self, test, result, out, logs):
        suite = test.__class__.__name__
        if suite not in out:
            out[suite] = {TESTCASES: []}
        if result is PASS:
            out[suite][TESTCASES].append(
                {LOGS: logs, STATUS: result, TITLE: test._testMethodName})
        elif result is FAIL:
            out[suite][TESTCASES].append(
                {LOGS: logs, STATUS: result, TITLE: test._testMethodName})
        elif result is ERROR:
            out[suite][TESTCASES].append(
# LINE 93:
                {LOGS: logs, STATUS: result, TITLE: test._testMethodName})
        elif result is SKIP:
            out[suite][TESTCASES].append(
                {LOGS: logs, STATUS: result, TITLE: test._testMethodName})
        else:
            raise KeyError("No such result: {}".format(result))
        return out

    def jsonify(self):
        json_out = dict()
        for t in self.successes:
            json_out = self.json_append(t, PASS, json_out, None)

        for t, k in self.failures:
            json_out = self.json_append(t, FAIL, json_out, k)

        for t, k in self.errors:
# LINE 110:
            json_out = self.json_append(t, ERROR, json_out, k)

        for t, k in self.skipped:
            json_out = self.json_append(t, SKIP, json_out, k)

        if BROWSER == 'chrome' and PLATFORM == 'LINUX':
            output_path = "{0}/linux_chrome.json".format(OUTPUT_FILE)
            json.dump(json_out, open(output_path, 'w'))
        elif BROWSER == 'chrome' and PLATFORM == 'WINDOWS':
            output_path = "{0}/windows_chrome.json".format(OUTPUT_FILE)
            json.dump(json_out, open(output_path, 'w'))
        if BROWSER == 'chrome' and PLATFORM == 'MAC':
            output_path = "{0}/mac_chrome.json".format(OUTPUT_FILE)
            json.dump(json_out, open(output_path, 'w'))
        return json_out

if __name__ == '__main__':
    with open(os.devnull, 'w') as null_stream:
        runner = TextTestRunner(stream=null_stream)
        runner.resultclass = JsonTestResult

        suite = TestSuite([tests])

        # run the testsuite
        result = runner.run(suite)
        # print json output
# LINE 215:
        pprint(result.jsonify())

测试通过 makefile 命令运行并将结果输出到 json 文件。

标签: pythonseleniumpython-unittest

解决方案


它与测试套件和派生对象的构造函数有关,它没有正确调用超类的构造函数。使用 Python 2.7 可以通过以下方式重现错误:

import unittest

class T(unittest.TestCase):
    def __init__(self,x):
       # unittest.TestCase.__init__(self,x)
       pass

    def test_X(self):
        pass

if __name__ == '__main__':
    unittest.main()

如果对基类的构造函数的调用进行了注释,它对我有用。如果我根本不定义init ,它也可以工作。


推荐阅读