How can I run NUnit tests in Visual Studio 2017?

前端 未结 9 636
后悔当初
后悔当初 2020-11-29 21:15

I\'ve just installed Visual Studio 2017. I have a project using NUnit for the test cases. Ctrl + R - T no longer runs the tests, and the Tes

相关标签:
9条回答
  • 2020-11-29 21:46

    For anyone having issues with Visual Studio 2019:

    I had to first open menu TestWindowsTest Explorer, and run the tests from there, before the option to Run / Debug tests would show up on the right click menu.

    0 讨论(0)
  • 2020-11-29 21:48

    You need to install NUnitTestAdapter. The latest version of NUnit is 3.x.y (3.6.1) and you should install NUnit3TestAdapter along with NUnit 3.x.y

    To install NUnit3TestAdapter in Visual Studio 2017, follow the steps below:

    1. Right click on menu Project → click "Manage NuGet Packages..." from the context menu
    2. Go to the Browse tab and search for NUnit
    3. Select NUnit3TestAdapter → click Install at the right side → click OK from the Preview pop up

    0 讨论(0)
  • 2020-11-29 21:48
    • You have to choose the processor architecture of unit tests in Visual Studio: menu TestTest SettingsDefault processor architecture

    • Test Adapter has to be open to see the tests: (Visual Studio e.g.: menu TestWindowsTest Explorer


    Additional information what's going on, you can consider at the Visual Studio 'Output-Window' and choose the dropdown 'Show output from' and set 'Tests'.

    0 讨论(0)
  • 2020-11-29 21:49

    Using the CLI, to create a functioning NUnit project is really easy. The template does everything for you.

    dotnet new -i NUnit3.DotNetNew.Template
    dotnet new nunit
    

    On .NET Core, this is definitely my preferred way to go.

    0 讨论(0)
  • 2020-11-29 21:52

    Add the NUnit test adapter NuGet package to your test projects

    • 2.* (https://www.nuget.org/packages/NUnitTestAdapter/)
    • 3.* (https://www.nuget.org/packages/NUnit3TestAdapter/)

    Or install the Test Adapter Visual Studio extension. There is one for

    • 2.* (https://marketplace.visualstudio.com/items?itemName=NUnitDevelopers.NUnitTestAdapter)
    • 3.* (https://marketplace.visualstudio.com/items?itemName=NUnitDevelopers.NUnit3TestAdapter).

    I prefer the NuGet package, because it will be in sync with the NUnit version used by your project and will thus automatically match the version used in any build server.

    0 讨论(0)
  • 2020-11-29 21:54

    This one helped me:

    Getting started with .NET unit testing using NUnit

    Basically:

    • Add the NUnit 3 library in NuGet.
    • Create the class you want to test.
    • Create a separate testing class. This should have [TestFixture] above it.
    • Create a function in the testing class. This should have [Test] above it.
    • Then go into TEST/WINDOW/TEST EXPLORER (across the top).
    • Click run to the left-hand side. It will tell you what has passed and what has failed.

    My example code is here:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using NUnit.Framework;
    
    namespace NUnitTesting
    {
        class Program
        {
            static void Main(string[] args)
            {
            }
        }
    
        public class Maths
        {
            public int Add(int a, int b)
            {
                int x = a + b;
                return x;
            }
        }
    
        [TestFixture]
        public class TestLogging
        {
            [Test]
            public void Add()
            {
                Maths add = new Maths();
                int expectedResult = add.Add(1, 2);
                Assert.That(expectedResult, Is.EqualTo(3));
            }
        }
    }
    

    This will return true, and if you change the parameter in Is.EqualTo it will fail, etc.

    0 讨论(0)
提交回复
热议问题