Skip to content
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

Access cmd line arguments within from a test #2358

Closed
inl-pd-autotest opened this issue Apr 10, 2017 · 4 comments
Closed

Access cmd line arguments within from a test #2358

inl-pd-autotest opened this issue Apr 10, 2017 · 4 comments

Comments

@inl-pd-autotest
Copy link

inl-pd-autotest commented Apr 10, 2017

Hi, how to access command line arguments within from a test_script.py?
suppose I do pytest --resolution 1280x1024 and my conftest.py is:

import pytest
def pytest_addoption(parser):
	parser.addoption('--resolution', metavar='resolution', default='1280x1024', help='specify resolution in format like that: "1280x720"')
def resolution(request):	# not sure this is needed for the example, but I need to access resolution in conftest.py's fixtures, so I have it.
	return request.config.getoption('--resolution')

and test_example.py is:

import pytest # to try 'pytestconfig.resolution'.
def test_example():
	print(resolution) # fails with NameError: name 'resolution' is not defined
	print(pytestconfig.resolution) # fails the same way. I don't know what pytestconfig really is, but docs say it is pytest config object with access to command line opts.
@The-Compiler
Copy link
Member

If you decorate your resolution function with @pytest.fixture, you should be able to add a resolution argument to your test. There's also an example here in the docs.

@inl-pd-autotest
Copy link
Author

inl-pd-autotest commented Apr 11, 2017

This still doesn't work for me:
coftest.py:

import pytest
def pytest_addoption(parser):
	parser.addoption('--resolution', metavar='resolution', default='1280x1024', help='specify resolution in format like that: "1280x720"')
@pytest.fixture
def resolution(request):
	return request.config.getoption('--resolution')

test_example.py:

def test_example(resolution):
	print(resolution) # this doesn't get printed

I've tried such cmd line calls: pytest -q --resolution 1920x1080 pytest -q --resolution 1920x1080 test_example.py

Output:

.
1 passed in 0.01 seconds

@The-Compiler
Copy link
Member

You still haven't added @pytest.fixture to resolution, so I don't know why this works. You'll also need to add -s to see output of passing tests.

@inl-pd-autotest
Copy link
Author

inl-pd-autotest commented Apr 11, 2017

Thank you! I did test with @pytest.fixture (just forgot to add it here in issue), but didn't know I needed -s to make passing tests output stuff!

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

2 participants