简体   繁体   English

单元测试在调试时通过但在运行时失败

[英]Unit test passes when in debug but fails when run

A search method returns any matching Articles and the most recent Non-matching articles up to a specified number.搜索方法返回任何匹配的文章和不超过指定数量的最近的非匹配文章。

Prior to being returned, the IsMatch property of the matching articles is set to true as follows:在返回之前,匹配文章的 IsMatch 属性设置为 true,如下所示:

articles = matchingArticles.Select(c => { c.IsMatch = true; return c; }).ToList();文章 = matchingArticles.Select(c => { c.IsMatch = true; 返回 c; }).ToList();

In a test of this method,在对该方法的测试中,

    [Test]
    public void SearchForArticle1Returns1MatchingArticleFirstInTheList()
    {
        using (var session = _sessionFactory.OpenSession())
        {
            var maxResults = 10;
            var searchPhrase = "Article1";
            IArticleRepository articleRepository = new ArticleRepository(session);
            var articles = articleRepository.GetSearchResultSet(searchPhrase, maxResults);
            Assert.AreEqual(10, articles.Count);
            Assert.AreEqual(1, articles.Where(a => a.Title.Contains(searchPhrase)).Count());
            var article = articles[0];
            Assert.IsTrue(article.Title.Contains(searchPhrase));
            Assert.IsTrue(article.IsMatch);
        }
    }

All assertions pass when the test is run in debug, however the final assertion fails when the test is run in release:当测试在调试中运行时,所有断言都会通过,但是当测试在发布中运行时,最终断言会失败:

Expected: True But was: False预期:正确但是是:错误

In the app itself the response is correct.在应用程序本身中,响应是正确的。

Any ideas as to why this is happening?关于为什么会发生这种情况的任何想法?

Edit:编辑:

I figured out what the problem is.我想出了问题是什么。 It's essentially a race condition.这本质上是一种竞争条件。 When I am setting up the tests, I am dropping the db table, recreating it and populating it with the test data.当我设置测试时,我删除了数据库表,重新创建它并用测试数据填充它。 Since the search relies on Full Text search, I am creating a text index on the relevant columns and setting it to auto populate.由于搜索依赖于全文搜索,因此我在相关列上创建了一个文本索引并将其设置为自动填充。 When this is run in debug, there appears to be sufficient time to populate the text index and the search query returns matches.当这在调试中运行时,似乎有足够的时间来填充文本索引并且搜索查询返回匹配项。 When I run the test I don't think the index has been populated in time, no matches are returned and the test fails.当我运行测试时,我认为索引没有及时填充,没有返回任何匹配项并且测试失败。 It's similar to issues with datetimes.它类似于日期时间问题。 If I put a delay between creating the catalog and running the test the test passes.如果我在创建目录和运行测试之间设置延迟,则测试通过。

Pones, you have since clarified that the unit test fails when not debugging. Pones,你后来澄清了单元测试在不调试时失败。

At this stage it could be anything however you should continue to run the unit test not debugging and insert the following statement somewhere you know (or think you know) is true在这个阶段它可以是任何东西但是你应该继续运行单元测试而不是调试并在你知道(或认为你知道)的地方插入以下语句是真的

 if(condition)
            Debugger.Launch();

This will do the obvious and allow you to zone in on whats going wrong.这将起到显而易见的作用,并让您了解发生了什么问题。 1 Place i suggest is on the IsMatch property (for starters) 1 我建议的地方是IsMatch属性(对于初学者)

Another common place you can run into issues like this is using DateTime's.另一个可能遇到此类问题的常见地方是使用 DateTime。 If your unit test is running 'too fast' then it may break an assumption you had.如果您的单元测试运行“太快”,那么它可能会打破您的假设。

try to print out the actual result that you are comparing them with expected on debug and normal run尝试print out您正在将它们与debugnormal run时的预期进行比较的实际结果

in my case, I created entities (JBA) in the test method就我而言,我in the test method I created entities (JBA)

in debug mode, the generated ids were 1, 2 and 3debug模式下, generated ids为 1、2 和 3

but in the normal running mode, they ware different但在normal running模式下,它们是different

that caused my hard-coded values to make the test fail, so I changed them to get id from entity instead of the hard-coded way这导致我的hard-coded值使测试失败,所以我将它们更改为从实体而不是硬编码方式获取 id

hope this helps希望这可以帮助

Obviously the problem will be different for other users, but I just hit it, and figured my solution may help some.显然,其他用户的问题会有所不同,但我刚刚解决了这个问题,并认为我的解决方案可能会有所帮助。 Basically when you are running in debug mode, you are running a single test only.基本上,当您在调试模式下运行时,您只会运行一个测试。 When you are running in run mode, you are running multiple tests in addition to the one you are having a problem with.当您在运行模式下运行时,除了遇到问题的测试之外,您还在运行多项测试。

In my situation the problem was those other tests writing to a global list that I was not explicitly clearing in my test setup.在我的情况下,问题是那些写入全局列表的其他测试我没有在我的测试设置中明确清除。 I fixed the issue by clearing the list at the beginning of the test.我通过在测试开始时清除列表来解决这个问题。

My advice to see if this is the type of problem you are facing would be to disable all other tests and only 'run' the test you have an issue with.我的建议是,看看这是否是您面临的问题类型,即禁用所有其他测试,只“运行”您遇到问题的测试。 If it works when ran by itself, but not with others, you'll know you have some dependency between tests.如果它在单独运行时有效,但与其他程序一起运行时无效,您就会知道测试之间存在某种依赖性。

Another tip is to use Console.WriteLine("test") statements in the test.另一个技巧是在测试中使用Console.WriteLine("test")语句。 That's actually how I found my list had items with it leftover from another test.实际上,这就是我发现我的列表中有其他测试遗留下来的项目的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MSTest单元测试在“调试”模式下通过,但在“运行”模式下未通过最终断言 - MSTest unit test passes when in “debug” mode, but fails final assertion in “run” mode MSTest单元测试自行通过,在运行其他测试时失败 - MSTest unit test passes by itself, fails when other tests are run 单元测试在调试版本中传递,但在版本构建中失败 - Unit test passes in debug build but fails in release build 单元测试因发布配置而失败,但在调试模式下通过 - Unit test fails with release config but passes in debug mode 在 Visual Studio IDE 中运行测试时,单元测试通过,但在使用 vstest.console.exe 和 Microsoft Fakes 时失败 - Unit test passes when running the test from within Visual Studio IDE but fails when using vstest.console.exe and Microsoft Fakes 具有时间延迟的C#单元测试通过调试,但在构建服务器上失败 - C# unit test with time delay passes debug but fails on build server MSTest无法通过VS 2013的单元测试 - MSTest fails Unit Test that passes in VS 2013 当单元测试中出现故障时,验证方法调用仍会通过 - Verifying a method call still passes when a failure is expected in unit test 单元测试在Visual Studio中“全部运行”但单独传递时失败 - Unit Tests Fail when “Run All” in Visual Studio but passes individually 未处于调试模式时,Nunit测试失败 - Nunit test fails when not in debug mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM