On Tue, Apr 23, 2013 at 2:08 PM, Paradox <para...@pobox.com> wrote: > > On 04/23/2013 04:31 PM, Simon King wrote: >> >> On Tue, Apr 23, 2013 at 6:54 AM, Paradox <para...@pobox.com> 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 >
When you say you created a setup fixture but it didn't work, what didn't work exactly? For example, if you just did something like this: def setup(): engine = ... Session = ... session = Session() ...then that won't work because session is a local variable inside the setup function. At a minimum you'd need to put something like "global session" at the beginning of the function. Simon PS. In answer to your question I use nosetests, but I think both packages are fairly similar. -- 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.