Reputation: 156
I'm trying to serialize / deserialize a unique type as part of an xunit c# test. The type is
List<GraphicTestCase<TPage>> _testCases = new();
GraphicTestCase looks like the following:
public interface IGraphicTestCase
{
string Name { get; set; }
IWebElement Execute();
}
public abstract class GraphicTestBase<TPage,TReturn> where TPage : BasePage
{
private readonly Func<TPage> _pageFactory;
public GraphicTestBase(Func<TPage> pageFactory)
{
_pageFactory = pageFactory;
}
public string Name { get; set; }
public Func<TPage, TReturn> Action { get; set; }
public TReturn Execute()
{
TPage page = _pageFactory();
return Action(page);
}
}
public class GraphicTestCase<TPage> : GraphicTestBase<TPage, IWebElement> , IGraphicTestCase where TPage : BasePage
{
public GraphicTestCase(Func<TPage> pageFactory) : base(pageFactory)
{
}
}
Here is my current serialization/deserialization
public void Serialize(IXunitSerializationInfo info)
{
JsonSerializerSettings settings = new()
{
TypeNameHandling = TypeNameHandling.All,
ContractResolver = new DefaultContractResolver
{
IgnoreSerializableInterface = false,
IgnoreSerializableAttribute = false,
},
Formatting = Formatting.Indented,
};
string json = JsonConvert.SerializeObject(_testCases, settings);
// Add serialized data to the serialization info
info.AddValue("TestCases", json);
}
public void Deserialize(IXunitSerializationInfo info)
{
string json = info.GetValue<string>("TestCases");
JsonSerializerSettings settings = new()
{
TypeNameHandling = TypeNameHandling.All,
ContractResolver = new DefaultContractResolver
{
IgnoreSerializableInterface = false,
IgnoreSerializableAttribute = false
},
Formatting = Formatting.Indented
};
var cases = JsonConvert.DeserializeObject<List<GraphicTestCase<TPage>>>(json, settings);
_testCases.Clear();
foreach (var testCase in cases)
{
_testCases.Add(testCase);
}
}
}
Attempting to serialize / deserialize these values prevents my tests from being discovered in the test explorer, thus troubleshooting is quite hard.
Upvotes: 0
Views: 102