I’m using C# 4.0.
I’m running a unit test using Microsoft.VisualStudio.TestTools.UnitTesting.
My UnitTest project tests another project, say ProjectA. ProjectA creates a process like this:
Process mplex = new Process();
psi = new ProcessStartInfo();
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.FileName = "cmd.exe";
psi.Arguments = "/C mplex omittedForBrevity";
mplex.StartInfo = psi;
mplex.Start();
/* This is telling me: "'mplex' is not recognized
* as an internal or external command, operable
* program or batch file." */
var a = mplex.StandardError.ReadToEnd();
I’ve placed “MPLex.exe” inside various bin’s, including the “AppDomain.CurrentDomain.BaseDirectory” (which is the \bin\Debug\ folder for my UnitTest project).
MPLex.exe is basically Microsoft’s derivative of GPLEX, which is a C# version of Flex/Lex. When I add a reference to MPLex.exe it in ProjectA and view it in the Object browser I don’t see any methods that look like Main, or public ways of interfacing with it, otherwise I would do that instead of calling it via cmd as I’ve done above.
I think what’s going on is when it’s being unit tested, it’s not searching the \bin\Debug\ directory. When I run the same code in ProjectA from a Main method in a non-unit-test ProjectB, it works fine. If I could somehow strongly reference the .exe so that it’d get copied everywhere it’s needed, then I think my issue would be solved. How can I do this?
EDIT:
Adding this fixed it, but for all I know I may have just broke something else that hasn’t made itself known, yet.
System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
mstest does not run your unit tests in place out of bin. It copies the tests and referenced assemblies to a test deployment folder, typically under TestResults in the solution directory. I’ve “unit” tested similar components and always need to use the deployment features, which are a headache to use for something so simple, but here goes.
First, your testsettings must have deployment enabled. Go to the testsettings file and select the deployment tab and click enable deployment. Depending on how your solution is structured, you can add the mplex file here. In our project, I don’t do that because the necessary files are often outside the source control tree for the tests and VS will complain, if this is the case use the file links I describe below.
You can also add a DeploymentItem attribute to specific tests or test classes and the file will only be copied if those tests are run. This is helpful if it is a large data file that you don’t want copied all the time.
If the necessary files are outside the test solution, add the necessary file(s) as links in the test project and set the build action to copy always. Then either add to the deployed files in the testsettings or use the DeploymentItem attribute.