Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow to setup env variables specific to tests. #172

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
DonJayamanne opened this issue Nov 14, 2017 · 17 comments
Closed

Allow to setup env variables specific to tests. #172

DonJayamanne opened this issue Nov 14, 2017 · 17 comments
Labels
area-testing feature-request Request for new features or functionality info-needed Issue requires more information from poster

Comments

@DonJayamanne
Copy link

From @eMerzh on August 30, 2017 9:33

Hi thanks for your project πŸ‘ ,

in my app i use Env var to select the configuration.
So to select the correct configuration in the test i was looking for a way to put Env variable via vscode or smth.

Copied from original issue: DonJayamanne/pythonVSCode#1184

@DonJayamanne
Copy link
Author

you can create a file named .env in the root directory an defined your environment variables in there. That should work (I've just tested it).

Here are the instructions:

  1. Create .env file in root directory
  2. Add variables as follows
VARIABLE1=VALUE1
VARIABLE2=VALUE2
  1. These variables are now available in your unit tests
os.getenv("VARIABLE1")

@DonJayamanne
Copy link
Author

From @eMerzh on September 22, 2017 6:50

Thanks for your reply :)

but with this method i can't make the distinction between tests and dev env ? or can i ?

@DonJayamanne
Copy link
Author

but with this method i can't make the distinction between tests and dev env ? or can i ?

No you can't.

@DonJayamanne
Copy link
Author

From @eMerzh on September 22, 2017 20:19

ok :/ that was the whole point of this issue....

@DonJayamanne
Copy link
Author

@eMerzh apologies for not understanding the issue clearly and closing it prematurely.

Will look into this.
Personal note (look at settings specific a to test files/folder)
https://github.com/Microsoft/vscode/wiki/Extension-Authoring:-Adopting-Multi-Root-Workspace-APIs#do-i-need-to-do-anything

@DonJayamanne
Copy link
Author

From @eMerzh on September 24, 2017 11:55

@DonJayamanne no problem... probably my fault too not being explicit enough...

Thank you for your help :)

@DonJayamanne
Copy link
Author

Could possibly be resolve with changes in #1128

@DonJayamanne
Copy link
Author

From @eMerzh on October 19, 2017 20:3

@DonJayamanne are you sure you linked the correct issue ? πŸ€”

@DonJayamanne
Copy link
Author

From @asfaltboy on November 2, 2017 13:10

@DonJayamanne I'm sorely missing this feature. Would a PR for this be appreciated? I'm new to VSCode extension development, but it seems by looking at the code, this could be relatively simple to fix. This is what I had in mind:

  1. Allow config.envFile to be a dictionary of values, where keys are mapped to different environment files (e.g .env, .envTest, etc...)
  2. Accept custom env file key in getCustomeEnvVars.
  3. Allow execPythonFile to be passed an optional env file key.
  4. Have runTest call execPythonFile with the env file key for test.

Alternatively, we could also hard-code the two settings (envFile and testEnvFile) and call execPythonFile with something like test: 1. But I like the flexibility if we were to have additional environments in mind, like a separate env for debugging, etc.

Please let me know if you'd like to see this sort of change, or you have something else in mind.

@DonJayamanne
Copy link
Author

@asfaltboy Next month VS Code will be releasing a new feature which would allow opening multiple folders in the same workspace, this would allow you to have different settings for different folders (provided they are opened as workspaces). I believe this would give you what you need.

I.e. lets wait for around 2 weeks untill the next release of VS Code and catch up then.

@DonJayamanne
Copy link
Author

DonJayamanne commented Nov 14, 2017

@asfaltboy with the current release of VS Code you can now create a separate workspace folder for your unit tests. This will allow you to have separate env environments for the unit tests.
Please try this configuration, it should work.

@brettcannon brettcannon added info-needed Issue requires more information from poster area-testing feature-request Request for new features or functionality labels Nov 14, 2017
@asfaltboy
Copy link

@DonJayamanne can you please point us to the docs regarding "create a separate workspace folder"? What does it mean in practice, that users need to switch out to another workspace to run tests and then switch back to develop?

@DonJayamanne
Copy link
Author

@asfaltboy , please check the latest version of VS Code for details on multi-root workspace folders
https://code.visualstudio.com/updates/v1_18#_support-for-multi-root-workspaces

@brettcannon
Copy link
Member

I'm closing this as it has been over a month since we requested more info. If we do get the info at a later date we can re-open this issue.

@pzelnip
Copy link

pzelnip commented Jun 2, 2018

I realize this has been closed, but wanted to inquire: am I understanding correctly that multi-root workspace folders would mean having to duplicate my checked out copy of my code? If so that's extremely cumbersome.

Right now as a workaround I create a shell task to run my tests & then just have the command source a file before running the command. Ie something like:

        {
            "label": "Run Unit Tests",
            "type": "shell",
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "command": "source local.env && ${config:python.pythonPath} -m nose2 --config tests/unittest.cfg"
        },

which works, but feels clunky and also means I give up the ability to debug my tests from VS Code. As an alternative to get breakpoints & debugability, I created a launch config in launch.json:

        {
            "name": "Python: Run Tests (nose2)",
            "type": "python",
            "request": "launch",
            "module": "nose2",
            "envFile": "${workspaceFolder}/local.env",
            "cwd": "${workspaceFolder}/server_app",
        },

which works, but doesn't integrate with the normal VS Code "Run Tests" stuff.

What would be really nice is a setting like "python.unitTest.envFile which is effectively the same as the envFile setting in the launch.json I have above.

@brettcannon
Copy link
Member

@pzelnip multi-root workspaces are for working with multiple directories as if they are a single project, so I'm not sure why you think you need to check out your code multiple times?

If you're after a way of defining a launch.json target that is used when you use Debug Tests commands then that's a separate feature to request in #332

@pzelnip
Copy link

pzelnip commented Jun 5, 2018

From the screencap on the supplied link (https://code.visualstudio.com/updates/v1_18#_support-for-multi-root-workspaces) it looked like multi-root implied multiple folders, apologies if I misinterpreted.

In any case, #332 sounds exactly like what I have in mind, thanks!

@lock lock bot locked as resolved and limited conversation to collaborators Jul 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-testing feature-request Request for new features or functionality info-needed Issue requires more information from poster
Projects
None yet
Development

No branches or pull requests

4 participants