Search code examples
c#.netunit-testingxunitxunit.net

There is no currently active test exception in XUnit C#


When I start all tests, response was There is no currently active test. The problem took place when ITestOutputHelper write log to output window.

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
        {
            var sb = new StringBuilder();
            sb.Append(GetLogLevelString(logLevel))
              .Append(" [").Append(_categoryName).Append("] ")
              .Append(formatter(state, exception));

            if (exception != null)
            {
                sb.Append('\n').Append(exception);
            }

            _scopeProvider.ForEachScope((scope, state) =>
            {
                state.Append("\n => ");
                state.Append(scope);
            }, sb);

           
            **_testOutputHelper.WriteLine(sb.ToString());**
        }

How to solve this problem?


Solution

  • I only can guess why it is so, maybe you use instance _testOutputHelper from test not related to current test. So this instance was injected for another test which was already completed.

    The reason can be different, for example:

    • you can forget to pass correct instance to use for logging
    • some code can still call callbacks referencing old instance, maybe some static code or similar

    The common symptom will be that running single test is successful but running all tests or group of tests is failed.