gpt4 book ai didi

c# - MsTest 数据驱动 : ignore specific datarows via testattribute?

转载 作者:太空宇宙 更新时间:2023-11-03 14:15:54 25 4
gpt4 key购买 nike

我关于 stackoverflow 的第一个问题,我希望这不是一个愚蠢的问题。 :)

基本上我所有的数据驱动测试都是这样的:

[TestMethod]
[DataSource(TestsDataSource)]
public void Test_With_Some_Fancy_Name()
{
if (myIsThisTestIgnored) return;
...

DataSource 属性 + app.config 连接字符串 + excel odbc 驱动程序 = excel 数据驱动测试。

myIsThisTestIgnored[TestInitialize] 方法中设置(对于每个数据行,都有一个单独的测试和单独的 [TestInitialize]),通过TestContext.DataRow["Ignore"],因此我可以通过 Excel 工作表中的一些 true/false 来“忽略”测试。

现在我的问题是:只要从测试方法返回就可以让测试通过。如何不让被忽略的测试在 MSTEST 测试运行器(以及通过 msbuild 的 CI 中)“通过”,或者更好的是,根本不显示它们?

在我们的 Ci Build 上,它总结了每个被忽略的(也就是通过的)测试,因为测试的计数是测试方法的 datarows x number,给人一种运行的测试数量的错误印象。但是,直到现在,我发现没有办法以编程方式将测试结果设置为“已忽略”(或除“通过”/“失败”之外的任何其他状态)。

此外,在每个测试中去掉这个烦人的忽略行并增强断言消息而不用将每个断言包装在某个函数中也会很好。

因此,我认为最好/唯一的方法是 AOP 样式编程和使用自定义属性。我发现了这篇关于使用自定义属性增强您的 mstest 代码的旧帖子,值得一读:http://callumhibbert.blogspot.com/2008/01/extending-mstest.html

特别是链接文章中的最后一条评论让我开始思考,因为这个人不仅通过展示如何访问所有测试状态数据到属性(MarshalObject 的东西)让我开心,而且他以某种方式让它工作,引用他的话“以这种方式,我可以根据来自文本上下文(使用 DataSource 属性)和我创建的新自定义属性的数据驱动信息矩阵来确定是否运行测试。” . 是的..但是怎么做呢?

我拿了一些代码并实现了它,看起来像这样:

public class MyCustomAspect : TestAspect<MyCustomAttribute>, IMessageSink, ITestAspect
{ ....

[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
public IMessage SyncProcessMessage(IMessage msg)
{
....
// see last comment in linked article to how to get this MarshalObject
MyTestsClass testClass = (MyTestsClass)MarshalObject;

if(testClass.myTestInitializeRan && testClass.myIsThisTestIgnored)
{
// what to do here? I would like to get rid of the SyncProcessMessage invocation, but...
// return msg or null -> Exception "The method was called with a Message of an unexpected type."
// constructing a IMessage in an easy way (specifically CallMessage) seems not possible, and I have no clue how to construct a valid CallMessage that says "ignore test!"
}
else
{
IMessage message = _nextSink.SyncProcessMessage(msg);
return message;
}

同样:我想抑制这个数据行的测试运行,或者以某种方式将测试结果设置为“忽略”/“未知”。

对返回的 messagemsg 做任何事情似乎毫无意义,因为 IMessage 接口(interface)只公开了 Properties字典里面只有一些 MethodName 和东西,看起来没什么有趣的。

IMessage 转换为具体实现是行不通的:它们都是 System.Runtime 内部的。尽管可以强制转换为 ResultMessage 并在引发异常时返回 != null(因此您可以获取异常消息并通过新的 throw 调整错误消息)。

有什么想法吗?非常感谢您到目前为止的阅读。 ;)

最佳答案

How to not let the ignored tests "pass" in the MSTEST testrunner (and in the CI via msbuild), or even better, do not show them at all?

如果您不希望将跳过的测试视为已通过,则可以使用 Assert.Fail 或 Assert.Inconclusive 而不是简单地从测试用例返回。

您还可以找到 Data driven tests with test attributes in MSTest有用。作者展示了如何使用 postsharp 创建参数化 Row 测试,以跳过使用 excel 进行数据驱动测试的需要,而是将参数作为属性发布在测试方法之上。因此,当您想要删除或忽略一个测试用例时,您只需注释掉该数据行,它就不会显示了。

关于c# - MsTest 数据驱动 : ignore specific datarows via testattribute?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6588078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com