-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Fixed Django tests discovery #21468
Fixed Django tests discovery #21468
Conversation
By invoking newly added function `setup_django_test_env` it will try to set up django environment by setting `DJANGO_SETTINGS_MODULE` environment variable and invoking `django.setup` method, if it succeeds django tests will be discovered as other tests currently are, otherwise it fails silently.
@karthiknadig @eleanorjboyd I have thoroughly tested and reviewed the changes I made to ensure they meet the project requirements and adhere to the coding standards. I believe that my changes will benefit the project and I would appreciate your approval. Thank you for taking the time to review my pull request. I look forward to hearing your feedback. |
Hello @mh-firouzjah! Apologies for the delay in getting back to you on this! Thank you for contributing and the detail you put into this PR, that is very much appreciated! As you know from this discussion which you have been a part of, the team has been thinking about the best way to provide Django support. My goal here is to provide the solution that works for the most number of people. Due to the fact that I am still a beginner in Django specifics, I have reached out to my colleague with Django expertise and will circle back. Thank you again, we rely on people in the community like you to make our extension useful for everyone! |
Hi @eleanorjboyd, appreciations. |
Hello again! I have discussed with my colleagues and think this is the flow we want to have for the setup/ discovery:
In terms of execution, about the same idea for that just replicating some imports and calling the same logic to parse args, try importing and run the script. How does this sound to you? It just makes it a bit more configurable where the extension provides the simplest option to setup Django with a default script but users can make their own if they want more control / need more setup. Also we are just now moving over to an architectural rewrite (it will go out on stable very shortly). You have most of your code in the new sections, we will no longer be using anything in the Sorry, this is a lot of info at once! Let me know your thoughts, ideas, or questions. Thanks! |
Thank you for explaining your requirements. Based on your explanation, I have reviewed the code and implemented the changes in a new way.
The function in
I also noticed your start_dir, pattern, top_level_dir, django_settings_module = parse_unittest_args(argv[index + 1 :])
# Setup django env to prevent missing django tests
if django_settings_module is not None:
setup_django_test_env(django_settings_module)
else:
# Try to be smart and find DJANGO_SETTINGS_MODULE
setup_django_test_env(root=start_dir)
The important part which is not clear to me is how we are going to pass The second question is, there is a test option in side menu of vscode which if user clicks on it it will possibly have two buttons there like what I have here in the image bellow, |
as far as I could go, there is a
import os
def setup_django_test_env(django_settings_module_path)
try:
import django
except ModuleNotFoundError:
# fail quietly
return
os.environ.setdefault("DJANGO_SETTINGS_MODULE", django_settings_module)
django.setup() |
Hello, I wanted to provide an update on the progress of the project. I am unsure if asking users to enter the Django settings module is the best approach, for a few reasons. Firstly, if there are multiple Django settings modules (for different phases of development/production), it could be overwhelming to constantly change test configurations. Additionally, if configurations are set in the workspace, there is no GUI option to change them, and it would require modifying the JSON file directly. I believe it may be more beneficial to ask users about the manage.py module instead, in order to read the DJANGO_SETTINGS_MODULE from that file. This would allow for automatic detection of changes to the settings module. It is still very handy to have the older logic I made. in case of user forgotten to configure django settings module but there are some django tests in the workspace that logic will help to detect the tests and in case of any exception it fails quietly so no harm to have it as a backup system All that have been said, I think this PR is better than what I prepared in V2 branch because with the changes I have here the Users will never be notified about that your extension will consider unittests and django tests two different kind of tests, which is not True and actually djnago tests are unittests (are subclasses of unittest). and if something goes wrong again it wont notify the user and will fail quietly I like this approach more than the other. I leave this PR opened and if anybody helps me to finish V2 I will open a new PR for that as well. If any of you could help me with TS that branch will be finished very soon. Thank you for considering this update, and please let me know if you have any further questions or if I can assist in any way. |
Thank you for your edits! Will review this soon as I have a few other items I need to get through. Will let you know if any more questions arise! |
71b0b1c
to
0f23238
Compare
- Extract the core logic to a new module - Make sure current work space exists in sys.path
When user is starting testing functionality it could be possible to ask users if they are going to work with django tests like when it prompts to select either
I'm wondering if we could use this |
Thank you for the updates! Will review sometime this week |
just to confirm that it could work as expected I did a check (of course I didn't generate TS/JS code as it's required for the final release but) passing
|
Hi @mh-firouzjah, looking at this again, so sorry for the delay. I have a meeting tomorrow to discuss with a coworker and then should come back with some more questions. Tried this PR out locally and was very impressed with how everything worked together. Excited to move this forward, thanks again! |
Hello! A few notes. First is that we are moving to a new design for testing args which will not effect this PR much but will instead have to do with the user experience of running tests. I have just created #22206 which proposes how Django enablement would look in this new setup. Sorry that this extra step is coming into play but we think the environment variables that we can then use with the new arg design will be the best place for these Django settings. I have written up this proposal to get feedback at Django-con so we can get some other perspectives on your design! This should only slightly change the script you wrote to just get these Django enablement variables from env vars instead of args. On another note, when reviewing this PR one question I did have that I thought you might be able to answer is about teardown. Are these specific steps we need to take at the end of Django testing to teardown databases etc and are those cases being covered via this implementation? I might add a few edits to this PR but otherwise, it is very great and we would just need to update it for this new arg design. I am more than happy to take this PR from here and see it to finish (obviously giving you credit for your amazing work) if you would like or can continue to involve you in the next steps (like switching to env vars, writing test cases, error handling etc). Let me know whatever you would like and either way, you have been instrumental in getting this on its way! Huge thank you! This is amazing! |
Hello, for the time being the script will uses the normal unittest so it will update database permanently. If any test needs to write/wipe database it will use the database defined inside settings.py` module not a temporary test database. But django way of running tests which is Now because of that, I wan to work on "How to use |
What are the advantages of using manage.py for discovery? Are there django tests that would show up / not show up given any setup that is required by manage.py? Wondering if unittest could do the discovery then django could run or if it would miss information like skips or ignores if manage.py didn't handle discovery. I would love to be able to use Would love to hear your thoughts on what is a good MVP vs a long term solution. Is there a way we can add support for Django and get it out soon then consider a more long term approach or is there a long-term solution we can consider now? Thanks |
hello! I took into consideration what you said about the ideal solution. I started experimented and came up with #22222. This allows us to run Django tests via |
Yes - as long as Django Tests are SubClasses of
|
Hello! Do you mind if I close this PR as we think the other one is a more likely plan for the solution? |
the important thing is to find a solution for the discovered problem, so I close this. |
By invoking newly added function
setup_django_test_env
it will try to set up django environment by settingDJANGO_SETTINGS_MODULE
environment variable and invokingdjango.setup
method, if it succeeds django tests will be discovered as other tests currently are, otherwise it fails silently.Currently the extension will fail because it doesn't set the required environment variable and also it doesn't invoke the django setup function, so make the environment ready for django tests.
I've added that function which tries to
import django
if it success then will try to findmanage.py
file which is a must have file for any django project and which is generated bydjango-admin startproject
command. inside this file there is an attempt to set theDJANGO_SETTINGS_MODULE
environment variable, and my function tries to extract the value of this variable from this file.at the end of the day, it will set the
DJANGO_SETTINGS_MODULE
environment variable and invokes thedjango.setup
function, if it succeeded the environment is prepared for django tests so the extension will no longer fail and we are good. if it fails to import django or find that file, the function will fail silently and has no effect on the extensions normal processes.This is the result of this change: