On 27 June 2012 19:36, holger krekel <hol...@merlinux.eu> wrote: > On Wed, Jun 27, 2012 at 16:59 +0100, Floris Bruynooghe wrote: >> On 27 June 2012 13:57, holger krekel <hol...@merlinux.eu> wrote: >> > Setting resources as class attributes >> > ------------------------------------------- >> > >> > If you want to make an attribute available on a test class, you can >> > use the resource_attr marker:: >> > >> > @pytest.mark.resource_attr("db") >> > class TestClass: >> > def test_something(self): >> > #use self.db >> >> I'm not convinced of creating a special purpose mark for this. >> Firstly it seems like an anti-pattern in py.test to me, more like >> xUnit style. > > unittest/xUnit-compat is the main idea for this new marker. It would > work on pytest and unittest.TestCase classes alike. It's also reminiscent > of Rob Collin's testscenario unittest-extension. > >> easily done with:: >> >> class TestClas(object): >> @classmethod >> def setup_class(cls, item): >> cls.db = item.getresource('db') > > Not really. Here we would need to check if the setup_class() > accepts an item parameter and setup_class methods do not follow > the hook-keyword-arg-calling convention. Also passing an > item would be slightly arbitrary as the setup_class would > only be called once for all of its test items (functions).
Oh, that will teach me to talk about an API I haven't used in a long time without looking it up. I thought a node (not item) was already passed in. Still, I think it would look nicer if it was possible to get to the resources API from within .setup_module(module), .setup_class(cls) and .setup_method(self, method) rather then needing a new marker for this. The first of these should not be a problem I guess, since it already has a node passed in. For .setup_method() the method argument could have an item attribute. But I guess .setup_class(cls) is the hardest. Would it be tricky to inspect the arguments as done for hooks? >> Also, I realised this API provides for what is probably most of the >> cases of where I want dynamic resources: >> >> def pytest_setup_init(session): >> for item in my_item_generator(): >> session.register_resource_factory(item.name, item) > > Not sure i understand this idea. Is it intended as a mixture of > collection (my_item_generator) and setup (as the hook name suggests)? My bad for writing a bad example, I shouldn't have used the word "item" in there. Anyway the main point is that thanks to .register_resource_factory() taking the name of the resource as an argument I believe most, if not all, the cases where I wanted to create funcargs/resources without knowing what they where beforehand are solved. Regards, Floris -- Debian GNU/Linux -- The Power of Freedom www.debian.org | www.gnu.org | www.kernel.org _______________________________________________ py-dev mailing list py-dev@codespeak.net http://codespeak.net/mailman/listinfo/py-dev