Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we print the command line arguments in setup_module() or setup_class in pytest?

I got to know how to pass command line parameters in pytest. I want to print or do some basic validation in my setup_class() or setup_module() against the variables passed as command line arguments.

I am able to access these variables in test methods but not in setup_class or setup_module. Is this even possible?

I want to do something like this...

conftest.py

def pytest_addoption(parser):
    parser.addoption('--user',action='store', help='email id Ex: [email protected]')

test_1.py

import pytest

def setup_module():
    print 'Setup Module'
def teardown_module():
    print "Teardown Module"

class Test_ABC:

    def setup_class(cls,request):
        user = request.config.option.user
        print user
        print 'Setup Class'

    def teardown_class(cls,request):
        print 'Teardown Class'

    def test_12(self,request):
        print "Test_12"
        assert 1==1
like image 644
manoj prashant k Avatar asked Oct 26 '25 03:10

manoj prashant k


1 Answers

In conftest.py:

def pytest_addoption(parser):
    """Add the --user option"""
    parser.addoption(
        '-U', '--user',
        action="store", default=None,
        help="The user")

option = None

def pytest_configure(config):
    """Make cmdline arguments available to dbtest"""
    global option
    option = config.option

Now you can just import option from conftest:

from conftest import option

class Test_ABC:

    def setup_class(cls):
        user = option.user
        print user
        print 'Setup Class'
like image 54
phd Avatar answered Oct 29 '25 05:10

phd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!