testfx icon indicating copy to clipboard operation
testfx copied to clipboard

Add support for injecting `TestContext` in ctor

Open Evangelink opened this issue 1 year ago • 5 comments
trafficstars

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

Evangelink avatar Jan 03 '24 08:01 Evangelink

This would be a welcome addition.

avivanoff avatar Jan 24 '24 22:01 avivanoff

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.

Duranom avatar Jun 18 '24 23:06 Duranom

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.

Evangelink avatar Jun 19 '24 07:06 Evangelink

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.

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

Duranom avatar Jun 19 '24 08:06 Duranom

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 avatar Jun 19 '24 08:06 Evangelink

@Evangelink, is there a beta NuGet package to try it out?

avivanoff avatar Jul 16 '24 16:07 avivanoff

@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

Evangelink avatar Jul 16 '24 19:07 Evangelink

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.

avivanoff avatar Jul 22 '24 19:07 avivanoff

Thanks for letting us know @avivanoff

Evangelink avatar Jul 22 '24 19:07 Evangelink