5

I use pytest for testing. My test files reside in a subdirectory tests and they are named Foo.py, Bar.py instead of test_Foo.py, TestFoo.py, etc. So, to make sure pytest find them, I have a pytest.ini file in the root dir of the project with the following contents:

[pytest]
python_files=tests/*py

How to I specify the path to the pytest.ini file in Visual Studio Code so that the vscode-python plugin can correctly/successfully discover my test files? No matter what I try, I get Test discovery failed, with no reasons given.

A.L.
  • 1,133
  • 1
  • 12
  • 19

2 Answers2

16

To set up VS Code to use a specific pytest.ini file, you need to do the following:

  1. Open a directory in VS Code (ctrl+k > ctrl+o)
  2. Select a Python interpreter (ctrl+shift+p > Python: Select Interpreter > Python interpreter)
  3. Configure the testing framework you want to use, in this case PyTest (ctrl+shift+p > Python: Configure Tests > Pytest > {pytest rootdir}
  4. Open the settings.json file generated inside the .vscode/ directory that was created in your working directory (the one you chose in step 1)
  5. Add the following setting to the file (it may already exist if you specified a rootdir when configuring pytest):
    "python.testing.pytestArgs": [
        "-c",
        "/path/to/your/pytest.ini"
    ],

That's it! VS Code should be using the pytest.ini file you specify in the last argument. You can specify any CLI options you want there.

Source

-1

Pytest requires the test function names to start with test or ends with test.

The ini file instructs py.test to treat all *_test.py files as unit tests.

Seema Nair
  • 155
  • 7
  • 1
    I know how to use `pytest.ini` files. What I don't know is how to tell Visual Studio Code where it is so that it can discover my tests. My question is specific to Visual Studio Code (see tag and question title). – A.L. Feb 03 '20 at 10:12