devices.xunit icon indicating copy to clipboard operation
devices.xunit copied to clipboard

Ability to filter tests

Open pellet opened this issue 6 years ago • 1 comments

The functionality to filter tests with a string the same as you would with the xunit ''filter" parameter, or basic string matching would be great for doing TDD on platform specific code and for kicking off particular suites of tests without needing to separate each suite into a new test assembly. One way to implement this could be to expose a "RunnerOptions.Filter" property which if set will populate the SearchQuery entry. If the "RunnerOptions.AutoStart" is set to true it will execute the RunFilteredTestsCommand button rather than the RunAllTestsCommand button.

pellet avatar Jun 28 '18 03:06 pellet

Here's a hack I used to get it working using reflection, I just called this method from the MainActivity.OnCreate method.

    /// <summary>
    /// Kick off your test/tests by using a filter.
    /// </summary>
    /// <param name="filter"></param>
    public void AutoStartFilteredTests(string filter)
    {
        var navigationPage = Xamarin.Forms.Application.Current.MainPage as NavigationPage;
        var homeView = navigationPage.RootPage;
        var homeViewModel = homeView.BindingContext as HomeViewModel;
        Observable
            .FromEventPattern<EventArgs>(homeViewModel, nameof(homeViewModel.ScanComplete))
            .Subscribe(_ =>
            {
                var testAssemblyViewModel = homeViewModel.TestAssemblies.First();
                testAssemblyViewModel.SearchQuery = filter;
                
                var filteredTests = testAssemblyViewModel
                    .GetNonPublicFieldValue<INotifyCollectionChanged>("filteredTests");
                
                Observable
                    .FromEventPattern<NotifyCollectionChangedEventArgs>(filteredTests, nameof(filteredTests.CollectionChanged))
                    .Where(__ => testAssemblyViewModel.RunFilteredTestsCommand.CanExecute(null))
                    .Do(__ => testAssemblyViewModel.RunFilteredTestsCommand.Execute(null))
                    .Subscribe();
                
                //TODO:after test runs
                //System.Environment.Exit(0);
            });
    }

pellet avatar Jul 19 '18 02:07 pellet