-
Notifications
You must be signed in to change notification settings - Fork 12
test: configure pytest for VS Code #293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
This will allow developers using VS Code to automatically detect, run, and deubg their tests using VS Code's Testing bar. To do this, we're explicitly passing --ds=test_settings to point pytest-django at the correct settings. Note that creating a pytest.ini file and adding DJANGO_SETTINGS_MODULE to it won't work for most developers who will be running these tests under Tutor. That's because Tutor will set the environment variable DJANGO_SETTINGS_MODULE for LMS or Studio (CMS), and this takes precedence over pytest.ini. This commit also starts a pattern for allow-listing specific configuration files in the .vscode directory. This is going to be a balancing act where we want to add just a few things that should be universally shared for convenience, like how to run tests or attach debugging to a running process. While VS Code does store user-specific settings elsewhere, individual developers may be running many plugins that put their own data in this directory.
|
After discussing with some Axim folks, I've changed this PR to make example config files instead. It's ready for review. |
…s debug task is Django
I have a virtualenv just for {
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
} Both the "run test" and "debug" test buttons are working well. I can set breakpoints in the debugger. I note that the coverage code is being run but doesn't seem to interfere with anything. |
{ | ||
"name": "Python: Debug Tests", | ||
"type": "debugpy", | ||
"request": "launch", | ||
"program": "${file}", | ||
"purpose": ["debug-test"], | ||
"console": "integratedTerminal", | ||
"justMyCode": false, | ||
|
||
// This is needed because pytest-cov is incompatible with using the | ||
// debugger. See https://code.visualstudio.com/docs/python/testing#_pytest-configuration-settings | ||
"env": {"PYTEST_ADDOPTS": "--no-cov"}, | ||
|
||
// Adds some magic for Django templates. | ||
"django": true | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This launch config isn't working for me for some reason - when I run it via the "Run and Debug" panel with a test file open, it gives No module named 'openedx_learning'
despite using the correct venv which has that installed. But I'm wondering why we even need this launch configuration? Because the "Debug Test" buttons on the "Testing" panel are working just fine for me already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. I've always been running it from the "Testing" panel where it works. But I see the same issues as you in the Run/Debug panel. I'll look into that, thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I didn't think it was affecting the "Testing" panel but now I can see that when I have this code in place it runs the debug mode tests differently. For me the "debug test" button works fine either with or without this launch.json
config. The only difference I can see is that with the config in place, it produces more output in the terminal.
I don't think it's an issue that it doesn't also work from "Run and Debug", as long as other people aren't confused like I was.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works fine for my non-containerized setup, although most of it is unnecessary. The only confusing thing is what I mentioned - that it shows up in the "Run and Debug" panel too, and that part doesn't work (but doesn't need to).
Add example VS Code configuration to automatically detect, run, and debug tests using VS Code's Testing bar. Developers will still need to copy these example files into their own settings in order to make use of them. This was done to avoid potential issues where developer-specific configuration could cause problems for other developers.
Some example screenshots:
Test Browsing/Running
Debugger
Code Coverage