" AttributeError:' _ErrorHolder'对象没有属性' _testMethodName'"错误

时间:2018-05-01 22:46:41

标签: python selenium python-unittest

当我进行测试时,我一直遇到这个问题而且我无法缩小测试范围,因为我对此并不熟悉,我似乎无法弄清楚或找到它别的帮助。测试执行后,如果有错误,我通常会得到这个输出:

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文件。

1 个答案:

答案 0 :(得分:0)

它与测试套件和派生对象的构造函数有关,后者不能正确调用超类的构造函数。 使用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 ,它也可以工作。