Tuesday, 29 April 2014

Readable Tests

It is good practice to make tests readable, your QA will thank you for this.
With this in mind we try to give our tests meaningful names. "Thanks," declares your colleague, "I now know what it is supposed to do". Everyone's happy. But will the QA or person reviewing the test suite know whether it does what it is supposed to do? If they cannot read the code then the answer is probably no.
Some of the tests that I have written in the past have used regular expressions, but I have been made aware that not everyone is comfortable translating these. So wouldn't it be nice if we could make the regular expression more 'Verbally Expressive'?

Enter Verbal Expressions...

The 'Verbal Expression' library is very good at removing the obfuscation from regular expression matching. In case you’re interested it is still regex, it’s just wrapped to make it ‘Fluent’. (see Martin Fowler - FluentInterface)

Here is an simple example of how it could be used to aid the readability of a test:
public void Convert_GivenTwoValues_TheFirstValueIsAPrefixToTheSecondValue()
    object[] collection = new object[2];
    collection[0] = "£";
    collection[1] = "100";

    CurrencyMultiValueConverter multiValueConverter = new CurrencyMultiValueConverter();
    var convertedValue = multiValueConverter.Convert(collection, null, null, System.Globalization.CultureInfo.CurrentCulture);

    VerbalExpressions verbalExpressions = new VerbalExpressions();

    Assert.IsTrue(verbalExpressions.Test(convertedValue.ToString()), "The Returned format was not as expected.");
I've used this, and quite like it, although it is at odds with the Regex Builder that I use as my regex test development tool.Regex Builder
Have fun!!

No comments:

Post a comment