Issue
I'm writing a python unit test using unittest
and I want to compile some Solidity contracts in the test setup.
However, when I did the compile by os.system()
or subprocess.run()
, it shows that solc
, the Solidity compiler, not found. While it runs properly when running in a non-test python program.
After this happens, I found a further interesting things: when I print(os.environ)
in both a test python program using unittest and a normal python program, the result is of hugh difference! Including the most important one: $PATH
. It looks like the following:
****PATH in unittest****
/usr/bin
/usr/local/bin
...(mostly the default $PATH set by Linux)
****PATH in normal program****
/usr/bin
/.../myEnvs/.../bin
...(as same as my console's $PATH, which is exported in .bashrc)
Since I'm working with others to develop this program, I should NOT add path such as "myEnvs" in program (by using environment setting like env=
parameter provided by subprocess.Popen()).
I think the abnormal $PATH
in python.unittest is caused by some configurations introduced by VScode, so what are these configs? How could I modify them? or this "PATH inconsistence"
is caused by some other reasons?
Solution
Okey..... After a period of time, I finally find out what happens:
- Exactly, The so-called "
$PATH
difference" is caused by VScode configurations in launch.json. - You can create a new term like this and make your unittest configuration same as a normal debug:
{
...
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false,
"args": []
},
{
"name": "Python: Debug Tests",
"type": "python",
"request": "launch",
"program": "${file}",
"purpose": ["debug-test"],
"console": "integratedTerminal", // Consistence with ```Python: CurrentFile```
"justMyCode": false,
},
]
}
This
Debug Test
configuration can be used in different test mode, see more in VSCode doc - Debug tests.The critial one is "console", the default value is "internalConsole", which seems to share the same
env
with system defaults. While I should set it to "integratedTerminal", which is consistent with my own shell.
Answered By - Alexious Answer Checked By - Marilyn (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.