Testfx: [Parallel] Console/Debug traces + Log.LogMessage are not associated with the right test case

Created on 28 Nov 2017  路  2Comments  路  Source: microsoft/testfx

Description

Currently all the logging does not get associated with the right test case because the underlying implementation Console.SetOut or the log message listener are global listeners and not thread specific. There isn't a workaround for this today.

bug enhancement

All 2 comments

@cltshivash this really is a bug, it makes investigating test failures in automated runs impossible using logs. I love the parallel feature, but this bug makes it hard to use in production CI/CD scenarios.

@sea1jxr Console.Writeline is a static instance and will not be thread safe. Using TestContext will ensure that the output for a test only has the relevant logs. Sample below

using ExecutionScope = Microsoft.VisualStudio.TestTools.UnitTesting.ExecutionScope;

[assembly: Parallelize(Workers = 2, Scope = ExecutionScope.MethodLevel)]

namespace UnitTestProject1
{

    [TestClass]

    public class UnitTest1
    {
        public TestContext TestContext { get; set; }

        [TestMethod]
        public async Task TestMethod1()
        {
            for (int index = 0; index < 100; ++index)
            {
                TestContext.WriteLine("Log From TestMethod...1");
                await Task.Delay(100);
            }
        }

        [TestMethod]
        public async Task TestMethod2()
        {
            for (int index = 0; index < 100; ++index)
            {
                TestContext.WriteLine("Log From TestMethod...2");
                await Task.Delay(100);
            }
        }
    }
}
Was this page helpful?
0 / 5 - 0 ratings