---
slug: "django-unittest-abstract-model"
title: "Unit Testing Abstract Models in Django"
description: "Django: Best way to unit-test an abstract model - Stack Overflowhttps://stackoverflow.com/questions/4281670/django-best-way-to-unit-test-an-abstract-model"
url: "https://www.ytyng.com/en/blog/django-unittest-abstract-model"
publish_date: "2018-09-25T10:10:09Z"
created: "2018-09-25T10:10:09Z"
updated: "2026-02-26T14:16:23.071Z"
categories: ["Django"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20230812/63fb2f0052784727b50f3ac861e43fe9.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "en"
---

# Unit Testing Abstract Models in Django

<p>Django: Best way to unit-test an abstract model - Stack Overflow<br /><a href="https://stackoverflow.com/questions/4281670/django-best-way-to-unit-test-an-abstract-model">https://stackoverflow.com/questions/4281670/django-best-way-to-unit-test-an-abstract-model</a></p>
<p>Refer to this for more information</p>
<p></p>
<pre style="background-color: #ffffff; color: #000000; font-family: 'Menlo'; font-size: 9.0pt;"><br /><span style="color: #000080; font-weight: bold;">class </span>CodeMemoBaseTest(TestCase):<br />    model = <span style="color: #000080; font-weight: bold;">None<br /></span><span style="color: #000080; font-weight: bold;"><br /></span><span style="color: #000080; font-weight: bold;">    </span><span style="color: #0000b2;">@classmethod<br /></span><span style="color: #0000b2;">    </span><span style="color: #000080; font-weight: bold;">def </span>setUpClass(<span style="color: #94558d;">cls</span>):<br />        <span style="color: #94558d;">cls</span>.model = ModelBase(<br />            <span style="color: #008080; font-weight: bold;">'__TestModel__' </span>+<br />            CodeMemoBase.<span style="color: #b200b2;">__name__</span>, (CodeMemoBase,),<br />            {<span style="color: #008080; font-weight: bold;">'__module__'</span>: CodeMemoBase.<span style="color: #b200b2;">__module__</span>}<br />        )<br /><br />        <span style="color: #808080; font-style: italic;"># Create the schema for our test model<br /></span><span style="color: #808080; font-style: italic;">        </span><span style="color: #000080; font-weight: bold;">with </span>connection.schema_editor() <span style="color: #000080; font-weight: bold;">as </span>schema_editor:<br />            schema_editor.create_model(<span style="color: #94558d;">cls</span>.model)<br />        <span style="color: #000080;">super</span>().setUpClass()<br /><br />    <span style="color: #0000b2;">@classmethod<br /></span><span style="color: #0000b2;">    </span><span style="color: #000080; font-weight: bold;">def </span>tearDownClass(<span style="color: #94558d;">cls</span>):<br />        <span style="color: #808080; font-style: italic;"># Delete the schema for the test model<br /></span><span style="color: #808080; font-style: italic;">        </span><span style="color: #000080; font-weight: bold;">with </span>connection.schema_editor() <span style="color: #000080; font-weight: bold;">as </span>schema_editor:<br />            schema_editor.delete_model(<span style="color: #94558d;">cls</span>.model)<br />        <span style="color: #000080;">super</span>().tearDownClass()</pre>
