How to Pass Arguments in Pytest by Command Line

How to pass multiple arguments in pytest using command line?

You can make it work this way:

conftest.py:

import pytest

def pytest_addoption(parser):
parser.addoption("--input1", action="store", default="default input1")
parser.addoption("--input2", action="store", default="default input2")

@pytest.fixture
def input1(request):
return request.config.getoption("--input1")

@pytest.fixture
def input2(request):
return request.config.getoption("--input2")

test.py:

import pytest

@pytest.mark.unit
def test_print_name(input1, input2):
print ("Displaying input1: %s" % input1)
print("Displaying input2: %s" % input2)

CLI:

>py.test -s test.py --input1 tt --input2 12
================================================= test session starts =================================================
platform win32 -- Python 3.7.0, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: pytest, inifile:
collected 1 item

test.py Displaying input1: tt
Displaying input2: 12
.

============================================== 1 passed in 0.04 seconds ===============================================

how to pass command line argument from pytest to code

A good practice might to have this kind of code, instead of reading arguments from main method.

# main.py
def main(arg1):
return arg1

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='My awesome script')
parser.add_argument('word', help='a word')
args = parser.parse_args()
main(args.word)

This way, your main method can easily be tested in pytest

import main
def test_case01():
main.main(your_hardcoded_arg)

I am not sure you can call a python script to test except by using os module, which might be not a good practice

Passing command line arguments in python by pytest

Your pytest <filename>.py arg1 command is trying to call pytest on two modules <filename>.py and arg1 , But there is no module arg1.

If you want to pass some argument before running pytest then run the pytest from a python script after extracting your variable.

As others suggested though you would probably want to parameterize your tests in some other way, Try:Parameterized pytest.

# run.py
import pytest
import sys

def main():
# extract your arg here
print('Extracted arg is ==> %s' % sys.argv[2])
pytest.main([sys.argv[1]])

if __name__ == '__main__':
main()

call this using python run.py filename.py arg1

How to pass command line arguments to pytest tests running in vscode

After much experimentation I finally found how to do it. What I needed was to pass user name and password to my script in order to allow the code to log into a test server. My test looked like this:

my_module_test.py

import pytest
import my_module

def login_test(username, password):
instance = my_module.Login(username, password)
# ...more...

conftest.py

import pytest

def pytest_addoption(parser):
parser.addoption('--username', action='store', help='Repository user')
parser.addoption('--password', action='store', help='Repository password')

def pytest_generate_tests(metafunc):
username = metafunc.config.option.username
if 'username' in metafunc.fixturenames and username is not None:
metafunc.parametrize('username', [username])

password = metafunc.config.option.password
if 'password' in metafunc.fixturenames and password is not None:
metafunc.parametrize('password', [password])

Then in my settings file I can use:

.vscode/settings.json

{
// ...more...
"python.testing.autoTestDiscoverOnSaveEnabled": true,
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true,
"python.testing.pytestArgs": [
"--exitfirst",
"--verbose",
"test/",
"--username=myname",
"--password=secret",
// ...more...
],
}

An alternative way is to use pytest.ini file instead:

pytest.ini

[pytest]
junit_family=legacy
addopts = --username=myname --password=secret



Related Topics



Leave a reply



Submit