I struggled with database tests in two applications, and I'm not sure
there is a wonderful and easy way. Regarding Roman's observation, you
could look at what the differences are between a Flax test and a
Pyramid test and that might answer why Pyramid is more difficult, and
if you come up with any reasons it would be good to let the Pyramid
developers know so we can think about addressing them. I don't know
much about Flask so I can't compare them. But I would guess that some
of the difficulty is setting up the setting up the
configurator/registry/routes/views and all that stuff, if you don't
just use 'get_app()' to do it all in one step (which you may not want
to do in a unit test if you're just testing part of the application).
As for testing "persistence", it depends on what exactly you mean.
Usually you write some date, flush/expire the SQLAlchemy session, then
query it back to make sure it has the new value. You can do that all
in one transaction using flush in the middle and a single rollback at
the end. But other tests may require a real commit and then checking
in another web request. In that case anything you write will remain
there, or if you order the tests you can have a later test delete it,
but py.test doesn't seem to let you control the order of tests.
Although you can use a module fixture or session fixture as an
umbrella around multiple tests, perhaps to delete what the tests did.
Or you just leave the changes, and restore the database from backup
whenever you want to go back to the start state. I don't do the
"create the database or tables in every request", I start with a copy
of the real data that has accumulated, and keep the same tables
throughout the tests, and either rollback after every test or know
that some scratch data will accumulate. Depending on the test,
persisted scratch data from an earlier test may or may not matter.
>
https://groups.google.com/d/msgid/pylons-discuss/r473Ps-10122i-69AD82F34B814C59AC3A361D0A5D8883%40Steves-iMac.local.
Mike Orr <
slugg...@gmail.com>