I recently encountered an elusive bug, which eventually I tracked down to the fact that environment variables are not replaced in a certain string even though it is set by calling os.path.expandvars(original string).
Turns out, the problem is that expandvars() doesn’t expand variables who are enclosed within a string literal. I couldn’t find it documented anywhere but facts are, this is the case.
>>> import os
>>> os.environ['VAR1'] = 'Value1'
>>> s1 = "this is '%VAR1%'"
"this is '%VAR1%'"
>>> s2 = "this is %VAR1%"
'this is Value1'
It may be a good idea in some cases, but for me it was quite an annoyance.
So, how to overcome this?
Here’s a short explanation how to execute python unit tests during VSTS/TFS builds, without the need of plug-ins. The basic idea is to create a JUnit XML from your test results, and then have it published to the TFS build dashboard.
First, on the relevant build agent(s), install the pytest module. You can use the following command, or your favorite CM tool.
python -m pip install pytest
Next, edit your build definition in TFS, and add two build steps: one (Batch Script) for running py.test to generate a junit XML, and one (Publish Test Results) for publishing the xml to the test results page.
The full command line to run the tests is as follows (in this example, the unittest code is in test_MyApp.py):
python -m py.test --junitxml test-results.xml test_MyApp.py
Here is a sample build definition which includes only those two steps:
Batch Script Settings
Finally, queue the build, and you will able to see the test results in the build page:
Python, like many other popular OSS (Git anyone?), does not support symbolic links on Windows platforms , although they have been around since Windows Vista. Note, I am referring to actual symbolic links, not NTFS directory junctions or shortcuts.
I’m not really sure why is that. Maybe because they do not distinguish between different Windows revisions?
My current approach is to use the OS commands to create/delete symlinks. It’s not very elegant but it works without compatibility issues, unlike other solutions (calling win32 api through DLLs, manipulating file attributes, and other stuff you find in StackOverflow or tech blogs)
For example, to create a symbolic link of directories, one can use:
child = subprocess.Popen(['MKLINK', '/D', link, target], stdout = subprocess.PIPE, stderr = subprocess.STDOUT, shell = True)
streamdata = child.communicate()
And check child.returncode for the result (and the output – stdout and stderr combined – available in the streamdata variable)
To remove a symbolic link to a directory, use the windows RMDIR command (os.rmdir or os.unlink won’t work)