using GalaSoft.MvvmLight; using System.Windows.Input; using System; using System.Windows; using GalaSoft.MvvmLight.CommandWpf; using Microsoft.Win32; using Xunit; using Xunit.Abstractions; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; namespace xunit.runner.wpf.ViewModel { public class MainViewModel : ViewModelBase { public MainViewModel() { ////if (IsInDesignMode) ////{ //// // Code runs in Blend --> create design time data. ////} ////else ////{ //// // Code runs "for real" ////} CommandBindings = CreateCommandBindings(); } public ICommand ExitCommand { get; } = new RelayCommand(OnExecuteExit); public CommandBindingCollection CommandBindings { get; } private CommandBindingCollection CreateCommandBindings() { var openBinding = new CommandBinding(ApplicationCommands.Open, OnExecuteOpen); CommandManager.RegisterClassCommandBinding(typeof(MainViewModel), openBinding); return new CommandBindingCollection { openBinding, }; } public ObservableCollection Assemblies { get; } = new ObservableCollection(); public ObservableCollection TestCases { get; } = new ObservableCollection(); private void OnExecuteOpen(object sender, ExecutedRoutedEventArgs e) { var fileDialog = new OpenFileDialog { DefaultExt = "dll", }; if (fileDialog.ShowDialog(Application.Current.MainWindow) != true) { return; } var fileName = fileDialog.FileName; try { var xunit = new XunitFrontController( useAppDomain: false, assemblyFileName: fileName, shadowCopy: false); var testDiscoveryVisitor = new TestDiscoveryVisitor(); xunit.Find(includeSourceInformation: false, messageSink: testDiscoveryVisitor, discoveryOptions: TestFrameworkOptions.ForDiscovery()); testDiscoveryVisitor.Finished.WaitOne(); Assemblies.Add(fileName); TestCases.AddRange(testDiscoveryVisitor.TestCases.Select(tc => tc.DisplayName)); } catch(Exception ex) { ex.ToString(); } } private class TestDiscoveryVisitor : TestMessageVisitor { public IList TestCases { get; } = new List(); public IDictionary> Traits { get; } = new Dictionary>(); protected override bool Visit(ITestCaseDiscoveryMessage testCaseDiscovered) { var testCase = testCaseDiscovered.TestCase; TestCases.Add(testCase); foreach (var k in testCase.Traits.Keys) { IList value; if (!Traits.TryGetValue(k, out value)) { value = new List(); Traits[k] = value; } value.AddRange(testCase.Traits[k]); } return true; } } private static void OnExecuteExit() { Application.Current.Shutdown(); } } }