Typemock Isolator Integration API
The Typemock Isolator Integration API is intended for developers of unit tests runners and CI frameworks who want to integrate their code with Typemock Isolator.
To use the Typemock Isolator Integration API, you need to link your application to TypeMock.Integration.dll.
The Integration API provides two classes:
Class |
Description |
TypeMock.Integration.Service |
Provides methods for checking the Typemock Isolator license, the installation path, and Auto-Deploy mode. |
TypeMock.Integration.TypeMockProcess |
Provides methods for running processes with Typemock Isolator and linking Typemock Isolator with code coverage tools. |
Sample 1: Basic Usage
The following sample shows how to create a class for running unit tests with Typemock Isolator. The UnitTestRunner property is used to set the framework that will run the test. The UnitTestRunnerParams property is used to set the runner command line arguments. In this case, only the assembly with the tests is set.
C# class TestsRunner { private string unitTestRunner; private string unitTestRunnerParams; ////// get or sets the unit test runner (nunit, mbunit etc ...) /// public string UnitTestRunner { get { return unitTestRunner; } set{ unitTestRunner = value; } } ////// get or set the unit test runner parameters /// public string UnitTestRunnerParams { get { return unitTestRunnerParams; } set { unitTestRunnerParams = value; } } public void RunTest() { //set the process to run and its command line arguments ProcessStartInfo info = new ProcessStartInfo(UnitTestRunner, UnitTestRunnerParams); //create process with TypeMock enabled TypeMockProcess typeMockProcess = new TypeMockProcess(info); //run the process typeMockProcess.Start(); } } class Program { static void Main(string[] args) { //calling the code TestsRunner runner = new TestsRunner(); runner.UnitTestRunner = @"C:\Program Files\NUnit-Net-2.0 2.2.8\bin\nunit-console.exe"; runner.UnitTestRunnerParams = @"..\..\..\..\csharp\bin\Debug\Examples.csharp.dll"; runner.RunTest(); } }
Sample 2: Linking with Code Coverage Profiler and Running the Tests
The following sample shows how to run a test is run with NCover:
1. The license is checked.
2. The GetCoverageTools() method prints all available profilers on the machine. When the GetCoverageTools() method has a false argument, the method will return a list of all the profilers with which Typemock Isolator can be linked.
3. The code unlinks any previous profiler that was linked with Typemock Isolator.
4. The static TypeMockProcess.Start() method runs the tests.
C# public void RunTest(string profilerName) { if(!Service.HasLicenseToLink()) { Console.WriteLine("In order to link Typemock Isolator you need a valid licence"); return; } Console.WriteLine("Availble profilers on this machine:"); foreach(string profiler in Service.GetCoverageTools(true)) { Console.WriteLine(profiler); } //First unlink Typemock Isoaltor with any previous profiler TypeMockProcess.UnlinkWithCovarage(); //Set the process to run and its command line arguments ProcessStartInfo info = new ProcessStartInfo(UnitTestRunner, UnitTestRunnerParams); //Run the process with TypeMockEnabled and link with profiler using(TypeMockProcess.Start(info, profilerName)) {} }
When you are debugging code that uses TypeMockProcess, disable Typemock Isolator using the MS Visual Studio's Tools menu. If Typemock Isolator is enabled, any process that you create will be enabled as well. This will not represent a real-life situation when the application is running outside of MS Visual Studio.
Sample 3: Using AutoDeploy
To use the Auto-Deploy mode, use the TypeMock.Integration.Service.InstallFrom() method. You can check if the machine has a valid license using the TypeMock.Integration.Service.HasLicenseForAutodeploy() method.
C# public void DoAutoDeploy(string path) { if(!Service.HasLicenseForAutodeploy()) { Console.WriteLine("You don't have license for Auto Deploy Typemock Isolator"); return; } Service.InstallFrom(path); }