1

What I'm trying to achieve: I want to use a web application to run all the tests on my local windows PC.

I am using the following command:

c:/code/myRepo && dotnet test

I have tried the following command through run:

cmd.exe /C "c:/code/myRepo && dotnet test"

which runs successfully and all my tests pass.

I now want to run this from my .net Core application as so:

public static string RunCommand( string cmd)
{
    var escapedArgs = cmd.Replace("\"", "\\\"");

    var process = new Process()
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "cmd.exe",
            Arguments = $"/C \"{escapedArgs}\"",
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = false,
        }
    };
    process.Start();
    string result = process.StandardOutput.ReadToEnd();
    process.WaitForExit();
    return result;
}

When I run the tests from here the tests run, but they all fail.

Question: there is something different between these 2 environments but I have no idea what, how /what can I check what might be different. I've tried the command:

set

and can't see anything obviously affecting the environment

demo
  • 6,038
  • 19
  • 75
  • 149
Mr Giggles
  • 2,483
  • 3
  • 22
  • 35
  • You don't need `cmd.exe` to execute a command line application. Just run the application. `cmd.exe /C` starts *another* console with whatever arguments you passed to it. What you're trying to do will start one console that starts another console that finally runs `dotnet` with the arguments you provided. Just start `dotnet` instead – Panagiotis Kanavos Apr 12 '19 at 11:47
  • As [this probably duplicate question shows](https://stackoverflow.com/questions/47326217/running-a-net-core-app-dll-with-cmd-by-using-process-start), all you need to do is call `dotnet` as the application and pass the option or path to the dll. Just make sure it's run on the correct working directory. You could even use `Process.Start("dotnet","test")` if your program is in the correct working directory – Panagiotis Kanavos Apr 12 '19 at 11:49
  • thanks @PanagiotisKanavos. The program i am running is in a different scope to the tests. My application is programmatically running the tests from many other solutions that are not linked – Mr Giggles Apr 12 '19 at 12:44
  • That sounds like an XY problem - how to test multiple projects. As for the current question, you still don't need `cmd`. Either add the path to the project file as the last of the arguments, or set the `WorkingDirectory` in `ProcessStartInfo` to the folder where the project file is – Panagiotis Kanavos Apr 12 '19 at 12:47
  • As for the *actual* problem, you could use a build script eg written in [FAKE](https://fake.build/) or [CAKE](https://cakebuild.net/) to find and run all tests. You could even use a Powershell or CMD script to just find all `*.csproj` files under a root folder and pass them as arguments to `dotnet run`. – Panagiotis Kanavos Apr 12 '19 at 12:51
  • [Check this as well](https://github.com/dotnet/cli/issues/6572#issuecomment-300921019). I'm not sure if that still works, but you may be able to crete a `proj` file that finds all csproj files and runs test on them – Panagiotis Kanavos Apr 12 '19 at 12:52

0 Answers0