Skip to content

Comparing files by NUnit DataDriven tests

Jakub Raczek edited this page Apr 25, 2019 · 12 revisions

If you need to compare files generated during test execution of your application under test you can use for that DataDriven mechanism from NUnit framework. Let say that we are generating a set of files during test execution on Live environment and same set of files on branch (under development) environment. We can compare those files in one test method.

All files in given folder will be find and paired. Files from each pair will be compared to each other. In order to be easily paired files from each pair should have the same name with different postfix e.g. "test_weekly_report_live.csv" - "test_weekly_report_branch.csv".

All pairs of files will be listed in results as separated tests, with test names the same as files names (without postfixes).

You can use FilesHelper.RenameFile method for setting proper names of downloaded files.

As a tool for comparing different types of files you can use e.g. Compare IT! (commercial software), it supports command line operations, so can be easily use in C# code, or you can implement your own class for comparing files.

In Ocaramba.Tests.NUnit sub project you can find examples of comparing files by NUnit DataDriven tests.

Test class CompareFilesDataDrivenTests:

namespace Ocaramba.Tests.NUnit.Tests
{
    using System.IO;
    using NLog;
    using global::NUnit.Framework;

    using Ocaramba.Tests.NUnit;
    using Ocaramba.Tests.NUnit.DataDriven;

    [TestFixture]
    public class CompareDataDrivenTests
    {
        private readonly Logger logger = LogManager.GetCurrentClassLogger();

        private readonly char separator = Path.DirectorySeparatorChar;

        /// <summary>
        /// Compares the CSV files, find files to compare by DataDriven methods from NUnit.
        /// Execute that tests after DownloadFilesTestsNUnit TestFeature
        /// </summary>
        /// <param name="liveFiles">The live files.</param>
        /// <param name="testFiles">The test files.</param>
        [Test, Category("CompareFiles")]
        [TestCaseSource(typeof(CompareFiles), "GetCsvFileToCompare")]
        public void CompareCsvFiles(string liveFiles, string testFiles)
        {
            var folder = ProjectBaseConfiguration.DownloadFolderPath;
            if (!File.Exists(folder + this.separator + testFiles))
            {
                this.logger.Info(ProjectBaseConfiguration.DownloadFolderPath + liveFiles);
                this.logger.Error("Missing file:\n{0}{1}", folder, testFiles);
                Assert.True(false, "File does not exist");
            }

            ////Implement here methods for comparing files
            ////if (Compare.Files(ProjectBaseConfiguration.DownloadFolderPath + this.separator, testFiles, liveFiles))
            ////{
            ////    Assert.True(false, "Files are different");
            ////}

            this.logger.Info("Files are identical");
        }
    }
}

NUnit DataDriven class CompareFiles

namespace Ocaramba.Tests.NUnit.DataDriven
{
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Text.RegularExpressions;

    using NLog;
    using global::NUnit.Framework;

    using Ocaramba.Helpers;
    using Ocaramba.Tests.NUnit;

    public static class CompareFiles
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        /// <summary>
        /// Gets the comma-separated values file to compare.
        /// </summary>
        public static IEnumerable GetCsvFileToCompare
        {
            get
            {
                return FindFiles(FileType.Csv);
            }
        }

        /// <summary>
        /// Get files to compare.
        /// </summary>
        /// <returns>
        /// Pairs of files to compare <see cref="IEnumerable"/>.
        /// </returns>
        private static IEnumerable<TestCaseData> FindFiles(FileType type)
        {
            Logger.Info("Get Files {0}:", type);
            var liveFiles = FilesHelper.GetFilesOfGivenType(ProjectBaseConfiguration.DownloadFolderPath, type, "live");

            if (liveFiles != null)
            {
                foreach (FileInfo liveFile in liveFiles)
                {
                    Logger.Trace("liveFile: {0}", liveFile);

                    var fileNameBranch = liveFile.Name.Replace("live", "branch");
                    var testCaseName = liveFile.Name.Replace("_" + "live", string.Empty);

                    TestCaseData data = new TestCaseData(liveFile.Name, fileNameBranch);
                    data.SetName(Regex.Replace(testCaseName, @"[.]+|\s+", "_"));
 
                    Logger.Trace("file Name Short: {0}", testCaseName);

                    yield return data;
                }
            }
        }
    }
}

ProjectBaseConfiguration class

Clone this wiki locally