Python unit tests not discovered in VSCode
Asked Answered
P

11

15

I've written a python test file called scraping_test.py, with a single test class, using unittest, called TestScrapingUtils

"""Tests for the scraping app"""
import unittest

from bs4 import BeautifulSoup as bs4

from mosque_scraper.management.commands import scraping_utils
from mosque_scraper.selectors import MOSQUE_INFO_ROWS_SELECTOR

class TestScrapingUtils(unittest.TestCase):
    """Test scraping_utils.py """
    def setup(self):
        """Setup McSetupface."""
        pass
    def test_get_keys_from_row(self):
        """ Test that we extract the correct keys from the supplied rows."""
        test_page_name = "test_page.html"
        with open(test_page_name) as test_page_file:
            test_mosque = bs4(test_page_file, 'html.parser')
            rows = test_mosque.select(MOSQUE_INFO_ROWS_SELECTOR)
            field_dict = scraping_utils.get_fields_from_rows(rows)
            self.assertDictEqual(field_dict, {})

My settings for unit tests are:

{
    "python.unitTest.unittestEnabled": true,
    "python.unitTest.unittestArgs": [
        "-v",
        "-s",
        ".",
        "-p",
        "*test.py"
    ]
}

It looks like it should work, but when I click to run the tests in VSCode it says that no tests were discovered:

No tests discovered, please check the configuration settings for the tests.

How do I make it work?

Preshrunk answered 24/12, 2017 at 12:18 Comment(0)
W
18

You have to run it once by using shortcut key shift+ctrl p, and type "Python run all unit tests".

It won't show up in the editor until it was successfully executed at least once or use the discover unit test method.

However one thing catch me many times is that the Python file has to be a valid Python file. The intellisense in VS Code for Python is not complex(compare to Javascript or Typescript), and it won't highlight any syntax error. You can verify that by force it to run all unit test and observe the Python Test Log window.

Wakayama answered 11/4, 2018 at 2:48 Comment(1)
Seems like the command is not called "Python run all unit tests" anymore but: "Test: Run All Tests".Parenthesize
P
8

What caught me is that the __init__.py file must be created in every subdirectory, from the root folder specified with -s option (in the example, the current directory ".") to the subdirectory where the test module is located. Only then was I able to discover tests successfully.

In the question example, both project_dir/ and project_dir/scraping_app/ should contain __init__.py. This is assuming that settings.json is located in project_dir/.vscode and the tests are run from project_dir/ directory.

Edit: Alternatively, use "-s", "./scraping_app/" as the root test directory so you don't have to put __init__.py to project_dir/.

Parenthesize answered 17/8, 2020 at 9:35 Comment(1)
and after adding the empty 'underscore underscore init underscore underscore.py' file(s) in your folder(s) (both the folder that has the class (.py) files to be tested and the folders leading to your test classes (*.py files)., the ctrl-shift-p Developer: Reload window. Then be sure to use the test tube to see your test(s) show up.Behavior
D
2

You can add the DJANGO_SETTINGS_MODULE variable and django.setup() inside the __init__.py file of tests package.

import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_app.settings')
django.setup()
Danford answered 30/6, 2021 at 1:12 Comment(0)
H
1

Instead of file name 'scraping_test.py' it shall be 'test_scraping.py' string shall start from 'test' prefix

Histrionic answered 4/8, 2020 at 6:38 Comment(2)
The file name is fine, since the format has been specified correctly in the settings: "-p", "*test.py". This catches all the files that end in test.py (in the question example, scraping_test.py)Parenthesize
I have "-p" in settings but again writing "test_" as a prefix worked, however "_test" as a suffix did not work on VSCode. Thanks @HistrionicGertrudegertrudis
A
1

I had the same error with a slightly different configuration. (I am posting this here because this is the question that comes up when you search for this error.)

In addition to what was said above, it is also important to not use periods in the test file names (e.g. use module_test.py instead of module.test.py).

Alexandra answered 23/9, 2020 at 15:29 Comment(0)
L
0

In my case, the problem was that my test was importing a module which was reading an environment variable using os.environ['ENV_NAME']. If the variable does not exist, it throws an error. But VS Code does not log anything (or at least I couldn't find it).

So, the reason was that my .env file was NOT in the workspace root. So I had to add "python.envFile": "${workspaceFolder}/path/to/.env" to the settings.json file.

After that, the test was discovered successfully.

Largehearted answered 16/7, 2021 at 17:3 Comment(0)
F
0

Also had this issue. for me the issue was, make sure there are no errors, and comment out all code in files that rely on pytest, just for the initial load up.

Fluorosis answered 18/2, 2022 at 11:22 Comment(0)
P
0

Another issue that causes the unit tests not be discovered is when using a conda environment that contains an explicit dependency on the conda package itself. This happens when the enviroment.yml contains the line:

- conda

Removing this line and creating the environment from scratch makes the unit tests discoverable. I have created a bug report in Github for this: https://github.com/microsoft/vscode-python/issues/19643

(This is my second solution to this issue; I decided to create another answer since this is entirely different from the previous one.)

Parenthesize answered 5/8, 2022 at 9:52 Comment(0)
P
0

This is my first time using unittest in vscode. I found that the file names cannot contain spaces and dots. and cannot start with numbers.

for the dots, I guess anything after a dot is considered by the extension by unittest.

for the spaces, I guess they do not use "" to surround the filename.

Phocis answered 25/8, 2022 at 6:26 Comment(0)
U
0

My solution was: the name of the test functions need to start with "*test_"!
It's not only the name of the file!

Configuration settings.json

    "python.testing.unittestArgs": [
        "-v",
        "-s",
        ".",
        "-p",
        "*_test.py"
    ],
    "python.testing.pytestEnabled": false,
    "python.testing.unittestEnabled": true

File:something_test.py --> this _test is important

    import unittest
    
    class something(unittest.TestCase):
        def test_case1(self):
            self.assertFalse(False)
    
    if __name__ == '__main__':
        unittest.main()

The test_ of test_case1(self) is important

Unmake answered 2/6, 2024 at 17:29 Comment(0)
W
-1

For me Discovering the unit tests did the trick.

SHIFT+CTRL+P and execute "Python: Discover unit tests"

After running this I get the "Run Test|Debug Test" over each test function.

Whitebeam answered 1/10, 2018 at 20:5 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.