Example #1
0
            public TestContextLogger()
            {
                var prop = _ctx.GetType().GetProperty("Listener", BindingFlags.Instance | BindingFlags.NonPublic);

                Debug.Assert(prop != null);

                _listener = (ITestListener)prop.GetValue(_ctx);
            }
        /// <summary>
        /// Create the initial TestExecutionContext used to run tests
        /// </summary>
        /// <param name="listener">The ITestListener specified in the RunAsync call</param>
        private void CreateTestExecutionContext(ITestListener listener)
        {
            Context = new TestExecutionContext();

            // Apply package settings to the context
            if (Settings.ContainsKey(FrameworkPackageSettings.DefaultTimeout))
            {
                Context.TestCaseTimeout = (int)Settings[FrameworkPackageSettings.DefaultTimeout];
            }
            if (Settings.ContainsKey(FrameworkPackageSettings.StopOnError))
            {
                Context.StopOnError = (bool)Settings[FrameworkPackageSettings.StopOnError];
            }

            // Apply attributes to the context

            // Set the listener - overriding runners may replace this
            //Debugger.Launch();
            var prop = Context.GetType().GetProperty("Listener", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

            prop.SetValue(Context, listener);
            //Context.Listener = listener;
#if NETSTANDARD1_6
            Debugger.Launch();
            Context.Dispatcher = new MainThreadWorkItemDispatcher();
#else
#if PARALLEL
            Debugger.Launch();
            int levelOfParallelism = GetLevelOfParallelism();

            if (Settings.ContainsKey(FrameworkPackageSettings.RunOnMainThread) &&
                (bool)Settings[FrameworkPackageSettings.RunOnMainThread])
            {
                Context.Dispatcher = new MainThreadWorkItemDispatcher();
            }
            else if (levelOfParallelism > 0)
            {
                Context.Dispatcher = new ParallelWorkItemDispatcher(levelOfParallelism);
            }
            else
            {
                Context.Dispatcher = new SimpleWorkItemDispatcher();
            }
#else
            //this seems to be the key
            //Debugger.Launch();
            Context.Dispatcher = new SimpleWorkItemDispatcher();
            //Context.Dispatcher = new MainThreadWorkItemDispatcher();
#endif
#endif
        }
        public Task <TestResult> Run(NUnit.Framework.Internal.Test test)
        {
            return(Task.Run(() =>
            {
                TestExecutionContext current = TestExecutionContext.CurrentContext;
                current.WorkDirectory = Environment.CurrentDirectory;
                //current.Listener = this; // Internal on Android
                current.GetType().GetField("listener", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(current, this);
                current.TestObject = test is TestSuite ? null : Reflect.Construct((test as TestMethod).Method.ReflectedType, null);
                WorkItem wi = test.CreateWorkItem(TestFilter.Empty);

                wi.Execute(current);
                return wi.Result;
            }));
        }