Incomplete result of Async Task Method Execution in NUnit 3.0

125 views
Skip to first unread message

Sathishkumar Kaliavaradhan

unread,
Apr 15, 2016, 3:20:19 AM4/15/16
to NUnit-Discuss
Hi All,

I have Async method like below.
  
 [Test] 
 public async Task SynchronizeSubscriptions()

After execution of method getting the result details like Result, Duration and Starttime from TestExecutionContext CurrentContext. For Async method I am not getting the duration and startTime value.

In Visual Studio NUnit Test 3 Adapator could able to show the duration for Async method.

Could you please let me know how do I get the duration/startTime of Async method in NUnit 3.2.0?

Best Regards,
Sathish

Charlie Poole

unread,
Apr 15, 2016, 3:28:03 AM4/15/16
to NUnit-Discuss
TestExecutionContext is an internal class in the nunit framework, as
indicated by its being in the NUnit.Framework.Internal namespace. It
is not supported for user access.

To your specific question. During the execution of an async method,
additional threads are started by .NET. Those tests do not have a
TestExecutionContext. When you attempt to access it, a dummy context
is created to avoid crashing but that context is subsequently
discarded. When the test is done, NUnit patches the
TestExecutionContext with the proper values, which are used for
reporting purposes.

Specifically on the question of Duration: at any point in the test
when you might access Duration, the test would still be running. So
any value you might retrieve would be incomplete. Note that by
"withihn" a test, we mean during execution of the test method, SetUp,
TearDown and any action attributes.
> --
> You received this message because you are subscribed to the Google Groups
> "NUnit-Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nunit-discus...@googlegroups.com.
> To post to this group, send email to nunit-...@googlegroups.com.
> Visit this group at https://groups.google.com/group/nunit-discuss.
> For more options, visit https://groups.google.com/d/optout.

Charlie Poole

unread,
Apr 15, 2016, 3:29:22 AM4/15/16
to NUnit-Discuss
Following up: I haven't suggested alternatives because I don't know
what you are trying to accomplish. Depending on what that is, there
may be some options.

Sathishkumar Kaliavaradhan

unread,
Apr 15, 2016, 9:20:51 AM4/15/16
to NUnit-Discuss
Hi Charlie,

Thank you so much for reply.

I am doing the automation of Nunit execution, in the tear down method like below reading all the Nunit execution result by using TestExecutionContext CurrentContext

[TearDown]
        public void TearDown()
        {
foreach (var nestedResult in testResult.Children)
                {
                taskExecution.DateTime = testResult.StartTime;
                taskExecution.Duration = testResult.Duration;

                }
        } 

I am able to get the async method execution result (passed, failed) but couldn't get the duration and startTime details.

Could you please let me know in the tear down method to read async method duration and starttime?

Best regards,
Sathish

Charlie Poole

unread,
Apr 15, 2016, 10:05:34 AM4/15/16
to NUnit-Discuss

As explained already,
1. It's not possible to get correct duration until the test is finished. TearDown is part of the test.
2. TestExecutionContext is internal and we do not support it's use. For example, it may not be public in future.

I asked you to explain why you are doing this so I could recommend some other way but you didn't respond. For example if you are creating a log, I would suggest a test event listener.

Again... why are you doing this? :-)

Charlie

Sathishkumar Kaliavaradhan

unread,
Apr 15, 2016, 11:42:58 AM4/15/16
to NUnit-Discuss
Hi Charlie,

Thanks Again.

We are automating the NUnit test by executing all the test through the our own Nunit Engine framework

using (var runner = nUnitEngine.GetRunner(testPackage))
                {
                    nUnitEngine.InternalTraceLevel = InternalTraceLevel.Error;
                    try
                    {
                        Trace.TraceInformation($"NUnit execution started...{executeAssembly}");
                        var executionResult = runner.Run(this, methodFilter);
                        var resultFilePath = Path.Combine(localDirectory, "NUnitExecutionResult.xml");
                        File.WriteAllText(resultFilePath, executionResult.OuterXml);
}
}

During execution of the test in each class tear down method we are collecting result through TestExecutionContext CurrentContext and generating the report using testcaseId, Name, ClassName, Duration and Result.

we are not getting the duration and start time for Async methods but getting all the details for normal methods.

Best regards,
Sathish

Charlie Poole

unread,
Apr 15, 2016, 12:18:35 PM4/15/16
to NUnit-Discuss
Hi Sathish,

I fear I must be communicating unlearly. Let's try again...

You cannot successfully do it that way. It isn't supported and will
never be supported. Additionally, it's really not such a good idea to
make test code responsible for collecting data about itself. It's
outside of the responsibility of the test. And anyway, some test data
is not really available until the test is finished. You are collecting
data before the test is finished.

Since you are using the engine, all the information you need is
available to the listener that you give to the runner when executing
the tests. You merely have to implement ITestEventListener in order to
get everything you like. As a bonus, you will be doing using our
published API rather than an internal class that may change at any
time.

Because NUnit has not had a public/published API in the past, many
people have fallen into the habit of using internal classes for this
sort of thing. It's a bad idea. In fact, this whole discussion is
suggesting to me that we should probably stop exposing such Types
publicly. We do it because NUnit has existed for a long time and it
was not possible in the past to test internal members. However now it
is and we use InternalsVisibleTo for our tests. The
TestExecutionContext could easily disappear from your view tomorrow!

Charlie

On Fri, Apr 15, 2016 at 8:42 AM, Sathishkumar Kaliavaradhan
Reply all
Reply to author
Forward
0 new messages