Skip to main content

pytest-django-model

PyPI version Python versions See Build Status on Travis CI

A Simple Way to Test your Django Models


Description

This plugin allows you to simply test your Django models, by writing your tests as you would write your models. On the other hand, pytest-django-model tests only fields, constants, and the Meta inner class. You will have to write tests of methods and properties.

The principle here is not really to test the behavior of your models but rather to make sure that the settings are the right ones.

Installation

You can install “pytest-django-model” via pip from PyPI:

$ pip install pytest-django-model

Usage

The plugin is very easy to use, there are only three things to remember:

  • The PytestDjangoModel metaclass.

  • The model attribute.

  • The parents attribute (optional).

Let’s take this model:

class Foo(Bar, Baz):
    class Meta:
        unique_together = ("name", "email")

    FAVORITE_COLOR_CHOICES = (
        ("BL", "blue"),
        ("YE", "yellow"),
        ("GR", "green"),
        ("RE", "red")
    )

    name = models.CharField(max_length=256)
    email = models.EmailField(blank=True)

    favorite_color = models.CharField(
        max_length=2, choices=FAVORITE_COLOR_CHOICES, default="BL"
    )
    is_awesome = models.BooleanField(default=True)

To test it, we just have to write this:

from pytest_django_model import PytestDjangoModel

# Name of the test class doesn't matter.
class TestFoo(metaclass=PytestDjangoModel):
    class Meta:
        model = Foo
        # Parents can be a model or a list/tuple of models.
        parents = (Bar, Baz)

        unique_together = ("name", "email")

    FAVORITE_COLOR_CHOICES = (
        ("BL", "blue"),
        ("YE", "yellow"),
        ("GR", "green"),
        ("RE", "red")
    )

    name = models.CharField(max_length=256)
    email = models.EmailField(blank=True)

    favorite_color = models.CharField(
        max_length=2, choices=FAVORITE_COLOR_CHOICES, default="BL"
    )
    is_awesome = models.BooleanField(default=True)

And voila! We can now launch tests with the command pytest.

From there, the class PytestDjangoModel will generate a fake Django model from constants, fields and options of the Meta class. This model will inherit all the models of the parents attribute.

The data of Foo model and the model created from the TestFoo class will be extracted and compared. If any constant differs or isn’t found, pytest will raise a error, same for fields and Meta options.

Contributing

Contributions are very welcome. Development Environment can be setup with make setup. Tests can be run with make test, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the GNU GPL v3.0 license, “pytest-django-model” is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Metadata

Release files for pytest-django-model 0.1.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-django-model 0.1.9
File Size Uploaded Provenance Actions
pytest-django-model-0.1.9.tar.gz 23.4 kB Without provenance Details for pytest-django-model-0.1.9.tar.gz

Release files / pytest-django-model-0.1.9.tar.gz

Download URL pytest-django-model-0.1.9.tar.gz
Size 23.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3783c503b4476385d3ab114ad579514b636f7ea58c0c0f5f58b922d1f8640cdc
BLAKE2b-256 checksum
How to use checksums
439bbea8b4395fe7bb07846c5697a5a2954350c017229bfa96625f201bc690f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

Security for pytest-django-model 0.1.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page