Click here to Skip to main content
16,016,783 members
Articles / Programming Languages / XML

XUnit and MSBuild

Rate me:
Please Sign up or sign in to vote.
3.59/5 (3 votes)
22 May 2015CPOL 8K   2   1
XUnit and MSBuild

Recently, I needed to execute xUnit tests with MSBuild, so I've spent some time for creating a MSBuild project running the tests. Luckily, xUnit already has MSBuild tasks so I just needed to hook it up.

I wanted to search for all xUnit unit test DLLs inside a folder and run the tests there. So I'm searching for xunit.core.dll file to get all the folders eventually containing such DLLs and then search all these folders for a pattern - *.Tests.dll. When the tests DLLs are found, xunit task is run for all of them. So, here's the result .proj file:

XML
<?xml version="1.0" encoding="utf-8"?>
<Project
    DefaultTargets="Test"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <UsingTask
      AssemblyFile="xunit.runner.msbuild.dll"
      TaskName="Xunit.Runner.MSBuild.xunit"/>

    <UsingTask TaskName="GetAssemblies" TaskFactory="CodeTaskFactory" 
               AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
        <ParameterGroup>
            <Path ParameterType="System.String" Required="true" />
            <XUnitFileName ParameterType="System.String" Required="false"/>
            <TestsSearchPattern ParameterType="System.String" Required="false"/>
            <Assemblies ParameterType="System.String[]" Output="true" />
        </ParameterGroup>
        <Task>
            <Code Type="Fragment" Language="cs">
                <![CDATA[
                    var xUnitFileName = XUnitFileName ?? "xunit.core.dll";
                    var testsSearchPattern = TestsSearchPattern ?? "*.Tests.dll";
                
                    // get all the directories containing xUnit dlls
                    var directories = System.IO.Directory.GetFiles
                                      (Path, xUnitFileName, System.IO.SearchOption.AllDirectories)
                        .Select(file => System.IO.Path.GetDirectoryName(file));

                    var assembliesList = new List<string>();

                    foreach(string directory in directories)
                    {
                        // get all test dlls from the given paths
                        assembliesList.AddRange(System.IO.Directory.GetFiles
                         (directory, testsSearchPattern, System.IO.SearchOption.TopDirectoryOnly));
                    }

                    Assemblies = assembliesList.ToArray();
                ]]>
            </Code>
        </Task>
    </UsingTask>

    <Target Name="Test">
        <GetAssemblies Path="$(BuildRoot).">
            <Output PropertyName="TestAssemblies" TaskParameter="Assemblies"/>
        </GetAssemblies>
        <xunit Assemblies="$(TestAssemblies)" />
    </Target>
</Project>

This article was originally posted at http://www.vtrifonov.com/2015/05/xunit-and-msbuild.html

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Telerik
Bulgaria Bulgaria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionEasier solution Pin
w1ld7-Sep-15 7:36
w1ld7-Sep-15 7:36 
It looks like there is an easier solution. Check it out at http://xunit.github.io/docs/running-tests-in-msbuild.html[^]

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.