代替“数据行 0",“数据行 1"等输出自定义名称 [英] Instead of "Data Row 0", "Data Row 1" etc. Output a custom name

查看:16
本文介绍了代替“数据行 0",“数据行 1"等输出自定义名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Visual Studio Team Foundation Server 2013 中,我使用的是单元测试框架.具体来说,我正在使用将从 XML 文件读取的数据驱动测试.

In Visual Studio Team Foundation Server 2013, I'm using the Unit Testing Framework. Specifically, I'm using data-driven testing that will read from an XML file.

这是一些示例代码:

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class DataDrivenTestingClass{
   public TestContext TestContext{get; set;}

   [TestMethod]
   [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "test.xml", "XMLTests", DataAccessMethod.Sequential)]
   public void MyOnlyDataDrivenTestFromXML(){
      var testName = TestContext.DataRow[0].ToString();
      //... blah blah blah other logic
      //<snip>
      Assert.IsTrue(true); //of course this is not what I really do
   }
}

输出结果如下:

测试通过 - MyOnlyDataDrivenTestFromXML(数据行 0)
测试通过 - MyOnlyDataDrivenTestFromXML(数据行 1)
测试通过 - ...
测试通过 - MyOnlyDataDrivenTestFromXML(数据行 N)

Test Passed - MyOnlyDataDrivenTestFromXML (Data Row 0)
Test Passed - MyOnlyDataDrivenTestFromXML (Data Row 1)
Test Passed - ...
Test Passed - MyOnlyDataDrivenTestFromXML (Data Row N)

显然,(Data Row i) 不是很具有描述性.XML 存储了我想使用的名称(我可以使用其他任何名称,但已经在 xml 中使用它似乎很方便).不过,我不确定这是否可行.

Clearly, (Data Row i) is not very descriptive. The XML stores the name I'd like to use instead (I could use anything else, but already having it in the xml seems convenient). I'm not sure this is possible, though.

问题是,虽然您可以通过 TestContext.TestNameTestContext 获取测试名称,但您无法设置它.此外,您不能(不应该)简单地从 TestContext 继承并制作一个允许您这样做的内容.

The problem is that, while you can get the test name from TestContext via TestContext.TestName, you cannot set it. Also, you cannot (should not) simply inherit from TestContext and make one that allows you to do so.

我也尝试过直接从 TestContextProperties 设置名称,但没有成功:

I've also tried to set the name directly from the Properties of TestContext like so, but without success:

TestContext.Properties["TestName"] = testName;
TestContext.Properties["FullyQualifiedTestClassName"] = testName;

问题是将测试输出到 .trx 文件,然后对其进行后处理.这是因为测试是在 SQL Server 支持的生成服务器上运行的.没有我将输出的测试日志.

What's out of the question is having the tests output to a .trx file, and then post-processing that. This is because the tests are run on a build server backed by a SQL Server. There are no test logs that I will output.

如果所有其他方法都失败了,我相信这可以通过连接到 TFS Web服务 API 并编写一个服务来查询测试结果并在那里进行编辑,但如果可能的话,我宁愿不走这条路.

If all else fails, I believe this is possible by hooking into the TFS Web Services API and writing a service that queries test results and does an edit there, but I'd rather not go this route if at all possible.

我在想的另一件事是,有可能进行某种通用编程、模板化方法,在编译时评估所有适当的测试名称,但这似乎既困难又过度.

Another thing I was thinking was that it might be possible to do some kind of generic programming, templated approach that will evaluate to all the appropriate test names at compile-time, but this seems both very difficult and overkill.

相关(但没有答案):MSTest 数据来自数据源的行的驱动测试集 DisplayName

推荐答案

我觉得这更适合作为评论而不是答案,但可惜我没有足够的声誉.

I feel like this would work better as a comment more than an answer, but alas I do not have enough reputation.

有一个类似的问题,这里有一个答案:Mstest改为使用数据驱动测试命名数据行

There is a similar question with an answer here: Mstest name instead Data Row with Data-Driven testing

看起来他们使用 TestContext.WriteLine() 来获取他们想要打印的值.

It looks like they do TestContext.WriteLine() to get the values they want printed.

这篇关于代替“数据行 0",“数据行 1"等输出自定义名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆