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.
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));
}
}
}
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With