Specify MSTest parameters in MSBuild

2019-08-10 19:14发布

问题:

I'm trying to prevent certain (MSTest) unit tests from being run on our build server. I'd really like to just add a TestCategory, and then specify:

/category:"!RequiresLoginCredentials"

But I'm not sure how to indicate that in the msbuild project file.

The relevant section of the build file currently has:

  <ItemGroup>
    <!--  TEST ARGUMENTS
     If the RunTest property is set to true then the following test arguments will be used to run 
     tests. Tests can be run by specifying one or more test lists and/or one or more test containers.

     To run tests using test lists, add MetaDataFile items and associated TestLists here.  Paths can 
     be server paths or local paths, but server paths relative to the location of this file are highly 
     recommended:

        <MetaDataFile Include="$(BuildProjectFolderPath)/HelloWorld/HelloWorld.vsmdi">
            <TestList>BVT1;BVT2</TestList>
        </MetaDataFile>

     To run tests using test containers, add TestContainer items here:

        <TestContainer Include="$(OutDir)\HelloWorldTests.dll" />
        <TestContainer Include="$(SolutionRoot)\TestProject\WebTest1.webtest" />
        <TestContainer Include="$(SolutionRoot)\TestProject\LoadTest1.loadtest" />

     Use %2a instead of * and %3f instead of ? to prevent expansion before test assemblies are built
    -->
    <TestContainer Include="$(OutDir)\UnitTests.dll" />

  </ItemGroup>

I'm guessing this is a simple addition, but I know very little about msbuild.

Thanks!

回答1:

I did a quick search for the answer and I think there are two possible solutions:

  1. From what you described, looks like you're trying to run the tests using the TestToolTask task of MSBuild. Unfortunately, I don't think you can pass MSTest arguments directly to this task. To accomplish what you want, you need to specify the tests you want to run in a test list, and pass the test list to this task. You need to use the MetadataFile property as shown in the example in your post.

  2. You can invoke MSTest.exe directly using the Exec task of MSBuild. This way you have the freedom to pass in the arguments you want.