I am attempting to run a series of tests for a python project in PyCharm and have been running into os.environ[] KeyErrors near non-stop the last few days. Device is a MacBook Pro M2.
I can access the env vars from the built in "Terminal" but booting up python and testing with os.environ, but not when I actually go and run the test or debug. That's when the KeyErrors come up.
Here's what I have done so far:
The current workaround is to add an EnvFile config for every individual test. It seems like Run>Edit Configurations does absolutely nothing unless I specify the exact file_name/python_function. Creating a run config for an entire test folder doesn't seem to do anything.
Here is the content of my config.xml file:
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Unittests in tests/unit" type="tests" factoryName="Unittests">
<module name="smde_refactor" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="$PROJECT_DIR$/venv/bin/python" />
<option name="SDK_NAME" value="Python 3.10 (smde_refactor)" />
<option name="WORKING_DIRECTORY" value="" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="net.ashald.envfile">
<option name="IS_ENABLED" value="true" />
<option name="IS_SUBST" value="false" />
<option name="IS_PATH_MACRO_SUPPORTED" value="false" />
<option name="IS_IGNORE_MISSING_FILES" value="false" />
<option name="IS_ENABLE_EXPERIMENTAL_INTEGRATIONS" value="false" />
<ENTRIES>
<ENTRY IS_ENABLED="true" PARSER="runconfig" IS_EXECUTABLE="false" />
<ENTRY IS_ENABLED="true" PARSER="env" IS_EXECUTABLE="false" PATH="tests/unit/environment.env" />
</ENTRIES>
</EXTENSION>
<option name="_new_pattern" value="""" />
<option name="_new_additionalArguments" value="""" />
<option name="_new_target" value=""$PROJECT_DIR$/tests/unit"" />
<option name="_new_targetType" value=""PATH"" />
<method v="2" />
</configuration>
</component>
^I know this is a folder instead of a script -- but even when a specific script is selected there's still no luck picking up env vars with any of the functions inside that script.
If I run a.py script as a whole (after adding env vars to the run config), it succeeds (os.environ picked up.). But if I run any of the tests within that,py script individually. they all fail (could not pick up os.environ).
Thank you!
Here is a sample configuration that invokes Pytest on tests/ folder:
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="All tests" type="tests" factoryName="py.test">
<module name="$PROJECT_NAME" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="ABC" value="XXX" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="_new_keywords" value="""" />
<option name="_new_parameters" value="""" />
<option name="_new_additionalArguments" value=""--tb\u003dshort""/>
<option name="_new_target" value=""$PROJECT_DIR$/tests"" />
<option name="_new_targetType" value=""PATH"" />
<method v="2" />
</configuration>
</component>
As you can see, there is an <envs>
section within which ABC
variable is being set to XXX
. Also take a note of factoryName
being py.test
instead of your Unittests
, which is a different type of run config.
To have the env variables automatically set for each run config that gets automatically created when you click on a green arrow next to a test function in the editor, you need two things:
However, I realise that you are using EnvFile plugin and that might be completely invalidating what I have written above which is demonstrating built-in PyCharm functionality. If you want to test my example, I suggest you disable the plugin for a while.
Well...
tmt was correct -- all I really needed to do was mess around in Run>Edit Configurations for a few minutes.
The key was to then go in and "Edit configuration templates". Just to make sure, I added the env vars desired to the templates for Python, Python tests/pytest and Python tests/Unittests.
^THIS did not work immediately. I had to go and delete all my existing configs that appear when I first opened Run>Edit Configurations.
Then voila -- all pytests scripts+modules in the folder started picking up on the required env vars.
*on a side note, I think the EnvFile plug-in adds that
<ENTRY IS_ENABLED="true" PARSER="env" IS_EXECUTABLE="false" PATH="tests/unit/environment.env" />
line under ENTRIES instead of specifying the keys/values directly in the.xml (not that it worked anyway)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.