Skip to content

Added support for Django unittests #20675

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

Closed
wants to merge 4 commits into from
Closed

Added support for Django unittests #20675

wants to merge 4 commits into from

Conversation

mh-firouzjah
Copy link

it would check to see if there is a manage.py in the root folder and then checks inside that file to see if it is generated by django and also checks if ast.literal_eval has no exception due to execution of the line which would set DJANGO_SETTINGS_MODULE and then call django.setup to load all django apps and prepare to run the tests.

to set `DJANGO_SETTINGS_MODULE` and call `django.setup()` so to be able to execute django unittests as well
@mh-firouzjah
Copy link
Author

@microsoft-github-policy-service agree

@karthiknadig
Copy link
Member

Thanks for the PR. We are investigating what is minimally needed to get this working, and how to expose that to user in a way that it does not interfere with other scenarios. We will consider the approach taken here.

@mh-firouzjah
Copy link
Author

I have another Idea, when I click the test button on the side menu in vacode, for the first time it will ask to set a python unittest or pytest and so on, also some dropdown menus are there to select more options in order to initiate the test environment. it would be very helpful and more reliable if such an option be available for djanog test/module in addition to python module and so.
then user is able to select the correct statement and also to enter the required information to set DJANGO_SETTINGS_MODULE or the interpreter which has django installed and also call django.setup or other requirements

@eleanorjboyd
Copy link
Member

Hello @mh-firouzjah, thank you for this PR. Could you just explain with this PR what outcome you get for the testing interface? For example, once this is run is it possible for the support we already have in the extension for unittest to work and find/run the django tesets? Does this populate the text explorer or handle run tests? I am not as familiar with the eval(line.strip()) and therefore thought it might be easier for you to describe what state this PR is able to reach instead of me trying that. If that is too hard tho I can investigate. Thanks

@mh-firouzjah
Copy link
Author

mh-firouzjah commented Mar 1, 2023

hello @eleanorjboyd
actually what I did, doesn't do anything about who the extension was running tests, but it will help the extension to recognize the tests that are instances of django TestCase, and the rest of the job is as before.
I took some steps as follows:
check if there is a manage.py file and then check if it's generated by django, django will use this file to set an environment variable DJANGO_SETTINGS_MODULE to point to a file which is the settings file of a django project. I use that line and by using eval on that it will set that environment variable. just because use of eval is not recommend as it's vulnerable I used strip, literal_eval and split, but it's very rare (if any), that someone has touched this file.
second thing necessary to run a django test is that django.setup function muat be called and it has been added too.

normally your extension as it's current state is not aware about django. it could not recognize if a test is subclass of TestCase and it will not setup django, so in a workspace of a django project it would not see(locate/recognize,...) all (django) tests and of it does see a test it can not run that test because neither DJANGO_SETTINGS_MODULE is not set and django.setup is not called.

if you don't want to added this piece of code. it would be an alternative way to ask the user if it's a django project and if it is, where is the settings file located.

@eleanorjboyd
Copy link
Member

Hello! I am going to go ahead and close this PR. We are just now finishing up our testing rewrite and therefore our testing infrastructure looks different. Although we can't take this fix as is we look forward to working together to get django support working in vscode!

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.

3 participants