TFS API Part 56 – Test Configurations

02/02/2015

tags: , , , ,
no comments

This post will show to manipulate MTM configurations and variables using TFS API, this post came after a customer raise a demand to add hundreds of configurations to MTM from external system.

image

Download Demo Project

I’ve created WPF Application and added the following assemblies into my project:

  • Microsoft.TeamFoundation.Client
  • Microsoft.TeamFoundation.Common
  • Microsoft.TeamFoundation.TestManagement.Client.dll
  • Microsoft.TeamFoundation.TestManagement.Common.dll

Step 1: Connect TFS

private void Button_Click(object sender, RoutedEventArgs e) { TeamProjectPicker tpp = new TeamProjectPicker(TeamProjectPickerMode.SingleProject, false); if (tpp.ShowDialog() != System.Windows.Forms.DialogResult.OK) return; Tfs = tpp.SelectedTeamProjectCollection; Grp.IsEnabled = true; ITestManagementService service = (ITestManagementService)Tfs.GetService(typeof(ITestManagementService)); TestProject = (ITestManagementTeamProject)service.GetTeamProject(tpp.SelectedProjects[0].Name); GetAllConfigurationAndVariables(); }

Step 2: Get Configurations and Variables

 

private async void _GetAllConfiguration() { await Task.Run(() => { configurations = TestProject.TestConfigurations.Query("Select * from TestConfiguration"); Execute ex = delegate() { ConfigurationList.ItemsSource = configurations; }; this.Dispatcher.Invoke(ex); }); // public interface ITestConfiguration : IIdentifiable<int>, INotifyPropertyChanged //{ // string AreaPath { get; set; } // string Description { get; set; } // bool IsDefault { get; set; } // DateTime LastUpdated { get; } // TeamFoundationIdentity LastUpdatedBy { get; } // string LastUpdatedByName { get; } // string Name { get; set; } // int Revision { get; } // TestConfigurationState State { get; set; } // IDictionary<string, string> Values { get; } // void Delete(); // void Refresh(); // void Save(); // void Save(bool updateInUse); //} } private async Task _GetAllVariables() { await Task.Run(() => { variables = TestProject.TestVariables.Query(); Execute ex = delegate() { variablesList.ItemsSource = variables; }; this.Dispatcher.Invoke(ex); }); //public interface ITestVariable : IIdentifiable<int>, INotifyPropertyChanged //{ // ObservableCollection<ITestVariableValue> AllowedValues { get; } // string Description { get; set; } // string Name { get; set; } // int Revision { get; } // void Delete(); // void Refresh(); // void Save(); //} }

Step 3: Delete Variable and delete variable value

private void btnDelVariableValue_Click(object sender, RoutedEventArgs e) { if (variablesList.SelectedItem == null || variableValues.SelectedItem == null) return; ITestVariable tv = (ITestVariable)variablesList.SelectedItem; ITestVariableValue tvv = (ITestVariableValue)variableValues.SelectedItem; if ( MessageBox.Show( string.Format("Are you sure you want to delete {1} value from {0}?", tv.Name, tvv.Value), "Delete Test Variable Value", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { ITestVariableValue value = tv.AllowedValues.First(v => v.Value.Equals(tvv.Value)); tv.AllowedValues.Remove(value); tv.Save(); } } private async void btnDelVariable_Click(object sender, RoutedEventArgs e) { if (variablesList.SelectedItem == null) return; ITestVariable tv = (ITestVariable)variablesList.SelectedItem; if (MessageBox.Show(string.Format("Are you sure you want to TestVariable {0}?", tv.Name), "Delete Test Variable", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { ShowLoading(true); tv.Delete(); await _GetAllVariables(); ShowLoading(false); } }

Step 4: Add New Variable Value

private async void btnAddVariableValue_Click(object sender, RoutedEventArgs e) { if (variablesList.SelectedItem == null || string.IsNullOrEmpty(txtVariableValue.Text)) return; ShowLoading(true); ITestVariable tv = (ITestVariable)variablesList.SelectedItem; string value = txtVariableValue.Text; await Task.Run(() => { ITestVariableValue newValue = TestProject.TestVariables.CreateVariableValue(value); Execute ex = delegate() { tv.AllowedValues.Add(newValue); }; this.Dispatcher.Invoke(ex); tv.Save(); }); await _GetAllVariables(); txtVariableValue.Text = string.Empty; ShowLoading(false); }

Step 5: Change Configuration Variable

private void configVariablesList_SelectionChanged(object sender, SelectionChangedEventArgs e) { configVariableValues.Items.Clear(); if (configVariablesList.SelectedItem == null || ConfigurationList.SelectedItem == null) return; ITestConfiguration config = (ITestConfiguration)ConfigurationList.SelectedItem; KeyValuePair<string, string> pair = (KeyValuePair<string, string>)configVariablesList.SelectedItem; foreach (var allowVal in variables.First(k => k.Name.Equals(pair.Key)).AllowedValues) { configVariableValues.Items.Add(allowVal.Value); } configVariableValues.SelectedItem = pair.Value; } private void btnSaveConfiguration_Click(object sender, RoutedEventArgs e) { if (configVariablesList.SelectedItem == null || ConfigurationList.SelectedItem == null) return; ITestConfiguration config = (ITestConfiguration)ConfigurationList.SelectedItem; KeyValuePair<string, string> pair = (KeyValuePair<string, string>)configVariablesList.SelectedItem; config.Values[pair.Key] = configVariableValues.SelectedItem.ToString(); config.Save(true); _GetAllConfiguration(); }

Download Demo Project

Add comment
facebook linkedin twitter email

Leave a Reply