public TestThread(int threadId, TestResultDictionary resultQueue, IEnumerable<MethodInfo> tests, Type testFixture, int testFixtureRepeatIndex, TestCaseAttribute testFixtureCase, object activator, Exception globalSetupEx, Exception fixtureSetupEx, Exception activatorEx, EdisonContext context, ConcurrencyType concurrenyType) { ThreadId = threadId; Context = context; ResultQueue = resultQueue; TestFixture = testFixture; TestFixtureRepeatIndex = testFixtureRepeatIndex; Activator = activator; TestFixtureCase = testFixtureCase; GlobalSetupException = globalSetupEx; FixtureSetupException = fixtureSetupEx; ActivatorException = activatorEx; Tests = tests; }
private void RunTests(Type testFixture, int testFixtureRepeat, TestCaseAttribute testFixtureCase, object activator) { var tests = ReflectionRepository.GetMethods<TestAttribute>(testFixture, Context.IncludedCategories, Context.ExcludedCategories, Context.Tests).ToList(); var singularTests = default(List<MethodInfo>); if (!tests.Any()) { return; } #region Parallel if (NumberOfTestThreads > 1 && tests.Count() != 1) { singularTests = tests.Where(t => ReflectionRepository.HasValidConcurrency(t, ConcurrencyType.Serial, ConcurrencyType)).OrderBy(t => t.Name).ToList(); tests = tests.Where(t => ReflectionRepository.HasValidConcurrency(t, ConcurrencyType.Parallel, ConcurrencyType)).OrderBy(t => t.Name).ToList(); } var testsCount = tests.Count(); if (testsCount < NumberOfTestThreads) { NumberOfTestThreads = testsCount; } ParallelThreads = new List<TestThread>(NumberOfTestThreads); var segment = testsCount == 0 ? 0 : (int)Math.Round((double)testsCount / (double)NumberOfTestThreads, MidpointRounding.ToEven); var threadCount = 1; for (threadCount = 1; threadCount <= NumberOfTestThreads; threadCount++) { var testsSegment = threadCount == NumberOfTestThreads ? tests.Skip((threadCount - 1) * segment) : tests.Skip((threadCount - 1) * segment).Take(segment); var thread = new TestThread(threadCount, ResultQueue, testsSegment, testFixture, testFixtureRepeat, testFixtureCase, activator, GlobalSetupException, FixtureSetupException, ActivatorException, Context, ConcurrencyType.Parallel); ParallelThreads.Add(thread); } ParallelTask = Task.Run(() => Parallel.ForEach(ParallelThreads, thread => thread.RunTests())); Task.WaitAll(ParallelTask); #endregion #region Singular if (NumberOfTestThreads > 1 && !EnumerableHelper.IsNullOrEmpty(singularTests)) { SingularThread = new TestThread(threadCount + 1, ResultQueue, singularTests, testFixture, testFixtureRepeat, testFixtureCase, activator, GlobalSetupException, FixtureSetupException, ActivatorException, Context, ConcurrencyType.Serial); SingularTask = Task.Factory.StartNew(() => SingularThread.RunTests()); Task.WaitAll(SingularTask); } #endregion }
private void RunTestCase(MethodInfo test, TestCaseAttribute testCase, int testRepeat, IEnumerable<MethodInfo> setup, IEnumerable<MethodInfo> teardown) { var timeTaken = new Stopwatch(); var testResult = default(TestResult); var setupDone = false; var teardownDone = false; var testDone = false; try { testResult = new TestResult( TestResultState.Success, Context.CurrentAssembly, test, TestFixtureCase.Parameters, testCase.Parameters, TestFixtureRepeatIndex, testRepeat, string.Empty, string.Empty, TimeSpan.Zero, string.Empty, default(IEnumerable<string>)); timeTaken.Restart(); if (GlobalSetupException != default(Exception)) { testResult = PopulateTestResultOnException(test, testResult, GlobalSetupException, false, false, setupDone, teardownDone, testDone, timeTaken.Elapsed); } else if (ActivatorException != default(Exception)) { testResult = PopulateTestResultOnException(test, testResult, ActivatorException, true, true, true, true, true, timeTaken.Elapsed); } else if (FixtureSetupException != default(Exception)) { testResult = PopulateTestResultOnException(test, testResult, FixtureSetupException, true, false, setupDone, teardownDone, testDone, timeTaken.Elapsed); } else { //setup ReflectionRepository.Invoke(setup, Activator); setupDone = true; //test ReflectionRepository.Invoke(test, Activator, testCase.Parameters); testDone = true; testResult = PopulateTestResult(test, testResult, TestResultState.Success, timeTaken.Elapsed); //teardown ReflectionRepository.Invoke(teardown, Activator, testResult); teardownDone = true; } timeTaken.Stop(); } catch (Exception ex) { testResult = PopulateTestResultOnException(test, testResult, ex, true, true, setupDone, teardownDone, testDone, timeTaken.Elapsed); //teardown if (testResult.State != TestResultState.TeardownError && testResult.State != TestResultState.TeardownFailure) { try { ReflectionRepository.Invoke(teardown, Activator, testResult); } catch (Exception ex2) { testResult = PopulateTestResultOnException(test, testResult, ex2, true, true, true, false, true, timeTaken.Elapsed); } } if (timeTaken.IsRunning) { timeTaken.Stop(); } } ResultQueue.AddOrUpdate(testResult); }