On Thursday, October 4, 2012 7:49:19 PM UTC+2, Daniele Procida wrote: > > I have started writing my first tests, for a project that has become > pretty large (several thousand lines of source code). >
That is too, late! ;-) > > What needs the most testing - where most of the bugs or incorrect appear > emerge - are the very complex interactions between objects in the system. > > To me, the intuitive way of testing would be this: > > * to set up all the objects, in effect creating a complete working > database > * run all the tests on this database > This method might work even if maintaining the fixture can be extra trouble. I never used fixture for doing tests, the useful case might not have presented itself but we had another way of doing it. > That's pretty much the way I test things without automated tests: is the > output of the system, running a huge database of objects, correct? > Yes, but UT, should also be some kind of a documentation of how the projects works, having a pre-built database doesn't give much information about how to setup a correct database for the project whereas fine grained UT do. > However, I keep reading that I should isolate all my tests. Yes, but there might be cases where *tests fixtures are* useful, I don't know them. So I have had a go at creating tests that do that, but it can mean setting > up a dozen objects sometimes for a single tiny test, then doing exactly the > same thing with one small difference for another test. > Use factories for that. there is factory boy<https://github.com/dnerdy/factory_boy>that seems to do the job, but I don't know its value, we were using our own factory and/or build the database manually, like I said earlier documenting/testing the way the database should look like in the tests is IMO significant. You can also test the factory which might serve as documentation/tests of the proper building of the database something like, but it can be OT of UT if the factory is not used in the project code: def test_build_base_site(self): Factory.build(Site) self.assertEquals(Site.objects.count(), 42) def test_build_section(self): Factory.build(Section, name='Test Section') self.assertEquals(Section.objects.all(), 1) self.assertEquals(Section.objects.all(), 1) def test_build_article(self): section = Factory.build('Article') self.assertEquals(Article.objects.all(), 42) # etc... The factory takes some arguments, builds the object you asked for I don't know how complex is your schema, the factory can be recursive so it's not problem. > > Often I have to run save() on these objects, because otherwise tests that > depend on many-to-many and other database relations won't work. > > That seems very inefficient, to create a succession of complex and > nearly-identical test conditions for dozens if not hundreds of tests. > > I'd appreciate any advice. > > Daniele > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/3csuRekSzosJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.