1

I've been creating test for net6.0 and my currently issue is with the Program.cs or Startup.cs files.

Since I need 80%+ test coverage on each of my projects, a couple of them are stuck with less than that because the Program.cs file is not covered, but I also don't know how exactly to unit test it.

Since there is no business rules inside of the Program.cs because it is mostly app configurations,there is the option to include the [ExcludeFromCodeCoverage] attribute, but I don't think that's appropriate. (Is it?)

So, regarding this issue, what is the best solution?

Does anyone know any good material that explains this?

EDIT: The files contain mostly service configuration and other relevant configurations for the application during startup.

  • This is a pretty broad question, and you didn't provide a lot of details. Is there a lot of testable logic in your `Program.cs` and `Startup.cs` files? – Joe Sewell Aug 07 '23 at 17:46
  • The files contain mostly service configuration and other relevant configurations for the application during startup. – Bruno Braga Aug 07 '23 at 17:54
  • Unit testing is used to ensure stuff works, not to satisfy arbitrary metrics. `Program.cs` shouldn't contain a lot of testable logic, which means the better it's designed, the less its test coverage will be. – Panagiotis Kanavos Aug 07 '23 at 17:54
  • `mostly service configuration and other relevant configurations` how do you intend to test those? Testing configuration means creating an `IConfiguration` instance and checking whether it returns the expected values, how it handles missing settings or invalid values. Testing service configuration means trying to instantiate types. This is almost integration testing. If you put related code in separate methods, you can test the methods. Those methods can easily be moved to other files and become extension methods. – Panagiotis Kanavos Aug 07 '23 at 17:58
  • In ASP.NET Core you can use WebApplicationFactory to create a test server using the configuration in `Program.cs` but overriding as needed. This is mainly used for [integration testing](https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-7.0#basic-tests-with-the-default-webapplicationfactory) – Panagiotis Kanavos Aug 07 '23 at 17:59
  • I have done something similar a year or two ago. I moved all the content out of `program`and `startup` into classes of their own. Then I made the tests run these classes. The good part is that it separated logic from process related code; a keen observer will notice how we have managed to integrate disc and process and logic into one monolith. Depending on your code it might be a waste of time though as the tests might not test stuff, but more just run the code. See code [here](https://github.com/LosManos/LehmanLaidun/blob/master/Source/Tests/LehmanLaidun.Console.Unit.Test/ProgramTest.cs) – LosManos Aug 07 '23 at 18:27

0 Answers0