Invalid cast exception Xunit deserialization error

0

When trying to run a test case that uses the xunit framework through Visual Studio I am currently getting the following error.

System.InvalidCastException
  HResult=0x80004002
  Message=Specified cast is not valid.
  Source=xunit.execution.desktop
StackTrace:
   at Xunit.Serialization.XunitSerializationInfo.GetValue[T](String key) in C:\Dev\xunit\xunit\src\common\XunitSerializationInfo.cs:line 40
   at Xunit.Sdk.XunitTestCase.Deserialize(IXunitSerializationInfo data) in C:\Dev\xunit\xunit\src\xunit.execution\Sdk\Frameworks\XunitTestCase.cs:line 177
...

I am the only person in my team seeing this error when they run the tests and I have forced a deep refresh of my code repository and reinstalled numerous things and still this issue persists.

We recently upgraded our xunit framework from 2.3.1 to 2.4.1. I have tried downgrading components back to 2.3.1 and this allows me to run tests again, however it is still a mystery as to why this issue only affects me.

c#
deserialization
xunit
xunit.net
xunit2
asked on Stack Overflow Jun 9, 2020 by ZoSal

1 Answer

0

Ok! So.

Deserialization errors can be tricky but it's important to briefly explain serialization and deserialization.

In a very abstract nutshell the process of serialization is taking an object in memory then putting it through a function so the output of that function can then be easily (and compactly) passed to systems outside the program. One form of serialization is when a simple game is saved, the game state is serialized and saved to a file. Deserialization is the inverse of the function, where you take some data that represents an object you want in memory and run it through a function which creates that object with all the desired values.

I would bet that the issue is based on either your serialization and deserialization processes not matching (are not the inverse of each other) or due to which serialization process you use it may incur problems with changes to your code.

For instance, the xunit.runner.visualstudio package changes some of the serialization process. This is fine in most cases since it will also handle the deserialization as well however if, for example, its process of serialization was a flat key-value dictionary then any name clashes can cause the deserialization to fail. Since you mention xunit components were updated then I would bet those updates have started using variable names which you were already using in your test classes.

This doesn't quite explain why only you see the issue and no other team members, since I would imagine your packages are also in your source control. It could potentially be something as simple as the order in which your test framework processes get run and you just get unlucky due to multiple factors unique to your machine.

A solution to your error, either:

  • Update your own code base to avoid the name clash. Debugging with VS may help you peer into the serialized object and notice any repeated key entries.
  • Use a different test runner package (i.e. not xunit.runner.visualstudio).
answered on Stack Overflow Jun 9, 2020 by ZoSal • edited Jun 9, 2020 by ZoSal

User contributions licensed under CC BY-SA 3.0