Do It the Old Way
You can automate builds, run tests, and deploy packages from the command line. This requires a little work and is often the simplest way to get started. In the following exercises, we set up some projects in Visual Studio and create a batch file that builds the files, runs the tests, and provides visible feedback as to whether anything failed.
A batch file is just a text file that executes a set of commands from the command prompt. To run the batch file, you need to have the correct command prompt environment variables set up. The simplest way to ensure this is to run the Visual Studio Command Prompt from the Visual Studio Tools folder in the Start menu.
Exercise 7-1: Creating an Integration Build Batch File
In this exercise, we create a simple batch file that gets a solution from Visual Source-Safe, builds it, and runs the tests.
1.
Create a new blank solution called MySolution. I have created it in a directory on my C: drive called Work; you can create it where you want, but make sure you replace anywhere I have C:\Work\ with the directory you have used.
2.
Right-click the solution and add a new C# Library project called MyLibrary.
3.
Right-click the solution again and create a new Visual Basic Windows application called MyWindowsApplication.
4.
Right-click the solution again and select Add Solution to Source Control from the pop-up menu (see Figure 7-1). I am adding the solution to a database I have created for this exercise called MySolution; if you use another database, remember what it is called.
Figure 7-1. Add solution to source control.

5.
We are now ready to create a batch file to get the latest version of the source from the database and build the solution. The batch file I created is shown below, and yours should look something like it. I used my favorite batch file editor, Notepad, but you can use any text editor, including Visual Studio.
Remember to change the names of the directories depending on where you have created the solution and where your Visual SourceSafe database is. Notice how easy it is with the devenv command to build a Visual Studio Solution; we don't have to worry about the fact that it contains a C# project and a VB.NET project.
@echo off
rem: set the source safe database
set SSDIR=C:\Work\SSDB\MySolution
rem: set the working directory for the source safe database root project
ss Workfold $/ C:\Work
rem:change to the directory where all the projects work is
C:
cd Work
rem: get the latest version of the source files from source safe
ss get $/ -R -I
rem: check that it worked
if errorlevel 1 goto DisplayResult
rem: use the Visual studio cmd line to rebuild the entire solution
devenv /rebuild Debug "C:\Work\MySolution\MySolution.sln"
rem: check that it worked
if errorlevel 1 goto DisplayResult
:DisplayResult
if errorlevel 1 goto failed
echo Success!! :-)
goto end
:failed
echo FAILURE :-(
:end
6.
Save the file as Build.bat and don't forget to save the solution. Then run the batch file to make sure it compiles. Remember, this needs to run in a command prompt with the correct environment variables (such as paths to the Visual Studio folders) set correctly. As mentioned previously, Visual Studio provides a Visual Studio command prompt that has this set up already.
7.
We can now add some unit test code to the library and get the batch file to run the tests as well. Back in Visual Studio, check out the My Library Project using the right-click pop-up menu.
8.
Add a reference to the NUnit.Framework.Dll, as you learned in Chapter 4.
9.
Add a new class called LibraryTests to the Library project.
10.
In this class, we will create two tests, one that always passes and one that always fails, as shown in the following code.
using System;
using NUnit.Framework;
namespace MyLibrary
{
[TestFixture]
public class LibraryTests
{
[Test]
public void AlwaysPasses()
{
}
[Test]
public void AlwaysFails()
{
Assert.Fail("This test should fail");
}
}
}
11.
Make sure it compiles in Visual Studio, and then save the file and the project. Check it back into SourceSafe, and then run the batch file to make sure it still compiles.
12.
We can now add two lines to the batch file to run the tests.
@echo off
rem: set the source safe database
set SSDIR=C:\Work\SSDB\MySolution
rem: set the working directory for the source safe database root project
ss Workfold $/ C:\Work
rem:change to the directory where all the projects work is
d:
cd Work
rem: get the latest version of the source files from source safe
ss get $/ -R -I-
rem: check that it worked
if errorlevel 1 goto DisplayResult
rem: use the Visual studio cmd line to rebuild the entire solution
devenv /rebuild Debug "C:\Work\MySolution\MySolution.sln"
rem: check that it worked
if errorlevel 1 goto DisplayResult
rem: run the tests
nunit-console C:\Work\MySolution\MyLibrary\bin\Debug\MyLibrary.dll
rem: check that they passed
if errorlevel 1 goto DisplayResult
:DisplayResult
if errorlevel 1 goto failed
echo Success!! :-)
goto end
:failed
echo FAILURE :-(
:end
13.
Run the batch file again. It should run the tests and report a failure. You can play with commenting out the test that fails, and the tests should pass.
14.
You can use this batch file for an integration machine in an XP environment. As I mentioned earlier, I like to be able to see the state of a build on the integration machine from the other side of the room so that I know where we are. Therefore, I have added a couple of lines in the batch file to change the color of the screen based on the result (red for failed and green for succeeded).
@echo off
rem: set the source safe database
set SSDIR=C:\Work\SSDB\MySolution
rem: set the working directory for the source safe database root project
ss Workfold $/ C:\Work
rem:change to the directory where all the projects work is
d:
cd Work
rem: get the latest version of the source files from source safe
ss get $/ -R -I-
rem: check that it worked
if errorlevel 1 goto DisplayResult
rem: use the Visual studio cmd line to rebuild the entire solution
devenv /rebuild Debug "C:\Work\MySolution\MySolution.sln"
rem: check that it worked
if errorlevel 1 goto DisplayResult
rem: run the tests
nunit-console C:\Work\MySolution\MyLibrary\bin\Debug\MyLibrary.dll
rem: check that they passed
if errorlevel 1 goto DisplayResult
:DisplayResult
if errorlevel 1 goto failed
color A1
echo Success!! :-)
goto end
:failed
echo FAILURE :-(
color C1
:end
rem: for continuous builds the line below
call C:\work\Build.bat
I have also added a line at the bottom of the file to run the batch file again when it has finished. If I now set this up on an integration machine, it will continuously be getting the latest version from Visual SourceSafe, building it, and running the tests. This is useful because the integration machine can now give the development team feedback as to the state of the system. For this reason, I always recommend putting the integration machine somewhere highly visible. As soon as something breaks, the whole team can do something about getting it fixed.