Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

MSTest Addin Sample

faustogut edited this page Mar 28, 2018 · 1 revision

Overview

A unit test addin that adds support for MSTest to SharpDevelop 4. The addin has been tested with MSTest that ships with Visual Studio 2010.

Features

Create a new project and add a reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll. Create a new test class.

    using System; 
    using Microsoft.VisualStudio.TestTools.UnitTesting;
     
    namespace MyTests 
    { 
     [TestClass] 
     public class MyClass 
     { 
      [TestMethod] 
      public void SuccessTest() 
      { 
       int expected = 0; 
       int actual = 0; 
       Assert.AreEqual(expected, actual); 
      } 
       
      [TestMethod] 
      public void FailureTest() 
      { 
       int expected = 0; 
       int actual = 1; 
       Assert.AreEqual(expected, actual); 
      } 
     } 
    }

In the Unit Tests window the MSTests will be displayed.

Screenshot of Unit Tests window

The unit tests can be run by right clicking and selecting Run tests.

The Unit Tests window will update to show the tests that passed and failed.

Screenshot of Unit Tests window after the tests were run

The output from the MSTest runner (mstest.exe) will be displayed in the Output window.

Screenshot of output window

Any test failures are displayed in the Errors window.

Screenshot of errors window

Note that running tests with the debugger or with code coverage does not work.

To configure the path to MSTest select Tools | Options | Tools | MS Test and specify the path to mstest.exe in the Path field.

Screenshot of options dialog

How it Works

The addin uses the /SharpDevelop/UnitTesting/TestFrameworks extension point in the addin xml to add its own test runners.

    <Path name="/SharpDevelop/UnitTesting/TestFrameworks"> 
      <TestFramework 
        id="MSTest" 
        class="MSTest.SharpDevelop.MSTestFramework" 
        supportedProjects=".csproj" /> 
     </Path>

The extension point specifies that C# are supported.

The MSTestFramework class implements the ITestFramework interface as shown below.

     public interface ITestFramework 
     { 
      bool IsTestMember(IMember member); 
      bool IsTestClass(IClass c); 
      bool IsTestProject(IProject project); 
      IEnumerable<TestMember> GetTestMembersFor(IClass c);
       
      ITestRunner CreateTestRunner(); 
      ITestRunner CreateTestDebugger(); 
       
      bool IsBuildNeededBeforeTestRun { get; } 
     }

The IsTestMember, GetTestMembersFor, IsTestClass and IsTestProject methods are called by SharpDevelop to determine whether a method, class or project contains a unit test and should be added to the Unit Tests tree.

The CreateTestRunner and CreateTestDebugger methods are called when the unit tests are run.

The IsBuildNeededBeforeTestRun should return true unless you are supporting a project that does not require any compilation before the tests are run.

A test runner needs to implement the ITestRunner interface as shown below.

     public interface ITestRunner : IDisposable 
     { 
      event TestFinishedEventHandler TestFinished; 
      event EventHandler AllTestsFinished; 
      event MessageReceivedEventHandler MessageReceived; 
      void Start(SelectedTests selectedTests); 
      void Stop(); 
     }

The TestFinished event should be raised when a single test has finished.

The AllTestsFinished event should be raised when the test run is complete.

The MessageReceived event can be used to show messages in SharpDevelop's Output window.

The MSTestRunner uses the mstest.exe test runner to execute the unit tests. The full path to this is defined via a property that can be set in the Tools Options dialog.

Building

  1. Download the source code for SharpDevelop 4
  2. Extract the source code for SharpDevelop 4
  3. Build SharpDevelop 4.0 by running debugbuild.bat.
  4. Open the samples\MSTest\MSTest.SharpDevelop.sln into SharpDevelop.
  5. Build the MSTest.SharpDevelop solution.
  6. Restart SharpDevelop built from source code and the addin will be loaded.

License

The code in this sample is licensed under the LGPL.

Clone this wiki locally