How to integrate MSTest in your TeamCity build process

后端 未结 1 1387
庸人自扰
庸人自扰 2021-02-03 12:01

How do I run MSTest as part of my build process in TeamCity? What are the pitfalls?

1条回答
  •  遥遥无期
    2021-02-03 12:57

    This answer is specifically for TeamCity 7.1 on Windows, but may apply to other environments.

    1. In your TeamCity build configuration, on the General Settings page
      1. Artifact paths: Artifacts\MSTest => MSTest
    2. Create a new Command Linebuild step
      1. Custom script: if not exist Artifacts\MSTest mkdir Artifacts\MSTest
    3. Create a new MSTestbuild step
      1. List assembly files: **\bin\**\*.Tests.dll
      2. Results file: Artifacts\MSTest\testResults.trx

    Pitfalls

    Using wildcards when specifying which test assemblies to run

    You can use wildcards when specifying which test assemblies to run in the MSTest build step, although it is unclear exactly how they work. A bug report has been filed.

    The build process doesn't stop when tests fail

    Be aware that if some of your tests fail and the build is marked as failed, the MSTest build step itself does not fail. This causes problems if you have build steps after the MSTest build step which you don't want to run if you have test failures (e.g. it may not make sense to produce an installer or documentation of a build you know has bugs). The problem will hopefully be fixed in later versions of TeamCity.

    If you want your build process to stop when you have test failures, you can create a new build step that uses the TeamCity REST API to detect if the current build has been marked as failed (remember that when tests fail, the build step is not marked as failed, but the build is), and then fail the current build step explicitly. Example:

    1. Create a new Powershell build step
      1. Script: Source code
      2. Source code: See script below
    2. Make sure your newly created build step comes immediately after your MSTest build step
    3. Make sure every build step after this one has Execute step set to Only if all previous steps were successful

    Script:

    $xml = [xml](curl --request GET http://USERNAME:PASSWORD@HOSTNAME/httpAuth/app/rest/builds/%teamcity.build.id%)
    Microsoft.PowerShell.Utility\Select-Xml $xml -XPath "/build" | % { $status = $_.Node.status }
    if ($status -eq "FAILURE") {
        throw "Failing build step on purpose"
    }
    

    0 讨论(0)
提交回复
热议问题