Search code examples
unit-testingassertionsvs-unit-testing-frameworkcreate-directorytest-explorer

How can I determine why my test method fails?


This method works (it creates a directory with the string passed in):

internal static void ConditionallyCreateDirectory(string dirName)
{
    Directory.CreateDirectory(dirName);
}

...but, in accord with my OCDish tendencies, I'm testing all methods "just in case"; I've got this test for it in my test project:

[TestMethod]
public void TestConditionallyCreateDirectory()
{
    string testDirName = "testDir";
    RoboReporterConstsAndUtils.ConditionallyCreateDirectory(testDirName);
    bool dirCreated = System.IO.File.Exists(string.Format("C:\\{0}", testDirName));
    // also tried @"C:\{0}" and @"C:\\{0}"
    Assert.IsTrue(dirCreated);
}

But it fails; dirCreated is not true. Why? I can't (apparently) step into the method to see what exactly is happening, and the "Failed" message tells me nothing other than "Assert.IsTrue failed":

enter image description here

Why would this fail (fish), and how can I determine why it's failing (teach to fish)?

UPDATE

I've also now tried each of the following:

bool dirCreated = System.IO.Directory.Exists(string.Format("C:\\{0}", testDirName));
bool dirCreated = System.IO.Directory.Exists(string.Format(@"C:\{0}", testDirName));
bool dirCreated = System.IO.Directory.Exists(string.Format(@"C:\\{0}", testDirName));

Solution

  • You must write it like this:

    [TestMethod]
    public void TestConditionallyCreateDirectory()
    {
        string testDirName = "C:\\testDir";
        RoboReporterConstsAndUtils.ConditionallyCreateDirectory(testDirName);
        bool dirCreated = System.IO.Directory.Exists(testDirName);
        Assert.IsTrue(dirCreated);
    }