You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Distribute the tests in VSTS pipeline across multiple agents
4
+
.DESCRIPTION
5
+
This script slices tests files across multiple agents for faster execution.
6
+
We search for specific type of file structure (in this example test*), and slice them according to agent number
7
+
If we encounter multiple files [file1..file10] and if we have 2 agents, agent1 executes tests odd number of files while agent2 executes even number of files
8
+
For detalied slicing info: https://docs.microsoft.com/en-us/vsts/pipelines/test/parallel-testing-any-test-runner
9
+
We use JUnit style test results to publish the test reports.
10
+
#>
11
+
12
+
$tests=Get-ChildItem .\ -Filter "test*"# search for test files with specific pattern.
13
+
$totalAgents= [int]$Env:SYSTEM_TOTALJOBSINPHASE# standard VSTS variables available using parallel execution; total number of parallel jobs running
14
+
$agentNumber= [int]$Env:SYSTEM_JOBPOSITIONINPHASE# current job position
15
+
$testCount=$tests.Count
16
+
17
+
# below conditions are used if parallel pipeline is not used. i.e. pipeline is running with single agent (no parallel configuration)
18
+
if ($totalAgents-eq0) {
19
+
$totalAgents=1
20
+
}
21
+
if (!$agentNumber-or$agentNumber-eq0) {
22
+
$agentNumber=1
23
+
}
24
+
25
+
Write-Host"Total agents: $totalAgents"
26
+
Write-Host"Agent number: $agentNumber"
27
+
Write-Host"Total tests: $testCount"
28
+
29
+
$testsToRun=@()
30
+
31
+
# slice test files to make sure each agent gets unique test file to execute
32
+
For ($i=$agentNumber; $i-le$testCount;) {
33
+
$file=$tests[$i-1]
34
+
$testsToRun=$testsToRun+$file
35
+
Write-Host"Added $file"
36
+
$i=$i+$totalAgents
37
+
}
38
+
39
+
# join all test files seperated by space. pytest runs multiple test files in following format pytest test1.py test2.py test3.py
40
+
$testFiles=$testsToRun-Join""
41
+
Write-Host"Test files $testFiles"
42
+
# write these files into variable so that we can run them using pytest in subsequent task.
0 commit comments