The ChannelAdam Test Framework Text Library

Overview

This is an open source, .NET Standard 1.3 library that provides helpers for comparing text and flat files (using DiffPlex), optionally in conjunction with one of the ChannelAdam Test Framework Libraries:

Below are the main features of the library. See the linked code for details.

Please contact me if you have any questions.

Getting Started

NuGet Package Installation

To install the ChannelAdam.TestFramework.Text NuGet package run the following command in the Package Manager Console:

1PM> Install-Package ChannelAdam.TestFramework.Text

Usage

 1using ChannelAdam.TestFramework.Text;
 2...
 3    [TextFixture]
 4    public class MyUnitTests : MoqTestFixture
 5    {
 6        private TextTester _textTester;
 7
 8        [SetUp]
 9        public Setup()
10        {
11            _textTester = new TextTester(base.LogAssert);
12        }
13
14        [Test]
15        public void MyTestMethod()
16        {
17            // ARRANGE
18            _textTester.ArrangeExpectedText("The brown bull");
19
20            // ACT
21            Logger.Log("About to do the ACT part of blah blah... so the test output reads like a story");
22            _textTester.ArrangeActualText("The red bull");
23
24            // ASSERT
25            Logger.Log("Comparing...");
26            LogAssert.IsTrue("Text samples are different", _textTester.IsEqual());
27            // OR
28            _textTester.AssertActualTextEqualsExpectedText();
29            // The test output will have a helpful Diff.
30        }
31    }

See TextTestingUnitSteps.cs for an example of using the TextTester helper - especially to see how to filter and override differences.

Reference

TextTester

The TextTester class is used in tests for asserting whether actual text is similar enough to the given expected text.

Under the covers, it uses the DiffPlex open source library to identify differences.

The TextTester class has the following properties:

  • string ActualText
  • string ExpectedText
  • DiffPaneModel Differences
  • Action<DiffPaneModel> TextDifferenceFilter - a filter for the diff (see below)

The TextTester class has the following Arrange methods:

  • void ArrangeActualText(Assembly assembly, string resourceName) - to arrange the actual text from an embedded resource in the given assembly
  • void ArrangeActualText(string text) - to arrange the actual text from the given string
  • void ArrangeExpectedText(Assembly assembly, string resourceName) - to arrange the expected text from an embedded resource in the given assembly
  • void ArrangeExpectedText(string text) - to arrange the expected text from the given string

The TextTester class has the following Assert methods:

  • virtual void AssertActualTextEqualsExpectedText() - to assert the actual text against the expected text
  • bool IsEqual() - determines if the given actual and expected text is equivalent
  • virtual bool IsEqual(string expected, string actual) - determines if the given actual and expected text is equivalent

All the comparison and logging and formatting of any differences is done for you. Easy!

Ignoring / Filtering Text Differences

The TextTester class provides 3 mechanisms for hooking into the DiffPlex DiffPaneModel difference result, allowing you to manipulate it before a final decision is made as
to whether or not the actual text is similar enough to the expected text.

  • An event that is raised with the differences
1    /// <summary>
2    /// Occurs when a text difference is detected, allowing a listener to filter the differences and
3    /// change the Line[x].Type to ChangeType.UnChanged so that the difference is no longer treated as a difference.
4    /// </summary>
5    /// <remarks>
6    /// This event or the TextDifferenceFilter property can be used for this purpose.
7    /// </remarks>
8    public event EventHandler<TextDifferenceDetectedEventArgs> TextDifferenceDetectedEvent;
  • An action delegate method that is called with the differences
1    /// <summary>
2    /// Gets or sets the Action delegate to be invoked when a text difference is detected, allowing differences to be filtered out by
3    /// changing the Line[x].Type to ChangeType.UnChanged - so that a difference is no longer treated as a difference.
4    /// </summary>
5    /// <value>The Action delegate.</value>
6    /// <remarks>
7    /// This property or TextDifferenceDetectedEvent can be used for this purpose.
8    /// </remarks>
9    public Action<DiffPaneModel> TextDifferenceFilter { get; set; }
  • A virtual method that allows you to inherit from the tester class and override the differences when they are detected.
1    protected virtual void OnTextDifferenceDetected(DiffPaneModel differences)

For a quick example, please see the Behaviour Specification for the TextTester class and the associated text tester code.

Mapping Tester

The MappingFromFlatFileToFlatFileTester class provides a wrapper around TextTester specifically for the purpose of testing the mapping from one flat file to another.

Please leave below any comments, feedback or suggestions, or alternatively contact me on a social network.

comments powered by Disqus