xunit/xunit

Static constructor of ref class being executed twice

开放

#2,717 创建于 2022年8月10日

 (1 条评论) (0 个反应) (0 位负责人)C# (761 个派生)batch import
BugCore frameworkhelp wanted

仓库指标

星标
 (3,873 个星标)
PR 合并指标
 (30 天内没有已合并 PR)

描述

I just started using xunit, and find it very nice to transition from MSTest (v2) to xunit. Nearly all my tests just worked after migrating. I came accross one test suite of mine, where I want to test a mixed mode (c++/cli) DLL that I use in production. In that DLL I implemented a ref class with some public static variables, that are initialized to reasonable default values in the static class constructor when used in production. For test purposes, I can alter the static variables and "inject" other behaviour into the class so that I can test what I want to test. With MSTest, all this worked as expected, the static (ref class) constructor is executed once, before I alter the static variables from within the test, than the static variable is changed, and when "act"ing the test, the variable has my altered value. With xunit (2.4.2, xunit.runner.visualstudio 2.4.5), this is not working. The ref class constructor is executed before I alter the static variable, and is then afterwards again called, before I "act" the test, thus re-setting the static variable to the default value.

I created a small repo xunit-testfailure which reproduces the issue. Of course there are possible workarounds, I just don't understand why a static constructor is called twice...

贡献者指南