Eric
Eric

Reputation: 3221

Create Unit Tests is supported only within a public class or a public method VS 2015

I'm getting a Create Unit Tests is supported only within a public class or a public method when I try to create unit tests for my app. I was trying to test a public method in a very simple app in Visual Studio 2015 Enterprise. Here's a screenshot.

enter image description here

Here's the Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Scratch
{
    public class Program    //Originally this was just class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Hello world!");

        }

        public static string TruncateOld(string value, int length)
        {
            string result = value;
            if (value != null) // Skip empty string check for elucidation
            {
                result = value.Substring(0, Math.Min(value.Length, length));
            }
            return result;
        }

        public static string Truncate(string value, int length)
        {
            return value?.Substring(0, Math.Min(value.Length, length));
        }

    }
}

I've found this msdn article, but it doesn't really offer any suggestions on how to solve the problem. Also, I've never installed 'ALM Rangers Generate Unit Test.'

I got rid of everything from my Program.cs except for the Main and added a new Public Class1 with the following code (I still get the error and the menu goes away):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Scratch
{
    public class Class1
    {


        public Class1()
        {

        }

        public string TruncateOld(string value, int length)
        {
            string result = value;
            if (value != null) // Skip empty string check for elucidation
            {
                result = value.Substring(0, Math.Min(value.Length, length));
            }
            return result;
        }

        public string Truncate(string value, int length)
        {
            return value?.Substring(0, Math.Min(value.Length, length));
        }


    }
}

Upvotes: 12

Views: 10660

Answers (5)

DooHickey
DooHickey

Reputation: 95

Apparently, this is still a rabbit hole to fall into, as I stepped into it today. In my case, running on VS 2019 Enterprise. My error was having NuGet packages installed on the test project which isn't needed. I had added NuGet packages MSTest.TestAdapter, and Microsoft.TestPlatform. Removing both, closing and reopening the IDE restored the wizard for creating a Test Method. For an initial or new test project, the only NuGet package needed for getting started is Microsoft.VisualStudio.TestPlatform.

Upvotes: 1

Nicholas Kalscheuer
Nicholas Kalscheuer

Reputation: 663

So I know this is late, and not really an answer to the above question due to your class definition, but I don't see enough forums on this error, so I decided to add my findings here.

I found that if you have a class with only properties and no true methods, 'Create Unit Tests' will not work. Even if your properties are public and have complex getters or setters. Here was the state of my code when running into this problem, as I just made the class:

namespace some.Namespace
{
   using some.Namespace.Interfaces;
   using System;
   using System.Collections.Generic;

   public class SomeData : ISomeData
   {
      public bool SomeFlag => throw new NotImplementedException();

      public Dictionary<string, string> SomeFields => throw new NotImplementedException();

      public Dictionary<string, string> SomeOtherFields => throw new NotImplementedException();
   }
}

However, you can work around this by either defining at least constructor or by temporarily making some random method. However, note that tests will only be made for true methods in your class so you will still have to manually write the tests for your properties' getters/setters.

Hope someone finds this answer helpful.

Upvotes: 4

Probably coming to the party too late, but since I had the same problem and was able to solve it, you never know who is going to need it. So... as soon as I removed the reference to the Microsoft.VisualStudio.QualityTools.UnitTestFramework the "Create Unit Tests" option reappeared in the context menu and I was able to create the tests I needed.

Upvotes: 3

praiseHellRaiseDale
praiseHellRaiseDale

Reputation: 2270

To anyone that is new to C# having this issue, you need to add the public keyword to your class.

For example,

class myProgram
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World");
    }
}

You'd need to change the first line to public class myProgram

Upvotes: 15

blit
blit

Reputation: 390

If you mean the built in Visual Studio 2015 test cases, you need to take out the static from the public members. Like this:

[TestClass]
public class UnitTests
{
    [TestMethod]
    public void Testing_01()
    {
        Assert.IsTrue( [some bool condition] );
    }
}

Upvotes: 0

Related Questions