Rolling back database transactions in SQLAlchemy tests with PostgreSQL

Mikko Ohtamaa

I am building a Pyramid web application which is built on the top of SQLAlchemy and solely relies PostgreSQL as its database backend.

What would be a way to have the unit tests structure so that

  • To speed up tests, database transactions are rolled back at the teardown(), or other clean up hook of the test suite

  • Other tricks to speed up tests could be used, e.g. if SQLAlchemy and PostgreSQL has anything corresponding SQLite's :in:memory: database

  • It is possible to choose a custom test runner á la py.test if a specific features outside the standard library unittest framework makes it easier to write test cases.

Sergey

Since this question is a bit similar to your other question, I'll copy the relevant part of my answer here:

All test case classes should be subclassed from a base class, which defines a common tearDown method:

class BaseTest(unittest.TestCase):

    def setUp(self):
        # This makes things nicer if the previous test fails
        # - without this all subsequent tests fail
        self.tearDown()

        self.config = testing.setUp()

    def tearDown(self):
        testing.tearDown()
        session.expunge_all()
        session.rollback()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rolling back transactions in cucumber-jvm tests

From Dev

Proper way to maintain database known state by rolling back transactions in NUnit, Sql Server and UI Testing

From Dev

Rolling back transactions after exception is thrown

From Dev

Sequelize.js transactions not rolling back

From Dev

Creating databases in SQLAlchemy tests with PostgreSQL

From Dev

Capybara feature tests does not commit database transactions

From Dev

Transactions and sqlalchemy

From Dev

Transactions and sqlalchemy

From Dev

Laravel Tests Database Doesn't Roll Back

From Dev

Migrate PostgreSQL database back to local machine

From Dev

Are transactions in SQLAlchemy thread safe?

From Dev

migration not rolling back

From Dev

Rails transaction not rolling back

From Dev

PetaPoco transaction not rolling back

From Dev

Secondary keeps rolling back

From Java

How to Disconnect from a database and go back to the default database in PostgreSQL?

From Dev

Payment Transactions vs Database Transactions

From Dev

WSO2 data services server distributed transactions can not roll back for different types of database

From Dev

How to sync the PostgreSQL database script changes with Django back-end

From Dev

Transactions - Oracle Vs PostgreSQL

From Dev

Cordova SQLite transaction not rolling back

From Dev

TransactionScope rolling back in a wrong order

From Dev

Rolling back transaction with Oracle OCCI

From Dev

Rolling back a Rails migrations with execute

From Dev

Transaction in stored procedure not rolling back

From Dev

MySQL transaction not rolling back on error

From Dev

TFS: Rolling back package references

From Dev

Rolling back last migration not working?

From Dev

Mocking Database transactions?

Related Related

HotTag

Archive