当我尝试为我的应用创建单元测试时,我只在公共类或公共方法中支持 创建单元测试 。我试图在Visual Studio 2015 Enterprise中的一个非常简单的应用程序中测试公共方法。这是一个截图。
这是Program.cs:
CreateMap<SMModel, VM_SMModel>()
.ForMember(d => d.myDate, o => o.NullSubstitute(new DateTime(2017,12,12)));
我发现了msdn article,但它并没有提供任何有关如何解决问题的建议。此外,我从未安装过ALM Rangers Generate Unit Test。&#39;
除了Main之外,我摆脱了Program.cs中的所有内容,并添加了一个带有以下代码的新Public Class1(我仍然收到错误并且菜单消失了):
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));
}
}
}
答案 0 :(得分:6)
对于有这个问题的C#新手,您需要在课程中添加公开关键字。
例如,
class myProgram
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
}
}
您需要将第一行更改为public class myProgram
答案 1 :(得分:1)
可能来晚会很晚,但由于我遇到了同样的问题并且能够解决它,你永远不知道谁会需要它。所以......一旦我删除了对Microsoft.VisualStudio.QualityTools.UnitTestFramework
的引用
&#34;创建单元测试&#34;选项重新出现在上下文菜单中,我能够创建我需要的测试。
答案 2 :(得分:0)
如果您的意思是内置的Visual Studio 2015测试用例,则需要从公共成员中取出静态内容。像这样:
[TestClass]
public class UnitTests
{
[TestMethod]
public void Testing_01()
{
Assert.IsTrue( [some bool condition] );
}
}
答案 3 :(得分:0)
所以我知道这很晚了,但由于您的类定义而不能真正回答上述问题,但是我没有看到足够的论坛来讨论此错误,所以我决定在此添加我的发现。
我发现,如果您有一个仅包含属性且没有真正方法的类,则“创建单元测试”将无法工作。即使您的财产是公开的并且具有复杂的吸气剂或吸气剂。当我遇到这个问题时,这就是我的代码状态,就像我刚创建该类一样:
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();
}
}
但是,您可以通过至少定义构造函数或临时创建一些随机方法来解决此问题。但是,请注意,测试仅针对类中的真实方法进行,因此您仍然必须手动为属性的getter / setter编写测试。
希望有人认为这个答案很有帮助。
答案 4 :(得分:0)
显然,当我今天踏入它时,这仍然是一个可以掉入的兔子洞。就我而言,在 VS 2019 Enterprise 上运行。我的错误是在不需要的测试项目上安装了 NuGet 包。我添加了 NuGet 包 MSTest.TestAdapter 和 Microsoft.TestPlatform。删除、关闭和重新打开 IDE 都可以恢复创建测试方法的向导。对于初始或新测试项目,入门所需的唯一 NuGet 包是 Microsoft.VisualStudio.TestPlatform。