21

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
Uri
  • 479
  • 1
  • 4
  • 10
  • 1
    Does this answer your question? [How to pass arguments in pytest by command line](https://stackoverflow.com/questions/40880259/how-to-pass-arguments-in-pytest-by-command-line) – quamrana Mar 21 '20 at 08:31
  • 1
    This post answers the question how pass parameters to test scripts. It is **NOT** the subject of this post. I'm interested in how to the same in **vscode** – Uri Mar 21 '20 at 14:10
  • I’m sorry I don’t see the difference. You quote a pytest command line in your question and there are example command lines with parameters in the answers in the link. – quamrana Mar 21 '20 at 14:14
  • 1
    Running pytest from the command line does not allow debugging the test inside vscode. Invoking from the command line allow one drop into pdb for stepping through the code. The ability to use vscode interactive debugger is far superior. – Uri Mar 21 '20 at 14:29

2 Answers2

15

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

Uri
  • 479
  • 1
  • 4
  • 10
  • Does vscode offer some way to change this in the UI easily? For example what if you wanted to pass it a different "username" and "password" _sometimes_. Is the only way to modify the settings.json before running tests or is there a UI feature to have test runs setup with different args? – red888 Aug 17 '20 at 15:04
  • @red888 I assume you're asking about prompting for username and password when the test is run. AFAIK, there's no interactive way of doing it. You must change the settings file before execution. – Uri Aug 22 '20 at 18:00
  • Actually, it looks like it can be done interactively. I haven't tried it so I'm not sure if it works for unit tests. Check out more info [here](https://code.visualstudio.com/docs/editor/variables-reference#_input-variables) – Uri Aug 23 '20 at 20:21
  • On my side, I needed to use the pytest.ini approach in order to get things working. Thx – Greg7000 May 16 '22 at 16:44
  • Is this also used when running tests in debug mode? It seems like fixing the seed leads to different results in debug vs non-debug mode. – stefanbschneider Jul 26 '22 at 10:56
0

If this is just for the debugger then you can specify things in "args" in your launch.json file. See https://code.visualstudio.com/docs/python/debugging#_args for more details.

Brett Cannon
  • 14,438
  • 3
  • 45
  • 40
  • 4
    The issue is not about simply debugging. I'm trying to use the unit test extension which expose a button on each unit test enabling debugging a single test case. The only way I know for invoking these test cases with arguments is to feed the arguments in the way described above. – Uri Mar 27 '20 at 14:13