protected override object CreateTestClass() { var testClass = base.CreateTestClass(); if (testClass is LoggedTest loggedTestClass) { var classType = loggedTestClass.GetType(); var logLevelAttribute = TestMethod.GetCustomAttribute <LogLevelAttribute>() as LogLevelAttribute; var testName = TestMethodArguments.Aggregate(TestMethod.Name, (a, b) => $"{a}-{(b ?? "null")}"); // Try resolving ITestOutputHelper from constructor arguments loggedTestClass.TestOutputHelper = ConstructorArguments?.SingleOrDefault(a => typeof(ITestOutputHelper).IsAssignableFrom(a.GetType())) as ITestOutputHelper; var useShortClassName = TestMethod.DeclaringType.GetCustomAttribute <ShortClassNameAttribute>() ?? TestMethod.DeclaringType.Assembly.GetCustomAttribute <ShortClassNameAttribute>(); var resolvedClassName = useShortClassName == null ? classType.FullName : classType.Name; // None resolved so create a new one and retain a reference to it for initialization/uninitialization if (loggedTestClass.TestOutputHelper == null) { loggedTestClass.TestOutputHelper = _output = new TestOutputHelper(); _output.Initialize(MessageBus, Test); } AssemblyTestLog .ForAssembly(classType.GetTypeInfo().Assembly) .StartTestLog( loggedTestClass.TestOutputHelper, resolvedClassName, out var loggerFactory, logLevelAttribute?.LogLevel ?? LogLevel.Trace, out var resolvedTestName, testName); // internal for testing loggedTestClass.ResolvedTestMethodName = resolvedTestName; loggedTestClass.ResolvedTestClassName = resolvedClassName; loggedTestClass.LoggerFactory = loggerFactory; loggedTestClass.Logger = loggerFactory.CreateLogger(classType); loggedTestClass.TestSink = new TestSink(); loggerFactory.AddProvider(new TestLoggerProvider(loggedTestClass.TestSink)); loggedTestClass.AdditionalSetup(); } return(testClass); }
public virtual void Initialize(MethodInfo methodInfo, object[] testMethodArguments, ITestOutputHelper testOutputHelper) { try { TestOutputHelper = testOutputHelper; var classType = GetType(); var logLevelAttribute = methodInfo.GetCustomAttribute <LogLevelAttribute>() ?? methodInfo.DeclaringType.GetCustomAttribute <LogLevelAttribute>() ?? methodInfo.DeclaringType.Assembly.GetCustomAttribute <LogLevelAttribute>(); var testName = testMethodArguments.Aggregate(methodInfo.Name, (a, b) => $"{a}-{(b ?? "null")}"); var useShortClassName = methodInfo.DeclaringType.GetCustomAttribute <ShortClassNameAttribute>() ?? methodInfo.DeclaringType.Assembly.GetCustomAttribute <ShortClassNameAttribute>(); // internal for testing ResolvedTestClassName = useShortClassName == null ? classType.FullName : classType.Name; _testLog = AssemblyTestLog .ForAssembly(classType.GetTypeInfo().Assembly) .StartTestLog( TestOutputHelper, ResolvedTestClassName, out var loggerFactory, logLevelAttribute?.LogLevel ?? LogLevel.Debug, out var resolvedTestName, out var logOutputDirectory, testName); ResolvedLogOutputDirectory = logOutputDirectory; ResolvedTestMethodName = resolvedTestName; LoggerFactory = loggerFactory; Logger = loggerFactory.CreateLogger(classType); } catch (Exception e) { _initializationException = ExceptionDispatchInfo.Capture(e); } }
public virtual void Initialize(TestContext context, MethodInfo methodInfo, object[] testMethodArguments, ITestOutputHelper testOutputHelper) { try { TestOutputHelper = testOutputHelper; var classType = GetType(); var logLevelAttribute = methodInfo.GetCustomAttribute <LogLevelAttribute>() ?? methodInfo.DeclaringType.GetCustomAttribute <LogLevelAttribute>() ?? methodInfo.DeclaringType.Assembly.GetCustomAttribute <LogLevelAttribute>(); // internal for testing ResolvedTestClassName = context.FileOutput.TestClassName; _testLog = AssemblyTestLog .ForAssembly(classType.GetTypeInfo().Assembly) .StartTestLog( TestOutputHelper, context.FileOutput.TestClassName, out var loggerFactory, logLevelAttribute?.LogLevel ?? LogLevel.Debug, out var resolvedTestName, out var logDirectory, context.FileOutput.TestName); ResolvedLogOutputDirectory = logDirectory; ResolvedTestMethodName = resolvedTestName; LoggerFactory = loggerFactory; Logger = loggerFactory.CreateLogger(classType); } catch (Exception e) { _initializationException = ExceptionDispatchInfo.Capture(e); } }
// For back compat public IDisposable StartLog(out ILoggerFactory loggerFactory, LogLevel minLogLevel, [CallerMemberName] string testName = null) { return(AssemblyTestLog.ForAssembly(GetType().GetTypeInfo().Assembly).StartTestLog(TestOutputHelper, GetType().FullName, out loggerFactory, minLogLevel, testName)); }