How to test a Django model with pytest?

后端 未结 2 1788
花落未央
花落未央 2021-02-08 05:12

I am getting started with pytest. I have configured pytest, anyway I couldn\'t found a resource on Django specific testing with pytest. How do I test a model with pytest_d

相关标签:
2条回答
  • 2021-02-08 05:41

    You can use this plugin that integrates pytest with Django: https://pytest-django.readthedocs.org/en/latest/tutorial.html

    The setup and changes to pytest.ini are described here: http://www.johnmcostaiii.net/2013/django-projects-to-django-apps-converting-the-unit-tests/

    You will find your example here starting from slide 57. This deck will be useful in testing views as well as models and the settings specific to testing models: https://speakerdeck.com/pelme/testing-django-applications-with-py-dot-test-europython-2013

    Specifically look at @pytest.mark.django_db helper documented here: http://pytest-django.readthedocs.org/en/latest/helpers.html

    An example for allowing db access in a test also mentioned in the deck above is copied here. Without mark.django_db the test would fail.

    import pytest
    @pytest.mark.django_db
    def test_user_count():
        assert User.objects.count() == 0
    
    0 讨论(0)
  • 2021-02-08 05:48

    pytest has support for running Python unittest.py style tests. It’s meant for leveraging existing unittest-style projects to use pytest features. Concretely, pytest will automatically collect unittest.TestCase subclasses and their test methods in test files. It will invoke typical setup/teardown methods and generally try to make test suites written to run on unittest, to also run using pytest.

    The given tests can be tested with py.test without any modification, however py.test makes the tests more pythonic.

    class SettingsTest(TestCase):    
        def test_account_is_configured(self):
            assert 'accounts' in INSTALLED_APPS
            assert 'accounts.User' == AUTH_USER_MODEL
    
    
    class UserTest(TestCase):
        def setUp(self):
            self.username = "testuser"
            self.email = "testuser@testbase.com"
            self.first_name = "Test"
            self.last_name = "User"
            self.password = "z"
    
            self.test_user = User.objects.create_user(
                username=self.username,
                email=self.email,
                first_name=self.first_name,
                last_name=self.last_name
            )
    
        def test_create_user(self):
            assert isinstance(self.test_user, User)
    
        def test_default_user_is_active(self):
            assert self.test_user.is_active
    
        def test_default_user_is_staff(self):
            assert not self.test_user.is_staff
    
        def test_default_user_is_superuser(self):
            assert not self.test_user.is_superuser
    
        def test_get_full_name(self):
            assert self.test_user.get_full_name() == 'Test User'
    
        def test_get_short_name(self):
            assert self.test_user.get_short_name() == self.email
    
        def test_unicode(self):
            assert self.test_user.__unicode__() == self.username
    

    as @Sid mentioned, you can use the @pytest.mark.django_db marker (decorator) to access the database when running a test without using django.test.TestCase,

    0 讨论(0)
提交回复
热议问题