Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've written tests to be run by pytest in a vscode project. The configuration file .vscode/settings.json allow passing additional command line parameters to pytest using:

    "python.testing.pytestArgs": [
        "test/",
        "--exitfirst",
        "--verbose"
    ],

How can I also pass custom script arguments to the test script itself? like invoking pytest from the command line as:

pytest --exitfirst --verbose test/ --test_arg1  --test_arg2
like image 686
Uri Avatar asked Mar 21 '20 08:03

Uri


People also ask

How do you send command-line arguments in pytest?

ArgumentParser(description="run test on --host") parser. add_argument('--host', help='host to run tests on (default: %(default)s)', default=DEFAULT_HOST) args, notknownargs = parser. parse_known_args() if notknownargs: print("pytest arguments? : {}".

How do you pass command-line arguments in Visual Studio Python?

To set command-line arguments in Visual Studio, right click on the project name, then go to Properties. In the Properties Pane, go to "Debugging", and in this pane is a line for "Command-line arguments." Add the values you would like to use on this line. They will be passed to the program via the argv array.

Can pytest take arguments?

Summary. You can pass arguments to fixtures with the params keyword argument in the fixture decorator, and you can also pass arguments to tests with the @pytest. mark. parametrize decorator for individual tests.

How do I run a test case in Visual Studio code?

The Testing Explorer is a tree view to show all the test cases in your workspace. You can select the beaker button on the left-side Activity bar of Visual Studio Code to open it. You can also run/debug your test cases and view their test results from there.


1 Answers

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

like image 53
Uri Avatar answered Oct 09 '22 11:10

Uri