Reputation:
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?
Upvotes: 16
Views: 42397
Reputation: 31
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
Upvotes: 0
Reputation: 479
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.
Upvotes: 0
Reputation: 581
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.)
Upvotes: 0
Reputation: 2316
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.
Upvotes: 0
Reputation: 1426
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.
Upvotes: 0
Reputation: 315
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()
Upvotes: 2
Reputation: 1903
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
).
Upvotes: 1
Reputation: 581
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/
.
Upvotes: 9
Reputation: 1565
Instead of file name 'scraping_test.py' it shall be 'test_scraping.py' string shall start from 'test' prefix
Upvotes: 1
Reputation: 27052
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.
Upvotes: -1
Reputation: 1222
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.
Upvotes: 18