On Tue, Apr 23, 2013 at 6:54 AM, Paradox <[email protected]> wrote:
> I have a question related to sqlalchemy and testing, not sure if this is the
> best place to ask so let me know if I am asking here in error.
>
> I am trying to learn to write and run tests using py.test. Currently I am
> working on a spreadsheet scrapper that gathers data from a directory tree of
> spreadsheets and puts them into an sqlite database to be manipulated from
> there.  In any function that has a session.query object however I get a
> "global name 'session' not defined" error when I run the tests.  Here is an
> example of the code:
>
> def get_list_of_filesprocessed():
>     '''Look in filesprocessed table and return list of all the values in the
> filename field''
>     return [r.filename for r in session.query(FilesProcessed).all()]
>
> if __name__ == '__main__':
>     engine = create_engine('sqlite:///scrapper.db', echo=True)
>     Base = declarative_base()
>     Session = sessionmaker(bind=engine)
>     session = Session()
>
> How can I make the session available to the tests when they are run?  I have
> tried creating a decorator in the test file to initialize the session to no
> avail.  Any hints are appreciated.
>
> thomas
>

I haven't used py.test, but it almost certainly doesn't run the "if
__name__ == '__main__':" block of your test file, so the session is
never being created.

You could create the session in the test function itself, but another
option, particularly if you have the same repetitive steps to perform
for multiple tests, is to create a setup function. I see py.test has a
number of options for doing this:

  http://pytest.org/latest/xunit_setup.html#xunitsetup

  http://pytest.org/latest/fixture.html

Hope that helps,

Simon

-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to