测试一个类是否继承自另一个类 [英] Test if a class is inherited from another

查看:35
本文介绍了测试一个类是否继承自另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题比 Django 更与 Python 相关.我想测试为这个函数编写一个测试,我用它来动态获取带有我设置的字段的 Django 表单.

This question is more Python related than Django related. I want to test write a test for this function that I am using to get a Django form dynamically with the fields I set.

def quiz_form_factory(question):
    properties = {
        'question': forms.IntegerField(widget=forms.HiddenInput, initial=question.id),
        'answers': forms.ModelChoiceField(queryset=question.answers_set)
    }
    return type('QuizForm', (forms.Form,), properties)

我想测试一下,返回的 QuizForm 类是否继承自 forms.Form.

I want to test if, the QuizForm class returned is inherited from forms.Form.

类似于:

self.assertTrue(QuizForm isinheritedfrom forms.Form)  # I know this does not exist

有没有办法做到这一点?

Is there any way to do this?

推荐答案

使用 issubclass(myclass, 父类).

就你而言:

self.assertTrue( issubclass(QuizForm, forms.Form) )

这篇关于测试一个类是否继承自另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆