SeanyMc
SeanyMc

Reputation: 475

Can't get PyTest to run in VSCode or terminal. No tests recognized

I have installed pytest using pip. Sample code below

import pytest


def example():
    assert 9 == 9

My settings.json file looks like this

{
    "python.testing.pytestArgs": [
        "."
    ],
    "python.testing.unittestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.pytestEnabled": true
} 

I keep getting no tests ran in terminal when using the pytest filename.py command.

Python version : 3.9.1 pytest version : 6.2.2.

Anyone have any ideas?

Update: Jason's answer below is what worked. The problem was the naming of the file and methods. File name needed to end in '_test.py' for vscode to run tests and functions needed to start with 'test_' for both terminal and vscode to run.

Upvotes: 3

Views: 5622

Answers (1)

Jason Cook
Jason Cook

Reputation: 1511

Try renaming the file to "test_filename.py" and the test function to "test_example" so that they are detected by pytest.

https://docs.pytest.org/en/reorganize-docs/new-docs/user/naming_conventions.html

Also, a useful pytest option for debugging the ability to run tests is "--collect-only". It will attempt to only find tests without running them.

pytest --collect-only

VSCode has the ability to run tests itself, without you executing them in the terminal.

https://code.visualstudio.com/docs/python/testing

Upvotes: 3

Related Questions