I'm getting this warning from my test code, so I know I'm setting something 
up wrong. If I have 10 test methods, I get this warning always on the 
second test method. Never any of the other ones. If I switch the order of 
test methods around I still get an error on the second method.

SAWarning: At least one scoped session is already present.  configure() can 
not affect sessions that have already been created.

Here is my test code. I patched it together from looking at various 
examples online, so I think I may have things in there that I don't need. 
Could someone please take a look and let me know if I should get rid of 
some things?

I notice that I have "DBSession.configure(bind=connection)" twice. Do I 
need it in both places or just in setup_module or setup_method?

Also in setup_method do I need to do "self.session = DBSession()" and in 
teardown_method "self.session.close()" or it's not necessary? In my tests 
I'm not sure what is the difference between referring to 
"DBSession.add(...)" and "self.session.add(...)". Which one should I be 
doing? I've gone through the documentation parts about sessions but I'm 
still unclear on this.

engine = None
connection = None
transaction = None

def setup_module():
    global engine, connection, transaction
    engine = create_engine('postgresql+psycopg2:///mydb', echo=False)
    connection = engine.connect()
    transaction = connection.begin()
    DBSession.configure(bind=connection)
    Base.metadata.create_all(connection)

def teardown_module():
    global engine, connection, transaction
    transaction.rollback()
    connection.close()
    engine.dispose()

class DatabaseTest(object):

    def setup_method(self, method):
        self.nested_transaction = connection.begin_nested()
        DBSession.configure(bind=connection)
        self.session = DBSession()

    def teardown_method(self, method):
        self.session.close()
        self.nested_transaction.rollback()

class TestRun(DatabaseTest):
    
    def test_run(self):
        # Tests go here
        DBSession.add(...)  # Or self.session.add(...), not sure


-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to