Best Python code snippet using pytest-django_python
test_database.py
Source:test_database.py  
...45        Item.objects.create(name="spam")46    def test_clean_db(self, all_dbs):47        # Relies on the order: test_access created an object48        assert Item.objects.count() == 049    def test_transactions_disabled(self, db):50        if not connections_support_transactions():51            pytest.skip("transactions required for this test")52        assert connection.in_atomic_block53    def test_transactions_enabled(self, transactional_db):54        if not connections_support_transactions():55            pytest.skip("transactions required for this test")56        assert not connection.in_atomic_block57    def test_transactions_enabled_via_reset_seq(self, django_db_reset_sequences):58        if not connections_support_transactions():59            pytest.skip("transactions required for this test")60        assert not connection.in_atomic_block61    def test_django_db_reset_sequences_fixture(62        self, db, django_testdir, non_zero_sequences_counter63    ):64        if not db_supports_reset_sequences():65            pytest.skip(66                "transactions and reset_sequences must be supported "67                "by the database to run this test"68            )69        # The test runs on a database that already contains objects, so its70        # id counter is > 1. We check for the ids of newly created objects.71        django_testdir.create_test_module(72            """73            import pytest74            from .app.models import Item75            def test_django_db_reset_sequences_requested(76                    django_db_reset_sequences):77                item = Item.objects.create(name='new_item')78                assert item.id == 179        """80        )81        result = django_testdir.runpytest_subprocess("-v", "--reuse-db")82        result.stdout.fnmatch_lines(83            ["*test_django_db_reset_sequences_requested PASSED*"]84        )85    @pytest.fixture86    def mydb(self, all_dbs):87        # This fixture must be able to access the database88        Item.objects.create(name="spam")89    def test_mydb(self, mydb):90        if not connections_support_transactions():91            pytest.skip("transactions required for this test")92        # Check the fixture had access to the db93        item = Item.objects.get(name="spam")94        assert item95    def test_fixture_clean(self, all_dbs):96        # Relies on the order: test_mydb created an object97        # See https://github.com/pytest-dev/pytest-django/issues/1798        assert Item.objects.count() == 099    @pytest.fixture100    def fin(self, request, all_dbs):101        # This finalizer must be able to access the database102        request.addfinalizer(lambda: Item.objects.create(name="spam"))103    def test_fin(self, fin):104        # Check finalizer has db access (teardown will fail if not)105        pass106class TestDatabaseFixturesAllOrder:107    @pytest.fixture108    def fixture_with_db(self, db):109        Item.objects.create(name="spam")110    @pytest.fixture111    def fixture_with_transdb(self, transactional_db):112        Item.objects.create(name="spam")113    @pytest.fixture114    def fixture_with_reset_sequences(self, django_db_reset_sequences):115        Item.objects.create(name="spam")116    def test_trans(self, fixture_with_transdb):117        pass118    def test_db(self, fixture_with_db):119        pass120    def test_db_trans(self, fixture_with_db, fixture_with_transdb):121        pass122    def test_trans_db(self, fixture_with_transdb, fixture_with_db):123        pass124    def test_reset_sequences(125        self, fixture_with_reset_sequences, fixture_with_transdb, fixture_with_db126    ):127        pass128class TestDatabaseMarker:129    "Tests for the django_db marker."130    @pytest.mark.django_db131    def test_access(self):132        Item.objects.create(name="spam")133    @pytest.mark.django_db134    def test_clean_db(self):135        # Relies on the order: test_access created an object.136        assert Item.objects.count() == 0137    @pytest.mark.django_db138    def test_transactions_disabled(self):139        if not connections_support_transactions():140            pytest.skip("transactions required for this test")141        assert connection.in_atomic_block142    @pytest.mark.django_db(transaction=False)143    def test_transactions_disabled_explicit(self):144        if not connections_support_transactions():145            pytest.skip("transactions required for this test")146        assert connection.in_atomic_block147    @pytest.mark.django_db(transaction=True)148    def test_transactions_enabled(self):149        if not connections_support_transactions():150            pytest.skip("transactions required for this test")151        assert not connection.in_atomic_block152    @pytest.mark.django_db...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
