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

Is there a global (process wide) setup or teardown available? #4106

Open
Netzeband opened this issue Apr 28, 2022 · 6 comments
Open

Is there a global (process wide) setup or teardown available? #4106

Netzeband opened this issue Apr 28, 2022 · 6 comments

Comments

@Netzeband
Copy link

Hello

I have a usecase, where an external library must be setup exactly once for the whole process and in the end it must be teardown just a single time. Therefore, I'm looking for a process wide setup/teardown mechanism. However, I just found the OneTimeSetUp and OneTimeTeatdown, which is done for every Assembly. This does not work in my case, because the library cannot be setup again after it has been shutdown.

Is there any solution for that available?

@manfred-brands
Copy link
Member

You can create a Setup fixture class.

@Netzeband
Copy link
Author

Thanks for the reply. As I wrote, I already tried out to use OneTimeSetUp and OneTimeTearDown. But it only works for the assembly and not for the whole test-process which runs several assemblies. Thus I would need to copy the SetUpFixture to different assemblies, which effectively will also setup/shutdown this library several times (for every assembly), which leads to issues.

@stevenaw
Copy link
Member

stevenaw commented May 3, 2022

Thanks @Netzeband
My first thought had also been SetupFixture though you are right: that is "global" only within a single assembly. I also don't know of a built-in feature which does what you're looking for but I think this could be a helpful to add it. @nunit/framework-team what do you think?

@Netzeband
Copy link
Author

Hello, does anyone has an idea?

@mikkelbu
Copy link
Member

I guess you'll have to code it yourself.

Depending on how you execute your tests then the different assemblies can be executed in parallel in different processes, so you'll need some some global lock (the OS, the filesystem, or a database) to ensure that only the first setup running - in a given assembly - will setup the external library.

If you also need to tear it down, then you'll need to keep track of the assemblies started, so only the last stopping assembly will tear it down.

I have the feeling that this problem will be hard to solve in the framework (alone).

@Netzeband
Copy link
Author

Netzeband commented May 20, 2022

The initialization is quite easy. Since my tests are not running in different processes, I just use a global flag to indicate, if the library was already initialized and prevent a double initialization.

The real problem is the tear down, which is necessary. At the moment, I just skip it, which means the library is throwing a lot of error messages when the process exits.
However, I cannot think about a solution which is out of the framework. Since only the framework knows when the last test has been finished. Outside of the framework there is (at the moment) no signal or call or anything to identify the shutdown of the process.

In the beginning, I was thinking, I could use the finanlizer of a static singleton instance to shutdown the library. However, I learned, that in C# finalizers of static instances are not called when the process exits. They are simply ignored. So it can only be handled on application level, which is for unit tests the framework.

When I look at the nunit.engine documentation, I think this can get very hard. There is not really an single entry and exit point for the test-runner. Only the Run(...) method of ITestRunner, but this could be called several times from the runner implementation (for example nunit3-console or ReSharperRunner. So actually, the only place would be the runner application, which is called. Which means every runner application which is used, needs it's own adaptions to setup and teardown this library.

Edit: Maybe an nunit.extension could be the solution? There is the ITestEventListener extension available, which triggers a call to OnTestEvent(...) with a string indicating the start and end of the test run. Or is the OnTestEvent(...) also executed per assembly and not per process?

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

4 participants