代码之家  ›  专栏  ›  技术社区  ›  demouser123

从HTMLTestRunner中删除has_key

  •  0
  • demouser123  · 技术社区  · 10 年前

    我正在使用HTMLTestRunner为我的单元测试创建HTML报告。我想HTMLTestRunner的代码 here 是为python 2和之前版本编写和优化的,因为我遇到了三个关于与python 3不兼容的错误,例如使用 StringIO而不是io .

    现在,639行有一个 has_key 方法,在此代码段中

    def sortResult(self, result_list):
        # unittest does not seems to run in any particular order.
        # Here at least we want to group them together by class.
        rmap = {}
        classes = []
        for n,t,o,e in result_list:
            cls = t.__class__
            if not rmap.has_key(cls):
                rmap[cls] = []
                classes.append(cls)
            rmap[cls].append((n,t,o,e))
        r = [(cls, rmap[cls]) for cls in classes]
        return r
    

    由于python 3具有 has_key键 已从中删除 python 3 ,所以我在这方面出错了。由于我对python不太熟悉,我搜索发现 in 可以是合适的替代品。那么我该如何替换这个 has_ key 方法我试着简单地替换 has key 具有 在里面 但它失败了,并出现了无效的语法错误。

    1 回复  |  直到 10 年前
        1
  •  1
  •   Selcuk    10 年前

    而不是

    if not rmap.has_key(cls):
    

    尝试

    if cls not in rmap:
    

    您可以看到 docs 详细信息。