Run Xunit unit test with MSBuild

Source: Internet
Author: User
Tags xunit

After Visual Studio 2015 was used, it was found unable to run XUnit unit test, xunit.net Runner does not support Vs2015,testdriven.net and does not support VS2015.

Wait for them to support VS2015, do not know to wait until monkey still wait until Ma Yue. So today I decided to wait and try to run the unit test with MSBuild to solve the problem.

NuGet on a search, found that someone has provided the xunit. MSBuild, you can use it directly.

Install the NuGet package in the Unit test project:

Install-package Xunit. Msbuild

Find the following configuration in the corresponding. csproj file:

<Project= ". \packages\xunit. Msbuild.1.9.2.3\build\xunit. Msbuild.targets "  Condition=" Exists ('.. \packages\xunit. Msbuild.1.9.2.3\build\xunit. Msbuild.targets ') "/>

Xunit. The contents of the Msbuild.targets file are as follows:

<?XML version= "1.0" encoding= "Utf-8"?><ProjectToolsVersion= "4.0"xmlns= "http://schemas.microsoft.com/developer/msbuild/2003">  <UsingTaskassemblyfile= "$ (msbuildthisfiledirectory): \tools\xunit.runner.msbuild.dll "TaskName= "Xunit.Runner.MSBuild.xunit" />    <PropertyGroupCondition= "' $ (runxunittests) ' = = '">    <runxunittestsCondition= "' $ (Configuration) ' = = ' Release '">True</runxunittests>  </PropertyGroup>  <PropertyGroup>    <BuildDependsOn>$ (BuildDependsOn); Executexunittests</BuildDependsOn>    <Rebuilddependson>$ (Rebuilddependson); Executexunittests</Rebuilddependson>  </PropertyGroup>    <TargetName= "Executexunittests"Condition= "' $ (runxunittests) ' = = ' true '" >    <XunitAssembly= "$ (OutputPath) $ (AssemblyName). dll" />  </Target></Project>

As you can see from this msbuild configuration, executexunittests is the MSBuild command that runs the unit test, and the trigger condition is when the project is compiled in release mode.

Then, just modify the configuration in the. csproj, and call this executexunittests command after the project is compiled:

<Name= "Afterbuild"></Target  >

When configured, the results of the unit tests are displayed in the Output window of Visual Studio at compile time:

Panax Notoginseng> postmodeltest.postmodel_required_validation100 0.410 seconds Panax Notoginseng>notoginseng>build succeeded.

If the unit test fails, it is displayed as follows:

1>    1100.428  seconds1>1> Build FAILED. 1>1: xx:03.9101 0 skipped ==========

The status bar of Visual Studio also displays build failed.

This display is not intuitive, but it is convenient to run unit tests at compile time, and unit test failures can cause compilation to fail, highlighting the importance of unit testing.

Run Xunit unit test with MSBuild

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.