Permalink
Browse files

Fixes #1495: ClassData and IClassFixture

  • Loading branch information...
bradwilson committed Oct 20, 2017
1 parent 1c1ded2 commit e41903442a6b03f5c1e6ee41c3a93b458c63e373
@@ -23,13 +23,16 @@ public class TestCollection : LongLivedMarshalByRefObject, ITestCollection
/// <param name="collectionDefinition">The optional type which contains the collection definition</param>
/// <param name="displayName">The display name for the test collection</param>
public TestCollection(ITestAssembly testAssembly, ITypeInfo collectionDefinition, string displayName)
: this(testAssembly, collectionDefinition, displayName, Guid.NewGuid()) { }
internal TestCollection(ITestAssembly testAssembly, ITypeInfo collectionDefinition, string displayName, Guid uniqueId)
{
Guard.ArgumentNotNull("testAssembly", testAssembly);
CollectionDefinition = collectionDefinition;
DisplayName = displayName;
TestAssembly = testAssembly;
UniqueID = Guid.NewGuid();
UniqueID = uniqueId;
}
/// <inheritdoc/>
@@ -62,6 +62,15 @@ protected internal override ITestClass CreateTestClass(ITypeInfo @class)
return new TestClass(TestCollectionFactory.Get(@class), @class);
}
internal ITestClass CreateTestClass(ITypeInfo @class, Guid testCollectionUniqueId)
{
// This method is called for special fact deserialization, to ensure that the test collection unique
// ID lines up with the ones that will be deserialized through normal mechanisms.
var discoveredTestCollection = TestCollectionFactory.Get(@class);
var testCollection = new TestCollection(discoveredTestCollection.TestAssembly, discoveredTestCollection.CollectionDefinition, discoveredTestCollection.DisplayName, testCollectionUniqueId);
return new TestClass(testCollection, @class);
}
/// <summary>
/// Finds the tests on a test method.
/// </summary>
@@ -155,7 +164,7 @@ public override string Serialize(ITestCase testCase)
var className = testCase.TestMethod?.TestClass?.Class?.Name;
var methodName = testCase.TestMethod?.Method?.Name;
if (className != null && methodName != null && (xunitTestCase.TestMethodArguments == null || xunitTestCase.TestMethodArguments.Length == 0))
return $":F:{className}:{methodName}:{(int)xunitTestCase.DefaultMethodDisplay}";
return $":F:{className}:{methodName}:{(int)xunitTestCase.DefaultMethodDisplay}:{testCase.TestMethod.TestClass.TestCollection.UniqueID.ToString("N")}";
}
return base.Serialize(testCase);
@@ -46,12 +46,13 @@ public override ITestCase Deserialize(string value)
{
if (value.Length > 3 && value.StartsWith(":F:"))
{
// Format from TestCaseDescriptorFactory: ":F:{typeName}:{methodName}:{defaultMethodDisplay}"
// Format from XunitTestFrameworkDiscoverer.Serialize: ":F:{typeName}:{methodName}:{defaultMethodDisplay}:{collectionId}"
var parts = value.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length > 3)
if (parts.Length > 4)
{
var typeInfo = discoverer.Value.AssemblyInfo.GetType(parts[1]);
var testClass = discoverer.Value.CreateTestClass(typeInfo);
var testCollectionUniqueId = Guid.Parse(parts[4]);
var testClass = discoverer.Value.CreateTestClass(typeInfo, testCollectionUniqueId);
var methodInfo = testClass.Class.GetMethod(parts[2], true);
var testMethod = new TestMethod(testClass, methodInfo);
var defaultMethodDisplay = (TestMethodDisplay)int.Parse(parts[3]);
@@ -24,8 +24,9 @@ public TestCaseBulkDeserializerTests()
[Fact]
public void CanDeserializeSpecialFactSerialization()
{
var guid = Guid.NewGuid();
var results = default(List<KeyValuePair<string, ITestCase>>);
var serializedTestCases = new List<string> { ":F:TestCaseBulkDeserializerTests+TestClass:FactMethod:2" };
var serializedTestCases = new List<string> { $":F:TestCaseBulkDeserializerTests+TestClass:FactMethod:2:{guid.ToString("N")}" };
Action<List<KeyValuePair<string, ITestCase>>> callback = r => results = r;
new TestCaseBulkDeserializer(discoverer, executor, serializedTestCases, callback);
@@ -34,6 +35,7 @@ public void CanDeserializeSpecialFactSerialization()
Assert.Equal(kvp.Value.UniqueID, kvp.Key);
Assert.Equal("TestCaseBulkDeserializerTests+TestClass", kvp.Value.TestMethod.TestClass.Class.Name);
Assert.Equal("FactMethod", kvp.Value.TestMethod.Method.Name);
Assert.Equal(guid, kvp.Value.TestMethod.TestClass.TestCollection.UniqueID);
}
[Fact]
@@ -60,6 +62,78 @@ public void CanDeserializeGeneralizedSerialization()
Assert.Equal("TestCaseBulkDeserializerTests+TestClass.TheoryMethod(x: 42)", kvp.Value.DisplayName);
}
[Fact]
public static void DeserializedFactsAndTheoriesFromTheSameClassStillShareFixtures()
{
var code = @"
using System;
using System.Threading;
using Xunit;
public class TestClassFixture : IDisposable
{
public static long StaticConstructorCount = 0;
public readonly long ConstructorInstance;
public TestClassFixture() { ConstructorInstance = Interlocked.Increment(ref StaticConstructorCount); }
public void Dispose() { Assert.Equal(1, StaticConstructorCount); }
}
public class TestClass : IClassFixture<TestClassFixture>
{
readonly TestClassFixture fixture;
public TestClass(TestClassFixture fixture) { this.fixture = fixture; }
[Fact]
public void FactMethod() { Assert.Equal(1, fixture.ConstructorInstance); }
[Theory]
[InlineData(42)]
public void TheoryMethod(int x) { Assert.Equal(1, fixture.ConstructorInstance); }
}
";
using (var assembly = CSharpAcceptanceTestV2Assembly.Create(code))
{
var discoverySink = new SpyMessageSink<IDiscoveryCompleteMessage>();
var serializedTestCases = default(List<string>);
var descriptors = default(List<TestCaseDescriptor>);
using (var xunit2 = new Xunit2(AppDomainSupport.Required, new NullSourceInformationProvider(), assembly.FileName))
{
xunit2.Find("TestClass", false, discoverySink, TestFrameworkOptions.ForDiscovery());
discoverySink.Finished.WaitOne();
var testCases = discoverySink.Messages
.OfType<ITestCaseDiscoveryMessage>()
.Select(x => x.TestCase)
.ToList();
serializedTestCases = testCases.Select(x => xunit2.Serialize(x)).ToList();
descriptors = xunit2.GetTestCaseDescriptors(testCases, true);
}
using (var xunit2 = new Xunit2(AppDomainSupport.Required, new NullSourceInformationProvider(), assembly.FileName))
{
var deserializations = default(List<ITestCase>);
Action<List<KeyValuePair<string, ITestCase>>> callback = r => deserializations = r.Select(x => x.Value).ToList();
new TestCaseBulkDeserializer(xunit2, xunit2, serializedTestCases, callback);
var executionSink = new SpyMessageSink<ITestAssemblyFinished>();
xunit2.RunTests(deserializations, executionSink, TestFrameworkOptions.ForExecution());
executionSink.Finished.WaitOne();
var passedTests = executionSink.Messages.OfType<ITestPassed>().ToList();
var failedTests = executionSink.Messages.OfType<ITestFailed>().ToList();
Assert.Equal(2, passedTests.Count);
Assert.Empty(failedTests);
}
}
}
class TestClass
{
[Fact]
@@ -109,7 +109,7 @@ public void XunitFactHasSpecialSerialization()
var result = Assert.Single(callbackResults);
var serialization = Assert.Single(result.Split('\n').Where(line => line.StartsWith("S ")));
Assert.Equal("S :F:TestCaseDescriptorFactoryTests+TestClass:FactMethod:1", serialization);
Assert.Equal($"S :F:TestCaseDescriptorFactoryTests+TestClass:FactMethod:1:{testCase.TestMethod.TestClass.TestCollection.UniqueID.ToString("N")}", serialization);
}
[Fact]
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
using Xunit.Abstractions;
@@ -30,6 +31,7 @@ public class Class1
using (var assembly = CSharpAcceptanceTestV2Assembly.Create(code))
{
var serializations = default(List<string>);
var testCollectionId = default(Guid);
using (var serializationController = new TestableXunitFrontController(assembly.FileName))
{
@@ -38,12 +40,13 @@ public class Class1
sink.Finished.WaitOne();
var testCases = sink.Messages.OfType<ITestCaseDiscoveryMessage>().OrderBy(tcdm => tcdm.TestCase.TestMethod.Method.Name).Select(tcdm => tcdm.TestCase).ToList();
testCollectionId = testCases[0].TestMethod.TestClass.TestCollection.UniqueID;
var descriptors = serializationController.GetTestCaseDescriptors(testCases, true);
serializations = descriptors.Select(d => d.Serialization).ToList();
}
Assert.Collection(serializations,
s => Assert.Equal(":F:Namespace1.Class1:FactMethod:1", s),
s => Assert.Equal($":F:Namespace1.Class1:FactMethod:1:{testCollectionId.ToString("N")}", s),
s => Assert.StartsWith("Xunit.Sdk.XunitTestCase, xunit.execution.{Platform}:", s)
);

0 comments on commit e419034

Please sign in to comment.