无效的强制转换异常Xunit反序列化错误

问题描述:

当尝试通过Visual Studio运行使用xunit框架的测试用例时,我当前遇到以下错误。

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.

我们最近将xunit框架从2.3.1升级到了2.4.1。我曾尝试将组件降级到2.3.1,这使我可以再次运行测试,但是对于为什么这个问题仅影响我仍然是一个谜。

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.

好!因此。

反序列化错误可能很棘手,但简要说明序列化和反序列化非常重要。

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.

例如, xunit.runner.visualstudio 包更改了一些序列化过程。在大多数情况下,这很好,因为它也可以处理反序列化,但是,例如,如果其序列化过程是平坦的键值字典,那么任何名称冲突都可能导致反序列化失败。 既然您提到xunit组件已更新,那么我敢打赌那些更新已经开始使用您已经在测试类中使用过的变量名称。

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.


  • 更新您自己的代码库以避免名称冲突。使用VS进行调试可能会帮助您查看序列化的对象并注意到重复的键条目。

  • 使用其他测试运行程序包(即不是 xunit.runner.visualstudio )。

  • 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).