Skip to content

Test file fails to import correct local module #22757

Closed
@mikevaux

Description

@mikevaux

Type: Bug

This appears related to #4566, but I'm not certain of that.

Behaviour

Expected vs. Actual

I would expect that module imports within a test file would [continue to] import modules from the project itself in preference of those in the extension.

What actually seems to be happening though (since upgrading to python 3.11.7, though I am not 100% this was the cause?) is that if I attempt to import utils, it is now loading the utils module from ~/.vscode/extensions/ms-python.python-2023.22.1/pythonFiles/unittestadapter. Prior to the python version upgrade this was working as expected, and as a test, I am also importing an arbitrarily named module foo_mike, which is in the same directory as my utils module, and that one is working fine.

Steps to reproduce:

[Simplified] Structure:

utils.py
foo_mike.py
Tests
-- utils_test.py

  1. Write utils_test.py as:
import unittest
import utils
import foo_mike

print(foo_mike)
print(utils)

utils.some_function()
  1. Run the tests within VS Code

  2. foo_mike is imported from the expected directory, but utils is imported from the extension modules. The above fails, because the utils module actually loaded has no attribute some_function

N.B. when running manually on the command line, it works as expected, i.e.

python3 -m unittest discover -v -s Tests -p "*test.py"

outputs

<module 'foo_mike' from '/Users/mikevaux/Documents/Software 1/Revision/src/foo_mike.py'>
<module 'utils' from '/Users/mikevaux/Documents/Software 1/Revision/src/utils.py'>
test_divisors (utils_test.UtilsTest.test_divisors) ... ok
test_factorial (utils_test.UtilsTest.test_factorial) ... ok
test_is_perfect_number (utils_test.UtilsTest.test_is_perfect_number) ... ok
test_sum_digits (utils_test.UtilsTest.test_sum_digits) ... ok

----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK

Diagnostic data

  • Python version (& distribution if applicable, e.g. Anaconda): 3.11.7
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Global
  • Value of the python.languageServer setting: Default
Test Results for Python in the Test Results panel

<module 'foo_mike' from '/Users/mikevaux/Documents/Software 1/Revision/src/foo_mike.py'>
<module 'utils' from '/Users/mikevaux/.vscode/extensions/ms-python.python-2023.22.1/pythonFiles/unittestadapter/utils.py'>
test_divisors (utils_test.UtilsTest.test_divisors) ... ERROR

======================================================================
ERROR: test_divisors (utils_test.UtilsTest.test_divisors)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/mikevaux/Documents/Software 1/Revision/src/Tests/utils_test.py", line 10, in test_divisors
    divisors = utils.divisors(220)
               ^^^^^^^^^^^^^^
AttributeError: module 'utils' has no attribute 'divisors'

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)
Finished running tests!

User Settings


languageServer: "Pylance"

testing
• unittestArgs: "<placeholder>"
• unittestEnabled: true

Extension version: 2023.22.1
VS Code version: Code 1.85.1 (Universal) (0ee08df0cf4527e40edc9aa28f4b5bd38bbff2b2, 2023-12-13T09:48:06.308Z)
OS version: Darwin x64 23.2.0
Modes:

System Info
Item Value
CPUs Intel(R) Core(TM) i5-1030NG7 CPU @ 1.10GHz (8 x 1100)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
Load (avg) 2, 2, 4
Memory (System) 8.00GB (0.01GB free)
Process Argv --crash-reporter-id 92c3754b-6fd2-4962-bfb3-45c670e28d6f
Screen Reader no
VM 0%

Metadata

Metadata

Assignees

Labels

triage-neededNeeds assignment to the proper sub-team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions