HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Is a unit test which uses LINQ to check multiple values acceptable?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
linqvaluesusestestmultiplewhichcheckacceptableunit

Problem

I'm writing unit tests for a library which parses colors from user input (example: the user input “#f00” gives a red color; the user input “60,100%,100%” gives a yellow color, etc.)

One of the conditions is that the method Parse returns null when there is actually nothing to parse, i.e. when the input is either an empty string or its length is inferior to four characters. Those are the two unit tests:

[TestMethod]
public void SubmitEmptyText()
{
    var actual = new ColorParser().Parse(string.Empty);
    Assert.IsNull(actual);
}

[TestMethod]
public void SubmitShortText()
{
    Assert.IsTrue(new[]{"#", "#f", "#ff"}.All(c => !new ColorParser().Parse(c).HasValue));
}


  • Is the second method doing too much?



  • Am I expected to create three method? Seriously?!



  • Is the usage of functional programming here justified, or should I instead stick to the most basic way to write the tests?

Solution

What you need is passing parameters to common test method and supplying it with informative error message.

[TestMethod]
    public void SubmitShortText()
    {
        var colorCodes = new[] { "#", "#f", "#ff" };
        DoAssertion(colorCodes);
    }

    private void DoAssertion(string[] colorCodes)
    {
        var parser = new ColorParser();
        foreach (var code in colorCodes)
        {
            var hasValue = parser.Parse(code).HasValue;
            Assert.IsTrue(hasValue, string.Format("Failed to parse color code '{0}'.", code));
        }
    }


Parametrized tests are also supported in XUnit:

[Theory]
    [InlineData("#")]
    [InlineData("#f")]
    [InlineData("#ff")]
    public void SubmitShortText(string code)
    {
        var parser = new ColorParser();
        var hasValue = parser.Parse(code).HasValue;
        Assert.True(hasValue, string.Format("Failed to parse color code '{0}'.", code));
    }


Tests will be executed one by one. Execution will be stopped on the first failed test.

Code Snippets

[TestMethod]
    public void SubmitShortText()
    {
        var colorCodes = new[] { "#", "#f", "#ff" };
        DoAssertion(colorCodes);
    }

    private void DoAssertion(string[] colorCodes)
    {
        var parser = new ColorParser();
        foreach (var code in colorCodes)
        {
            var hasValue = parser.Parse(code).HasValue;
            Assert.IsTrue(hasValue, string.Format("Failed to parse color code '{0}'.", code));
        }
    }
[Theory]
    [InlineData("#")]
    [InlineData("#f")]
    [InlineData("#ff")]
    public void SubmitShortText(string code)
    {
        var parser = new ColorParser();
        var hasValue = parser.Parse(code).HasValue;
        Assert.True(hasValue, string.Format("Failed to parse color code '{0}'.", code));
    }

Context

StackExchange Code Review Q#26820, answer score: 3

Revisions (0)

No revisions yet.