How do I run a with asynchronous method testing? RRS feed

  • General discussion

  • like this.

            public void ffff()
                //call test2
                //just a log
           //the test2 is a async method,
            private async void test2()
                AbClient ac = new AbClient();
                IEnumerable<Stat> stats = await       ac.parseJsonData();
                foreach (Stat s in stats)
                //just a log
                Logger.LogMessage("end test2 method");

    and,this test result is :


    regardless the effect of test2 method!but logMessage "end test2 method" should be printed。

    and authentic result isnot!!

    the issue is "How do  I run a test with a async method"?

    PS: run on win 8

    thanks all

    • Moved by Sheng Jiang 蒋晟 Sunday, November 11, 2012 7:33 PM testing related (From:.NET Framework 一般性问题讨论区)
    Sunday, November 11, 2012 3:18 PM

All replies

  • Hello Yangqinjiang,

    I did some research and find this thread about testing async void method, you can reference it to check if it can help:


    Best regards,

    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, November 12, 2012 8:11 AM
  • Thank you very much and thank that link!  Amanda Zhu.

    The other day,I dont't understand how to solve the issue. But today,the solution is(ok,see following code,but not like ubi supra code):

    [TestMethod] public void TestGetLines() { AbParameters param = new AbParameters() { City="mm", Q="1", WithXys=1 }; //notice the following line code Task<IEnumerable<Line>> lines =ac.GetLines(param); IEnumerable<Line> ll = lines.Result;

    foreach (Line l in ll) { Logger.LogMessage("Name:{0},Info: {1},Stats:{2},StatsXys: {3},Xys: {4}", l.Name, l.Info, l.Stats, l.StatsXys, l.Xys); } }


    because the code "ac.GetLines(param)" is a async method,must was modified by keyWord "awaith". like this:

    [TestMethod] // keyWord async public async void TestGetLines2() { AbParameters param = new AbParameters() { City = "MM", Q = "1", WithXys = 1 }; //keyWord await IEnumerable<Line> lines = await ac.GetLines(param);

    // ignore following line code //IEnumerable<Line> ll = lines.Result; //more code... }

    and,this code run in a test class ,the result  is ... unable see output.

    but now !Delete the method  keyword "async" and "await“,just like first code block.

    and the return of "ac.GetLines(param);" method is Task<IEnumerable<Line>> type,

    but how to get it‘s result???

    yes!-->” IEnumerable<Line> ll = lines.Result;“ just do it!!

    ps: Like first code block , it run like a sync method ! The effect is Profit from the keyWord "async" and "await" !!

    pps:I not good at English!  *_*

    Thursday, November 15, 2012 3:20 AM