Skip to content

Pass dinamically command line option into a test body #1150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fontealpina opened this issue Oct 21, 2015 · 6 comments
Closed

Pass dinamically command line option into a test body #1150

fontealpina opened this issue Oct 21, 2015 · 6 comments

Comments

@fontealpina
Copy link

Hi, in my use case I need to set dinamically a self attribute of a test class with a command line option, without passing explicitly the parameter to the test definition, with a plugin.
For example:

# content test.py 
class TestSuite():
     my_cmd_opt = 'foo'

     def test_case(self):
          assert self.my_cmd_opt != 'foo' # here the test should pass because self.my_cmd_opt
                                          # has been dinamically changed from pytest

so if i run:

   $> pytest --set-my-cmd-opt=alpha test.py
   test.py::TestSuite::test_case PASSED

because I've set self.my_cmd_opt to 'alpha' instead of 'foo'
There is a way to do that?

@nicoddemus
Copy link
Member

Yes, please take a look at this section in the docs.

Based on the example, you can now access the option directly instead:

# content test.py 
class TestSuite():

     def test_case(self, my_cmd_opt):
          assert my_cmd_opt != 'foo'

But if you really want to set it as an attribute of the class, you can use an autouse-fixture in the class scope:

class TestSuite():
     my_cmd_opt = 'foo'

     @pytest.fixture(autouse=True)
     def setup_getopt(my_cmd_opt):
         self.my_cmd_opt = my_cmd_opt

     def test_case(self):
          assert self.my_cmd_opt != 'foo' 

Hope this helps. 🍻

@fontealpina
Copy link
Author

Thanks! I tried your suggestion but I get an error:

     E       NameError: global name 'self' is not defined

Morover I was wondering if instead of use a fixture solution, there is a chance to do that directly from a

      def pytest_runtest_protocol(item):

Setting the attribute my_cmd_opt before the calling of

      reports = runtestprotocol(item)

There is a way to do something like this?

@nicoddemus
Copy link
Member

Thanks! I tried your suggestion but I get an error:

Sorry:

     @pytest.fixture(autouse=True)
     def setup_getopt(self, my_cmd_opt):
         self.my_cmd_opt = my_cmd_opt

Morover I was wondering if instead of use a fixture solution, there is a chance to do that directly from

It is possible, you can access the config object using item.config in pytest_runtest_protocol, but IIMO this is too implicit... I would go with the fixture approach, it is simple and explicit.

@fontealpina
Copy link
Author

Thanks! but in which of the item sub-object I need to set the attribute so that the test instance can see the command line option?

@RonnyPfannschmidt
Copy link
Member

The fixture as shown by @nicoddemus is in the object itself, no need to mess around with items

@RonnyPfannschmidt
Copy link
Member

Closing due to lack of feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants