On 04/23/2013 04:31 PM, Simon King wrote:
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
I appreciate the input. I have put a setup fixture in the test file and
that didn't work. I had thought py.test ran the script like it was
running from the command line but you are probably correct - it is only
importing the specific function and not running the "if __name__" line
at all.
What do sqlalchemy users usually use for testing? I am willing to learn
other ways to do this that are more suited to the packages.
thomas
--
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.