Search code examples
powershellmstestvstest.console.exe

How to run mstest with VSTEST.CONSOLE.EXE in powershell


After reading and experimenting available posts in Stack overflow I am asking this question.

I'm struggling to trigger MSTEST in powershell

Here is my attempt (obviously with the help of posts available in stack overflow)

$testDLL = "C:\Automation\Tests\My.Tests.dll"
$fs = New-Object -ComObject Scripting.FileSystemObject
$f = $fs.GetFile("C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe")
$vstestPath = $f.shortpath   
$arguments = " " + $testDLL + ' /TestCaseFilter:"TestCategory=FunctionalTests"'
Write-Host $arguments
& $vstestPath $arguments

Console Output:

    PS C:\Users\myuser\Desktop\Powershell scripts> .\exp5.ps1
     C:\Automation\Tests\My.Tests.dll /TestCaseFilter:"TestCategory=FunctionalTests"
    Microsoft (R) Test Execution Command Line Tool Version 15.9.1
    Copyright (c) Microsoft Corporation.  All rights reserved.

    VSTEST~1.EXE : The test source file "C:\Automation\Tests\My.Tests.dll /TestCaseFilter:TestCategory=FunctionalTests" provided was not found.
    At C:\Users\myuser\Desktop\Powershell scripts\exp5.ps1:7 char:1
    + & $vstestPath $arguments
    + ~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (The test source... was not found.:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError

    Usage: vstest.console.exe [Arguments] [Options] [[--] <RunSettings arguments>...]]

    Description: Runs tests from the specified files.

    Arguments:

    [TestFileNames]
          Run tests from the specified files. Separate multiple test file names
.
.
.
. etc

If I run in normal windows cmd prompt then tests are running without any problem but when I tried to trigger from powershell getting above error.

can someone help me what is wrong with my script.

If my script is complicated or doesn't make sense please excuse me as I'm novice in powershell. My goal is to use the above powershell script in Octopus Deploy step process to trigger tests. If anyone know easy or better way of doing it please share...

Thank you


Solution

  • Thank you all, I have used below code to make it work.

    $command = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe"
    $arguments = @('C:\Automation\Tests\My.Tests.dll', '/TestCaseFilter:"TestCategory=FunctionalTests"')
    & $command $arguments
    

    @Leee_Dailey - Sorry for the confusion. I am just trying to trigger tests from Octopus Deploy (OD) 'Inline Source Code' for 'Run Script' template using powershell commands. I have started experimenting in PowerShell to make it work locally before using in OD. There is no straight answer found when I do a search. I got sometime and figured it out. Hope this code will helpful for someone. Thank you.