Setting up environment variables for hundreds of tests get old very quick. Is there a way to declare an environmental variable globally in Eclipse?
Can this be done in Eclipse? Can this be done outside of Eclipse?
It seems that the only way to do it is to enable "Run all tests in the selected project .." and set Environment variables once there.
If you want to run a single test, and that test requires an environment variable set, it looks like you need to set that environment variable as part of that tests's settings.
In windows use the "start" command to spawn eclipse from command line with defined variables (linux has similar functionality)
Make file starteclipse.cmd
================================
SET VAR1=SOMEVALUE
SET VAR2=SOMEVALUE
start d:\eclipse\eclipse.exe
================================
From command line go to the dir with starteclipse.cmd file and run it.
This will spawn eclipse with proper environment settings.
I also find it frustrating having to set env vars one by one.
If you own the code and have a chance to change it, you'd be better off using a System property instead, which can be passed as VMArgs -Dprop=val.
You can set VM args in a global level - Preferences | Java | Installed JREs, and Plugin Development | Target platform.
Even if you can't change the code, most well civilised java libs support properties as well if not more than env vars- so it's worth double checking.
(of course it's not an option for external processes).
Related
I am currently Using Pytest though the Python Test Explorer (see screen). I would like to pass my tests some environment variables at runtime. However, I currently get my AWS Parameter Store BEFORE I run tests. Meaning, I run a command in BASH that loads my environment variables into my current terminal session. Then I can run my test from the command line using pytest exampletest.py, and it finds the environment variable perfectly.
Trying to run exampletest.py from the integrated VSCode Test Explorer results in an error telling me that it could not find my environment variable. This means that whatever environment that is used when you hit the "Play Button" on the Test Explorer is not the same terminal session that I set my environment variables in.
The conventional way of setting environment variables such that they can be accessed when using the Test Explorer in VSCode is by either using an ini file or a .env file. The only caveat there is that you must hardcode the key value pair. I would not like to hardcode anything, as I would just like to inject them in at runtime from aws Parameter Store.
I really would like to continue using the Test Explorer to run my tests, as it has been great in the past, but if I cannot find a way to somehow get the Test Explorer to use my environment variables without using an ini or a .env file, I may have to abandon it.
Some questions:
When you click the play button on the Test Explorer to execute a test or suite of tests, what exactly happens? Does it open a Terminal session? Can you access that terminal session before runtime?
How does the Test Explorer set environment variables. When I raise Exception(os.environ), I can see all the environment variables that are quite different from the ones that I get when I run from the command line.
Is there a way to say "Hey Test Explorer, please run the tests inside of this specified Terminal window that is already running"
Perhaps a virtual environment is a way to go (not really a question more of a thought). Doing some cursory research reveals more headaches to come if I pursue this route I believe.
Do you have any further reading about that VsCode Test Explorer that goes beyond the official documentation. I feel like I can make some changes to it's source code or something, but I feel like documentation on it is a bit lacking.
Let me know your thoughts on this matter, and thanks in advance.
I have the same question. The only way I could make it work was by this unwieldy series of steps before running the tests via the vscode UI:
Execute your script or command in a terminal to set the env
variables.
Copy those env variables into your .env file (placed at the root of your project/vscode-workspace). I usually do :
env | grep AWS_ or whatever you want and you'll see them as name-value pairs. I then copy and paste those entries at the bottom of the .env file each time I run the test via the UI.
Now execute your tests via the vscode UI (the test 'beaker'/flask). The testing UI/flask seems to execute the .env file every time before it runs the tests and hence picks up those env variables.
I know, it's not preferable - you would want it to just pick it up from the env values you see in your integrated terminal but so far that is not happening.
So, I am having a project which I am required to update an environmental variable of the testing execution due to external reasons. In parallel, I am executing the tests in different ways (unittesting, performance, specific module...), so that I need to change the environmental variables of all these configurations everytime I am externally requesting, creating some administrative work.
Is there any way to set up a common environmental variable for different test configurations?
Looking to eclipse documentation I do not see any way to perform it, so I am really looking for a hack (maybe modifying configuration file programmatically)
You can define your own Variables in Preferences > Run/Debug > String substitution. Then you can use those variables in "Environment" settings of your Run/Debug configuration.
I am developing with openerp using PyDev on a Linux box.
So far I have to use 6 different run configurations, with an argument whose value is the same for all these configurations, and an other argument which is specific to each one.
Example: openerp -d <common_value> -u <specific_value>
I want to be able to do this : openerp -d $COMMON_VALUE -u <specific_value>, so that everytime I have to change the value of db, I don't have to go and change its value manually in all the six run configurations.
I tried to use environment variables, but these do not get reloaded even after I restart Eclipse. Would there be a way to read it from a bash script or other ?
Thank you for your help
Well, I think the best way in this case would be creating a launcher script (which in turn would fix sys.argv and call execfile with the openerp module) and create a launch for that module instead.
If you want, you can load that value from an environment variable (then set the environment variable in preferences > PyDev > Interpreters > Python Interpreter > Environment) -- and that's also the place you have to use to change it later on, as the process will by default inherit the environment variables that were used when Eclipse was started up.
Or load it from a file -- or leave it hardcoded and change on that module -- which may be the most straightforward way :)
Another option:
You can 'connect' your launch configurations to a file (run > run configurations > select the configuration > common > save as > shared file), that way you can do a search/replace to fix the run configurations (which should be faster than doing it in the UI).
Using PyDev with Eclipse Juno, I need to set some environment variables to debug every single of the many scripts in my project. I have found how to set them for a given script but it would be totally impractical to do it for all of them. The only solution I have found consists in adding those variable to the Python interpreter configuration in the Preferences. But then it will be used by all projects, which I don't want.
I thought of creating a script setting those environment variables and then launching Python, and then add it as a new Python interpreter but PyDev does not accept two interpreters with the same actual exe (I guess it checks sys.executable).
The recommended approach is really setting it at the Python interpreter level.
You can use virtualenv ( http://pypi.python.org/pypi/virtualenv ) to create a copy of your interpreter in a different path.
Just to elaborate that #FabioZadrozny's answer meets the need of the OP...
In case it hasn't occurred to you, do this:
From the menu "Window->Preference"
Navigate to "PyDev->Interpreters->Python Interpreter"
Create a new interpreter instance with "New..." button
Name it something like "python-MyProj", but use the same executable you are already using
Now in the "Environment" tab for that interpreter, set up the needed environment variable
Now, see the PyDev project to use THAT interpreter:
Right-click the project to get "Properties..."
Navigate to "PyDev-Interpreter/Grammar"
Select the newly created interpreter instance by name
Now run any python file within the project, and it should "see" the configured environment variable.
By this mechanism, you do have a "project specific setting"... which, again, for the reasons #FabioZadrozny pointed out, need to be set at the interpreter level.
I am trying to create an eclipse runtime tool to diff 2 files. For my specific instance the files have the same name but are comparing versions.
I created a runtime external tool configuration using p4merge using these arguments:
gen\${string_prompt:File Name} gen2\${string_prompt:File Name}
What I really want is 1 dialog box where I can use the variable 2ce.
Anyone know if this is possible directly in eclipse other than writing a batch or shell script?
In the "External Tools Configurations" window, on the "Environment" tab, create a new environment variable. Set it's name to tmp_StrInput or something similar and it's value to "${string_prompt:Hint:DefaultText}".
Then, in your arguments (or anywhere else) on the "Main" tab, use ${env_var:tmp_StrInput} every time you want to insert the result from the string_prompt dialog.
The prompt appears and sets the environmental variable before the tool is run, the only negative side-effect is that you have created an extra environment variable. It does seem a bit like a work around but it works well!