从批处理脚本

时间:2016-07-30 12:12:55

标签: unit-testing batch-file command-line mstest

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

我的脚本工作正常,直到在MsTest调用中使用/ testcontainer和/ test标志实际调用MsTest.exe。

下面的批处理脚本:

@echo OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

echo Detecting System specification...
echo.

reg Query "HKLM\Hardware\Description\System\CentralProcessor\0" | 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

我遇到麻烦的地方是最后一个电话。我在命令窗口中得到以下输出:

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"相同。 。目录

谢谢!

1 个答案:

答案 0 :(得分:2)

当涉及将参数传递给/testcontainer开关时,它失败了。

  

无效的开关`src \ dictionarydash.testing \ bin \ release \ dictionarydash.testing.dll。

     

请指定/ testcontainer开关的参数。

使用以下代码:

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

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

该空格导致mstest!testContainerArg!解释为传递给mstest的其他(无效)切换,而不是/testcontainer:切换的一部分。< / p>

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

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

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

进一步阅读