单元测试在全部运行时失败,但在单独运行时通过 [英] Unit test fails when running all, but passes when running individually

查看:78
本文介绍了单元测试在全部运行时失败,但在单独运行时通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约 12 个针对不同场景的单元测试,我需要在这些测试中调用一个异步方法(有时在一个测试中多次调用).当我执行全部运行"时,其中 3 个总是会失败.如果我使用运行所选测试"一一运行它们,它们将通过.我得到的输出例外是这样的:

I have about 12 unit tests for different scenarios, and I need to call one async method in these tests (sometimes multiple times in one test). When I do "Run all", 3 of them will always fail. If I run them one by one using "Run selected test", they will pass. The exception in output I'm getting is this:

System.AppDomainUnloadedException:试图访问一个卸载的应用程序域.如果测试启动了一个线程但没有停下来.确保测试启动的所有线程都是完成前停止.

System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain. This can happen if the test(s) started a thread but did not stop it. Make sure that all the threads started by the test(s) are stopped before completion.

我无法真正分享代码,因为它很大而且我不知道从哪里开始,所以这里是示例:

I can't really share the code, as it's quite big and I don't know where to start, so here is example:

[TestMethod]
public async Task SampleTest()
{
    var someProvider = new SomeProvider();

    var result = await someProvider.IsSomethingValid();

    Assert.IsTrue(result == SomeProvider.Status.Valid);

    NetworkController.Disable();

    result = await someProvider.IsSomethingValid();

    Assert.IsTrue(result == SomeProvider.Status.Valid);

    NetworkController.Enable();
}

其他两种失败的方法分别为未来和过去设置时间.

The other 2 failing methods set time to the future and to the past respectively.

[TestMethod]
public async Task SetTimeToFutureTest()
{
    var someProvider = new SomeProvider();

    var today = TimeProvider.UtcNow().Date;

    var result = await someProvider.IsSomethingValid();

    Assert.IsTrue(result == SomeProvider.Status.Valid);

    TimeProvider.SetDateTime(today.AddYears(1));

    var result2 = await someProvider.IsSomethingValid();

    Assert.IsTrue(result2 == SomeProvider.Status.Expired);
}

TimeProvider 如下所示:

Where TimeProvider looks like this:

public static class TimeProvider
{
    /// <summary> Normally this is a pass-through to DateTime.Now, but it can be overridden with SetDateTime( .. ) for testing or debugging.
    /// </summary>
    public static Func<DateTime> UtcNow = () => DateTime.UtcNow;

    /// <summary> Set time to return when SystemTime.UtcNow() is called.
    /// </summary>
    public static void SetDateTime(DateTime newDateTime)
    {
        UtcNow = () => newDateTime;
    }

    public static void ResetDateTime()
    {
        UtcNow = () => DateTime.UtcNow;
    }
}

编辑 2:

    [TestCleanup]
    public void TestCleanup()
    {
        TimeProvider.ResetDateTime();
    }

其他方法类似,我会模拟时间/日期变化等

Other methods are similar, I will simulate time/date change, etc.

我尝试通过获取 .Result() 等方法同步调用该方法,但没有帮助.我在网上阅读了大量关于此的资料,但仍在挣扎.

I tried calling the method synchronously by getting .Result() out of it, etc, but it didn't help. I read ton material on the web about this but still struggling.

有人遇到同样的问题吗?任何提示将不胜感激.

Did anyone run into the same problem? Any tips will be highly appreciated.

推荐答案

我看不到你的测试初始化​​或清理做了什么,但可能是因为你的所有测试方法都试图异步运行,测试运行程序不允许在执行清理之前完成所有任务.

I can't see what you're doing with your test initialization or cleanup but it could be that since all of your test methods are attempting to run asynchronously, the test runner is not allowing all tasks to finish before performing cleanup.

当您运行所有测试时,相同的几种方法是否都失败了,还是随机出现的?你确定你在做单元测试而不是集成测试吗?NetworkController"类给我的印象是您可能正在做更多的集成测试.如果是这种情况,并且您使用的是通用类、提供程序、服务或存储介质(数据库、文件系统),那么由一种方法引起的交互或状态更改可能会影响另一种测试方法的功效.

Are the same few methods failing when you run all of the tests or is it random? Are you sure you are doing unit testing and not integration testing? The class "NetworkController" gives me the impression that you may be doing more of an integration test. If that were the case and you are using a common class, provider, service, or storage medium (database, file system) then interactions or state changes caused by one method could affect another test method's efficacy.

这篇关于单元测试在全部运行时失败,但在单独运行时通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆