2

Is there a way to execute all of the C# unit tests (xUnit) within the console application? So that the end user can see the results while running the program? Let's say that we have simple switch case menu like:

  1. Do something,
  2. Do something different
  3. Run unit tests.

So that when the input from user's keyboard equals 3 all of the unit tests from the project are executed and the result is printed?

Thanks in an advance for the help

The Robcio
  • 33
  • 4
  • Simply automate the testrunner for xunit? If this is a net core project you can call "dotnet test" with parameters/filters to do what you want. – Ralf Jun 03 '22 at 12:04
  • Is it possible to run the dotnet test command from the code level? All I want to do is basically to trigger all of the tests once user's input is 3 and print the results to the console for him – The Robcio Jun 03 '22 at 12:07
  • It's a net core project with it's related test project - dotnet test works when I type it into the terminal but I'd have to use it in code so that end user can see the results – The Robcio Jun 03 '22 at 12:09
  • Start the Test process via a `Process.Start` call for the test runner. For the result you may redirect the console output. Presumably its better to let dotnet test create a result file in a format of your choice and show that to the user. – Ralf Jun 03 '22 at 12:11
  • Sorry, but to make that clear - how to start the Test Process via Process.Start? I'm quite not sure how to call for the test runner – The Robcio Jun 03 '22 at 12:14

1 Answers1

1

Assuming that your tests live in a C# project called MyTest.csproj, you'd start them from the CLI via dotnet test MyTest.csproj.
If you want to do this from within your own application, you could use Process.Start like this:

if (chosenMenuItem == 3)
  Process.Start("dotnet", "test MyTest.csproj");

That will start the test run.

If you want to get the test output as well, you could follow this great answer: https://stackoverflow.com/a/4291965/4919526

mu88
  • 4,156
  • 1
  • 23
  • 47