Trying to use Moles with NUnit. Getting “Moles requires tests to be an instrumented process”

和自甴很熟 提交于 2019-12-21 05:16:09

问题


I am trying to use moles with NUnit and am getting the following error "Moles requires tests to be an instrumented process". I am also using Visual NUnit within Visual Studio 2008 to get this working. Any help is welcome.


回答1:


This is what I did in order to make Moles work with NUnit:

  1. Grab the archive at C:\Program Files (x86)\Microsoft Moles\Documentation\moles.samples.zip and extract the Moles solution folder.

  2. Build the NUnit project in Visual Studio (2008) for Release.

  3. Copy the output files Microsoft.Moles.NUnit.dll and Microsoft.Moles.NUnit.xml from ...\Moles\NUnit\bin\Release\ to C:\Program Files (x86)\NUnit 2.5.9\bin\net-2.0\addins\. I suspect that this step of re-compiling the NUnit addin instead of using the one coming from the download & install was the actual solving point.

  4. In your VS test project, make sure you add a reference to the C:\Program Files (x86)\NUnit 2.5.9\bin\net-2.0\addins\Microsoft.Moles.NUnit.dll you just copied.

  5. In your VS test class, mark a test method with the [Moled] attribute (this will require an using Microsoft.Moles.Framework.NUnit). As an alternative, wrap its implementation within an using (MolesContext.Create()) { ... } block (this will require an using Microsoft.Moles.Framework).

  6. From the command line, invoke NUnit test runner through the moles runner with the following: "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"

I found that [Moled] attribute does not work with [TestCase(...)] and it brings you back to the uninstrumented error scenario. Instead the using (MolesContext.Create()) block works in that case too.

Once you find that everything works, you might consider running moles runner as an external tool within Visual Studio. Follows instructions in Running Moles using NUnit Console from Visual Studio, updating the arguments as in step 6.

Please note that this was on a Windows 7 64bit machine, with NUnit 2.5.9, Microsoft Pex and Moles (x86) 0.94.51006.1. Consider your actual folders for different paths, versions, etc.




回答2:


I'm using Moles version 0.94.51023.0.

As far as I'm aware, you need to add the below attribute to your test method. I'm using the Visual Studio testing framework, not sure if it's the same with NUnit, et. al.

[HostType("Moles")]

I've also read that you can add the [Moled] attribute to the test method, but that was not available to me, and I didn't go into why, assuming it's old documentation - which there seems to be a lot of with Moles.

Update: As per the Moles Reference Manual, pg. 26, the MoledAttribute on the test method is the way to go with NUnit. You must register the Microsoft.Moles.NUnit.dll assembly with NUnit by copying it to the NUnit bin/addins folder.

Then you would add the [Moled] attribute to the test method.

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

Otherwise, you would add a using block to wrap the test method as below:

[Test]
public void MyTest() {
  using (MolesContext()) {
  ...
  }
}



回答3:


In addition to adding the [HostType("Moles")] attribute, you need to wrap the test runner with the moles runner. For example:

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

The Moles runner is probably located in C:\Program Files\Microsoft Moles\bin. For usage, execute:

moles.runner.exe help



回答4:


superjos has the most correct answer and using the "Continuous Testing" addin you can get Visual Studio to run the Moles runner through the NUnit console runner with this batch file:

@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

Just update the paths to your version of the software packages. This can also be used to run it from other programs if you have the time to update it.




回答5:


You can't run MS Moles with NUnit from within Visual Studio. You must either use MSTest (Visual Studio Unit Tests) to do that or you can run your NUnit tests with Moles from the command line.See the reference manual.

A possible alternative: If it fits your needs, you may use the Gallio automation platform to run all kinds of test frameworks (in your case NUnit and MSTest) side by side...

HTH! Thomas



来源:https://stackoverflow.com/questions/3815946/trying-to-use-moles-with-nunit-getting-moles-requires-tests-to-be-an-instrumen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!