仅在公共类或公共方法VS 2015中支持创建单元测试

Eri*_*ric 4 c# unit-testing visual-studio visual-studio-2015

我得到一个创建单元测试是只在一个公共类或公共方法的支持,当我尝试了我的应用程序创建单元测试.我试图在Visual Studio 2015 Enterprise中的一个非常简单的应用程序中测试公共方法.这是一个截图.

在此输入图像描述

这是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));
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

我发现了这篇msdn文章,但它并没有提供任何关于如何解决问题的建议.此外,我从未安装过"ALM Rangers Generate Unit Test".

除了Main之外,我从Program.cs中删除了所有内容,并使用以下代码添加了一个新的Public Class1(我仍然收到错误并且菜单消失了):

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));
        }


    }
}
Run Code Online (Sandbox Code Playgroud)

tru*_*ype 7

对于有这个问题的C#新手,您需要将public关键字添加到您的班级.

例如,

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

您需要将第一行更改为 public class myProgram


bli*_*lit 0

如果您指的是内置的 Visual Studio 2015 测试用例,则需要从公共成员中取出静态。像这样:

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