xunit / devices.xunit

xUnit.net Runners for Devices
Other
73 stars 36 forks source link

Ability to filter tests #68

Open pellet opened 6 years ago

pellet commented 6 years ago

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 commented 6 years ago

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);
            });
    }