C# 单元测试:将 NUNIT/MBUNIT 与 Microsoft PEX 集成
C# 单元测试:如何将 NUNIT/MBUNIT 与 Microsoft PEX(参数化单元测试)集成。 在我看过一些 Microsoft PEX 的视频后,想知道我是否可以在我的 NUNIT 测试项目中添加 PEX 测试,并且还想将它们添加到 nightly build 中。这可能吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是可能的。
我们使用 运行一个非常普通的(非前沿政府风格)设置CruiseControl.Net 运行 NAnt 运行 MSBuild,NUnit,代码合同,< a href="http://research.microsoft.com/en-us/projects/pex/" rel="nofollow">Pex。使用 Pex Visual Studio(专业版)GUI 生成测试,然后将生成的文件签入我们的 SVN。服务器上没有额外的 Pex 步骤,但它仍然像普通测试套件一样运行所有 NUnit 测试。由于我们使用的是代码契约,因此仍然需要运行代码契约构建后步骤。
当在 Visual Studio 中运行 Pex 来生成参数化测试时,它会让您选择要使用的测试框架。对于新项目,选择 NUnit,并在出现提示时浏览到包含链接到项目的
nunit.framework.dll
的文件夹。到目前为止(至少在我部门的每个人都习惯 Pex 之前),我们已经将手写测试与生成的 Pex 测试分开,将每个代码项目拆分为两个测试项目。例如,
Product.XYZ
既有Product.XYZ.Tests
(普通的手写 NUnit 测试)和Product.XYZ.PexTests
(一个完全生成的项目,仅包含生成的测试)。在手写测试中,我们编写预期的(基于业务案例的)输入和结果,然后使用 Pex 确保我们不会错过任何潜在危险的前置条件或后置条件。这两个测试套件都会在开发人员计算机的 ReSharper 测试运行程序 和 CCNet 构建上自动执行服务器,此外我们还使用 OpenCover 进行代码覆盖率检查。使用 Pex 和代码协定 无需在构建服务器上安装它们
下面是我在新鲜的时候写的一些笔记。可能会被移动或删除。
请注意,无需在构建服务器上运行 Pex 和代码合约的安装程序。如果您有吝啬的系统管理员或可能使用冲突版本的 Pex 或代码合同的项目,这会有所帮助。 痣仍然必须安装在构建服务器上,因此,如果使用 Moles,请跳过此步骤。
Product.XYZ.PexTests
。还生成一些(开始时仅一两个)参数化 Pex 方法测试,并让 Pex 生成带有实际 NUnit 测试的.g.cs
文件。Microsoft.Pex.Framework.dll
本地副本,并为您的本地副本重新添加Product.XYZ.PexTests
项目引用。删除.moles
以及对 Moles、Microsoft.ExtendedReflection.dll
、Microsoft.Moles.Framework.dll
的引用。CodeContractsInstallDir
设置为指向您的 Code Contracts 文件夹(包括尾部斜杠)。Product.XYZ.csproj
文件中的其他
标记之后导入Microsoft.CodeContracts.targets
(见下文) 。Product.XYZ.csproj
之前添加代码合同构建时间检查(请参阅下面的 XML)。您可能想要移动检查,具体取决于您是否仅为某些构建启用了代码契约运行时检查。代码合同构建目标导入
代码合同检查 来自 userdoc.pdf 第 5.1.3 章< /a>
cruisecontrol.net <一个href="/questions/tagged/nunit" class="post-tag" title="显示标记为“nunit”的问题" rel="tag">nunit pex 代码合同 c#-4.0
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 bothProduct.XYZ.Tests
(ordinary, hand-written NUnit tests) andProduct.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.
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.Microsoft.Pex.Framework.dll
for your solution and re-add theProduct.XYZ.PexTests
project reference for your local copy. Remove your.moles
and references to Moles,Microsoft.ExtendedReflection.dll
,Microsoft.Moles.Framework.dll
.CodeContractsInstallDir
to point to your Code Contracts folder (include a trailing slash) before building (for example in your build script).Microsoft.CodeContracts.targets
(see below) after other<Import />
tags in your code project'sProduct.XYZ.csproj
file.</Project>
your code project'sProduct.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.Code Contracts build target import
Code Contracts check from userdoc.pdf chapter 5.1.3
cruisecontrol.net nunit pex code-contracts c#-4.0