Problem
When you’re trying to use the Create Unit Tests command (from the context menu) in Visual Studio, you get the following error message:
Create Unit Tests is supported only on a non-test project and within a public class or a public method
The Create Unit Tests command can only be used on a public method (an empty public class won’t work, despite what the error message says).
Solution
You have to use the Create Unit Tests command from the context of a public method. In other words, right-click on a public method and click Create Unit Tests from the context menu.
If don’t have a public method yet, you can get around this requirement by adding a dummy public class/method, like this:
public class Dummy
{
public void Test() { }
}
Code language: C# (cs)
Note: You can just copy/paste this dummy class into an existing source file. That way you don’t need to create a new file.
Now right-click on the dummy Test() method and click Create Unit Tests from the context menu. This’ll create the unit test project with the proper dependencies and add a test method for the dummy class/method:
[TestClass()]
public class Class1Tests
{
[TestMethod()]
public void TestTest()
{
Assert.Fail();
}
}
Code language: C# (cs)
Note: I suggest running this test just to verify the test project has been wired up properly.
Finally, delete the dummy class/method, since these were only added so you could use the Create Unit Tests command.