[TIP] instantiate class and use in all tests

holger krekel holger at merlinux.eu
Wed Oct 26 08:01:57 PDT 2016


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


     
On Wed, Oct 26, 2016 at 10:52 -0400, James wrote:
> Can I instantiate a class (DoStuff) and make it available to all my tests?
> I get  an error with the following code:
> ==================================== ERRORS
> ====================================
> ________________________ ERROR collecting testscript.py
> ________________________
> testscript.py:14: in <module>
>     @pytest.mark.incremental
> testscript.py:16: in TestAclass
>     ds=DoStuff(a,b)
> E   NameError: name 'a' is not defined
> 
> Even if I can't pass in a and b, can I still make a ds and use it in my
> tests?
> 
> 
> # conftest.py
> import pytest
> 
> @pytest.fixture(scope='module')
> def need_a(request):
>     value = request.config.getoption("A")
>     if not value:
>         pytest.skip('test needs -A option to run')
>     return value
> 
> @pytest.fixture(scope='module')
> def a(request):
>     a = pytest.config.getoption("A")
>     return a
> 
> @pytest.fixture(scope='module')
> def b(request):
>     b = pytest.config.getoption("B")
>     return b
> 
> def pytest_addoption(parser):
>     parser.addoption("--A", action="store", default=None, help="a option")
>     parser.addoption("--B", action="store", default=None, help="b option")
> 
> import pytest
> 
> # testscript.py
> 
> class DoStuff():
>     def __init__(a,b):
>         self.a=a
>         self.b=b
> 
>     def log(self,msg):
>         # do logging stuff
>         pass
> 
> @pytest.mark.incremental
> class TestAclass():
>     ds=DoStuff(a,b)
> 
>     def test_1(self,request):
>         print 'inside {0}-{1}
> step'.format(self.__class__.__name__,request.function.__name__)
>         print 'a={0}, b={1}'.format(ds.a,ds.b)
>         ds.log("we are here")
>         pass
> 
> 
>     def test_2(self,request):
>         print 'inside {0}-{1}
> step'.format(self.__class__.__name__,request.function.__name__)
>         print 'a={0}, b={1}'.format(ds.a,ds.b)
>         ds.log("we are here2")
>         pass
> 
> 
> 
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python



More information about the testing-in-python mailing list