A H
A H

Reputation: 2570

Django only create model for testing

I want to test a custom field.

How can I create a (dummy) model just for testing?

e.g.:

myapp/tests/test_fields.py

import pytest
from django.db import models
from ..fields import Percentage100Field

class DummyModel(models.Model):
    percentage = Percentage100Field()


@pytest.mark.django_db
def test_percentage_100_field():
    d = DummyModel.objects.create(percentage=19)

Upvotes: 2

Views: 922

Answers (1)

A H
A H

Reputation: 2570

1) Create a test setting file

test_settings.py

from .settings import *
INSTALLED_APPS += ["myapp.tests"]

2) Create a pytest.ini file to use the new settings.py file:

pytest.ini

[pytest]
addopts = --ds=path.to.test_settings

3) Create a myapp/test/Models.py file with all your models

4) Now all models in the Test folder will automatically be created.

Upvotes: 2

Related Questions