none
UnitTest for async Method - with Debugging RRS feed

  • Question

  • I try to unit test async method. 

    However, the following code does not work for break point and Test run from Test Explorer

    [TestMethod]      public async voidTestMethod1()      {

    ...

    List<stringlist = await ph.BuildPathNavigation(fullPath);

      ...

    }

    public async Test<List<string>> BuildPathNavigation(string path){..}

    I researched but not found clear way or best practice for unit test Async method.


    MCSD .NET, SCJP, SCJWD

    Wednesday, September 25, 2019 10:42 PM

Answers

  • Hi friend,

    Sorry for delay in reply.

    According to your description, I have reproduced your issue in my side. This issue is caused because your method is async void.

    Using void will make this method finish immediately after doing the codes inside, without returning. It means your method immediately returns as soon as BuildPathNavigation returns it’s Task.

    If your test framework supports Task-returning tests, we recommend you could try to change the return type to Task instead of void.

    [TestMethod]
    public async Task TestMethod1()
    {
         List<string> list = await ph.BuildPathNavigation(fullPath);
    }

    If your test framework does not support Task-returning tests, you could use .Result (instead of using await) to block the current thread until the Task returned by BuildPathNavigation is completed.

    [TestMethod]
    public void TestMethod1()
    {
         List<string> list = ph.BuildPathNavigation(fullPath).Result;
    }

    After you change the codes like above, you could set breakpoints and right-click in the Code Editor -> choose Debug Test(s) to debug your program. You will be able to debug your unit test this time.

    In addition, this similar issue has more details and explains better, you could also refer to How to use a breakpoint after await in unit test.

    Any feedback will be expected and wish you a nice day : )

    Best Regards,

    Tianyu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 27, 2019 4:12 AM

All replies

  • Hi friend,

    Sorry for delay in reply.

    According to your description, I have reproduced your issue in my side. This issue is caused because your method is async void.

    Using void will make this method finish immediately after doing the codes inside, without returning. It means your method immediately returns as soon as BuildPathNavigation returns it’s Task.

    If your test framework supports Task-returning tests, we recommend you could try to change the return type to Task instead of void.

    [TestMethod]
    public async Task TestMethod1()
    {
         List<string> list = await ph.BuildPathNavigation(fullPath);
    }

    If your test framework does not support Task-returning tests, you could use .Result (instead of using await) to block the current thread until the Task returned by BuildPathNavigation is completed.

    [TestMethod]
    public void TestMethod1()
    {
         List<string> list = ph.BuildPathNavigation(fullPath).Result;
    }

    After you change the codes like above, you could set breakpoints and right-click in the Code Editor -> choose Debug Test(s) to debug your program. You will be able to debug your unit test this time.

    In addition, this similar issue has more details and explains better, you could also refer to How to use a breakpoint after await in unit test.

    Any feedback will be expected and wish you a nice day : )

    Best Regards,

    Tianyu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 27, 2019 4:12 AM
  • It works! Thank yoU!

    MCSD .NET, SCJP, SCJWD

    Wednesday, October 16, 2019 5:18 PM