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. On Wed, Jan 25, 2017 at 10:39 PM, Steve Piercy <[email protected]> wrote: > As of Pyramid 1.8, we use py.test in the tutorials, scaffolds, and > cookiecutters. > https://github.com/Pylons/pyramid/search?utf8=%E2%9C%93&q=pytest > > But that's just the test runner, and doesn't address the core matter you > bring up. > > Switching from the setup/teardown to pytest fixtures would be a tremendous > undertaking. As with any unsponsored open source project, it would happen > at the speed of volunteers, and would necessitate buy in from the core > maintainers and experienced developers. I'd be interested in hearing more, > both for and against, about a switch. > > --steve > > > On 1/26/17 at 7:27 AM, [email protected] (Roman Suzi) pronounced: > >> I do not know what is so hard about tests in Pyramid. Recently wrote all >> kinds of tests in Flask (completely new framework for me), and with pytest >> it is a dream: pytest fixtures and their combination is very intuitive >> compared to the classic approach (with setup/teardown methods), I was able >> to with just a little googling to do selenium, requests-level, database, >> etc. >> >> Compared to my Pyramid experience: >> >> http://stackoverflow.com/questions/33776475/setting-up-pyramid-1-5-testing-the-right-way >> >> I think, it's maybe time to rethink pyramid tutorial with pytest in mind, >> and provide a tool set (examples of fixtures) in addition to whole >> example? >> >> Regards, >> Roman >> >> On Thu, Jan 26, 2017 at 12:29 AM, Jonathan Vanasco <[email protected]> >> wrote: >> >>> >>> >>> On Tuesday, January 24, 2017 at 4:50:55 PM UTC-5, Steve Piercy wrote: >>>> >>>> >>>> Take a look at this step in the SQLAlchemy + URL Dispatch wiki tutorial. >>>> http://docs.pylonsproject.org/projects/pyramid/en/latest/tut >>>> orials/wiki2/tests.html >>> >>> >>> >>> +1 for that tutorial. one of the projects we opensourced used that as >>> inspiration. >>> >>> a general pattern I used for those type of tests is: >>> >>> 1. insert objects into database via http tests >>> 2. check objects exist in database via raw sqlalchemy >>> >>> in terms of datastores, I suggest using a different connection string for >>> sqalalchemy -- test against a testdb (i would not test against sqlite if >>> you're deploying on postgres/mysql as things can differ slightly) >>> >>> >>> at some point your functional tests will work better as part of >>> integrated >>> tests... at that point, i like using the `requests` library to work like >>> a >>> headless browser. beautfifulsoup can help ensure you have the right >>> content on the page. >>> >>> it can take a little longer to write, but I found that `requests` worked >>> significantly faster than selenium (as in "seconds instead of minutes", >>> and >>> one 26+ minute selenium test dropped to around 90 seconds when it was >>> rewritten to requests.) >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "pylons-discuss" 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]. >>> To view this discussion on the web visit https://groups.google.com/d/ >>> msgid/pylons-discuss/9cdded60-72af-4345-81b3-b5bfdf47c250% >>> 40googlegroups.com >>> >> >> <https://groups.google.com/d/msgid/pylons-discuss/9cdded60-72af-4345-81b3-b5bfdf47c250% >> 40googlegroups.com?utm_medium=email&utm_source=footer> >>> >>> . >>> >>> For more options, visit https://groups.google.com/d/optout. >>> >> > > ------------------------ > Steve Piercy, Soquel, CA > > -- > You received this message because you are subscribed to the Google Groups > "pylons-discuss" 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]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/pylons-discuss/r473Ps-10122i-69AD82F34B814C59AC3A361D0A5D8883%40Steves-iMac.local. > > For more options, visit https://groups.google.com/d/optout. -- Mike Orr <[email protected]> -- You received this message because you are subscribed to the Google Groups "pylons-discuss" 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]. To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/CAH9f%3DuoXFU83vv%2Bs-qOyh%2BijZbgSe6-en1di0VaMr1JQtUZZEQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
