Re: [py-dev] using tmpdir/monkeypatch/... from non-function scopes

2012-11-20 Thread holger krekel
On Tue, Nov 20, 2012 at 09:52 +, Floris Bruynooghe wrote:
> On 19 November 2012 22:04, holger krekel  wrote:
> > A tmpdir requested in function-scope and a tmpdir requested with session
> > scope would be two different directories.  I don't see a problem with this,
> > do you?
> 
> When they both have a side-effect, like e.g. chdir, this could be an
> issue I thought.
> 
> Also, which value does the test function see when it requests tmpdir
> in this case?  I'm guessing it would get the tmpdir instance "closest"
> to itself, i.e. function-scope over module- or session-scope.  But
> maybe it would be useful if it could also retrieve the value of other
> scopes?  E.g. tmpdir.session_scope is the other tmpdir instance?

if you need differentiation you could do::

@pytest.fixture(scope="module")
def tmpdir_module(tmpdir):
return tmpdir

def test_function(tmpdir_module):
...

Alternatively, we could think about::

@pytest.mark.usefixtures("tmpdir:module")
def test_function(tmpdir):
...

best,
holger
___
py-dev mailing list
py-dev@codespeak.net
http://codespeak.net/mailman/listinfo/py-dev


Re: [py-dev] using tmpdir/monkeypatch/... from non-function scopes

2012-11-20 Thread Floris Bruynooghe
On 20 November 2012 07:27, holger krekel  wrote:
> It's btw probably better to name it "each" as in "scope='each'" which
> makes it clearer that something happens for each scope separately.

agreed

--
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


Re: [py-dev] using tmpdir/monkeypatch/... from non-function scopes

2012-11-20 Thread Floris Bruynooghe
On 19 November 2012 22:04, holger krekel  wrote:
> A tmpdir requested in function-scope and a tmpdir requested with session
> scope would be two different directories.  I don't see a problem with this,
> do you?

When they both have a side-effect, like e.g. chdir, this could be an
issue I thought.

Also, which value does the test function see when it requests tmpdir
in this case?  I'm guessing it would get the tmpdir instance "closest"
to itself, i.e. function-scope over module- or session-scope.  But
maybe it would be useful if it could also retrieve the value of other
scopes?  E.g. tmpdir.session_scope is the other tmpdir instance?


Regards,
Floris
___
py-dev mailing list
py-dev@codespeak.net
http://codespeak.net/mailman/listinfo/py-dev


Re: [py-dev] using tmpdir/monkeypatch/... from non-function scopes

2012-11-19 Thread holger krekel
On Mon, Nov 19, 2012 at 22:04 +, holger krekel wrote:
> > > the one in ``something(monkeypatch)`` above.  monkeypatch-finalizers
> > > would raher be called after a test function using the "other"
> > > fixture has finalized.  I am not sure if there is confusion potential
> > > about this.
> > 
> > For monkeypatch this would not be too bad as you can have two
> > instances which don't cause harm to each other.  But what happens with
> > e.g. tmpdir?  How can you avoid a temporary directory being created at
> > the session-level and then later one at the function level?
> 
> A tmpdir requested in function-scope and a tmpdir requested with session
> scope would be two different directories.  I don't see a problem with this,
> do you?

It's btw probably better to name it "each" as in "scope='each'" which
makes it clearer that something happens for each scope separately.

best,
holger
___
py-dev mailing list
py-dev@codespeak.net
http://codespeak.net/mailman/listinfo/py-dev


Re: [py-dev] using tmpdir/monkeypatch/... from non-function scopes

2012-11-19 Thread holger krekel
On Mon, Nov 19, 2012 at 21:53 +, Floris Bruynooghe wrote:

> > @pytest.fixture(scope="any")
> > def monkeypatch(...):
> > # unmodified builtin monkeypatch implementation
> >
> > @pytest.fixture(scope="module")
> > def something(monkeypatch):
> > ...
> >
> > This would not raise a ScopeMismatchError but just work:
> > monkeypatch-finalizers would be called when the last test in a module
> > using the "something" fixture has run.
> >
> > However, if we additionally have a function-scoped fixture::
> >
> > @pytest.fixture(scope="function")
> > def other(monkeypatch):
> > ...
> >
> > The "monkeypatch" instance could obviously not be the same object as
> > the one in ``something(monkeypatch)`` above.  monkeypatch-finalizers
> > would raher be called after a test function using the "other"
> > fixture has finalized.  I am not sure if there is confusion potential
> > about this.
> 
> For monkeypatch this would not be too bad as you can have two
> instances which don't cause harm to each other.  But what happens with
> e.g. tmpdir?  How can you avoid a temporary directory being created at
> the session-level and then later one at the function level?

A tmpdir requested in function-scope and a tmpdir requested with session
scope would be two different directories.  I don't see a problem with this,
do you?

> It's almost as the any-scoped fixture needs to be able to specify how
> things should be handled when it's requested from different scopes.

An any-scoped fixture function can look at request.scope and act accordingly.

An interesting question maybe is: how can a function-scoped resource
_use_ itself but higher scoped :)  (actually not too hard, i guess:
you could just invent a fixture name, have its fixture function use a
higher scope and pass through the resource (e. g. tmpdir).  you can 

best and good night,
holger

___
py-dev mailing list
py-dev@codespeak.net
http://codespeak.net/mailman/listinfo/py-dev


Re: [py-dev] using tmpdir/monkeypatch/... from non-function scopes

2012-11-19 Thread Floris Bruynooghe
Hello Holger,

On 19 November 2012 18:38, holger krekel  wrote:
> But currently if you do::
>
> @pytest.fixture(scope="module")
> def something(monkeypatch):
> ...
>
> you get a ScopeMismatchError because the function-scoped monkeypatch
> fixture cannot be called from a module-scoped fixture.

I agree this is a problem, I've worked around this using a hack in
pytest-django for the live_server fixture:
https://github.com/flub/pytest_django/blob/pytest23/pytest_django/fixtures.py#L180.
 In particular see the helper autouse fixture defined just below the
fixture.  For builtin fixtures I believe it is still possible to split
up the session-scoped fixture into a session- and a function-scoped
part to hack around this.  It is ugly though.

>  I am considering
> introducing an "any" scope for a fixture declaration that would avoid
> this error.  The "monkeypatch" and "something" fixture would then look
> like this::
>
> @pytest.fixture(scope="any")
> def monkeypatch(...):
> # unmodified builtin monkeypatch implementation
>
> @pytest.fixture(scope="module")
> def something(monkeypatch):
> ...
>
> This would not raise a ScopeMismatchError but just work:
> monkeypatch-finalizers would be called when the last test in a module
> using the "something" fixture has run.
>
> However, if we additionally have a function-scoped fixture::
>
> @pytest.fixture(scope="function")
> def other(monkeypatch):
> ...
>
> The "monkeypatch" instance could obviously not be the same object as
> the one in ``something(monkeypatch)`` above.  monkeypatch-finalizers
> would raher be called after a test function using the "other"
> fixture has finalized.  I am not sure if there is confusion potential
> about this.

For monkeypatch this would not be too bad as you can have two
instances which don't cause harm to each other.  But what happens with
e.g. tmpdir?  How can you avoid a temporary directory being created at
the session-level and then later one at the function level?  It's
almost as the any-scoped fixture needs to be able to specify how
things should be handled when it's requested from different scopes.

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