MSTEST - async Testinitialize guarantee test fail

后端 未结 4 1692
春和景丽
春和景丽 2020-12-18 21:13

Just wondering, if anyone thought like this:

This is incorrect design to have async call within TestInitialize, as TestInitialize has to happen befo

4条回答
  •  情歌与酒
    2020-12-18 21:35

    What you want to do is to use .Result or .Wait() to synchronously block the TestInitialize decorated method. You can do the following:

    private int val = 0;
    
    [TestInitialize]
    public void TestMehod1()
    {
        Task result = await LongRunningMethod();
        result.Wait();
    
        val = 10;
    }
    
    [TestMethod]
    public void  TestMehod2()
    {
        Assert.AreEqual(10, val);
    }
    
        

    提交回复
    热议问题