C# Unit Testing: Integrate NUNIT/MBUNIT with MIcro

2019-04-16 07:13发布

问题:

C# Unit Testing: How to integrate NUNIT/MBUNIT with Microsoft PEX(Parameterized unit testing). After I have seen few videos of Microsoft PEX, wondering whether I can able to add PEX testing in my NUNIT test project and also want to add them to nightly build. Is that possible?

回答1:

Yes, it is possible.

We run a pretty ordinary (non-bleeding edge government style) setup with CruiseControl.Net running NAnt running MSBuild, NUnit, Code Contracts, Pex. Tests are generated with the Pex Visual Studio (Professional edition) GUI and then the generated files are checked in to our SVN. There is no extra Pex step on the server but it still runs all the NUnit tests as a normal test suite. Since we are using Code Contracts, the Code Contract post-build step still needs to be run.

When running Pex in Visual Studio to generated parameterized tests, it will let you select what test framework to use. For a new project select NUnit and, when prompted, browse to the folder containing your nunit.framework.dll linked to your project.

So far (at least until everyone in my department is used to Pex) we've separated hand-written tests from the generated Pex tests by splitting them to two test projects per code project. For example Product.XYZ has both Product.XYZ.Tests (ordinary, hand-written NUnit tests) and Product.XYZ.PexTests (a fully generated project with only generated tests). In the hand-written tests we write expected (business-case based) input and outcomes, and then use Pex to make sure we're not missing any potentially dangerous pre- or postconditions. Both tests suites are automatically executed on developer machines' ReSharper test runner and the CCNet build server, plus we use both for code coverage checks using OpenCover.

Using Pex and Code Contracts without installing them on the build server

Below are some notes that I'm writing while it's fresh. Might be moved or deleted.

Note that it's not necessary to run the installer for neither Pex nor Code Contracts on the build server. This helps if you have stingy sysadmins or projects that might use conflicting versions of Pex or Code Contracts. Moles still has to be installed on the build server, so skip this if using Moles.

  1. Let Pex generate an NUnit test project Product.XYZ.PexTests for you. Generate some (only one or two to start with) parameterized Pex method tests as well, and let Pex generate the .g.cs files with the actual NUnit tests.
  2. Make a local copy of Microsoft.Pex.Framework.dll for your solution and re-add the Product.XYZ.PexTests project reference for your local copy. Remove your .moles and references to Moles, Microsoft.ExtendedReflection.dll, Microsoft.Moles.Framework.dll.
  3. Copy the installed Code Contracts files to your solution's build tools folder. (You could clean out some unused non-build files later.)
  4. Set the variable CodeContractsInstallDir to point to your Code Contracts folder (include a trailing slash) before building (for example in your build script).
  5. Import Microsoft.CodeContracts.targets (see below) after other <Import /> tags in your code project's Product.XYZ.csproj file.
  6. (Optional) Add Code Contracts build time check before </Project> your code project's Product.XYZ.csproj (see XML below). You might want to move the check depending on if you have Code Contracts runtime checking enabled only for some builds.
  7. Test your Pex setup by building and running the tests on the build server.

Code Contracts build target import

<Import Project="$(CodeContractsInstallDir)MsBuild\v4.0\Microsoft.CodeContracts.targets" />

Code Contracts check from userdoc.pdf chapter 5.1.3

<PropertyGroup>
  <CompileDependsOn>$(CompileDependsOn);CheckForCodeContracts</CompileDependsOn>
</PropertyGroup>
<Target Name="CheckForCodeContracts" Condition="'$(CodeContractsImported)' != 'true'">
  <Error Text="Project requires Code Contracts" />
</Target>

cruisecontrol.net nunit pex code-contracts c#-4.0



标签: c# nunit mbunit