A push button build script that does exactly one thing isn’t that useful - we could achieve as much by building from within Visual Studio itself. The benefits of build automation begin to accrue when we start orchestrating multiple steps. Let’s extend the script to automatically run all our unit tests.
Using NuGet, install the xUnit console we need:
As we’ve already seen, we need to work out how to do something by hand before we can automate it. Looking through the download folder for the NuGet package, we find the application and work out the required command line:
You’ll observe that the required assembly is found underneath the
build output directory - I’ve explicitly configured the output folder for each project:
Unfortunately, this is a bit tedious because it has to be done independently for each configuration of each project.
As we did for msbuild, let’s write a task that finds the console when we need it:
resolve-path step uses a wildcard for the version number of the package so that it will automatically find whichever version is currently installed. This contributes to the robustness of our build - we don’t want it breaking next time we upgrade the xUnit NuGet packages.
Now we can write a task that runs the unit tests:
-Depends option on this task specifies that we need to both compile everything and find the test runner before we can run the tests.
By including the
-html option, we get an HTML report that summarizes the results of the test run; this can be a useful artifact to have available from your continuous integration server, easier to read than the raw build output.
From our PowerShell window, we can now run the entire build sequence with one single command:
Try this for yourself and you’ll see that Psake runs all of the following build steps:
I hope the power of scripting the build process is starting to become evident. With our build script capturing knowledge about the required steps and their dependencies, running the build is becoming very simple.