Wednesday, September 13, 2017

Make a unit test project double as a console app in Visual Studio 2015!

Make a unit test project and add in a Program.cs file for the head of the console app to be:

using System;
namespace MyApplication
{
   class Program
   {
      static void Main(string[] args)
      {
         Console.Write("Welcome to the application.\r\n");
         Console.Write("Press almost any key to end the experience.");
         Console.ReadKey(true);
      }
   }
}

 
 

Make an App.config also:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
   </startup>
</configuration>

 
 

Open up the .csproj file in Notepad and doctor this line of XML...

<OutputType>Library</OutputType>

 
 

...into this:

<OutputType>Exe</OutputType>

 
 

This last step ensures that you will not be told "A project with an Output Type of Class Library cannot be started directly." when you attempt to run the console app. Now you may either run your app or run your tests. Yay! If you were just planning to make a solution that had only two projects for tests and a for a console app, you may now make a solution with only one project! I'm not saying that this is the best idea in the world. I'm just saying you have the power.

No comments:

Post a Comment