How can I execute code at the beginning of an NUnit test SESSION (and not a fixture setup)

StackOverflow https://stackoverflow.com/questions/23296454

  •  09-07-2023
  •  | 
  •  

Question

I'm executing a large sequence of many fixtures with tests within them. Within each test is code to log any errors to a file based on NUnit attributes. Once the execution is complete, the file contains all of the errors in a nice format to send off to the consultants.

First of all, I need a hook to delete the previous copy of the file so that the file can be freshly generated. If this were a series of tests in a single fixture, it would be a chip-shot - put it in the fixture setup. But I need these dozens of test fixtures to share a common file, so putting it in the fixture setup doesn't work.

Second of all, this all just feels like reinventing the wheel...if there's some awesome NUnit standard for shunting results to an alternate output, I'd love to hear about that.

TIA.

Était-ce utile?

La solution

The easiest way to do this is to merge the many NUnit files your suite generates into a single one. This answer links to a library created to do the merge as an MSBuild task.

The first step is to compile that library (no issues for me when I tried) and put the built assembly in a directory with the following MSBuild project (or something similar):

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
                 ToolsVersion="4.0"
                 DefaultTargets="UnitTest">
  <UsingTask AssemblyFile="$(MSBuildProjectDirectory)\15below.NUnitMerger.dll" 
             TaskName="FifteenBelow.NUnitMerger.MSBuild.NUnitMergeTask" />
  <Target Name="UnitTest">
    <ItemGroup>
      <ResultsFiles Include="$(MSBuildProjectDirectory)\nunit-result*.xml" />
    </ItemGroup> 
    <NUnitMergeTask FilesToBeMerged="@(ResultsFiles)"
                    OutputPath="$(MSBuildProjectDirectory)\TestResult.xml" />
  </Target>
</Project>

Here, I'm assuming your files are set up in the same directory, i.e.:

enter image description here

Once your NUnit results are in the directory, run MSBuild from this directory to get your output:

msbuild merge.msbuildproj

The resulting file (here it's TestResult.xml) has the same format as the normal Xml output from NUnit, but has all the test-suite type=Assembly nodes from the input files.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top