C#单元测试更改测试结果 [英] c# unittest change test outcome

查看:79
本文介绍了C#单元测试更改测试结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于非常具体的原因(如下所述),我尝试在执行单元测试后更改测试结果. 我尝试了以下代码:

for very specific reasons (explained below), I try to change the testoutcome after the execution of a unit test. I tried the following code:

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

    [TestMethod]
    public void TestMethod1()
    {
        Assert.Fail();
    }

    [TestCleanup]
    public void Cleanup()
    {
        TestContext.GetType()
            .GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
            .First((fieldInfo) => fieldInfo.Name.ToLowerInvariant().Contains("outcome"))
            .SetValue(TestContext, UnitTestOutcome.Inconclusive);
    }
}

和其他基于反射的变体.当我在执行TestCleanup方法后检查TestContext对象时,将属性CurrentTestOutcome正确设置为Inconclusive,但是此后,测试仍然失败.

and other variants based on reflection. When I inspect the TestContext object after the execution of the TestCleanup method, the property CurrentTestOutcome is properly set to Inconclusive, but after that, the test still fails.

你有什么主意吗?我很困在这里...

Do you please have any idea? I'm quite stuck here...

谢谢

原因: 这些不是真实的"单元测试,而是更多的端到端"测试.如果某些外部依赖项失败(我知道如何检测到),并且测试失败,我想覆盖其结果以减少误报的数量.

Reasons: These are not "real" unit tests, but more "end to end" tests. If some external dependency failed (I know how to detect it), and the test failed, I want to override its outcome to reduce the amount of false negatives.

推荐答案

可能已经晚了,但是我设法找到了解决方案.

It may be late, but I managed to find a solution for this.

    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.VisualStudio.TestTools.Common;
    using System;
    using System.Linq;
    using System.Reflection;

    namespace ServiceTests
    {
        [TestClass]
        public class Test
        {
            public TestContext TestContext { get; set; }

            [TestCleanup()]
            public void MyTestCleanup()
            {
                if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed)
            {
                Type classType = this.GetType(TestContext.FullyQualifiedTestClassName);
                if (classType != null)
                {
                    object instance = Activator.CreateInstance(classType);
                    MethodInfo method = classType.GetMethod(TestContext.TestName);
                    try
                    {
                        method.Invoke(instance, null);

                        // if the above call does not throw exception it passes for sure, 
                        // so changing the previous status and message is needed to be done.
                        FieldInfo resultField = TestContext.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
                            .Where(x => x.Name.Contains("m_currentResult")).First();

                        object currentTestResult = resultField.GetValue(TestContext);
                        FieldInfo outcomeProperty = currentTestResult.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
                            .Where(x => x.Name.Equals("m_outcome")).First();
                        outcomeProperty.SetValue(currentTestResult, TestOutcome.Passed);

                        FieldInfo errorInfoProperty = currentTestResult.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
                            .Where(x => x.Name.Equals("m_errorInfo")).First();

                        object errorInfoValue = errorInfoProperty.GetValue(currentTestResult);
                        FieldInfo messageProperty = errorInfoValue.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
                            .Where(x => x.Name.Equals("m_message")).First();
                        messageProperty.SetValue(errorInfoValue, "Passed.");

                        FieldInfo stackTraceProperty = errorInfoValue.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
                            .Where(x => x.Name.Equals("m_stackTrace")).First();
                        stackTraceProperty.SetValue(errorInfoValue, null);
                    }
                    catch
                    {
                    }
                }
            }
            }

            private static int i = 0;
            [TestMethod]
            public void TMethod()
            {
                i++;
                if (i == 1)
                    Assert.Fail("Failed");
            }
        }
    }

这篇关于C#单元测试更改测试结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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