0

Is there a way to add a Program.cs to a Unit Test project (.Net 6.0)?

My goal would be to set a ServiceProvider that runs when the tests execution starts (where I would set my DI / connection strings...).

I know that I can set it via a static class, but this has some donwbsides for me. ServicesProvider / Dependency Injection in C# Unit test classes

I need to make sure that the ServiceProvider is built immediately, whatever tests sequence I run. Well, I could obviously set a method that initializes it and call it in all the Initialize methods of my test classes but that looks awkward to me.

A.D.
  • 1,062
  • 1
  • 13
  • 37
  • `Program.cs` is just a source file, it doesn't create a host or DI container automagically. In minimal APIs it's the Generic or Web host builders that create hosts and the DI container – Panagiotis Kanavos Jun 16 '22 at 14:43
  • `I can set it via a static class` do the same here then. There's absolutely no difference. You don't need a static class at all - in web apps, there are no static classes to begin with. When all controllers are generated through DI, there's no reason to look for static `IServiceProvider` instances. It's the service provider itself that creates everything – Panagiotis Kanavos Jun 16 '22 at 14:45
  • Yes I meant that I would like to add a file that is sure to be automatically run at startup in a Unit Test sequence. In this code, I would set the ServiceProvider with the necessary code with the host builders. This is achieved with a program.cs in web app but I don't know how to do it with a Test project. – A.D. Jun 16 '22 at 14:46
  • The answer remains the same. Create either the host or service provider in your unit test class. How you do that depends on your unit testing library. In XUnit you can do that in the class constructor or static constructor, depending on how long you want the container (and its services) to live. Or you can use a test fixture – Panagiotis Kanavos Jun 16 '22 at 14:48
  • Well, it would work I suppose. Making ALL my test classes from each Unit Test project inherit from the same super class (one super class per project). It would solve the dependency problem described into the link. I will most likly do that if no better suggestion. Thx very much for your time :) – A.D. Jun 17 '22 at 06:45
  • A base class for all unit tests isn't the same as fixtures, although both are common solutions. You can add multiple fixtures to the same unit test class. Or you can create a static helper (factory) method that gives you a ready-made host which you can use in every test as needed. There's nothing wrong with creating a new `host` instance in every test method. You still haven't tagged the testing framework you use though – Panagiotis Kanavos Jun 17 '22 at 06:54
  • I use Microsoft.VisualStudio.TestTools.UnitTesting. My understanding is that Fixture is a XUnit concept, this is why I suggested using the superclass ctor to adapt the XUnit post that you linked to my MS UnitTesting environment? – A.D. Jun 17 '22 at 07:07

0 Answers0