Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ormsbee
Copy link
Contributor

@ormsbee ormsbee commented Mar 24, 2025

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

Screenshot 2025-03-25 130542
Screenshot 2025-03-25 130609

Debugger

Screenshot 2025-03-25 131637

Code Coverage

Screenshot 2025-03-25 135751
Screenshot 2025-03-25 131011

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.
@ormsbee
Copy link
Contributor Author

ormsbee commented Mar 25, 2025

Note: I need to tweak this a bit to make test coverage work at the same time as debugging. Update: Done.

@ormsbee
Copy link
Contributor Author

ormsbee commented Mar 25, 2025

After discussing with some Axim folks, I've changed this PR to make example config files instead. It's ready for review.

@bradenmacdonald
Copy link
Contributor

I have a virtualenv just for openedx-learning on my host and always open that repo directly in VS Code, not using Tutor / containerization at all. In that case, almost everything is already working out of the box (and fast). The only thing that's necessary is putting the following into .vscode/settings.json:

{
    "python.testing.unittestEnabled": false,
    "python.testing.pytestEnabled": true,
}

Screenshot 2025-04-03 at 12 54 09 PM

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.

Comment on lines +7 to +22
{
"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
}
Copy link
Contributor

@bradenmacdonald bradenmacdonald Apr 3, 2025

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor

@bradenmacdonald bradenmacdonald left a 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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants