[pytest-dev] py.test and decorators

Sarah Mount mount.sarah at gmail.com
Wed Jan 22 22:21:19 CET 2014


Hi there,

I am currently converting some very idiosyncratic hand-rolled tests into
more sensible unit tests with pytest. I had a problem running tests which
dealt with decorators and following the advice here:

http://stackoverflow.com/questions/19614658/how-do-i-make-pytest-fixtures-work-with-decorated-functions

I refactored all my decorators to use the decorator library rather than
functools.wraps.

The win there was that now the Python 2.7 version of my code behaves in the
same way as the Python 3.3 code (whereas before the 2.7 code passed and the
3.3 code errored). The fail is that now none of the tests run at all!

I tried running tests without py.test (as in: python -m
mylib.test.test_one) and they ran as expected. If I try to use the library
just from a REPL session it seems OK. I suspect that the issue has occurred
because I haven't yet understood where to use pytest fixtures.

The (simplified) code looks roughly like this:

### FILE base.py

@process
def foo(channel):
    channel.write(100)

@process
def bar(channel):
    channel.read()

### FILE test_one.py

def test_one_one():
    channel = Channel()
    par(foo(channel), bar(channel)).start()


And the results look like this:

$ py.test mylib/
================================================================ test
session starts
=================================================================
platform linux2 -- Python 2.7.5 -- pytest-2.5.1
collected 44 items

mylib/test/test_one.py (venv)$


I have tried putting the @pytest.fixture decorator on both the functions in
base.py and those in test_one.py but neither works.

Any ideas?

Thanks,

Sarah

-- 
Sarah Mount, Senior Lecturer, University of Wolverhampton
website:  http://www.snim2.org/
twitter: @snim2
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20140122/2e9662b6/attachment.html>


More information about the Pytest-dev mailing list