-2

Im trying to have interchangeable config files and I'm using py.test to send the testing suite to the cloud.

The following works when I run them locally using python main.py --site site1 in the terminal.

I'm trying to figure out how I can add cli arguments so that it will work with py.test

I have 3 files. main, config, and site1_config

main.py

if __name__ == "__main__":

parser = argparse.ArgumentParser(description='CLI tests for an environment')
parser.add_argument('--site', nargs='?', help='the site to use for the config')
parser.add_argument('unittest_args', nargs='*')
#Get our property
args = parser.parse_args()
if args.site:
    config.localizeConfig(args.site)
sys.argv[1:] = args.unittest_args

unittest.main(verbosity = 2)

config.py

def localizeConfig(site):
    localizedConfig = __import__(site+'_config');
    # localizedConfig = __import__(site);
    #print dir(localizedConfig)
    props = filter(lambda a: not a.startswith('__'), dir(localizedConfig))
    #Iterate over each property and set it on the config
    for prop in props:
        if prop != 'os' and prop != 'sys' and prop != 'webdriver':
            globals()[prop] = getattr(localizedConfig, prop)
host_url = www.google.com

site1_config.py

host_url = www.yahoo.com

Im trying to set a flag so that when py.test -n6 --boxed main.py site1 is run, the site1_config.py will copy over its contents into config.py

I'm not sure how I can get this to work with py.test

usage: py.test [options] [file_or_dir] [file_or_dir] [...] py.test: error: unrecognized arguments:

Kevnyou
  • 73
  • 1
  • 8

2 Answers2

1

I don't really get what you are trying to do, but in order to add CLI arguments to py.test check: http://pytest.org/latest/example/simple.html:

# content of conftest.py 
import pytest

def pytest_addoption(parser):
    parser.addoption("--cmdopt", action="store", default="type1",
        help="my option: type1 or type2")
k2110
  • 33
  • 5
1

The equivalent of your unittest.main(verbosity = 2) is pytest.main(['-v'])

Things to note:

  • In pytest, I'm not sure verbosity level is selectable
  • Some of the references on pytest.main(input) don't specify that the input has to be a list, but as of today it must be.

https://docs.pytest.org/en/latest/usage.html

Das_Geek
  • 2,284
  • 7
  • 16
  • 24
Brett B
  • 11
  • 3