0

I am setting up a Python project in VisualStudio Code. My folder and file structure looks like follows:

  • Base project folder
    • source_folder
      • package1_folder
        • module1.py
      • package2_folder
        • module2.py
    • source_folder_unittests
      • package1_unittests
        • module1_test.py

Sadly, the unittest discovery does not see the unit tests inside 'module_test.py'. As soon as I move the module1_test.py file one folder up, it works:

  • Base project folder
    • source_folder
      • package1_folder
        • module1.py
      • package2_folder
        • module2.py
    • source_folder_unittests
      • module1_test.py

This is my settings.json file:

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

Does VS Code not recognize unit tests in subfolders? Or am I missing something I have to do? I already added the source_folder to the PYTHONPATH environment variable.

1
  • Try adding the file __init__.py in every folder on your project. I'm not sure this is the cause, but it is worth a try. Commented Jan 11 at 21:39

1 Answer 1

0

From Test discovery

Tip: Sometimes tests placed in subfolders aren't discovered because such test files cannot be imported. To make them importable, create an empty file named __init__.py in that folder.

Add __init__.py in both

package1_folder, package2_folder, source_folder_unittests folder.

Then add the path in module1_test:

import sys
import os


sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../source_folder')))

import unittest   
from package2_folder import module2

Settings.json add

"python.analysis.extraPaths": [
        "./source_folder"
    ],
Sign up to request clarification or add additional context in comments.

1 Comment

@Blackbriar May I know if you have got any chance to check my answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.