[pytest-dev] [TIP] instantiate class and use in all tests
holger krekel
holger at merlinux.eu
Wed Oct 26 13:17:36 EDT 2016
pytest captures print/logging output during test runs and only shows it when there is a failure. Try ending a test function with "assert 0" to see what the output is.
holger
On Wed, Oct 26, 2016 at 12:36 -0400, James wrote:
> On 2016-10-26 12:08 PM, holger krekel wrote:
> >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
> Go it I think. :-)
> Where does the output go though?
>
> class TestAclass():
> def test_1(self,request,do_stuff):
> print 'inside {0}-{1}
> step'.format(self.__class__.__name__,request.function.__name__)
> print 'a={0}, b={1}'.format(do_stuff.a,do_stuff.b)
> do_stuff.log("we are here")
> pass
More information about the pytest-dev
mailing list