Vannevelj / VSDiagnostics

A collection of static analyzers based on Roslyn that integrate with VS
GNU General Public License v2.0
64 stars 16 forks source link

Prettify tests #316

Open Vannevelj opened 8 years ago

Vannevelj commented 8 years ago

Our tests are very bloated -- often we only test one or two specific lines of code whereas the rest of the test scenario stays in the same. Look into moving from a test scenario like this:

        [TestMethod]
        public void StringDotFormatWithDifferentAmountOfArguments_WithValidScenario()
        {
            var original = @"
using System;
using System.Text;

namespace ConsoleApplication1
{
    class MyClass
    {   
        void Method(string input)
        {
            string s = string.Format(""abc {0}, def {1}"", 1, 2);
        }
    }
}";
            VerifyDiagnostic(original);
        }

To this:

[TestMethod]
public void StringDotFormatWithDifferentAmountOfArguments_WithValidScenario()
{
    Test("string s = string.Format(\"abc {0}, def {1}\", 1, 2);")
}

    void Test(string scenario)
    {
            var original = $@"
using System;
using System.Text;

namespace ConsoleApplication1
{
    class MyClass
    {   
        void Method(string input)
        {
            {scenario}
        }
    }
}";
            VerifyDiagnostic(original);
    }
Hosch250 commented 5 years ago

You could also utilize xUnit's/NUnit's parameterized tests for this.