MSTEST - async Testinitialize guarantee test fail

后端 未结 4 1690
春和景丽
春和景丽 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条回答
  •  萌比男神i
    2020-12-18 21:28

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

    [TestClass]
    public class UnitTestAsync
    {
        private Task val = null;
    
        [TestInitialize]
        public void TestInitializeMethod()
        {
            val = TestInitializeMethodAsync();
        }
    
        private async Task TestInitializeMethodAsync()
        {
            return await LongRunningMethod();
        }
    
        private async Task LongRunningMethod()
        {
            await Task.Delay(20);
            return 10;
        }
    
        [TestMethod]
        public async Task TestMehod2()
        {
            Assert.AreEqual(10, await val);
        }
    }
    

提交回复
热议问题