manage.py tool Test framework - Test Framework quit unexpectedly - Environment Variables?
I use CTRL + ALT + R to open the Pycharm manage.py tool window. If I type "test" and hit enter, a Run window opens with a Test tab. In it, a red message reads "Test framework quit unexpectedly".
Judging by the errors, it's because my environment variables that exist within my settings.py file are not taken into account.
Where in PyCharm can I define the environment variables for the Tests framework to work correctly?
```Testing started at 2:24 PM ...
C:\Users\Jarad\Documents\PyCharm\knowledgetack\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm 2020.1.2\plugins\python\helpers\pycharm\django_test_manage.py" test knowledgebase C:\Users\Jarad\Documents\PyCharm\knowledgetack
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2020.1.2\plugins\python\helpers\pycharm\django_test_manage.py", line 148, in <module>
custom_settings = __import__(settings_file)
File "C:\Users\Jarad\Documents\PyCharm\knowledgetack\knowledgetack\settings.py", line 23, in <module>
SECRET_KEY = os.environ['SECRET_KEY']
File "C:\Python37\lib\os.py", line 678, in __getitem__
raise KeyError(key) from None
KeyError: 'SECRET_KEY'
Process finished with exit code 1```
Please sign in to leave a comment.
Does the issue reproduce in a brand new project?
I can't reproduce it in my environment.
Thanks for your help Sergey. I'm new to testing so I may be doing something wrong. Here's the full recreation steps from a brand new project named testproj
On startup screen, Create new project > Django > new project named testproj, virtual environment created, app created at the same time called "knowledgebase" (screen wouldn't resize so I tried to show as much as I could of each)
Next, in settings.py, import os, remove Django's secret key and replace to look like this:
SECRET_KEY = os.environ['SECRET_KEY']
Next, open Edit configurations for the project, add this secret key to the environment variables:
Apply > OK
Next, File > Settings > Languages & Frameworks > Django > Environment variables > Paste SECRET_KEY here as well (for manage.py tool)
In <app>/tests.py, add some kind of test (doesn't matter what it is):
Next, CTRL + ALT + R to pull open the built-in manage.py task window
We come to the error I'm referring to:
If we add the SECRET_KEY to Tools > Terminal:
And run tests from the terminal instead of through PyCharm's manage.py:
(remember to reload the terminal window to take the new environment variable into account)
We see that the tests work this way:
My current version of PyCharm is (redacted parts)
PyCharm 2020.1.2 (Professional Edition)
Build #PY-201.7846.77, built on May 31, 2020
Licensed to ##### #######
You have a perpetual fallback license for this version
Subscription is active until ##############
Runtime version: 11.0.7+10-b765.53 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10 10.0
GC: ParNew, ConcurrentMarkSweep
Memory: 1973M
Cores: 8
Registry: documentation.show.toolbar=true, debugger.watches.in.variables=false, ide.tooltip.initialDelay=0, js.debugger.webconsole=false
Non-Bundled Plugins: com.floobits.unique.plugin.id, mobi.hsz.idea.gitignore, com.intellij.plugins.html.instantEditing, net.fhtagn.pycharm.cellmode, org.nik.presentation-assistant, ua.in.dej.myEmmet
Ahh, I see now.
I was able to reproduce it and found the root cause of the problem.
So when you run your tests from manage.py, PyCharm uses Django tests run configuration to run your tests which doesn't have your environment variable. So adding your SECRET_KEY to Django tests run configuration template should solve the problem:
Also, have you considered running your tests using PyCharm's interface instead of manage.py?
See https://www.jetbrains.com/help/pycharm/testing-your-first-python-application.html for more info about running tests in PyCharm.
Got it. In conclusion, I made a Run/Debug configuration from Django Tests template, added the environment variables there, and will run tests through the run / debug buttons instead of through the manage.py tool window.
Also, thanks for the tips on generating test stubs from that link. I will continue to look further into that.