[pytest-dev] [TIP] instantiate class and use in all tests
holger krekel
holger at merlinux.eu
Wed Oct 26 12:08:07 EDT 2016
On Wed, Oct 26, 2016 at 11:49 -0400, James wrote:
> On 2016-10-26 11:01 AM, holger krekel wrote:
>
> >James, as you have posted several questions already wrt to pytest may i suggest you rather subscribe and start posting to:
> >
> > https://mail.python.org/mailman/listinfo/pytest-dev
> >
> >Moreover, your examples so far contained some glitches which indicate you are not that experienced with Python yet. That's totally fine but i suggest you try to keep things as simple as you can before using all kinds of Python features.
> >
> >As to the mail below you can instantiate a class from a fixture and provide that to a test:
> > class DoStuff():
> > def __init__(self, a,b):
> > self.a=a
> > self.b=b
> > def log(self, msg):
> > # do logging stuff
> > pass
> >
> > @pytest.fixture
> > def do_stuff(a, b):
> > return DoStuff(a, b)
> >
> >best,
> >holger
> I still can't make it work:
> =================================== FAILURES
> ===================================
> ______________________________ TestAclass.test_1
> _______________________________
>
> self = <testscript.TestAclass instance at 0x7f87395bffc8>
> request = <FixtureRequest for <Function 'test_1'>>
>
> def test_1(self,request):
> > ds=do_stuff(a,b)
> E NameError: global name 'do_stuff' is not defined
The "NameError" means that there is no reference to "do_stuff" in the test module. You need to list "do_stuff" in the function parameters, for example:
def test_1(self, request, do_stuff):
# here you can use "do_stuff" because it was passed in from
# pytest by calling the "do_stuff" fixture function in
# conftest.py and passing its result to test_1.
holger
More information about the pytest-dev
mailing list