运行 MsTest.exe 来自批处理脚本

Running MsTest.exe from Batch script

我正在尝试通过批处理脚本自动执行 运行 C# 解决方案的单元测试。

我的脚本运行良好,直到在 MsTest 调用中使用 /testcontainer 和 /test 标志实际调用 MsTest.exe。

下面的批处理脚本:

@echo OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

echo Detecting System specification...
echo.

reg Query "HKLM\Hardware\Description\System\CentralProcessor[=12=]" | find /i "x86" > NUL && set OS=32BIT || set OS=64BIT

set programFilesPath=xyz

if %OS%==32BIT (
    echo 32bit operating system detected.
    echo.
    set programFilesPath=%ProgramFiles%\Microsoft Visual Studio
) ELSE (
    echo 64bit operating system detected.
    echo.
    set programFilesPath=%ProgramFiles(x86)%\Microsoft Visual Studio
)

echo Checking for Visual Studio installations...
echo.

set /a highestVSVersion=54
set /a installationPath=xyz
set /a vsCount=0

for /d %%a in ("%programFilesPath%"*) do (

    rem echo "%%a"
    rem set installationPath=%%a
    rem echo !installationPath!
    rem echo !vsCount!

    for /f "tokens=5" %%g in ("%%a") do (
        set tempStr=%%g
        set tempStr=!tempStr:~0,2!

        if not "!tempStr!"=="!tempStr:.=!" (
            set tempStr=!tempStr:.=!
        )

        set tempTwo=0
        set tempTwo=!tempStr!

        if !vsCount! EQU 0 (
            set highestVSVersion=!tempTwo!
        ) else (
            if !tempTwo! GTR !highestVSVersion! (
                set highestVSVersion=!tempTwo!
            )
        )
    )

    set /a vsCount=!vsCount!+1
)

if !vsCount! EQU 0 (
    echo No Visual Studio installation found. Please install visual studio to run unit tests.   
    echo.
) else (

    if !highestVSVersion! GTR 9 (
        set highestVSVersion=!highestVSVersion!.0
    )

    echo Visual Studio !highestVSVersion! found.
    echo.
    echo Verifiying MsTest.exe location for Visual Studio !highestVSVersion!...
    echo.
    set fullPath=!programFilesPath! !highestVSVersion!\Common7\IDE\MsTest.exe

    if exist !fullPath! (
        echo MsTest.exe found at: !fullPath!
        echo.
        echo Running all tests in src\DictionaryDash.Testing
        set testContainerArg=src\DictionaryDash.Testing\bin\Release\DictionaryDash.Testing.dll

        call "!fullPath!" /testcontainer: !testContainerArg!
    )
)

pause

我遇到问题的地方是最后一条电话线。我在命令 window:

中得到以下输出
Detecting System specification...

32bit operating system detected.

Checking for Visual Studio installations...

Visual Studio 12.0 found.

Verifiying MsTest.exe location for Visual Studio 12.0...

MsTest.exe found at: C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE\M
sTest.exe

Running all tests in src\DictionaryDash.Testing

Microsoft (R) Test Execution Command Line Tool Version 12.0.21005.1

Copyright (c) Microsoft Corporation. All rights reserved.

Invalid switch "src\dictionarydash.testing\bin\release\dictionarydash.testing.dl
l".

Please specify a parameter for the /testcontainer switch.

For switch syntax, type "MSTest /help"

Press any key to continue . . .

它成功地找到了 MsTest.exe,但是在将参数传递给 /testcontainer 开关时失败了。脚本文件位于项目根目录下,与"src"目录同级

谢谢!

在将参数传递给 /testcontainer 开关时失败。

Invalid switch `src\dictionarydash.testing\bin\release\dictionarydash.testing.dll.

Please specify a parameter for the /testcontainer switch.

使用以下代码:

set testContainerArg=src\DictionaryDash.Testing\bin\Release\DictionaryDash.Testing.dll
call "!fullPath!" /testcontainer: !testContainerArg!

上述命令的语法不正确,/testcontainer:!testContainerArg!之间应该没有space。

space 导致 mstest!testContainerArg! 解释为传递给 mstest 的附加(无效)开关,而不是 [=] 的一部分13=]开关。

正确的语法是/testcontainer:[file name]

将批处理文件中的 call 命令更改为以下内容:

call "!fullPath!" /testcontainer:!testContainerArg!

进一步阅读