How can I run NUnit tests in Visual Studio 2017?

前端 未结 9 641
后悔当初
后悔当初 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: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.

提交回复
热议问题