Skip to content

Instantly share code, notes, and snippets.

@JeffJacobson
Last active February 23, 2017 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffJacobson/000193d035906907bcf2db61bcede11a to your computer and use it in GitHub Desktop.
Save JeffJacobson/000193d035906907bcf2db61bcede11a to your computer and use it in GitHub Desktop.
Runs Python unittests against multiple Python environments
<# Runs Python unit tests in multiple environments #>
# Get python.exe paths.
$pyenvs = Get-ChildItem -Path "C:\Python*\" -Filter "python.exe" -Recurse
$pyenvs += Get-ChildItem -Path "C:\Program Files\ArcGIS" -Filter "python.exe" -Recurse
# Build the list of modules that will be tested.
$modules_to_test = [string]::Join(" ", @(
"test_travelerinfo",
"test_armcalc",
"test_routeshields"
)
)
# Initialize collection of jobs
[System.Diagnostics.Process[]]$jobs = $()
Write-Output "Running unittests..."
# Loop through the paths to the various python executables.
foreach ($pypath in $pyenvs) {
# Run the unittests and store the process as a variable.
$jobs += Start-Process -FilePath $pypath -ArgumentList "-m unittest $modules_to_test" -PassThru
}
# Wait for the processes to complete.
Wait-Process -InputObject $jobs
# Collect Python paths and unittest exit codes
$jobResults = @{}
for ($i = 0; $i -lt $jobs.Count; $i++) {
$path = $pyenvs[$i]
$proc = $jobs[$i]
$jobResults.Add($path.FullName, $proc.ExitCode)
}
# Write-Output $jobResults | Format-Table -AutoSize
return $jobResults
<#
.SYNOPSIS
Runs Python unit tests in multiple environments
.DESCRIPTION
Runs Python unit tests in multiple environments, omitting some tests when a specific module is not installed.
#>
<#
.DESCRIPTION
Gets paths to Python executables
#>
function getPythonExePaths ($searchPathList=@("C:\Python*\", "C:\Program Files\ArcGIS")) {
<# Gets paths to python EXE files #>
$pyenvs = @()
foreach ($path in $searchPathList) {
$pyenvs += Get-ChildItem -Path $path -Filter "python.exe" -Recurse
}
return $pyenvs
}
<#
.DESCRIPTION
Tests to see if "arcpy" in installed in the given python environment.
.PARAMETER pythonPath
Path to a Python executable (i.e., python.exe)
#>
function isArcPyInstalled ($pythonPath) {
return (Start-Process -FilePath $pypath -ArgumentList '-c "import arcpy"' -Wait -PassThru).ExitCode -eq 0
}
$pyenvs = getPythonExePaths
# Tests to run when arcpy is not installed
$no_arcpy_tests = "test_armcalc"
# Tests to run when arcpy is installed
$yes_arcpy_tests = "$no_arcpy_tests test_armcalc_arcpy"
# Initialize collection of jobs
[System.Diagnostics.Process[]]$jobs = $()
Write-Output "Running unittests..."
# Loop through the paths to the various python executables.
$i = 0
foreach ($pypath in $pyenvs) {
# Run the unittests and store the process as a variable.
$arcpyexists = isArcPyInstalled
Write-Debug "$pypath.FullName has arcpy: $arcpyexists"
$logfile = ".\testlog$i.log"
[string]$testlist = $null
if ($arcpyexists) {
$testlist = $yes_arcpy_tests
} else {
$testlist = $no_arcpy_tests
}
# Start test job and add to list
$jobs += Start-Process -FilePath $pypath -ArgumentList "-m unittest $testlist" -PassThru
$i++
}
# Wait for the processes to complete.
Wait-Process -InputObject $jobs
# Collect Python paths and unittest exit codes
$jobResults = @{}
for ($i = 0; $i -lt $jobs.Count; $i++) {
$path = $pyenvs[$i]
$proc = $jobs[$i]
$jobResults.Add($path.FullName, $proc.ExitCode)
}
# Write-Output $jobResults | Format-Table -AutoSize
return $jobResults
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment