Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode + Pytest: "Error: TypeError: Cannot read property '$' of undefined"

I'm trying to execute 2 very basic unit tests, following the guide @ https://code.visualstudio.com/docs/python/testing. However, when attempting to run the unit tests, I receive the error:

Error: TypeError: Cannot read property '$' of undefined

I'm on Windows 10, Python 3.7.4, using latest version of VSCode.

On first attempting to run the tests (using the latest version of Pytest), I got the error "Error: TypeError: Cannot read property '$' of undefined". This led me to: https://github.com/microsoft/vscode-python/issues/6990

I then attempted to use the pip command outlined to downgrade my Pytest to the previous version (5.0.1). However, the 'pip' command was not recognized, so I figured it was something I had to enable in VSCode before using. Further googling led me to learn that it should be included by default when installing any version of Python > 3.0. I then thought it might need an extension, so I added the pip-packages extension to VSCode. Restarted & tried again - same error when atepmting to downgrade Pytest.

Further googling led me to: How to use pip with Visual Studio Code which pointed to instructions for adjusting system variables @ How to add to the PYTHONPATH in Windows, so it finds my modules/packages?.

I then attempted to adjust those variables, but it seemed to just cause further issues, so I undid those changes. Then, I asked a friend who said it should not be so complicated and advised that I first setup a virtual environment.

I went down the Venv path and after having a bunch of issues setting that up, I was successful and was able to set my Venv as the interpreter as well as install Pytest on the Venv as well.

I attempted to execute the tests again, now in the Venv and received the same error as before. This led me back to: https://github.com/microsoft/vscode-python/issues/6990

I was able to successfully downgrade my Pytest in the Venv to 5.0.1, as suggested in the github thread, then restarted VScode and executed the test again. Same error.

I was unable to find any further info - downgrading Pytest seems to have worked for everyone else.

The code that I am trying to test is:

inc_dec.py:

def increment(x):
    return x + 1

def decrement(x):
    return x - 1

The test cases I am trying to execute are:

pytest_test.py:

import inc_dec    # The code to test

def test_increment():
    assert inc_dec.increment(3) == 4

def test_decrement():
    assert inc_dec.decrement(3) == 4

I would expect the first unit test to pass, and the second unit test to fail. Instead, I receive the error: Error: TypeError: Cannot read property '$' of undefined

like image 890
attard.andrew Avatar asked Oct 28 '25 15:10

attard.andrew


1 Answers

Alright - so after many headaches, my issue was fairly simple, though not obvious to me due to lack of experience.

Basically - I had:

  • Installed a package to create virtual environments (pip install virtualenv)
  • Used the command virtualenv C:/path/to/venv/folder to create a virtual environment
  • In VSCode settings, I searched for "virtual environments" and found the setting to specify my virtual environment folder
  • At this point - I had read that I need to 'activate' the virtual environment I'd just created. I tried the activation instructions here first: https://virtualenv.pypa.io/en/stable/userguide/ but received an error, so I (incorrectly) thought I could instead activate it by selecting it as the interpreter within the VSCode GUI
  • I then installed Pytest, attempted to run the unit test and failed, so I again downgraded it to 5.0.1 and tried again - but still failed. This is the point where I came to StackOverflow
  • I decided to try again - deleted my venv, made sure that Pytest was uninstalled from the global environment, re-added a venv etc.
  • At this point - when attempting to run the activation command, I decided to explore the error message I received further, which led me to: https://superuser.com/questions/106360/how-to-enable-execution-of-powershell-scripts
  • Turns out - I didn't have sufficient privileges to run the activation script, so my venv was never 'activated' to begin with and the commands I was running to install & downgrade Pytest were being applied to the global environment, not the venv
  • I updated my permissions with set-executionpolicy remotesigned (via windows powershell as admin)
  • I then attempted the activation script again (with success), installed the latest version of Pytest to see if it would work (I got the same error), then downgraded to 5.0.1, and ran the tests again
  • Success :)
like image 175
attard.andrew Avatar answered Oct 31 '25 05:10

attard.andrew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!