有人知道如何在Visual Studio 2015中的测试运行期间显示description属性吗?
例如,给出了这个测试:
[TestMethod]
[TestCategory("With Fakes")]
[Description("Posting a blog entry without the required data should return a BadRequest")]
public async Task BlogEntryPostTest1()
{
... do test
}
如何在运行时显示说明?我不认为测试资源管理器会这样做;还有其他选择吗?
答案 0 :(得分:1)
我don't think这可以在test explorer(至少2015年)中实现。我也相信ReSharpers" Unit Testing" tool。我认为查看单元测试旁边显示的描述标签的唯一方法是使用NUnit。
您可以将语音添加到建议的更改here。也许在下一个版本的测试资源管理器中,他们会添加它...现在我会使用NUnit(plus GUI)如果你真的需要看到描述,如果没有,也许你可以考虑其中一些{{3 }}。
答案 1 :(得分:0)
你可以制作一个使用“System.Reflection”的实用方法 通过在运行时获取当前实例的类型,您可以通过在每次测试初始化时调用它来精确定位具有描述属性的方法
[TestInitialize]
public void TestInit()
{
DisplayDescription(this.GetType());
}
这里实际发生的是
实用方法...
public void DisplayDescription(Type methodType)
{
string testName = TestContext.TestName;
MemberInfo method = methodType.GetMethod(testName);
Attribute attr = method.GetCustomAttribute(typeof(DescriptionAttribute));
if (attr != null)
{
DescriptionAttribute descAttr = (DescriptionAttribute)attr;
TestContext.WriteLine("Test Description: " + descAttr.Description.ToString());
}
}
我几个小时以来一直在寻找干净的解决方案,很高兴我在网上找到了这个教程。我想分享这个聪明的解决方案,所以我把它贴在这里,以防将来有人需要它。
参考: https://app.pluralsight.com/library/courses/basic-unit-testing-csharp-developers/table-of-contents