Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Typemock Isolator snippet template for Snippet Compiler 2008.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using NUnit.Framework;
using TypeMock;
public class MyClass
{
}
[TestFixture]
[VerifyMocks]
public class MyTestFixture
{
[Test]
public void MyTest()
{
}
}
public class Configuration
{
// Set this to the folder that contains NUnit.
public static readonly string NUnitLocation = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "NUnit"), "bin");
}
#region Test Runner
public class ConsoleApp
{
public static void Main()
{
try
{
NUnitTestRunner runner = new NUnitTestRunner();
runner.TextReceived += new EventHandler<OutputEventArgs>(WriteOutput);
runner.Run();
}
catch (Exception e)
{
Console.WriteLine("---\nThe following error occurred while executing the snippet:\n{0}\n---", e);
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
public static void WriteOutput(Object sender, OutputEventArgs e)
{
if(e.OutputType == OutputType.StandardOutput)
{
Console.Write(e.Text);
}
else
{
Console.Error.Write(e.Text);
}
}
}
public class NUnitTestRunner
{
private Process _process;
private StreamReader _standardOutput;
private StreamReader _standardError;
private byte[] _errorBuffer = new byte[512];
private byte[] _outputBuffer = new byte[512];
private AsyncCallback _outputReady;
private OutputAsyncState _outputState;
private AsyncCallback _errorReady;
private OutputAsyncState _errorState;
public event EventHandler<OutputEventArgs> TextReceived;
public void Run()
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Arguments = String.Format("\"{0}\"", System.Reflection.Assembly.GetExecutingAssembly().Location);
startInfo.FileName = Path.Combine(Configuration.NUnitLocation, "nunit-console.exe");
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
// Set up TypeMock to run
startInfo.EnvironmentVariables.Add("Cor_Enable_Profiling", "0x1");
startInfo.EnvironmentVariables.Add("COR_PROFILER", "{B146457E-9AED-4624-B1E5-968D274416EC}");
_process = new Process();
_process.StartInfo = startInfo;
_process.Start();
_standardOutput = _process.StandardOutput;
_standardError = _process.StandardError;
_outputReady = new AsyncCallback(OutputCallback);
_outputState = new OutputAsyncState(_standardOutput, _outputBuffer, OutputType.StandardOutput);
_errorReady = new AsyncCallback(OutputCallback);
_errorState = new OutputAsyncState(_standardError, _errorBuffer, OutputType.StandardError);
_standardOutput.BaseStream.BeginRead(_outputBuffer, 0, _outputBuffer.Length, _outputReady, _outputState);
_standardError.BaseStream.BeginRead(_errorBuffer, 0, _errorBuffer.Length, _errorReady, _errorState);
_process.WaitForExit();
}
private void OutputCallback(IAsyncResult ar)
{
OutputAsyncState state = (OutputAsyncState)ar.AsyncState;
int count = state.Stream.BaseStream.EndRead(ar);
if(count > 0)
{
if(TextReceived != null)
{
string text = System.Text.Encoding.ASCII.GetString(state.Buffer, 0, count);
TextReceived(this, new OutputEventArgs(text, state.OutputType));
}
state.Stream.BaseStream.BeginRead(state.Buffer, 0, state.Buffer.Length, _outputReady, state);
}
}
}
public class OutputEventArgs : EventArgs
{
private string _text;
private OutputType _outputType;
public OutputEventArgs(string text, OutputType outputType)
{
_text = text;
_outputType = outputType;
}
public string Text
{
get { return _text; }
set { _text = value; }
}
public OutputType OutputType
{
get { return _outputType; }
set { _outputType = value; }
}
}
public enum OutputType
{
StandardOutput,
StandardError
}
internal class OutputAsyncState
{
private StreamReader _stream;
private byte[] _buffer;
private OutputType _outputType;
public OutputAsyncState(StreamReader stream, byte[] buffer, OutputType outputType)
{
_stream = stream;
_buffer = buffer;
_outputType = outputType;
}
public StreamReader Stream
{
get { return _stream; }
}
public byte[] Buffer
{
get { return _buffer; }
}
public OutputType OutputType
{
get { return _outputType; }
}
}
#endregion
@tillig
Copy link
Author

tillig commented Jan 5, 2017

Here is a screen shot of the template in action:

Snippet in action

@tillig
Copy link
Author

tillig commented Jan 5, 2017

To set up the Typemock snippet template:

  1. Make sure you have Typemock Isolator installed.
  2. Make sure you have NUnit in a location you can reference it from.
  3. Edit the TypemockSnippetTemplate.cs file - find the Configuration class and update the location for NUnit (if necessary).
  4. Open Snippet Compiler.
  5. Go to Tools -> Organize Templates. This will open your Snippet Compiler template folder in Explorer.
  6. Put the TypemockSnippetTemplate.cs file in your template folder, close the folder, and return to Snippet Compiler.
  7. Go to Tools -> References... and add references to nunit.framework.dll and TypeMock.dll. References in Snippet Compiler are persistent, so you should only have to do this once.

Now you can go to File -> New -> TypemockSnippetTemplate.cs in Snippet Compiler and automatically have a light framework for working with Typemock Isolator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment