January 2, 2009
Create TFS Work Item when Test Run Fails
Visual Studio Team Foundation Server provides an SDK that is pretty easy to use when it comes to working with work items. The following is a simple sample script that creates a work item of type "bug". The script is launched when a test run fails in TestTrack. By having a TestTrack Trigger launch the script, we will have information from the failed test run available to us as environment variables. The script is an adaptation of a sample provided with the SDK that creates a new work item. I made the following changes:
Updated for TFS 2008 and TestTrack 2010
- Added a logger class
- Added a configuration class to read in values from an XML file.
- Substituted all of the Console.WriteLine statements to use the Logger.WriteLine method.
- Added variables for the TestTrack test run values and assigned values using the available environment variables.
- Set other work items field not set in the example
- Removed section that logged historical information about the work item.
About the scriptI created the script using Visual Studio 2008 and wrote it in C#. The updated version reads a configuration file, so there isn't any information hard coded (previous example did).
TFSWorkItemCreate DownloadsThese downloads are provided on an as-is basis. These are not supported by Seapine Software
Version 2.0Download:TFSWorkItemCreate_2_0.zipDate: April 5th, 2010 Comments: Only includes binary file with sample config file. Source code is not provided as all the information needed to connect to the TFS server is configurable.
Version 1.0Download:TFSWorkItemCreate.zipDate: January 2nd, 2009 Comments: Includes source code and compiled binaries. All of the information used to connect to the TFS server is hard coded, so changes to the source code is needed before using this.
Script flowThe flow of the script is as follows:
- Declares and initializes the variables that it will use to log information about the failed test run in the bug work item.
- Checks that the config file exists, if it does not, the script exits and logs a message.
- If the config file does exist, it reads the following information:
- TFS url
- TFS project
- TFS username
- TFS password
- The script uses this information to establish a connection to the TFS server.
- Makes sure that it has projects and tries to connect to the project.
- Creates a work item object.
- Sets the type to "bug".
- Sets the various fields.
Field mappingThe following information is gathered from the test run. This information is gathered using the TestTrack environment variables. For a complete list, please review the TestTrack User Guide.
- Test Run Number: Used in the work item title.
- Test Run Summary: Used in the work item title.
- TestRun Problem Statement: Used in the work item description.
- TestRun Steps: Used in the work item description.
- Test run set: Used in the work item description.
- TestRun lst modified by: Used in the work item description.
- ttstudio hyperlink: Added as a work item link of type hyperlink.
About the triggerThe trigger in TestTrack is very straight forward. Simply create a trigger that fires when a test run enters the failed state. For more information on how to create triggers, please review the TestTrack User Guide.
Configuration FileThe script will read in information used to connect to the TFS server from a configuration file called TFSConfig.xml. This file should be located in the same directory as the script. Following is a sample config file that is saving all connection information
<?xml version="1.0"?> <ROOT> <ServerAddress>http://tfssrv:8080</ServerAddress> <Project>WysiCorp</Project> <UserName>tfsuser</UserName> <Password>tfssetup</Password> <Domain></Domain> </ROOT>Couple of things to note:
- The server address is in entered in http:// format. This needs to be the case if the TestTrack server is running under local system account.
- No domain is provided in this sample. You may need to provide one if one is required to log in to the TFS server.
Use CaseTo test this, I used the sample project that comes with TestTrack TCM. I opened test run #128, whose summary is "About Dialog - Verify that the about dialog contains correct information". This test run already contains a problem statement on the steps tab. From the Activities menu, I selected Start, and then from the same menu I selected Fail. I applied the changes. I opened Visual Studio 2008 Team System edition and opened the Team Project hard coded in the script. Below are two screenshots of the work item created. Note that the Description: field did not correctly translate the carriage returns between each step.
- You may get the following error:
- At first this trigger did not work. I had the TestTrack server running as local admin and this user did not have the proper permissions to create a work item. To solve it, I changed the TestTrack server to run as an account that did have permissions to create a work item.
- Set the URL in http:// format. Simpy providing the machine name worked when I ran the script under my Windows account. When I ran the script as a trigger in TestTrack (which runs it under "local system account") it did not work until I set the TFS server as http://tfsserver:8080
EnhancementsThis is just an example so there are some enhancements:
- Map the test run set and/or test run number to the Details tab. This tab has fields about the test that lead to the finding the bug.
- Add the vstfs: link to the failed test run. Ideally this would be added to a custom field of type "hyperlink". This would allow you to open the work item from the test run in TestTrack. This would require the TestTrack SDK.
- Get the file attachments from the test run and attach them to the work item. This would require the TestTrack SDK.
- Enhance the script to return an exit code of not 0 if the work item is not created. This would prevent the user from failing the test run. This would enforce that test runs can only be failed if the corresponding work item is created.