Make Tests Run After Event

I am trying to make an addin which would make my tests run automagically.

First off, when the solution is loaded

    [SolutionComponent]
    public class ContinuousTestSolutionComponent
    {
        const string ALL_TESTS_SESSION_ID = "All Tests";

        public ContinuousTestSolutionComponent(
            ISolution solution,
            UnitTestProvidersManager unitTestProvidersManager,
            UnitTestManager unitTestManager,
            IUnitTestSessionManager unitTestSessionManager,
            IHostProvider provider)
        {
            if (unitTestSessionManager.CurrentSession == null)
            {
                var session = unitTestSessionManager.GetSession(ALL_TESTS_SESSION_ID);
                if (session == null)
                {
                    var providers = unitTestProvidersManager.GetEnabledProviders();
                    var unitTestElements = new List<IUnitTestElement>();
                    foreach (var p in providers)
                    {
                        p.ExploreSolution(solution, element => { unitTestElements.Add(element); });
                    }

                    session = unitTestSessionManager.CreateSession(unitTestElements, "All Tests");
                }
                session.RunAll(provider);
            }
        }
    }



The unitTestElements does not contain any tests (although there are some), so nothing is run

I also tried

    var unitTestElements = unitTestManager.GetElements(
                        solution.GetAllProjects().SelectMany(p=>p.GetSubItems()));


So I suspect its where/when I am calling that means I get nada ..

Ideally I would also like to run tests on events like change or save or build of any projects

Can you point me in the right direction?
1 comment
Comment actions Permalink
When do you want the tests to run?

Looking at the code, the first thing that strikes me is that you're trying to run tests from the constructor of your component. It would make more sense to run the tests in response to some kind of action, or event. While ReSharper ensures that any dependencies are created before a specific component is, component creation is largely non-deterministic - for example, previously saved unit test sessions might not be loaded by the time your component is created.

Secondly, ExploreSolution doesn't explore the whole solution hierarchy. Instead, it is intended to explore the items that live in the solution level folders, so this method won't find any tests, because it doesn't enter the projects, and doesn't look at source files. Instead, you would need to use IUnitTestFileExplorer.ExploreFile on each file in a project, or use IUnitTestMetadataExplorer.ExploreAssembly to explore a compiled assembly. However, both of these methods are really intended for internal use by ReSharper, because unit test elements are intended to be reused, which means the delegate passed in has certain responsibilities in keeping UnitTestElementManager up to date.

Depending on what you're looking to achieve, it might be best to try and invoke one of the ReSharper actions, such as run all tests, or run a previously saved session, rather than try to handle this added bookkeeping.

Thanks
Matt
0

Please sign in to leave a comment.