I have tests that require arguments, in my case these are credentials to some service:
example_test.py:
@pytest.fixture()
def artuser(pytestconfig):
return pytestconfig.getoption("artuser")
@pytest.fixture()
def artpass(pytestconfig):
return pytestconfig.getoption("artpass")
def test_nuget_get_nuget(artuser, artpass):
assert artuser == "someuser"
assert artpass == "somepassword"
conftest.py:
import pytest
def pytest_addoption(parser):
parser.addoption("--artuser", action="store")
parser.addoption("--artpass", action="store")
It works if I run:
pytest --artuser someuser --artpass somepassword
.
However, it fails if run:
python setup.py test --artuser someuser --artpass somepassword
with an an error error: option --artuser not recognized
I then tried to integrate some code from this question, just to see if the argument gets passed along, so I have something like this in my setup.py:
from setuptools import setup, find_packages
from os import path
import argparse
import sys
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument('--artuser', help='required artuser argument', required=True)
args, unknown = argparser.parse_known_args()
sys.argv = [sys.argv[0]] + unknown
print(args)
setup(
name='myapp',
...
But when I run the tests, I get E AssertionError: assert None == 'someuser'
meaning the argument wasn't passed along.
I was able to do it with the help of this answer .
setup.py:
from setuptools import setup, find_packages
from setuptools.command.test import test as TestCommand
import sys
...
class PyTest(TestCommand):
user_options = [('artuser=', 'a', "Artifactory user"),
('artpass=', 'b', "Artifactory password")]
def initialize_options(self):
TestCommand.initialize_options(self)
self.artpass = None
self.artuser = None
def finalize_options(self):
TestCommand.finalize_options(self)
self.test_args = []
self.test_suite = True
def run_tests(self):
import pytest
errno = pytest.main(
["--artuser", self.artuser, "--artpass", self.artpass])
sys.exit(errno)
setup(
cmdclass={
'test': PyTest,
},
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.