Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set an environment variable when running test from run button in VSCODE

I am creating some tests in Flutter, but I need to set an environment variable before running the tests. Is there a way to set an environment variable when running the app from these "Run | Debug" buttons?

enter image description here

like image 544
Leonardo da Silva Avatar asked Sep 05 '25 03:09

Leonardo da Silva


2 Answers

Using the --dart-define flag worked for me:

"configurations": [
    {
      "name": "Launch App",
      "request": "launch",
      "type": "dart",
      "args": [
        "--dart-define", "GOOGLE_OAUTH_CLIENT_ID=xxxxx",
        "--dart-define", "GOOGLE_OAUTH_CLIENT_SECRET=yyyyy"
      ],
    }
]

And then use it in the code as such:

String.fromEnvironment('GOOGLE_OAUTH_CLIENT_ID');

Note you may need to explicitly specify a const variable or assignment

like image 132
Oded Ben Dov Avatar answered Sep 07 '25 22:09

Oded Ben Dov


With the new update to the Dart SDK it is possible: https://dartcode.org/releases/v3-11/

[...] For example, to add CodeLens for a launch config that sets a RELEASE_MODE=true environment variable to tests in test/integration_tests:

{
    "name": "Current File (release mode)",
    "type": "dart",
    "request": "launch",
    "codeLens": {
        // Types of CodeLens to inject
        "for": [ "run-test", "run-test-file", "debug-test", "debug-test-file" ],
        // Restrict to certain folders
        "path": "test/integration_tests",
        // Text for CodeLens link (${debugType} will be replaced with "run" or "debug")
        "title": "${debugType} (release)"
    },
    "env": { "RELEASE_MODE": true }
}

This will insert additional CodeLens links into tests, groups and main functions:

like image 24
Leonardo da Silva Avatar answered Sep 07 '25 21:09

Leonardo da Silva