Resharper测试运行器没有输出Console.WriteLine()?

时间:2017-11-13 19:38:14

标签: c# unit-testing resharper

为什么Console.Writeline()从控制台应用main()方法执行时会起作用;但是当我使用resharpers测试运行器执行相同的操作时,我在测试运行器输出窗口中看不到Console.Writeline()

解释这个的最好方法是举个例子。

我正在使用:Resharper Ultimate 2017.1.3,Visual Studio 2017社区和.Net 4.6.1框架。语言是C#。我还安装了(通过nuget)nunit框架2.6.4。

首先创建一个类库,然后将以下内容粘贴到.cs文件中。

using System;
using System.Collections;
using System.Threading;
using NUnit.Framework;

namespace ObserverPatternExample
{
    [TestFixture]
    internal class ObserverTestFixture
    {
        [Test]
        public void DemonstrateObserverPattern()
        {
            var subject = new Subject();
            var a = new Observer(subject, "a");
            var b = new Observer(subject, "b"); // etc. as many observers as you want.

            subject.Go();
        }
    }

    // "subject" is observer pattern lingo. The "subject" will do the broadcasting to the observers.
    public class Subject 
    {
        public delegate void CallbackHandler(string s);
        public event CallbackHandler NotifyEvent;
        private const int waitTimeInMilliseconds = 200;
        private readonly Simulator simulator = new Simulator();
        public string FakeSimulatorState { get; set; }

        public void Go()
        {
            new Thread(Run).Start(); // a good thing to notice: events cross thread boundaries!!!
        }

        private void Run()
        {
            foreach (string s in simulator)
            {
                Console.WriteLine("Subject: " + s);
                FakeSimulatorState = s;
                NotifyEvent?.Invoke(s);
                Thread.Sleep(
                    waitTimeInMilliseconds); // we do this to "pretend" that the simulator is actually doing someting.
            }
        }


    }

    public class Observer : IObserverPattern // the "observer" will subscribe to the event being broadcast by the "subject"
    {
        private readonly string _name;
        public Observer(Subject subject, string name)
        {
            _name = name;
            subject.NotifyEvent += Update;
        }
        public void Update(string state)
        {
            Console.WriteLine("Observer {0}: {1}", _name, state);
        }
    }

    internal interface IObserverPattern
    {
        void Update(string state);
    }

    public class Simulator : IEnumerable
    {
        private readonly string[] _stateSequence = { "BEGIN", "CRAWL", "WALK", "JUMP", "END" };

        public IEnumerator GetEnumerator()
        {
            foreach (var s in _stateSequence)
                yield return s;
        }
    }
}

现在执行测试。我希望看到Console.WriteLine()在Resharper测试运行器输出窗口中调用显示字符串。但我不是。例如,这是一个截图:

enter image description here

现在让我们执行完全相同的序列,但这次我们将从新的控制台项目main()方法调用客户端代码。要设置此复制,请粘贴以下代码并引用您在上述步骤中创建的类库。

using ObserverPatternExample;

namespace ConsoleApp1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var subject = new Subject();
            var a = new Observer(subject, "a");
            var b = new Observer(subject, "b"); // etc. as many observers as you want.

            subject.Go();
        }
    }
}

接下来执行控制台应用。您应该看到以下内容:

enter image description here

任何人都可以解释我如何配置我的代码或测试运行器以在测试运行器输出窗口中显示输出?

*更新* 我取得了部分成功。 InBetween建议使用TraceListener让我意识到我应该使用ConsoleTraceListener。为了促进这一点,我修改了单元测试,如下所示:

using System.Threading;
using NUnit.Framework;

namespace ObserverPatternExample.DontUse
{
    [TestFixture]
    internal class ObserverTestFixture
    {
        [SetUp]
        public void Setup()
        {
            Trace.Listeners.Add(new ConsoleTraceListener());
        }

        [TearDown]
        public void TearDown()
        {
            Trace.Flush();
        }

        [Test]
        public void DemonstrateObserverPattern()
        {
            var subject = new Subject();
            var a = new Observer(subject, "a");
            var b = new Observer(subject, "b"); // etc. as many observers as you want.

            subject.Go();
        }
    }

结果令人惊讶:我确实获得了一些输出;但只有最初的BEGIN状态。看起来像这样:

enter image description here

短篇小说:我还在寻找解决方案。

***解决方案****

        [Test]
        public void DemonstrateObserverPattern()
        {
            var subject = new Subject();
            var a = new Observer(subject, "a");
            var b = new Observer(subject, "b"); // etc. as many observers as you want.

            subject.Go();

Thread.Sleep(1000); // <--- add this to force test runner to wait for other thread to complete.
        }

1 个答案:

答案 0 :(得分:2)

看起来Resharper在你的线程完成之前完成了。您致电

Thread(Run).Start();

是非阻止的。这意味着测试线程将在Go线程之前完成,因此没有结果。

请参阅https://msdn.microsoft.com/en-us/library/6x4c42hc(v=vs.110).aspx where is states“请注意,对Start的调用不会阻止调用线程。”