testfx
testfx copied to clipboard
Add support for injecting `TestContext` in ctor
Summary
Allow to inject TestContext object in ctor of the test class as alternate design.
Background and Motivation
To be able to access the TestContext object, we currently need to declare a property of type TestContext which gets injected by MSTest. When enabling nullable on the project, we start getting many warnings about the fact this property could be null because it's not assigned any non-null value in all path. Although this is a FP, there is no way for the compiler to know that this will be injected at runtime.
A better pattern for this would be to be able to retrieve the TestContext object from the ctor and have the property readonly (or have a field) which would eliminate this nullability issue.
This is also preventing to change the TestContext value as it should not be set by user.
Proposed Feature
The user should be able to have a single parameter of type TestContext in the ctor of his/her test class.
Alternative Designs
Keep current design and either:
-
assign
null!(e.g.public TestContext TestContext { get; set; } = null!) -
disable nullable for the tests
This would be a welcome addition.
Really want this, it is very annoying you either have to use ClassInitialize or the property to have it added. The constructor to test and then dispose(async) is really a nicer preference as it keeps also in line of how the rest of the code base is, instead of strange hooks. The whole current approach feels so out of place after switching to MSTest.
Would even be a prelude for dependency injection for unit tests, where you at the class level you define a class/container through generic attribute which will register services and those are then used for injection to ctor/method, but that is getting ahead of a desire to reduce common stub/fakes/mocks retrieval.
Really want this, it is very annoying you either have to use ClassInitialize or the property to have it added.
Please don't store the TestContext object from the ClassInitialize as it's not updated with instance test info. We have added an analyzer for that.
Really want this, it is very annoying you either have to use ClassInitialize or the property to have it added.
Please don't store the
TestContextobject from theClassInitializeas it's not updated with instance test info. We have added an analyzer for that.
Apologies on that part then of my statement, but the out of place feeling remains cause when need for the TestContext you will need initialization code in a TestInitializeAttributed method and the constructor, or go with a TestInitializeAttributed method only and none of the fields decorated with readonly or NRT defined (or rather heavily = null! polluted).
No worries :) Yes, I totally agree that the property usage is causing many issues with proper NRT and that's actually what triggered this ticket. I am planning this for our next iteration.
@Evangelink, is there a beta NuGet package to try it out?
@Evangelink, is there a beta NuGet package to try it out?
You can test it out using our AzDO feed and use any version greater or equal to 3.6.0-preview.24366.13
You can test it out using our AzDO feed and use any version greater or equal to
3.6.0-preview.24366.13
Tested in a few of our internal projects, no issues found.
Thanks for letting us know @avivanoff