Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Unit Test Design Issue: How to reduce redundancy in unit test writing?

Here is my c# unit test design issue:

I have one test that I want to run on some code that modifies a file. My test will run the code that modifies the file, then check the result. Pretty straight forward so far...

The issue is, I have about 10 files (soon to be a lot more) that I want to run the same unit test against. I dont want to write a new unit test for each file, when the test itself is really the same.

I could write a single test that queries the files in the folder and then runs the test logic on each file, but using this method would only report one pass/fail result for the entire set of files.

Id like to create some sort of dynamic system where every file that I place in a particular folder gets ran though this same unit test. So if 25 files are in the folder, the test gets ran 25 times, and the unit test reults report that 25 tests were ran, and includes seperate pass/fails for each.

Any ideas how or if this can be done in a c# unit test? Or with a nunit test?

Thanks! And I hope this is not a duplicate question. I looked around, but I could not find anything.

like image 646
fj40bryan Avatar asked Oct 14 '11 05:10

fj40bryan


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in coding language?

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Why was C written?

The C language was actually created to move the UNIX kernel code from assembly to a higher level language, which would do the same tasks with fewer lines of code. Oracle database development started in 1977, and its code was rewritten from assembly to C in 1983. It became one of the most popular databases in the world.


1 Answers

It sounds like what you need is a parameterised test case: see http://www.nunit.org/index.php?p=testCaseSource&r=2.5.

So:

[TestFixture]
public class Tests
{
    static string[] FileNames = new string[] 
                    { "mary.txt", "mungo.txt", "midge.txt" };

    [Test, TestCaseSource("FileNames")]
    public void TestMethod(string fileName)
    {
        Assert.That(File.Exists(fileName));
    }
}

The [TestCaseSource] attribute tells NUnit to get the values of the string parameter from the string array.

That approach, however, requires static constants for filenames. If you'd rather have a programmatic approach that reads the files from a database or suchlike try a factory class like so:

[TestFixture]
public class Tests
{
    [Test, TestCaseSource(typeof(FilenameFactory), "FileNames")]
    public bool FileCheck(string fileName)
    {
        return File.Exists(fileName);
    }
}

public class FilenameFactory
{
    public static IEnumerable FileNames
    {
        get
        {
            foreach (var filename in 
                   Directory.EnumerateFiles(Environment.CurrentDirectory))
            {
                yield return new TestCaseData(filename).Returns(true);
            }
        }
    }
}

The TestCaseData class generates "test cases" which have expectations that can be set through a fluent interface.

like image 112
Jeremy McGee Avatar answered Oct 13 '22 00:10

Jeremy McGee