MSTEST - 异步Testinitialize保证测试失败 [英] MSTEST - async Testinitialize guarantee test fail

查看:478
本文介绍了MSTEST - 异步Testinitialize保证测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道,如果有人想是这样的:

Just wondering, if anyone thought like this:

这是不正确设计有内TestInitialize异步调用,如TestInitialize有任何TestMethod的前发生的。

This is incorrect design to have async call within TestInitialize, as TestInitialize has to happen before any TestMethod.

可这是正确的做法以任何方式有异步TestInitialize呢?

Can this be correct approach in any way to have async TestInitialize as well?

    private int val = 0;

    [TestInitialize]
    public async Task  TestMehod1()
    {
        var result = await LongRunningMethod();
        val = 10;
    }

    [TestMethod]
    public void  TestMehod2()
    {
        Assert.AreEqual(10, val);
    }

任何想法?

推荐答案

也许这样做最彻底的方法是让 TestInitialize 启动的的异步操作,因为这样的:

Probably the cleanest way to do this is to have TestInitialize start the asynchronous operation, as such:

[TestClass]
public class UnitTestAsync
{
    private Task<int> val = null;

    [TestInitialize]
    public void TestInitializeMethod()
    {
        val = TestInitializeMethodAsync();
    }

    private async Task<int> TestInitializeMethodAsync()
    {
        return await LongRunningMethod();
    }

    private async Task<int> LongRunningMethod()
    {
        await Task.Delay(20);
        return 10;
    }

    [TestMethod]
    public async Task TestMehod2()
    {
        Assert.AreEqual(10, await val);
    }
}

这篇关于MSTEST - 异步Testinitialize保证测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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