Sharing my experience trying to override class construction/fixture construction #5825
Notorious-Coding
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
|
If you wanna re-use the same logic that
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello !
I struggled a lot with something, i found the solution, but i wanted to share my experience.
I don't know if it's relevant, feel free to ignore if not.
Context:
I'm building an integration test framework that have a integration for every TestFramework, including NUnit, MSTest, XUnit, and, of course, TUnit.
My framework define an
Environment(wich is in fact aFixture, orDataSource, depending on the nomenclature).An
Environmenthandle the lifecycle of a collection ofInfrastructures.For now,
Environmentconfigure aIServiceCollection, build aServiceProvider, and use it to injectInfrastructurewith their dependencies.Inside
Environment, i was usingIServiceProvideras a service locator. It works, but i don't like it.Thats why i wanted to move up the service provider and instantiate my
environmentwith it.Here's how it work :
IDependencyInjectionConfigurator(for example,MyDependencyConfigurator)[InjectionConfigurator(typeof(MyDependencyConfigurator))]to the test class.Fixture<Environment>class passed or built by the test class (depending on the framework)IServiceCollectionEnvironment.For reminder, i need to make an integration of that framework for every test framework, so i try to be as consistant as i can, for maintenance purpose.
That's why every test framework use a Fixture, even if i could do it differently (eg, MSTest could instantiate the environment directly, but i still use the Fixture class).
Every framework are using the same code.
The only thing that differs is the way to find the current test class type.
Here's where i struggled (the solution was easy but i missed it)
in XUnit, i passed (TestContext.Current.TestClass as XunitTestClass)?.Class
in NUnit, i passed GetType(), (since OneTimeSetup is a test class instance method.
in MSTest, i used Type.GetType(testContext.FullyQualifiedTestClassName) inside the static ClassInitialize method (decorated with [ClassInitialize(InheritanceBehavior.BeforeEachDerivedClass)]µ
And, i really struggled to find a way to do that in TUnit.
Here's some code, so you have a bit more context :
My problem was : How TUnitFixture could know wich testclass is currently tested.
Here's all the thing i tried to :
THEN, by looking trought the code, i found that TestBuilderContext.Context is available at this moment, and i could use it to get the class type.
(Yes, all of this, just to find out there a static property that do the job, and i find later that it was in the TestContext & Output docs, kinda mad but kinda happy that there is a simple way).
And then, i made my fixture, and everything work just fine :
Here's my thoughts about that :
That's it.
I don't know if it's relevant to make improvement to TUnit, maybe all of this is made on purpose and i just don't know why.
Just thought my experience with it could be interesting !
Thanks for your work, have a good day !
Beta Was this translation helpful? Give feedback.
All reactions