Why do I get an AttributeError with Python3.4#39;s `unittest` library?(为什么我在使用Python3.4#39;的‘unitest’库时出现AttributeError?)
本文介绍了为什么我在使用Python3.4';的‘unitest’库时出现AttributeError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的代码:
import unittest
class Primes:
@staticmethod
def first(n):
<python code here>
class Test(unittest.TestCase):
def __init__(self):
pass
def assert_equals(self, l, m):
self.assertEqual(l, m)
Test = Test()
Test.assert_equals(Primes.first(1), [2])
每当我运行代码时,都会收到以下错误:
Traceback (most recent call last):
File "firstNPrimes.py", line 37, in <module>
Test.assert_equals(Primes.first(1), [2])
File "firstNPrimes.py", line 34, in assert_equals
self.assertEqual(l, m)
File "/usr/lib/python3.4/unittest/case.py", line 796, in assertEqual
assertion_func = self._getAssertEqualityFunc(first, second)
File "/usr/lib/python3.4/unittest/case.py", line 777, in _getAssertEqualityFunc
asserter = self._type_equality_funcs.get(type(first))
AttributeError: 'Test' object has no attribute '_type_equality_funcs'
我不明白这里有什么问题。
推荐答案
您收到该错误是因为您没有正确使用unittest
。根据the example in the docs,您的测试应如下所示:
import unittest
class TestPrimes(unittest.TestCase):
def test_singlePrime_returnsListContainingTwo(self):
self.assertEqual(Primes.first(1), [2])
def test_whateverCase_expectedOutcome(self):
self.assertEqual(Primes.first(...), ...)
if __name__ == '__main__': # optional, but makes import and reuse easier
unittest.main()
您不能只自己实例化测试用例类并调用方法,这会跳过所有测试发现和设置。
这篇关于为什么我在使用Python3.4';的‘unitest’库时出现AttributeError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:为什么我在使用Python3.4';的‘unitest’库时出现AttributeError?


猜你喜欢
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01