Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static constructor of ref class being executed twice #2717

Open
DSanchen opened this issue Aug 10, 2022 · 1 comment
Open

Static constructor of ref class being executed twice #2717

DSanchen opened this issue Aug 10, 2022 · 1 comment

Comments

@DSanchen
Copy link

DSanchen commented Aug 10, 2022

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

@bradwilson
Copy link
Member

The reality is we have nothing to do with when static objects are initialized. That's down to coordination between the compiler and the runtime. Unfortunately, this isn't something that I'm honestly equipped to diagnose and fix (if such a fix is even reasonable).

That said, if there is a fix to be done on our side, it's probably in the core framework. I'm going to transfer this issue over there, and then mark it as help wanted.

@bradwilson bradwilson transferred this issue from xunit/visualstudio.xunit May 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants