Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a MSTest unit test support to a C# application

How do I add MsTest support to an existing C# application project in VS2012?

I want to be able to add unit tests to the application project and have the IDE integration show the 'click to run' icons in the editor.

I do not want to have to create a separate project.

[Edit1]

For those who are interested this is trivially easy in NUnit and works 'out of the box'. If you have a large WPF application and want to be able to launch each UI Window from the IDE to preview or debug without having to run the entire application and navigate to the Window you want to view, then a simple inline test like this overcomes the lack of this basic feature in the VS designer.

#if DEBUG
    [TestFixture]
    public class MainWindowTests
    {
        [Test]
        [STAThread]
        public void Test1()
        {
            Application a = new Application();
            MainWindow w = new MainWindow();
            a.Run(w);
        }
    }
#endif

The NUnit support assembly reference can also be easily excluded for a release build using a conditional reference in the .csproj file.

(Support to run dialogs from the designer used to be a built in feature all the way back in Visual Studio 6, but like so many other useful features was lost as MS evolved VS).

like image 545
Neutrino Avatar asked Jun 02 '26 06:06

Neutrino


1 Answers

Do not fight against the way Microsoft has designed the testing framework - you need to create a separate project. Test projects contain a lot of stuff (e.g., references to the Microsoft test assemblies) that you do not want to be part of your application.

like image 192
Polyfun Avatar answered Jun 03 '26 19:06

Polyfun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!