Helper class used to save and restore certain static or singleton settings in the environment that affect tests or which might be changed by the user tests. An internal class is used to hold settings and a stack of these objects is pushed and popped as Save and Restore are called.
Inheritance: System.MarshalByRefObject, ILogicalThreadAffinative
Ejemplo n.º 1
0
            public override TestResult Execute(TestExecutionContext context)
            {
                Type caughtType = null;

                try
                {
                    innerCommand.Execute(context);
                }
                catch (Exception ex)
                {
                    if (ex is NUnitException)
                        ex = ex.InnerException;
                    caughtType = ex.GetType();
                }

                if (caughtType == _expectedType)
                    context.CurrentResult.SetResult(ResultState.Success);
                else if (caughtType != null)
                    context.CurrentResult.SetResult(ResultState.Failure,
                        string.Format("Expected {0} but got {1}", _expectedType.Name, caughtType.Name));
                else
                    context.CurrentResult.SetResult(ResultState.Failure,
                        string.Format("Expected {0} but no exception was thrown", _expectedType.Name));

                return context.CurrentResult;
            }
Ejemplo n.º 2
0
            public override TestResult Execute(TestExecutionContext context)
            {
                string setCulture = (string)Test.Properties.Get(PropertyNames.SetCulture);
                if (setCulture != null)
                    context.CurrentCulture = new System.Globalization.CultureInfo(setCulture);

                return innerCommand.Execute(context);
            }
		public void Initialize()
		{
            setupContext = new TestExecutionContext(TestExecutionContext.CurrentContext);
#if !NETCF
            currentCulture = CultureInfo.CurrentCulture;
            currentUICulture = CultureInfo.CurrentUICulture;
			currentDirectory = Environment.CurrentDirectory;
            currentPrincipal = Thread.CurrentPrincipal;
#endif
		}
Ejemplo n.º 4
0
        public static TestResult RunTestFixture(Type type)
        {
            TestSuite suite = MakeFixture(type);
            
            TestExecutionContext context = new TestExecutionContext();
            context.TestObject = null;

            CompositeWorkItem work = new CompositeWorkItem(suite, TestFilter.Empty);
            return ExecuteAndWaitForResult(work, context);
        }
Ejemplo n.º 5
0
        public void Initialize()
        {
            setupContext = new TestExecutionContext(TestExecutionContext.CurrentContext);
#if !NETCF && !PORTABLE
            originalCulture = CultureInfo.CurrentCulture;
            originalUICulture = CultureInfo.CurrentUICulture;
#endif

#if !NETCF && !SILVERLIGHT && !PORTABLE
            originalDirectory = Environment.CurrentDirectory;
            originalPrincipal = Thread.CurrentPrincipal;
#endif
        }
			public override TestResult Execute(TestExecutionContext context)
			{
				Type      caughtType = null;
				Exception exception = null;

				try
				{
					innerCommand.Execute(context);
				}
				catch (Exception ex)
				{
					exception = ex;

					if (exception is NUnitException)
						exception = ex.InnerException;

					caughtType = exception.GetType();
				}

				if (caughtType == _expectedType)
				{
					if (_expectedMessage == null || _expectedMessage == exception.Message)
						context.CurrentResult.SetResult(ResultState.Success);
					else
						context.CurrentResult.SetResult(ResultState.Failure,
							"Expected {0} but got {1}".Args(_expectedMessage, exception.Message));

				}
				else if (caughtType != null)
				{
					context.CurrentResult.SetResult(ResultState.Failure,
						"Expected {0} but got {1}".Args(_expectedType.Name, caughtType.Name));
				}
				else
				{
					context.CurrentResult.SetResult(ResultState.Failure,
						"Expected {0} but no exception was thrown".Args(_expectedType.Name));
				}

				return context.CurrentResult;
			}
Ejemplo n.º 7
0
 internal static void SetCurrentContext(TestExecutionContext ec)
 {
     current = ec;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="TestExecutionContext"/> class.
        /// </summary>
        /// <param name="other">An existing instance of TestExecutionContext.</param>
		public TestExecutionContext( TestExecutionContext other )
		{
			this.prior = other;

            this.currentTest = other.currentTest;
            this.currentResult = other.currentResult;
            this.testObject = other.testObject;
			this.workDirectory = other.workDirectory;
            this.listener = other.listener;
            this.stopOnError = other.stopOnError;
            this.testCaseTimeout = other.testCaseTimeout;

#if !NETCF
            this.currentCulture = CultureInfo.CurrentCulture;
            this.currentUICulture = CultureInfo.CurrentUICulture;
#endif

#if !NETCF && !SILVERLIGHT
			this.outWriter = other.outWriter;
			this.errorWriter = other.errorWriter;
            this.traceWriter = other.traceWriter;
            this.tracing = other.tracing;
#endif

#if !NUNITLITE
			this.logging = other.logging;
			this.currentDirectory = Environment.CurrentDirectory;
            this.logCapture = other.logCapture;
            this.currentPrincipal = Thread.CurrentPrincipal;
#endif
        }
Ejemplo n.º 9
0
 public void OneTimeSetUp()
 {
     _fixtureContext = TestExecutionContext.CurrentContext;
     _fixtureResult  = _fixtureContext.CurrentResult.ResultState;
 }
Ejemplo n.º 10
0
Archivo: Fakes.cs Proyecto: alfeg/nunit
 public static FakeWorkItem GetWorkItem(Test test, TestExecutionContext context)
 {
     return new FakeWorkItem(test, context);
 }
Ejemplo n.º 11
0
        public void ExecutionStatusIsPromulgatedAcrossBranches()
        {
            var topContext = new TestExecutionContext();
            var leftContext = new TestExecutionContext(new TestExecutionContext(new TestExecutionContext(topContext)));
            var rightContext = new TestExecutionContext(new TestExecutionContext(new TestExecutionContext(topContext)));

            leftContext.ExecutionStatus = TestExecutionStatus.StopRequested;

            Assert.That(rightContext.ExecutionStatus, Is.EqualTo(TestExecutionStatus.StopRequested));
        }
Ejemplo n.º 12
0
        public void ExecutionStatusIsPushedToHigherContext()
        {
            var topContext = new TestExecutionContext();
            var bottomContext = new TestExecutionContext(new TestExecutionContext(new TestExecutionContext(topContext)));

            bottomContext.ExecutionStatus = TestExecutionStatus.StopRequested;

            Assert.That(topContext.ExecutionStatus, Is.EqualTo(TestExecutionStatus.StopRequested));
        }
Ejemplo n.º 13
0
        // This method can't currently be used. It would be more efficient
        // to run test cases using the command directly, but that would
        // cause errors in tests that have a timeout or that require a
        // separate thread or a specific apartment. Those features are
        // handled at the level of the WorkItem in the current build.
        // Therefore, we run all tests, both test cases and fixtures,
        // by creating a WorkItem and executing it. See the RunTest
        // method below.

        //public static ITestResult RunTestMethod(TestMethod testMethod, object fixture)
        //{
        //    TestExecutionContext context = new TestExecutionContext();
        //    context.CurrentTest = testMethod;
        //    context.CurrentResult = testMethod.MakeTestResult();
        //    context.TestObject = fixture;

        //    TestCommand command = testMethod.MakeTestCommand();

        //    return command.Execute(context);
        //}

        //public static ITestResult RunTest(Test test)
        //{
        //    return RunTest(test, null);
        //}

        public static ITestResult RunTest(Test test, object testObject)
        {
            TestExecutionContext context = new TestExecutionContext();
            context.TestObject = testObject;

            WorkItem work = WorkItem.CreateWorkItem(test, TestFilter.Empty);
            work.InitializeContext(context);
            work.Execute();

            // TODO: Replace with an event - but not while method is static
            while (work.State != WorkItemState.Complete)
            {
#if PORTABLE
                System.Threading.Tasks.Task.Delay(1);
#else
                Thread.Sleep(1);
#endif
            }

            return work.Result;
        }
Ejemplo n.º 14
0
 public void OneTimeSetUp()
 {
     fixtureContext = TestExecutionContext.CurrentContext;
 }
        /// <summary>
        /// Run selected tests and return a test result. The test is run synchronously,
        /// and the listener interface is notified as it progresses.
        /// </summary>
        /// <param name="listener">Interface to receive EventListener notifications.</param>
        /// <param name="filter">A test filter used to select tests to be run</param>
        /// <returns></returns>
        public ITestResult Run(ITestListener listener, ITestFilter filter)
        {
            log.Info("Running tests");
            if (_loadedTest == null)
            {
                throw new InvalidOperationException("Run was called but no test has been loaded.");
            }

            // Save Console.Out and Error for later restoration
            TextWriter savedOut = Console.Out;
            TextWriter savedErr = Console.Error;

            TestExecutionContext initialContext = CreateTestExecutionContext(_settings);

#if NUNITLITE
            initialContext.Listener = listener;

            WorkItem workItem = WorkItem.CreateWorkItem(_loadedTest, initialContext, filter);
            workItem.Completed += new EventHandler(OnRunCompleted);
            workItem.Execute();

            _runComplete.WaitOne();

            return(workItem.Result);
#else
            QueuingEventListener queue = new QueuingEventListener();

            if (_settings.Contains("CaptureStandardOutput"))
            {
                initialContext.Out = new EventListenerTextWriter(queue, TestOutputType.Out);
            }
            if (_settings.Contains("CapureStandardError"))
            {
                initialContext.Error = new EventListenerTextWriter(queue, TestOutputType.Error);
            }

            initialContext.Listener = queue;

            int levelOfParallelization = _settings.Contains("NumberOfTestWorkers")
                ? (int)_settings["NumberOfTestWorkers"]
                : _loadedTest.Properties.ContainsKey(PropertyNames.LevelOfParallelization)
                    ? (int)_loadedTest.Properties.Get(PropertyNames.LevelOfParallelization)
                    : Math.Max(Environment.ProcessorCount, 2);

            WorkItemDispatcher dispatcher = null;

            if (levelOfParallelization > 0)
            {
                dispatcher = new WorkItemDispatcher(levelOfParallelization);
                initialContext.Dispatcher = dispatcher;
                // Assembly does not have IApplyToContext attributes applied
                // when the test is built, so  we do it here.
                // TODO: Generalize this
                if (_loadedTest.Properties.ContainsKey(PropertyNames.ParallelScope))
                {
                    initialContext.ParallelScope =
                        (ParallelScope)_loadedTest.Properties.Get(PropertyNames.ParallelScope) & ~ParallelScope.Self;
                }
            }

            WorkItem workItem = WorkItem.CreateWorkItem(_loadedTest, initialContext, filter);
            workItem.Completed += new EventHandler(OnRunCompleted);

            using (EventPump pump = new EventPump(listener, queue.Events))
            {
                pump.Start();

                if (dispatcher != null)
                {
                    dispatcher.Dispatch(workItem);
                    dispatcher.Start();
                }
                else
                {
                    workItem.Execute();
                }

                _runComplete.WaitOne();
            }

            Console.SetOut(savedOut);
            Console.SetError(savedErr);

            if (dispatcher != null)
            {
                dispatcher.Stop();
                dispatcher = null;
            }

            return(workItem.Result);
#endif
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Clear the current context. This is provided to
 /// prevent "leakage" of the CallContext containing
 /// the current context back to any runners.
 /// </summary>
 public static void ClearCurrentContext()
 {
     CurrentContext = null;
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Save the original current TestExecutionContext and
 /// make a new isolated context current.
 /// </summary>
 public IsolatedContext()
 {
     _originalContext = CurrentContext;
     CurrentContext   = _originalContext.CreateIsolatedContext();
 }
Ejemplo n.º 18
0
        public void SetAndRestoreValueFormatter()
        {
            var context = new TestExecutionContext(setupContext);
            var originalFormatter = context.CurrentValueFormatter;

            try
            {
                ValueFormatter f = val => "dummy";
                context.AddFormatter(next => f);
                Assert.That(context.CurrentValueFormatter, Is.EqualTo(f));

                context.EstablishExecutionEnvironment();
                Assert.That(MsgUtils.FormatValue(123), Is.EqualTo("dummy"));
            }
            finally
            {
                setupContext.EstablishExecutionEnvironment();
            }

            Assert.That(TestExecutionContext.CurrentContext.CurrentValueFormatter, Is.EqualTo(originalFormatter));
            Assert.That(MsgUtils.FormatValue(123), Is.EqualTo("123"));
        }
Ejemplo n.º 19
0
 /// <summary>
 /// Construct a TestContext for an ExecutionContext
 /// </summary>
 /// <param name="ec">The ExecutionContext to adapt</param>
 public TestContext(TestExecutionContext ec)
 {
     this.ec = ec;
 }
Ejemplo n.º 20
0
 public void SingleThreadedIsInherited()
 {
     var parent = new TestExecutionContext();
     parent.IsSingleThreaded = true;
     Assert.True(new TestExecutionContext(parent).IsSingleThreaded);
 }
Ejemplo n.º 21
0
 public void SetUp()
 {
     _context = new TestExecutionContext();
 }
Ejemplo n.º 22
0
        public void ExecutionStatusIsPulledFromHigherContext()
        {
            var topContext = new TestExecutionContext();
            var bottomContext = new TestExecutionContext(new TestExecutionContext(new TestExecutionContext(topContext)));

            topContext.ExecutionStatus = TestExecutionStatus.AbortRequested;

            Assert.That(bottomContext.ExecutionStatus, Is.EqualTo(TestExecutionStatus.AbortRequested));
        }
Ejemplo n.º 23
0
Archivo: Fakes.cs Proyecto: alfeg/nunit
 public static FakeWorkItem GetWorkItem(object obj, string name, TestExecutionContext context)
 {
     return GetWorkItem(obj.GetType(), name, context);
 }
Ejemplo n.º 24
0
 public void OneTimeSetUp()
 {
     fixtureContext = TestExecutionContext.CurrentContext;
 }
Ejemplo n.º 25
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TestExecutionContext"/> class.
        /// </summary>
        /// <param name="other">An existing instance of TestExecutionContext.</param>
        public TestExecutionContext(TestExecutionContext other)
        {
            _priorContext = other;

            this.CurrentTest = other.CurrentTest;
            this.CurrentResult = other.CurrentResult;
            this.TestObject = other.TestObject;
            this.WorkDirectory = other.WorkDirectory;
            _listener = other._listener;
            this.StopOnError = other.StopOnError;
            this.TestCaseTimeout = other.TestCaseTimeout;
            this.UpstreamActions = new List<ITestAction>(other.UpstreamActions);

            _currentCulture = CultureInfo.CurrentCulture;
            _currentUICulture = CultureInfo.CurrentUICulture;

#if !NETCF && !SILVERLIGHT && !PORTABLE
            _currentPrincipal = other.CurrentPrincipal;
#endif

            this.Dispatcher = other.Dispatcher;
            this.ParallelScope = other.ParallelScope;
        }
Ejemplo n.º 26
0
Archivo: Fakes.cs Proyecto: alfeg/nunit
 public FakeWorkItem(Test test, TestExecutionContext context)
     : base(test) 
 {
     InitializeContext(context);
 }
Ejemplo n.º 27
0
		/// <summary>
		///		Initializes the underlying test engine.
		/// </summary>
		private static void InitializeTestEngine()
		{
			TestExecutionContext context = new TestExecutionContext();
			context.WorkDirectory = Environment.CurrentDirectory;
			CallContext.SetData( "NUnit.Framework.TestContext", context );
		}
Ejemplo n.º 28
0
Archivo: Fakes.cs Proyecto: alfeg/nunit
 public static FakeWorkItem GetWorkItem(Type type, string name, TestExecutionContext context)
 {
     return GetWorkItem(GetTestMethod(type, name), context);
 }
Ejemplo n.º 29
0
 /// <summary>
 /// Saves the old context and makes a fresh one
 /// current without changing any settings.
 /// </summary>
 public static void Save()
 {
     TestExecutionContext.current = new TestExecutionContext(current);
 }
Ejemplo n.º 30
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TestExecutionContext"/> class.
        /// </summary>
        public TestExecutionContext()
        {
            _priorContext = null;
            this.TestCaseTimeout = 0;
            this.UpstreamActions = new List<ITestAction>();

            _currentCulture = CultureInfo.CurrentCulture;
            _currentUICulture = CultureInfo.CurrentUICulture;

#if !NETCF && !SILVERLIGHT && !PORTABLE
            _currentPrincipal = Thread.CurrentPrincipal;
#endif
        }
Ejemplo n.º 31
0
 /// <summary>
 /// Saves the old context and makes a fresh one 
 /// current without changing any settings.
 /// </summary>
 public static void Save()
 {
     TestExecutionContext.current = new TestExecutionContext(current);
 }
Ejemplo n.º 32
0
 /// <summary>
 /// Clear the current context. This is provided to
 /// prevent "leakage" of the CallContext containing
 /// the current context back to any runners.
 /// </summary>
 public static void ClearCurrentContext()
 {
     CurrentContext = null;
 }
Ejemplo n.º 33
0
        /// <summary>
        /// Restores the last saved context and puts
        /// any saved settings back into effect.
        /// </summary>
        public static void Restore()
        {
            current.ReverseChanges();

            int latestAsserts = current.AssertCount;
            current = current.prior;
            current.assertCount += latestAsserts;
        }
Ejemplo n.º 34
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TestExecutionContext"/> class.
        /// </summary>
        public TestExecutionContext()
		{
			this.prior = null;
            this.testCaseTimeout = 0;

#if !NETCF
            this.currentCulture = CultureInfo.CurrentCulture;
            this.currentUICulture = CultureInfo.CurrentUICulture;
#endif

#if !NETCF && !SILVERLIGHT
			this.outWriter = Console.Out;
			this.errorWriter = Console.Error;
            this.traceWriter = null;
            this.tracing = false;
#endif

#if !NUNITLITE
			this.logging = false;
			this.currentDirectory = Environment.CurrentDirectory;
            this.logCapture = new Log4NetCapture();
            this.currentPrincipal = Thread.CurrentPrincipal;
#endif
        }
Ejemplo n.º 35
0
        public void SetAndRestoreCurrentUICulture()
        {
            var context = new TestExecutionContext(setupContext);

            try
            {
                CultureInfo otherCulture =
                    new CultureInfo(originalUICulture.Name == "fr-FR" ? "en-GB" : "fr-FR");
                context.CurrentUICulture = otherCulture;
                Assert.AreEqual(otherCulture, CultureInfo.CurrentUICulture, "UICulture was not set");
                Assert.AreEqual(otherCulture, context.CurrentUICulture, "UICulture not in new context");
                Assert.AreEqual(setupContext.CurrentUICulture, originalUICulture, "Original context should not change");
            }
            finally
            {
                setupContext.EstablishExecutionEnvironment();
            }

            Assert.AreEqual(CultureInfo.CurrentUICulture, originalUICulture, "UICulture was not restored");
            Assert.AreEqual(setupContext.CurrentUICulture, originalUICulture, "UICulture not in final context");
        }
Ejemplo n.º 36
0
        internal static void SetCurrentContext(TestExecutionContext ec)
        {
#if SILVERLIGHT || NETCF || __MOBILE__
            current = ec;
#else
            CallContext.SetData(CONTEXT_KEY, ec);
#endif
        }
Ejemplo n.º 37
0
        public void SetAndRestoreCurrentPrincipal()
        {
            var context = new TestExecutionContext(setupContext);

            try
            {
                GenericIdentity identity = new GenericIdentity("foo");
                context.CurrentPrincipal = new GenericPrincipal(identity, new string[0]);
                Assert.AreEqual("foo", Thread.CurrentPrincipal.Identity.Name, "Principal was not set");
                Assert.AreEqual("foo", context.CurrentPrincipal.Identity.Name, "Principal not in new context");
                Assert.AreEqual(setupContext.CurrentPrincipal, originalPrincipal, "Original context should not change");
            }
            finally
            {
                setupContext.EstablishExecutionEnvironment();
            }

            Assert.AreEqual(Thread.CurrentPrincipal, originalPrincipal, "Principal was not restored");
            Assert.AreEqual(setupContext.CurrentPrincipal, originalPrincipal, "Principal not in final context");
        }