[TIP] Learning fixtures in py.test

Brianna Laugher brianna.laugher at gmail.com
Thu Oct 24 19:12:44 PDT 2013


While it's difficult to debug without seeing your actual test code (your
example doesn't use session or setup_session), it seems like you may be
close. If your fixture's name is 'setup_session', that's the name you need
to use within the test function - not 'session'.

cheers
Brianna


On 25 October 2013 08:02, Paradox <paradox at pobox.com> wrote:

> I am trying to understand fixtures and how they are used.  I am writing
> tests for my program that interacts with an Sqlite database using
> Sqlalchemy.  I am working on Python 2.7.5 in Ubuntu 13.10.
>
> My setup_session fixture is:
>
> =================
> @pytest.fixture (autouse = True)
> def setup_session():
>     db_file = 'database.db'
>     engine = create_engine('sqlite:///' + db_file, echo = True)
>     Base = declarative_base()
>     Session = sessionmaker(bind=engine)
>     session = Session()
>     return session
> ==================
>
> Then when I try to use that fixture in a test it is something like:
> ==================
> def test_get_id_of_row(setup_**session):
>     assert(get_id_of_row('cell known to be in test data')==1)
> ==================
>
> I am getting an error:
> ==================
> NameError: global name 'session' is not defined
> ==================
>
> Shouldn't that be taken care of by passing the setup_session fixture to
> the test function?  There is something I don't get about fixtures and would
> appreciate any pointers on where to read to better understand, I have read
> the pytest.org site on fixtures several times and it is still not sinking
> in!
>
> thomas
>
> ______________________________**_________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.**org <testing-in-python at lists.idyll.org>
> http://lists.idyll.org/**listinfo/testing-in-python<http://lists.idyll.org/listinfo/testing-in-python>
>



-- 
They've just been waiting in a mountain for the right moment:
http://modernthings.org/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20131025/26ca7630/attachment.htm>


More information about the testing-in-python mailing list