Visual Studio单元测试通过然后立即失败而不更改代码

时间:2016-05-09 12:05:14

标签: c# visual-studio unit-testing

我对C#很新,并且已经创建了一个包含两个测试的单元测试项目。但是,我在其中一个测试中得到了一个奇怪的结果。

当我点击“运行所有测试”时,测试失败。但是,当我立即点击“运行失败的测试”时,测试通过。

问题是什么?

这是测试方法

//40 seconds
long mToastLength = 40*1000 
//this toast will be displayed for 40 seconds.
Toast.makeText(this, "Hello!!!!!", mToastLength).show(); 

修改 测试失败的消息是“测试失败 - 断言。等于失败。预期:3实际2。

Strange test result

编辑二: Heres My Filer课程。我正在尝试创建一个静态类,可用于返回目录中的所有文件(以及递归的任何子目录)

        [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);
    }

更新

归功于@Matthewwatson以获得以下改进(我认为:))其肯定不那么冗长:

 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);
        }
    }
}

现在阅读LINQ以了解我如何过滤结果以生成不包含给定字符串的列表。或者有人可以给我一个提示吗?

还会查看模拟文件系统,然后再次测试并更新结果。欢呼声

欢呼声

2 个答案:

答案 0 :(得分:0)

这通常是因为旧测试中的残余物而发生的。如果单元测试取决于不存在的东西,但旧单元测试留下了一些东西,则可能会出现问题。它真的取决于错误信息,这似乎是一个不平等的错误。这可能意味着某些东西正在创建一个与该目录中的另一个文件/文件夹冲突的文件/文件夹。我会在单元测试中坚持一个断点并进入它。当错误被抛出时(你会得到更详细的错误),只需阅读消息就可以得到解释

答案 1 :(得分:0)

看起来你有一个测试依赖。您的其他测试会影响测试失败的结果。我敢打赌,另一个测试看同一个目录,然后锁定/删除文件,你的另一个测试是试图查看同一目录,但不能添加其中一个文件,因为另一个测试已打开它。