I am using xUnit and my tests are implemented as Theories with MemberData. I am using custom classes for test's input and I use IXunitSerializable interface to implement serialization for these classes. This way each test case gets discovered as an individual test instead of all cases being lumped together under one test per method.
During the test discovery nCrunch (3.15.0.6) under Visual Studio 2017 gives me this error:
An error occurred while analysing this project after it was built: System.Exception: Error while discovering test 'TestsClass.MyTests("TestsClass+MyTestData")':System.Runtime.Serialization.SerializationException: Type 'TestsClass+MyTestData' in Assembly 'Tests1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
at System.Runtime.Serialization.FormatterServices.<>c__DisplayClass9_0.<GetSerializableMembers>b__0(MemberHolder _)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
at nCrunch.TestExecution.Frameworks.XUnit2.TestCaseArgumentData.StoreInTest(FrameworkTest test)
at nCrunch.Module.XUnit2.Integration.XUnitNCrunchDiscoveredTestContainer.StoreDiscoveredTest(ITestCase testCase, TestName testName)
at nCrunch.Module.XUnit2.Integration.XUnitDiscoveryMessageSink.discoverTest(ITestCase testCase)
Here is the sample code:
public class MyTestData : IXunitSerializable
{
public string Name { get; set; }
public void Deserialize(IXunitSerializationInfo info)
{
Name = info.GetValue<string>("name");
}
public void Serialize(IXunitSerializationInfo info)
{
info.AddValue("name", Name, typeof(string));
}
}
public static TheoryData<MyTestData> AllMyTests = new TheoryData<MyTestData>
{
new MyTestData{ Name = "test1" },
new MyTestData{ Name = "test2" },
};
[Theory]
[MemberData("AllMyTests")]
public void MyTests(MyTestData test)
{
}
If I remove IXunitSerializable interface, it all works, but all tests from the same method are shown as a single test in nCrunch and in the Visual Studio Test Explorer.
Adding [Serializable] attribute to all classes that are involved would not be a reasonable solution, but as an experiment I have tried that and it does not work as expected - only the first set of parameters values gets used, and the test method runs only once, all other tests age completely ignored.
Please note that Theories with parameters supplied via InlineData get discovered and executed as expected.
If possible, please provide an ETA for the fix.
Thank you,
Dmytro Zakharov