Debugging xUnit tests in .NET Core and Visual Studio Code

后端 未结 4 1092
死守一世寂寞
死守一世寂寞 2020-12-05 02:02

I\'m on a Mac, running .NET Core 1.0 and Visual Studio Code.

I have a console project and a test project. I have setup launch.json so that I can debug the console pr

4条回答
  •  [愿得一人]
    2020-12-05 02:14

    Tyler's answer of clicking the debug test code lens icons is the easiest way of debugging a single test.

    A way of testing all unit tests would be to add while(!Debugger.IsAttached) Thread.Sleep(500); inside the tests. This will make the tests wait until you attach a debugger.

    using System;
    using System.Diagnostics;
    using System.Threading;
    using NUnit.Framework;
    
    namespace SomeNamespace
    {
        [TestFixture]
        public class SomeClassTests
        {
            [Test]
            public void ShouldDoTest()
            {
                while(!Debugger.IsAttached) Thread.Sleep(500);
                Assert.That(true, Is.True);
            }
    
            [Test]
            public void ShouldDoTest2()
            {
                while(!Debugger.IsAttached) Thread.Sleep(500);
                Assert.That(true, Is.True);
            }
        }
    }
    

    This then allows you to attach the Visual Studio Code debugger to the running testhost.dll. Simple select .NET Core Attach and then the dotnet testhost.dll.

    Debug .NET Core Attach

提交回复
热议问题