【发布时间】:2019-12-19 13:01:42
【问题描述】:
我有这个功能要测试:
def get_django_model(django_model):
try:
app_config = apps.get_app_config("myapp")
model = app_config.get_model(django_model)
return model
except Exception:
raise DjangoModelMissing(f"Missing django model: {django_model}")
这是我的测试:
class ModelInstanceTest(TestCase):
def test_get_django_model(self):
model_class = get_djagno_model("Foo")
self.assertIsInstance(model_class, models.Foo)
上面的测试失败了,说AssertionError: <class 'models.Foo'> is not an instance of <class 'models.Foo'>。
但是,如果我将 assertIsInstance 替换为 assertIs,则测试通过。
有人能解释一下这里发生了什么吗?
这篇文章是相关的,但并没有真正解释不同的结果:Python test to check instance type。
【问题讨论】:
-
一个类确实不是该类的一个实例。
-
你的测试中还有一个错字
get_djagno_model(django)