简体   繁体   中英

Visual studio unit test passing then failing immediately with no changes to code

I quite new to C# and have created a unit test project with two tests. However, I'm getting a weird result in one of the tests.

When i click "Run all tests", the test fails. But when i immediately click "Run Failed tests", the test passes.

What could the problem be please?

Here is the test method

        [TestMethod]
    public void GetFiles_WithNoIgnoreValueProvided_ReturnsAllFiles()
    {
        var path = @"C:\Users\myDocs\Desktop\New folder";
        var thefiles = Filer.GetFiles(path) as List<string>;

        Assert.AreEqual(3, thefiles.Count);
    }

EDIT The message on test failure is "Test Failed - Assert.Areequal failed. Expected: 3 Actual 2.

测试结果奇怪

EDIT TWO: Heres My Filer class. I am trying to create a static class that can be used to return all files in a directory (and any subdirectories recursively)

 public static class Filer
{
    private static List<string> ignorethesefiles;
    private static List<string> thefiles;


    public static IEnumerable<string> GetFiles(string path, IEnumerable<string> ignorefilescontaining = null)
    {
        if (ignorefilescontaining !=null)
        {
            ignorethesefiles = new List<string>(); ignorethesefiles.AddRange(ignorefilescontaining);
        }
        else
        {
            ignorethesefiles = new List<string>() { "@" };

        }

        if (File.Exists(path))
        {
            // This path is a file
            ProcessFile(path, ref thefiles);
        }


        if (Directory.Exists(path))
        {
            // This path is a directory
            //if (ignorethesefiles.Count > 0)
            //{

            ProcessDirectory(path, ref thefiles, ref ignorethesefiles);


        }

        return thefiles;

    }

    private static void ProcessDirectory(string path, ref List<string> thefiles, ref List<string> ignorethesefiles)
    {

        //Process files in the directory
        IEnumerable<string> filesindir = Directory.GetFiles(path);
        foreach (var filename in filesindir)
        {
            //  if (ignorefilescontaining != string.Empty)
          //  if (ignorethesefiles.Count > 0)
           // {
                if (!ignorethesefiles.Any(s=>filename.Contains(s)))
                {
                    ProcessFile(filename, ref thefiles);
                }
             // ProcessFile(filename, ref thefiles);

           // }
        }

        //Recurse subdirectories
        IEnumerable<string> subdirectories = Directory.GetDirectories(path);
        foreach (var sub in subdirectories)
        {
            ProcessDirectory(sub, ref thefiles, ref ignorethesefiles);
        }

    }

    private static void ProcessFile(string path, ref List<string> thefiles)
    {
        if (thefiles == null)
        {
            thefiles = new List<string>();
            thefiles.Add(path);
        }
        else
        {
            thefiles.Add(path);
        }
    }
}

UPDATE:

Credit to @Matthewwatson for the following improvement (i think :) ) its certainly less verbose:

    public static class SOFinder
{
    public static IEnumerable<string>GetListOfFiles(string path,string pattern, SearchOption searchoption)
    {
        var files = Directory.GetFiles(path, pattern, SearchOption.AllDirectories);
        return files;
    }
}

Now reading up on LINQ to see how i can filter the result to produce a list that DOES NOT contain a given string. Or can someone give me a hint please?

Will also look to mock the file system then test again and update on the result. cheers

cheers

This can often happen because of a Remnant from the old test. if the unit test depends on something not existing, but the old Unit Test left something behind, it can have an issue. it really depends on the error message, which seems to be an inequality error. This could mean that something is creating a file/folder which collides with another file/folder in that directory. I would stick a breakpoint on the Unit Test and step into it. When the error gets thrown (you'll get a more detailed error), just read the message and you should get an explanation

It looks like you have a test dependency. Your other test is affecting the result of your failing test. I bet the other test looks at the same directory, it is then locking/removing a file, your other test is trying to look in the same directory but can't add one of the files because the other test has opened it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM