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

Request: Analzyer to error when ctor/disposable is used in combination with [FixtureLifeCycleAttribute(LifeCycle.SingleInstance)] attribute #628

Open
CoenraadS opened this issue Nov 7, 2023 · 3 comments

Comments

@CoenraadS
Copy link

CoenraadS commented Nov 7, 2023

Hello

In my project, I have set [assembly: FixtureLifeCycle(LifeCycle.InstancePerTestCase)]

This makes it safe to use ctor/IDisposable to setup/teardown the test.

Sometimes, I need state, so then I can add [FixtureLifeCycleAttribute(LifeCycle.SingleInstance)] to my class.
However it may be I still have ctor/IDisposable on this class and forget to change it to [SetUp]/[TearDown]

I would like the analyzer to warn me about this situations (and autofix if possible)

It would also be great if the analyzer is aware of assembly level attributes, they may not be defined on the class.

@manfred-brands
Copy link
Member

Thanks @CoenraadS for your report

Could you please create a separate issue for the recognition of the assembly level attribute:
[assembly: FixtureLifeCycle(LifeCycle.InstancePerTestCase)]

@manfred-brands
Copy link
Member

In theory, even unit tests that are LifeCycle.SingleInstance can be disposable.
Most will use OneTimeSetUp and OneTimeTearDown, but doing the same actions in the constructor and Dispose is allowed.

When you say you need state, do you mean state between tests in the same fixture?
Note that moving items to [SetUp]/[TearDown] won't give you state and is the equivalent of constructor/Dispose

You only can get state with static fields which you initialize in OneTimeSetUp.

@CoenraadS
Copy link
Author

@manfred-brands Ah interesting, I didn't realize that when LifeCycle.SingleInstance, constructor takes the role of OneTimeSetUp, but it makes sense. I guess that makes it not suitable for the analyzer after all, since it cannot know the developers intention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants