4

我正在尝试将 moles 与 NUnit 一起使用,并收到以下错误“Moles 需要测试是一个仪表化的过程”。我还在 Visual Studio 2008 中使用 Visual NUnit 来完成这项工作。欢迎任何帮助。

4

5 回答 5

10

这就是我为了让 Moles 与 NUnit 一起工作所做的:

  1. 获取存档C:\Program Files (x86)\Microsoft Moles\Documentation\moles.samples.zip并提取Moles解决方案文件夹。

  2. NUnit在 Visual Studio (2008) 中构建项目以供发布。

  3. 将输出文件复制Microsoft.Moles.NUnit.dll到. 我怀疑重新编译 NUnit 插件而不是使用来自下载和安装的插件的这一步骤是实际的解决点。Microsoft.Moles.NUnit.xml...\Moles\NUnit\bin\Release\C:\Program Files (x86)\NUnit 2.5.9\bin\net-2.0\addins\

  4. 在您的 VS 测试项目中,确保添加对C:\Program Files (x86)\NUnit 2.5.9\bin\net-2.0\addins\Microsoft.Moles.NUnit.dll刚刚复制的引用。

  5. 在您的 VS 测试类中,使用[Moled]属性标记测试方法(这将需要一个using Microsoft.Moles.Framework.NUnit)。作为替代方案,将其实现包装在一个using (MolesContext.Create()) { ... }块中(这将需要一个using Microsoft.Moles.Framework)。

  6. 在命令行中,通过 moles 运行器调用 NUnit 测试运行器,使用以下命令:"C:\Program Files (x86)\Microsoft Moles\bin\moles.runner.exe" "path\to\your\test\assembly.dll" /r:"C:\Program Files (x86)\NUnit 2.5.9\bin\net-2.0\nunit-console.exe" /x86 /args:"/domain=None"

我发现 [Moled] 属性不适用于 [TestCase(...)] ,它让您回到未检测的错误场景。相反,该using (MolesContext.Create())块也适用于这种情况。

一旦你发现一切正常,你可能会考虑在 Visual Studio 中将 moles runner 作为外部工具运行。按照使用 Visual Studio 中的 NUnit 控制台运行 Moles 中的说明,更新参数,如步骤 6 所示。

请注意,这是在 Windows 7 64 位机器上使用 NUnit 2.5.9、Microsoft Pex 和 Moles (x86) 0.94.51006.1。考虑不同路径、版本等的实际文件夹。

于 2011-05-09T20:20:12.170 回答
1

我正在使用 Moles 版本 0.94.51023.0。

据我所知,您需要将以下属性添加到您的测试方法中。我正在使用 Visual Studio 测试框架,不确定它是否与 NUnit 等相同。人。

[HostType("Moles")]

我还读到您可以将[Moled]属性添加到测试方法中,但这对我来说是不可用的,而且我没有说明原因,假设它是旧文档 - Moles 似乎有很多。

更新:根据 Moles 参考手册,pg。26、测试方法上的MoledAttribute是NUnit走的路。您必须通过将Microsoft.Moles.NUnit.dll程序集复制到 NUnit bin/addins文件夹来向 NUnit 注册它。

然后,您将该[Moled]属性添加到测试方法中。

[Test]
[Moled]
public void MyTest() {
  ...
}

否则,您将添加一个 using 块来包装测试方法,如下所示:

[Test]
public void MyTest() {
  using (MolesContext()) {
  ...
  }
}
于 2011-02-21T11:12:29.570 回答
1

除了添加 [HostType("Moles")] 属性外,您还需要用 moles runner 包裹测试 runner。例如:

moles.runner.exe MyAssembly.dll /r:nunit-console.exe

Moles runner 可能位于C:\Program Files\Microsoft Moles\bin中。要使用,请执行:

moles.runner.exe help
于 2011-05-04T15:06:17.937 回答
1

superjos 有最正确的答案,使用“持续测试”插件,您可以让 Visual Studio 使用此批处理文件通过 NUnit 控制台运行器运行 Moles 运行器:

@echo off

rem Uses the Microsoft Moles runner and fires off the NUnit console runner so you can use Moles with NUnit.
rem This batch file is intended to be run from the Continuous Testing plugin in Visual Studio.
rem However, it can be used to run nunit tests from anyhere with Moles as long as the first parameter
rem is the assembly to be tested.  All other parameters are passed to NUnit.

set ASSEMBLY=%1
set MOLESPATH="c:\Program Files\Microsoft Moles\bin\moles.runner.exe"
set NUNITPATH="C:\Program Files\NUnit 2.5.10\bin\net-2.0\nunit-console.exe"
shift

if [%ASSEMBLY%]==[] goto HelpScreen
if [%1]==[] goto RunAlone
if [%2]==[] goto RunParams1 
if [%3]==[] goto RunParams2 
if [%4]==[] goto RunParams3 
if [%5]==[] goto RunParams4 
if [%6]==[] goto RunParams5 
if [%7]==[] goto RunParams6 
if [%8]==[] goto RunParams7 
if [%9]==[] goto RunParams8 
goto RunParams9
:HelpScreen
echo "The parameters are the same as NUnit Console runner with the exception that:
echo "   1) Only one assembly is supported and it must come first"
echo "   2) Only 9 extra parameters may be specified"
echo
%NUNITPATH% /?
exit 1
:RunAlone
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY%
goto ExitRunner
:RunParams1
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1
goto ExitRunner
:RunParams2
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1 /args:%2
goto ExitRunner
:RunParams3
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1 /args:%2 /args:%3
goto ExitRunner
:RunParams4
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1 /args:%2 /args:%3 /args:%4
goto ExitRunner
:RunParams5
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1 /args:%2 /args:%3 /args:%4 /args:%5
goto ExitRunner
:RunParams6
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1 /args:%2 /args:%3 /args:%4 /args:%5 /args:%6
goto ExitRunner
:RunParams7
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1 /args:%2 /args:%3 /args:%4 /args:%5 /args:%6 /args:%7
goto ExitRunner
:RunParams8
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1 /args:%2 /args:%3 /args:%4 /args:%5 /args:%6 /args:%7 /args:%8
goto ExitRunner
:RunParams9
%MOLESPATH% /r:%NUNITPATH% %ASSEMBLY% /args:%1 /args:%2 /args:%3 /args:%4 /args:%5 /args:%6 /args:%7 /args:%8 /args:%9
goto ExitRunner
:ExitRunner

只需更新软件包版本的路径即可。如果您有时间更新它,这也可以用于从其他程序运行它。

于 2011-12-22T20:59:48.380 回答
0

您不能在 Visual Studio 中使用 NUnit 运行 MS Moles。您必须使用 MSTest(Visual Studio 单元测试)来执行此操作,或者您可以使用 Moles 从命令行运行 NUnit 测试。请参阅参考手册

一个可能的替代方案:如果它符合您的需求,您可以使用Gallio自动化平台并排运行各种测试框架(在您的情况下为 NUnit 和 MSTest)......

!托马斯

于 2010-09-28T18:48:56.870 回答