Executing In A Unit Test

There may be times when you want to call Wyam from a unit test (or other bit of code) without actually embedding it. Here's an example NUnit unit test that does this (though the technique should work for any test framework):

public void ExecuteExample()
    string rootPath = "[your root path here]";

    // Note that you may need to use a different exe path depending 
    // on where you got Wyam from (I.e., from the tools package)
    string wyamPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "Wyam.dll");

    string arguments = "--preview"; // or whatever as appropriate

    Process process = new Process();
    process.StartInfo.FileName = "dotnet";
    process.StartInfo.Arguments = $"{wyamPath} {rootPath} {arguments}";
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.OutputDataReceived += (s, e) => TestContext.Out.WriteLine(e.Data);
    process.ErrorDataReceived += (s, e) => TestContext.Out.WriteLine(e.Data);
    Assert.AreEqual(0, process.ExitCode);