Skip to content

Commit 60e5057

Browse files
committed
added more diagnostic options to sqlserver test (for the buildserver)
1 parent 6d60d5e commit 60e5057

9 files changed

+218
-0
lines changed

src/Server/Coderr.Server.SqlServer.Tests/Analysis/IncidentBeingAnalyzedMapper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class IncidentBeingAnalyzedMapperTests : IntegrationTest
1313
{
1414
public IncidentBeingAnalyzedMapperTests(ITestOutputHelper helper) : base(helper)
1515
{
16+
helper.WriteLine("Hello world");
1617
ResetDatabase();
1718
}
1819

src/Server/Coderr.Server.SqlServer.Tests/IntegrationTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
using System;
2+
using System.IO;
23
using codeRR.Server.SqlServer.Core.Accounts;
34
using codeRR.Server.SqlServer.Tests.Helpers;
5+
using codeRR.Server.SqlServer.Tests.Xunit;
46
using Griffin.Data;
57
using Griffin.Data.Mapper;
8+
using log4net;
9+
using log4net.Config;
10+
using Xunit;
611
using Xunit.Abstractions;
12+
[assembly: TestFramework("codeRR.Server.SqlServer.Tests.Xunit.XunitTestFrameworkWithAssemblyFixture", "codeRR.Server.SqlServer.Tests")]
713

814
namespace codeRR.Server.SqlServer.Tests
915
{
@@ -17,6 +23,13 @@ public class IntegrationTest : IDisposable
1723

1824
static IntegrationTest()
1925
{
26+
var path2 = AppDomain.CurrentDomain.BaseDirectory;
27+
XmlConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(path2, "log4net.config")));
28+
var logger = LogManager.GetLogger(typeof(IntegrationTest));
29+
logger.Info("Loaded");
30+
31+
32+
2033
AppDomain.CurrentDomain.DomainUnload += (o, e) =>
2134
{
2235
if (_databaseManager == null)
@@ -35,9 +48,11 @@ static IntegrationTest()
3548
mapper.Scan(typeof(AccountRepository).Assembly);
3649
EntityMappingProvider.Provider = mapper;
3750
}
51+
3852
public IntegrationTest(ITestOutputHelper output)
3953
{
4054
LogAttribute.Logger = output;
55+
MethodLogger.OutputHelper = output;
4156
_testDataManager = new TestDataManager(_databaseManager.OpenConnection);
4257
}
4358

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using log4net;
6+
using Xunit.Abstractions;
7+
using Xunit.Sdk;
8+
9+
namespace codeRR.Server.SqlServer.Tests.Xunit
10+
{
11+
public class MethodLogger: XunitTestClassRunner
12+
{
13+
private readonly IMessageSink _diagnosticMessageSink;
14+
private ILog _logger = LogManager.GetLogger(typeof(MethodLogger));
15+
internal static ITestOutputHelper OutputHelper;
16+
public MethodLogger(ITestClass testClass, IReflectionTypeInfo @class, IEnumerable<IXunitTestCase> testCases, IMessageSink diagnosticMessageSink, IMessageBus messageBus, ITestCaseOrderer testCaseOrderer, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource, IDictionary<Type, object> collectionFixtureMappings) : base(testClass, @class, testCases, diagnosticMessageSink, messageBus, testCaseOrderer, aggregator, cancellationTokenSource, collectionFixtureMappings)
17+
{
18+
_diagnosticMessageSink = diagnosticMessageSink;
19+
}
20+
21+
protected override Task<RunSummary> RunTestMethodAsync(ITestMethod testMethod, IReflectionMethodInfo method, IEnumerable<IXunitTestCase> testCases,
22+
object[] constructorArguments)
23+
{
24+
try
25+
{
26+
OutputHelper?.WriteLine($"Running {testMethod.TestClass.Class.Name}.{testMethod.Method.Name}");
27+
_logger.Info($"Running {testMethod.TestClass.Class.Name}.{testMethod.Method.Name}");
28+
_diagnosticMessageSink.OnMessage(
29+
new DiagnosticMessage($"Running {testMethod.TestClass.Class.Name}.{testMethod.Method.Name}"));
30+
var result= base.RunTestMethodAsync(testMethod, method, testCases, constructorArguments);
31+
OutputHelper?.WriteLine($"..completed {testMethod.TestClass.Class.Name}.{testMethod.Method.Name}");
32+
_logger.Info(".. completed " + testMethod.TestClass.Class.Name + "." + testMethod.Method.Name);
33+
_diagnosticMessageSink.OnMessage(
34+
new DiagnosticMessage($".. completed {testMethod.TestClass.Class.Name}.{testMethod.Method.Name}"));
35+
return result;
36+
}
37+
catch (Exception e)
38+
{
39+
OutputHelper?.WriteLine($"..failed {testMethod.TestClass.Class.Name}.{testMethod.Method.Name}");
40+
_logger.Info(".. failed " + testMethod.TestClass.Class.Name + "." + testMethod.Method.Name, e);
41+
_diagnosticMessageSink.OnMessage(
42+
new DiagnosticMessage($".. failed {testMethod.TestClass.Class.Name}.{testMethod.Method.Name} failed: " + e));
43+
throw;
44+
}
45+
46+
}
47+
}
48+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using Xunit.Abstractions;
7+
using Xunit.Sdk;
8+
9+
namespace codeRR.Server.SqlServer.Tests.Xunit
10+
{
11+
public class XunitTestAssemblyRunnerWithAssemblyFixture : XunitTestAssemblyRunner
12+
{
13+
readonly Dictionary<Type, object> assemblyFixtureMappings = new Dictionary<Type, object>();
14+
15+
public XunitTestAssemblyRunnerWithAssemblyFixture(ITestAssembly testAssembly,
16+
IEnumerable<IXunitTestCase> testCases,
17+
IMessageSink diagnosticMessageSink,
18+
IMessageSink executionMessageSink,
19+
ITestFrameworkExecutionOptions executionOptions)
20+
: base(testAssembly, testCases, diagnosticMessageSink, executionMessageSink, executionOptions)
21+
{ }
22+
23+
protected override async Task AfterTestAssemblyStartingAsync()
24+
{
25+
// Let everything initialize
26+
await base.AfterTestAssemblyStartingAsync();
27+
}
28+
29+
protected override Task BeforeTestAssemblyFinishedAsync()
30+
{
31+
// Make sure we clean up everybody who is disposable, and use Aggregator.Run to isolate Dispose failures
32+
foreach (var disposable in assemblyFixtureMappings.Values.OfType<IDisposable>())
33+
Aggregator.Run(disposable.Dispose);
34+
35+
return base.BeforeTestAssemblyFinishedAsync();
36+
}
37+
38+
protected override Task<RunSummary> RunTestCollectionAsync(IMessageBus messageBus,
39+
ITestCollection testCollection,
40+
IEnumerable<IXunitTestCase> testCases,
41+
CancellationTokenSource cancellationTokenSource)
42+
43+
44+
=> new XunitTestCollectionRunnerWithAssemblyFixture(assemblyFixtureMappings, testCollection, testCases, DiagnosticMessageSink, messageBus, TestCaseOrderer, new ExceptionAggregator(Aggregator), cancellationTokenSource).RunAsync();
45+
}
46+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Xunit.Abstractions;
6+
using Xunit.Sdk;
7+
8+
namespace codeRR.Server.SqlServer.Tests.Xunit
9+
{
10+
public class XunitTestCollectionRunnerWithAssemblyFixture : XunitTestCollectionRunner
11+
{
12+
private readonly Dictionary<Type, object> _assemblyFixtureMappings;
13+
private readonly IMessageSink _diagnosticMessageSink;
14+
15+
public XunitTestCollectionRunnerWithAssemblyFixture(Dictionary<Type, object> assemblyFixtureMappings,
16+
ITestCollection testCollection,
17+
IEnumerable<IXunitTestCase> testCases,
18+
IMessageSink diagnosticMessageSink,
19+
IMessageBus messageBus,
20+
ITestCaseOrderer testCaseOrderer,
21+
ExceptionAggregator aggregator,
22+
CancellationTokenSource cancellationTokenSource)
23+
: base(testCollection, testCases, diagnosticMessageSink, messageBus, testCaseOrderer, aggregator,
24+
cancellationTokenSource)
25+
{
26+
_assemblyFixtureMappings = assemblyFixtureMappings;
27+
_diagnosticMessageSink = diagnosticMessageSink;
28+
}
29+
30+
protected override Task<RunSummary> RunTestClassAsync(ITestClass testClass, IReflectionTypeInfo @class,
31+
IEnumerable<IXunitTestCase> testCases)
32+
{
33+
// Don't want to use .Concat + .ToDictionary because of the possibility of overriding types,
34+
// so instead we'll just let collection fixtures override assembly fixtures.
35+
var combinedFixtures = new Dictionary<Type, object>(_assemblyFixtureMappings);
36+
foreach (var kvp in CollectionFixtureMappings)
37+
combinedFixtures[kvp.Key] = kvp.Value;
38+
39+
// We've done everything we need, so let the built-in types do the rest of the heavy lifting
40+
return new MethodLogger(testClass, @class, testCases, _diagnosticMessageSink, MessageBus, TestCaseOrderer,
41+
new ExceptionAggregator(Aggregator), CancellationTokenSource, combinedFixtures).RunAsync();
42+
}
43+
}
44+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
using System.Reflection;
3+
using Xunit.Abstractions;
4+
using Xunit.Sdk;
5+
6+
namespace codeRR.Server.SqlServer.Tests.Xunit
7+
{
8+
public class XunitTestFrameworkExecutorWithAssemblyFixture : XunitTestFrameworkExecutor
9+
{
10+
public XunitTestFrameworkExecutorWithAssemblyFixture(AssemblyName assemblyName, ISourceInformationProvider sourceInformationProvider, IMessageSink diagnosticMessageSink)
11+
: base(assemblyName, sourceInformationProvider, diagnosticMessageSink)
12+
{ }
13+
14+
protected override async void RunTestCases(IEnumerable<IXunitTestCase> testCases, IMessageSink executionMessageSink, ITestFrameworkExecutionOptions executionOptions)
15+
{
16+
using (var assemblyRunner = new XunitTestAssemblyRunnerWithAssemblyFixture(TestAssembly, testCases, DiagnosticMessageSink, executionMessageSink, executionOptions))
17+
await assemblyRunner.RunAsync();
18+
}
19+
}
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Reflection;
2+
using Xunit.Abstractions;
3+
using Xunit.Sdk;
4+
5+
namespace codeRR.Server.SqlServer.Tests.Xunit
6+
{
7+
public class XunitTestFrameworkWithAssemblyFixture : XunitTestFramework
8+
{
9+
public XunitTestFrameworkWithAssemblyFixture(IMessageSink messageSink)
10+
: base(messageSink)
11+
{ }
12+
13+
protected override ITestFrameworkExecutor CreateExecutor(AssemblyName assemblyName)
14+
=> new XunitTestFrameworkExecutorWithAssemblyFixture(assemblyName, SourceInformationProvider, DiagnosticMessageSink);
15+
}
16+
}

src/Server/Coderr.Server.SqlServer.Tests/codeRR.Server.SqlServer.Tests.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
<RootNamespace>codeRR.Server.SqlServer.Tests</RootNamespace>
55
<AssemblyName>Coderr.Server.SqlServer.Tests</AssemblyName>
66
</PropertyGroup>
7+
<ItemGroup>
8+
<None Remove="log4net.config" />
9+
</ItemGroup>
10+
<ItemGroup>
11+
<Content Include="log4net.config">
12+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
13+
</Content>
14+
</ItemGroup>
715
<ItemGroup>
816
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
917
<PackageReference Include="Microsoft.VisualStudio.TestPlatform.ObjectModel" Version="14.0.0" />
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<log4net>
4+
5+
<appender name="StandardAppender" type="log4net.Appender.RollingFileAppender">
6+
<file value=".\logs\CoderrTests.log" />
7+
<appendToFile value="true" />
8+
<rollingStyle value="Date" />
9+
<PreserveLogFileNameExtension value="true" />
10+
<datePattern value="yyyy-MM-dd" />
11+
<layout type="log4net.Layout.PatternLayout">
12+
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] – %message%newline" />
13+
</layout>
14+
</appender>
15+
16+
<root>
17+
<level value="ALL" />
18+
<appender-ref ref="StandardAppender" />
19+
</root>
20+
</log4net>

0 commit comments

Comments
 (0)