public void ExecuteMethodThrowsException() { var fixture = new TestFixture(typeof(Foo)); var tm = new TestMethod(fixture, typeof(Foo).GetMethod("MethodWithException")); tm.Execute(_listener); Assert.Equals(" MethodWithException", _sequence); Assert.StartsWith(" TS:MethodWithException TE:MethodWithException ", SimpleTestListener.Messages); }
public TestRepository(ILdapManager ldapManagerObj, TestUserRepository testUserRepository) { _testImplementation = new TestImplementation.TestImplementation(testUserRepository, ldapManagerObj); TestList = new Dictionary<TestType, TestMethod> { {TestType.TestAdminConnection, _testImplementation.TestAdminConnect}, {TestType.TestCreateUser, _testImplementation.TestCreateUser}, {TestType.TestDeleteUser, _testImplementation.TestDeleteUser}, {TestType.TestInitLibrary, _testImplementation.TestCompleteInitLibrary}, {TestType.TestInitLibraryNoAdmin, _testImplementation.TestStardardInitLibraryNoAdmin}, {TestType.TestModifyUserDescription, _testImplementation.TestModifyUserAttribute}, {TestType.TestSearchUsers, _testImplementation.TestSearchUser}, {TestType.TestStandardInitLibraryNoAdmin, _testImplementation.TestStardardInitLibraryNoAdmin}, {TestType.TestUserChangePassword, _testImplementation.TestChangeUserPassword}, { TestType.TestConnectUser, () => { var testMethod = new TestMethod(_testImplementation.TestUserConnectWithoutWritePermissions); if (_writePermission) testMethod = _testImplementation.TestUserConnect; return testMethod(); } }, { TestType.TestSearchUserAndConnect, () => { var testMethod = new TestMethod(_testImplementation.TestSearchUserAndConnectWithoutWritePermissions); if (_writePermission) testMethod = _testImplementation.TestSearchUserAndConnect; return testMethod(); } } }; }
public void ShouldNotIgnoreMethodNotMarkedIgnore() { MethodInfo methodInfo = typeof(TestMethodTest).GetMethod("MethodNotMarkedIgnore"); var testMethod = new TestMethod(methodInfo); Assert.IsFalse(testMethod.Ignore); }
/// <summary> /// Creates a test command for use in running this test. /// </summary> /// <returns></returns> public static TestCommand MakeTestCommand(TestMethod test) { // Command to execute test TestCommand command = new TestMethodCommand(test); // Add any wrappers to the TestMethodCommand foreach (IWrapTestMethod wrapper in test.Method.GetCustomAttributes(typeof(IWrapTestMethod), true)) command = wrapper.Wrap(command); // Wrap in TestActionCommand command = new TestActionCommand(command); // Wrap in SetUpTearDownCommand command = new SetUpTearDownCommand(command); // Add wrappers that apply before setup and after teardown foreach (ICommandWrapper decorator in test.Method.GetCustomAttributes(typeof(IWrapSetUpTearDown), true)) command = decorator.Wrap(command); // Add command to set up context using attributes that implement IApplyToContext IApplyToContext[] changes = (IApplyToContext[])test.Method.GetCustomAttributes(typeof(IApplyToContext), true); if (changes.Length > 0) command = new ApplyChangesToContextCommand(command, changes); return command; }
/// <summary> /// Initializes a new <see cref="MSTestAutomation"/>. /// </summary> public MSTestAutomation(TestMethod testMethod) { Name = String.Format("{0}.{1}", testMethod.FullClassName, testMethod.Name); Identifier = _identifierFactory.CreateIdentifier(Name); Storage = Path.GetFileName(testMethod.AssemblyName); TestType = "Unit Test"; }
public static void DoTimedRun(TestMethod testMethod, int reps) { System.GC.Collect(); long rigging_time = 0; long start; long finish; // start = System.DateTime.Now.Ticks; // for (int i = 1; i <= reps; i++) // { // new TestMethod(DoNothingTestMethod)(); // } // finish = System.DateTime.Now.Ticks; // rigging_time = (finish - start); Console.Out.Write("TIME: {0}", testMethod.Method.Name); start = System.DateTime.Now.Ticks; for (int i = 1; i <= reps; i++) { testMethod(); } finish = System.DateTime.Now.Ticks; long millis = ((finish - start) - rigging_time) / TimeSpan.TicksPerMillisecond; Console.Out.WriteLine("; repeats = {0} {1}ms ({2} millisec/eval)", reps, millis, (millis/reps)); }
public void ExecuteMethod() { var fixture = new TestFixture(typeof(Foo)); var tm = new TestMethod(fixture, typeof(Foo).GetMethod("AMethod")); tm.Execute( _listener); Assert.Equals(" AMethod", _sequence); Assert.Equals(" TS:AMethod TP:AMethod", SimpleTestListener.Messages); }
static void Main(string[] args) { Program program = new Program(); TestMethod testMethod = new TestMethod(program.SearchShopbyFood); //program.OptimisticConcurrencyTest(); testMethod += program.SearchShopbyFood3; testMethod(); }
public void SetUp() { _test = new TestMethod(new MethodWrapper(typeof(DummySuite), "DummyMethod")); _testResult = _test.MakeTestResult(); _suite = new TestSuite(typeof(DummySuite)); _suiteResult = (TestSuiteResult)_suite.MakeTestResult(); }
public void DoTest(IWin32Window owner, TestMethod method, object data) { _alarming = true; _method = method; _data = data; MakePrompt(); ShowDialog(owner); }
public void SetsTestClassOnTestMethods() { TestMethod testMethod = new TestMethod(null, null, null); var testClass = new TestClass("typeName", new[] { testMethod }); Assert.Same(testClass, testMethod.TestClass); }
protected override void TestSkipped(TestMethod method, TestSkippedResult result) { Console.WriteLine( "##teamcity[testIgnored name='{0}' message='{1}']", Escape(method.DisplayName), Escape(result.Reason) ); }
public void OneNameCriterion() { ITestMethod tm = new TestMethod(new TestFixture(typeof(SampleTestFixture)), typeof(SampleTestFixture).GetMethod("Foo")); var nameCriterion = new NameCriterion(typeof(SampleTestFixture).FullName + ".Foo"); var selection = new TestRun(nameCriterion); Assert.True(selection.Contains(tm)); }
public void OneTestConfigured() { var testSpec = new TestSpec(); var testMethod1 = new TestMethod(new TestFixture(typeof(Foo)), typeof(Foo).GetMethod("Bar")); var testMethod2 = new TestMethod(new TestFixture(typeof(Foo)), typeof(Foo).GetMethod("Nope")); testSpec.AddTest(testMethod1); Assert.True(testSpec.Contains(testMethod1)); Assert.False(testSpec.Contains(testMethod2)); }
public static void RunTest(TestMethod r) { try { r(); } catch (TestFailedException) { } }
/// <summary> /// Filters the given method using the defined filter values. /// </summary> /// <param name="method">The methods to filter.</param> /// <returns>Returns true if the method passed the filter; return false otherwise.</returns> public bool Filter(TestMethod method) { if (!FilterIncludedTraits(method)) return false; if (!FilterExcludedTraits(method)) return false; return true; }
public void CreateWorkItems() { IMethodInfo method = new MethodWrapper(typeof(DummyFixture), "DummyTest"); ITest test = new TestMethod(method); _workItem = WorkItem.CreateWorkItem(test, TestFilter.Empty); _context = new TestExecutionContext(); _workItem.InitializeContext(_context); }
public override bool TestStart(TestMethod method) { Console.WriteLine( "##teamcity[testStarted name='{0}']", Escape(method.DisplayName) ); return true; }
public void EmptyFiltersListAlwaysPasses() { var filters = new XunitFilters(); var method = new TestMethod("method1", "Display Name 1", traits: null); bool result = filters.Filter(method); Assert.True(result); }
public void DoNap(IWin32Window owner, TimeSpan length, TimeSpan cooldown, TestMethod testMethod, object testData) { _testMethod = testMethod; _testData = testData; _engine.BeginNap(length, cooldown); ShowDialog(owner); PseudoDispose(); }
public void MethodWithAllPassingResultsIsInPassedRunState() { TestMethod method = new TestMethod("methdName", "displayname", null); method.RunResults.Add(new TestPassedResult(0.0, "displayname", null)); method.RunResults.Add(new TestPassedResult(1.2, "displayname", null)); Assert.Equal(TestStatus.Passed, method.RunStatus); }
public void MethodWithMixedPassingAndSkippedResultsButNoFailedResultsIsInSkippedRunState() { TestMethod method = new TestMethod("methdName", "displayname", null); method.RunResults.Add(new TestPassedResult(0.0, "displayname", null)); method.RunResults.Add(new TestSkippedResult("displayname", "reason")); method.RunResults.Add(new TestPassedResult(1.2, "displayname", null)); Assert.Equal(TestStatus.Skipped, method.RunStatus); }
public void TwoNameCriteria() { ITestMethod tm = new TestMethod(new TestFixture(typeof(SampleTestFixture)), typeof(SampleTestFixture).GetMethod("Foo")); var addCriterion = new OrCriterion(); addCriterion.Add(new NameCriterion(typeof(SampleTestFixture).FullName + ".Foo")); addCriterion.Add(new NameCriterion("bar")); var testRun = new TestRun(addCriterion); Assert.True(testRun.Contains(tm)); }
protected override void TestFailed(TestMethod method, TestFailedResult result) { Console.WriteLine( "##teamcity[testFailed name='{0}' details='{1}|r|n{2}']", Escape(method.DisplayName), Escape(result.ExceptionMessage), Escape(result.ExceptionStackTrace) ); WriteOutput(result.DisplayName, result.Output); }
public void NamespaceCriterion() { ITestMethod tm1 = new TestMethod(new TestFixture(typeof(SampleTestFixture)), typeof(SampleTestFixture).GetMethod("Foo")); ITestMethod tm2 = new TestMethod(new TestFixture(typeof(CriteriaTests)), typeof(CriteriaTests).GetMethod("NamespaceCriterion")); var selection = new TestRun(new NamespaceCriterion(tm1.DeclaringTypeFullName)); Assert.True(selection.Contains(tm1)); Assert.False(selection.Contains(tm2)); }
protected override bool TestFinished(TestMethod testMethod, TestResult testResult) { if (!silent) { Console.ForegroundColor = ConsoleColor.DarkGray; Console.Write("\rTests complete: {0} of {1}", ++testCount, totalCount); Console.ResetColor(); } return true; }
private static TimeSpan TestRunTime(TestMethod testMethod, int[] arr, int repeat) { var sw = Stopwatch.StartNew(); for (int i = 0; i < repeat; i++) { testMethod(arr); } sw.Stop(); return sw.Elapsed; }
public void AddExecutedTest(TestMethod test) { _lastRun.Add(test); ExecutedCount = _tests.Count(t => t.Result.Outcome != TestOutcome.Unknown); ProgressBarColor = _tests.Any(t => t.Result.Outcome == TestOutcome.Failed) ? Colors.Red : _tests.Any(t => t.Result.Outcome == TestOutcome.Inconclusive) ? Colors.Gold : Colors.LimeGreen; }
public void MethodWithMixedPassingAndSkippedAndFailedResultsIsInFailedRunState() { TestMethod method = new TestMethod("methdName", "displayname", null); method.RunResults.Add(new TestPassedResult(0.0, "displayname", null)); method.RunResults.Add(new TestSkippedResult("displayname", "reason")); method.RunResults.Add(new TestFailedResult(2.3, "displayname", null, "exceptionType", "exceptionMessage", "stackTrace")); method.RunResults.Add(new TestSkippedResult("displayname", "reason2")); method.RunResults.Add(new TestPassedResult(1.2, "displayname", null)); Assert.Equal(TestStatus.Failed, method.RunStatus); }
public void CanFilterItemsByTrait() { var filters = new XunitFilters(); var method1 = new TestMethod("method1", "Display Name 1", MakeTraits("foo", "bar")); var method2 = new TestMethod("method2", "Display Name 2", MakeTraits("baz", "biff")); var method3 = new TestMethod("method3", "Display Name 3", traits: null); filters.IncludedTraits.AddValue("foo", "bar"); Assert.True(filters.Filter(method1)); Assert.False(filters.Filter(method2)); Assert.False(filters.Filter(method3)); }
public TestMethodCommand(Test test) : base(test) { testMethod = test as TestMethod; arguments = test.arguments; }
/// <summary> /// Create the class Info /// </summary> /// <param name="classType"> The class Type. </param> /// <param name="testMethod"> The test Method. </param> /// <returns> The <see cref="TestClassInfo"/>. </returns> private TestClassInfo CreateClassInfo(Type classType, TestMethod testMethod) { var constructors = classType.GetTypeInfo().DeclaredConstructors; var constructor = constructors.FirstOrDefault(ctor => ctor.GetParameters().Length == 0 && ctor.IsPublic); if (constructor == null) { var message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_NoDefaultConstructor, testMethod.FullClassName); throw new TypeInspectionException(message); } var testContextProperty = this.ResolveTestContext(classType); var assemblyInfo = this.GetAssemblyInfo(classType); var classInfo = new TestClassInfo(classType, constructor, testContextProperty, this.reflectionHelper.GetDerivedAttribute <TestClassAttribute>(classType, false), assemblyInfo); var testInitializeAttributeType = typeof(TestInitializeAttribute); var testCleanupAttributeType = typeof(TestCleanupAttribute); var classInitializeAttributeType = typeof(ClassInitializeAttribute); var classCleanupAttributeType = typeof(ClassCleanupAttribute); // List of instance methods present in the type as well its base type // which is used to decide whether TestInitialize/TestCleanup methods // present in the base type should be used or not. They are not used if // the method is overridden in the derived type. var instanceMethods = new Dictionary <string, string>(); foreach (var methodInfo in classType.GetTypeInfo().DeclaredMethods) { // Update test initialize/cleanup method this.UpdateInfoIfTestInitializeOrCleanupMethod(classInfo, methodInfo, isBase: false, instanceMethods: instanceMethods, testInitializeAttributeType: testInitializeAttributeType, testCleanupAttributeType: testCleanupAttributeType); if (this.IsAssemblyOrClassInitializeMethod(methodInfo, classInitializeAttributeType)) { // update class initialize method classInfo.ClassInitializeMethod = methodInfo; } else if (this.IsAssemblyOrClassCleanupMethod(methodInfo, classCleanupAttributeType)) { // update class cleanup method classInfo.ClassCleanupMethod = methodInfo; } } var baseType = classType.GetTypeInfo().BaseType; while (baseType != null) { foreach (var methodInfo in baseType.GetTypeInfo().DeclaredMethods) { if (methodInfo.IsPublic && !methodInfo.IsStatic) { // Update test initialize/cleanup method from base type. this.UpdateInfoIfTestInitializeOrCleanupMethod(classInfo, methodInfo, true, instanceMethods, testInitializeAttributeType, testCleanupAttributeType); } } baseType = baseType.GetTypeInfo().BaseType; } return(classInfo); }
/// <summary> /// Builds a ParameterizedMetodSuite containing individual /// test cases for each set of parameters provided for /// this method. /// </summary> /// <param name="method">The MethodInfo for which a test is to be built</param> /// <returns>A ParameterizedMethodSuite populated with test cases</returns> public static Test BuildParameterizedMethodSuite(MethodInfo method, Test parentSuite) { ParameterizedMethodSuite methodSuite = new ParameterizedMethodSuite(method); NUnitFramework.ApplyCommonAttributes(method, methodSuite); if (parentSuite != null) { if (parentSuite.RunState == RunState.NotRunnable && methodSuite.RunState != RunState.NotRunnable) { methodSuite.RunState = RunState.NotRunnable; methodSuite.IgnoreReason = parentSuite.IgnoreReason; } if (parentSuite.RunState == RunState.Ignored && methodSuite.RunState != RunState.Ignored && methodSuite.RunState != RunState.NotRunnable) { methodSuite.RunState = RunState.Ignored; methodSuite.IgnoreReason = parentSuite.IgnoreReason; } } foreach (object source in CoreExtensions.Host.TestCaseProviders.GetTestCasesFor(method, parentSuite)) { ParameterSet parms; if (source == null) { parms = new ParameterSet(); parms.Arguments = new object[] { null }; } else { parms = source as ParameterSet; } if (parms == null) { if (source.GetType().GetInterface("NUnit.Framework.ITestCaseData") != null) { parms = ParameterSet.FromDataSource(source); } else { parms = new ParameterSet(); ParameterInfo[] parameters = method.GetParameters(); Type sourceType = source.GetType(); if (parameters.Length == 1 && parameters[0].ParameterType.IsAssignableFrom(sourceType)) { parms.Arguments = new object[] { source } } ; else if (source is object[]) { parms.Arguments = (object[])source; } else if (source is Array) { Array array = (Array)source; if (array.Rank == 1) { parms.Arguments = new object[array.Length]; for (int i = 0; i < array.Length; i++) { parms.Arguments[i] = (object)array.GetValue(i); } } } else { parms.Arguments = new object[] { source } }; } } TestMethod test = BuildSingleTestMethod(method, parentSuite, parms); methodSuite.Add(test); } return(methodSuite); }
/// <summary> /// Gets a UnitTestElement from a MethodInfo object filling it up with appropriate values. /// </summary> /// <param name="method">The reflected method.</param> /// <param name="isDeclaredInTestTypeAssembly">True if the reflected method is declared in the same assembly as the current type.</param> /// <param name="warnings">Contains warnings if any, that need to be passed back to the caller.</param> /// <returns> Returns a UnitTestElement.</returns> internal UnitTestElement GetTestFromMethod(MethodInfo method, bool isDeclaredInTestTypeAssembly, ICollection <string> warnings) { Debug.Assert(this.type.AssemblyQualifiedName != null, "AssemblyQualifiedName for method is null."); // This allows void returning async test method to be valid test method. Though they will be executed similar to non-async test method. var isAsync = ReflectHelper.MatchReturnType(method, typeof(Task)); var testMethod = new TestMethod(method.Name, this.type.FullName, this.assemblyName, isAsync); if (!method.DeclaringType.FullName.Equals(this.type.FullName)) { testMethod.DeclaringClassFullName = method.DeclaringType.FullName; } if (!isDeclaredInTestTypeAssembly) { testMethod.DeclaringAssemblyName = PlatformServiceProvider.Instance.FileOperations.GetAssemblyPath( method.DeclaringType.GetTypeInfo().Assembly); } var testElement = new UnitTestElement(testMethod); // Get compiler generated type name for async test method (either void returning or task returning). var asyncTypeName = method.GetAsyncTypeName(); testElement.AsyncTypeName = asyncTypeName; testElement.TestCategory = this.reflectHelper.GetCategories(method, this.type); testElement.DoNotParallelize = this.reflectHelper.IsDoNotParallelizeSet(method, this.type); var traits = this.reflectHelper.GetTestPropertiesAsTraits(method); var ownerTrait = this.reflectHelper.GetTestOwnerAsTraits(method); if (ownerTrait != null) { traits = traits.Concat(new[] { ownerTrait }); } testElement.Priority = this.reflectHelper.GetPriority(method); var priorityTrait = this.reflectHelper.GetTestPriorityAsTraits(testElement.Priority); if (priorityTrait != null) { traits = traits.Concat(new[] { priorityTrait }); } testElement.Traits = traits.ToArray(); var cssIteration = this.reflectHelper.GetCustomAttribute(method, typeof(CssIterationAttribute)) as CssIterationAttribute; if (cssIteration != null) { testElement.CssIteration = cssIteration.CssIteration; } var cssProjectStructure = this.reflectHelper.GetCustomAttribute(method, typeof(CssProjectStructureAttribute)) as CssProjectStructureAttribute; if (cssProjectStructure != null) { testElement.CssProjectStructure = cssProjectStructure.CssProjectStructure; } var descriptionAttribute = this.reflectHelper.GetCustomAttribute(method, typeof(DescriptionAttribute)) as DescriptionAttribute; if (descriptionAttribute != null) { testElement.Description = descriptionAttribute.Description; } var workItemAttributeArray = this.reflectHelper.GetCustomAttributes(method, typeof(WorkItemAttribute)) as WorkItemAttribute[]; if (workItemAttributeArray != null) { testElement.WorkItemIds = workItemAttributeArray.Select(x => x.Id.ToString()).ToArray(); } // Get Deployment items if any. testElement.DeploymentItems = PlatformServiceProvider.Instance.TestDeployment.GetDeploymentItems( method, this.type, warnings); return(testElement); }
public void TestFinished(ITestResult r) { TestResult result = r as TestResult; TestSuite ts = result.Test as TestSuite; if (ts != null) { TestSuiteElement tse; if (suite_elements.TryGetValue(ts, out tse)) { tse.Update(result); } } else { TestMethod tc = result.Test as TestMethod; if (tc != null) { case_elements [tc].Update(result); } } if (result.Test is TestSuite) { if (!result.IsFailure() && !result.IsSuccess() && !result.IsInconclusive() && !result.IsIgnored()) { Writer.WriteLine("\t[INFO] {0}", result.Message); } string name = result.Test.Name; if (!String.IsNullOrEmpty(name)) { Writer.WriteLine("{0} : {1} ms", name, result.Duration.TotalMilliseconds); } } else { if (result.IsSuccess()) { Writer.Write("\t[PASS] "); passed++; } else if (result.IsIgnored()) { Writer.Write("\t[IGNORED] "); ignored++; } else if (result.IsFailure()) { Writer.Write("\t[FAIL] "); failed++; } else if (result.IsInconclusive()) { Writer.Write("\t[INCONCLUSIVE] "); inconclusive++; } else { Writer.Write("\t[INFO] "); } Writer.Write(result.Test.Name); string message = result.Message; if (!String.IsNullOrEmpty(message)) { Writer.Write(" : {0}", message.Replace("\r\n", "\\r\\n")); } Writer.WriteLine(); string stacktrace = result.StackTrace; if (!String.IsNullOrEmpty(result.StackTrace)) { string[] lines = stacktrace.Split(new char [] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (string line in lines) { Writer.WriteLine("\t\t{0}", line); } } } }
/// <inheritdoc/> protected override async Task <decimal> InvokeTestMethodAsync(object testClassInstance) { var oldSyncContext = SynchronizationContext.Current; try { var asyncSyncContext = new AsyncTestSyncContext(oldSyncContext); SetSynchronizationContext(asyncSyncContext); Exception testException = null; await Aggregator.RunAsync( () => Timer.AggregateAsync( async() => { var parameterCount = TestMethod.GetParameters().Length; var valueCount = TestMethodArguments == null ? 0 : TestMethodArguments.Length; if (parameterCount != valueCount) { Aggregator.Add( new InvalidOperationException( $"The test method expected {parameterCount} parameter value{(parameterCount == 1 ? "" : "s")}," + $"but {valueCount} parameter value{(valueCount == 1 ? "" : "s")} {(valueCount == 1 ? "was" : "were")} provided.") ); } else { try { var result = TestMethod.Invoke(testClassInstance, TestMethodArguments); var task = result as Task; if (task != null) { await task; } else { var ex = await asyncSyncContext.WaitForCompletionAsync(); if (ex != null) { testException = ex; Aggregator.Add(ex); } } } catch (Exception ex) { testException = ex; throw; } } } ) ); if (testException != null) { var handleTestFailure = testClassInstance as INeedToKnowTestFailure; if (handleTestFailure != null) { await Aggregator.RunAsync( () => handleTestFailure.HandleFailureAsync(Test, testException)); } } } finally { SetSynchronizationContext(oldSyncContext); } return(Timer.Total); }
public TestAction(TestMethod test, AbstractAction parent, IncrementalTestRunner runner) : base(test, parent, runner) { this.test = test; }
public AsyncTestMethodCommand(TestMethod testMethod) : base(testMethod) { this.testMethod = testMethod; }
/// <summary> /// call a method of ReplaceWithNewGuid /// </summary> /// <param name="replaceWithGuid">instance of ReplaceWithNewGuid</param> /// <param name="testMethod">invoke method</param> /// <param name="input">input data</param> /// <returns>result of ReplaceWithNewGuid</returns> private string CallMethodOfReplaceWithNewGuid(ReplaceWithNewGuid replaceWithGuid, TestMethod testMethod, string input) { var output = string.Empty; switch (testMethod) { case TestMethod.ReplaceNewGuid: output = replaceWithGuid.ReplaceNewGuid(input); break; case TestMethod.ReplaceSameGuidToSameGuid: output = replaceWithGuid.ReplaceSameGuidToSameGuid(input); break; default: throw new ArgumentException(testMethod.ToString()); } return(output); }
public bool TestStart(TestMethod testMethod) { Console.WriteLine("{0,4}: {1}", ++index, testMethod.DisplayName); return(true); }
/// <summary> /// Processes the file. /// </summary> /// <param name="trackedMethods">The tracked methods.</param> /// <param name="fileIds">The file ids of the class.</param> /// <param name="filePath">The file path.</param> /// <param name="methods">The methods.</param> /// <returns>The <see cref="CodeFile"/>.</returns> private static CodeFile ProcessFile(IDictionary <string, string> trackedMethods, HashSet <string> fileIds, string filePath, XElement[] methods) { var seqpntsOfFile = methods .Elements("SequencePoints") .Elements("SequencePoint") .Where(seqpnt => (seqpnt.Attribute("fileid") != null && fileIds.Contains(seqpnt.Attribute("fileid").Value)) || (seqpnt.Attribute("fileid") == null && seqpnt.Parent.Parent.Element("FileRef") != null && fileIds.Contains(seqpnt.Parent.Parent.Element("FileRef").Attribute("uid").Value))) .Select(seqpnt => new { LineNumberStart = int.Parse(seqpnt.Attribute("sl").Value, CultureInfo.InvariantCulture), LineNumberEnd = seqpnt.Attribute("el") != null ? int.Parse(seqpnt.Attribute("el").Value, CultureInfo.InvariantCulture) : int.Parse(seqpnt.Attribute("sl").Value, CultureInfo.InvariantCulture), Visits = seqpnt.Attribute("vc").Value.ParseLargeInteger(), TrackedMethodRefs = seqpnt.Elements("TrackedMethodRefs") .Elements("TrackedMethodRef") .Select(t => new { Visits = t.Attribute("vc").Value.ParseLargeInteger(), TrackedMethodId = t.Attribute("uid").Value }) }) .OrderBy(seqpnt => seqpnt.LineNumberEnd) .ToArray(); var branches = GetBranches(methods, fileIds); var coverageByTrackedMethod = seqpntsOfFile .SelectMany(s => s.TrackedMethodRefs) .Select(t => t.TrackedMethodId) .Distinct() .ToDictionary(id => id, id => new CoverageByTrackedMethod { Coverage = new int[] { }, LineVisitStatus = new LineVisitStatus[] { } }); int[] coverage = new int[] { }; LineVisitStatus[] lineVisitStatus = new LineVisitStatus[] { }; if (seqpntsOfFile.Length > 0) { coverage = new int[seqpntsOfFile[seqpntsOfFile.LongLength - 1].LineNumberEnd + 1]; lineVisitStatus = new LineVisitStatus[seqpntsOfFile[seqpntsOfFile.LongLength - 1].LineNumberEnd + 1]; for (int i = 0; i < coverage.Length; i++) { coverage[i] = -1; } foreach (var trackedMethodCoverage in coverageByTrackedMethod) { trackedMethodCoverage.Value.Coverage = (int[])coverage.Clone(); trackedMethodCoverage.Value.LineVisitStatus = new LineVisitStatus[seqpntsOfFile[seqpntsOfFile.LongLength - 1].LineNumberEnd + 1]; } foreach (var seqpnt in seqpntsOfFile) { for (int lineNumber = seqpnt.LineNumberStart; lineNumber <= seqpnt.LineNumberEnd; lineNumber++) { int visits = coverage[lineNumber] == -1 ? seqpnt.Visits : coverage[lineNumber] + seqpnt.Visits; coverage[lineNumber] = visits; if (lineVisitStatus[lineNumber] != LineVisitStatus.Covered) { bool partiallyCovered = false; ICollection <Branch> branchesOfLine = null; // Use 'LineNumberStart' instead of 'lineNumber' here. Branches have line number of first line of seqpnt if (branches.TryGetValue(seqpnt.LineNumberStart, out branchesOfLine)) { partiallyCovered = branchesOfLine.Any(b => b.BranchVisits == 0); } LineVisitStatus statusOfLine = visits > 0 ? (partiallyCovered ? LineVisitStatus.PartiallyCovered : LineVisitStatus.Covered) : LineVisitStatus.NotCovered; lineVisitStatus[lineNumber] = (LineVisitStatus)Math.Max((int)lineVisitStatus[lineNumber], (int)statusOfLine); } if (visits > -1) { foreach (var trackedMethodCoverage in coverageByTrackedMethod) { if (trackedMethodCoverage.Value.Coverage[lineNumber] == -1) { trackedMethodCoverage.Value.Coverage[lineNumber] = 0; trackedMethodCoverage.Value.LineVisitStatus[lineNumber] = LineVisitStatus.NotCovered; } } } foreach (var trackedMethod in seqpnt.TrackedMethodRefs) { var trackedMethodCoverage = coverageByTrackedMethod[trackedMethod.TrackedMethodId]; int trackeMethodVisits = trackedMethodCoverage.Coverage[lineNumber] == -1 ? trackedMethod.Visits : trackedMethodCoverage.Coverage[lineNumber] + trackedMethod.Visits; LineVisitStatus statusOfLine = trackeMethodVisits > 0 ? (LineVisitStatus)Math.Min((int)LineVisitStatus.Covered, (int)lineVisitStatus[lineNumber]) : LineVisitStatus.NotCovered; trackedMethodCoverage.Coverage[lineNumber] = trackeMethodVisits; trackedMethodCoverage.LineVisitStatus[lineNumber] = statusOfLine; } } } } var codeFile = new CodeFile(filePath, coverage, lineVisitStatus, branches); foreach (var trackedMethodCoverage in coverageByTrackedMethod) { string name = null; // Sometimes no corresponding MethodRef element exists if (trackedMethods.TryGetValue(trackedMethodCoverage.Key, out name)) { string shortName = name.Substring(name.Substring(0, name.IndexOf(':') + 1).LastIndexOf('.') + 1); TestMethod testMethod = new TestMethod(name, shortName); codeFile.AddCoverageByTestMethod(testMethod, trackedMethodCoverage.Value); } } var methodsOfFile = methods .Where(m => m.Element("FileRef") != null && fileIds.Contains(m.Element("FileRef").Attribute("uid").Value)) .ToArray(); SetMethodMetrics(codeFile, methodsOfFile); SetCodeElements(codeFile, methodsOfFile); return(codeFile); }
public bool TestFinished(TestMethod testMethod) { return(true); }
/// <summary> /// Construct a simple work item for a test. /// </summary> /// <param name="test">The test to be executed</param> /// <param name="filter">The filter used to select this test</param> /// <param name="debugger">An <see cref="IDebugger"/> instance</param> internal SimpleWorkItem(TestMethod test, ITestFilter filter, IDebugger debugger) : base(test, filter) { _debugger = debugger; _testMethod = test; }
public SimpleWorkItem(TestMethod test, ITestFilter filter) : this(test, filter, new DebuggerProxy()) { }
private static dynamic ConvertExpected(TestMethod testMethod) { Dictionary <string, object> expected = testMethod.Expected !; return(expected.ToDictionary(kv => kv.Key[0], kv => Convert.ToInt32(kv.Value))); }
/// <summary> /// Construct a simple work item for a test. /// </summary> /// <param name="test">The test to be executed</param> /// <param name="filter">The filter used to select this test</param> public SimpleWorkItem(TestMethod test, ITestFilter filter) : base(test, filter) { _testMethod = test; }
/// <summary> /// Construct one or more TestMethods from a given MethodInfo, /// using available parameter data. /// </summary> /// <param name="method">The MethodInfo for which tests are to be constructed.</param> /// <param name="suite">The suite to which the tests will be added.</param> /// <returns>One or more TestMethods</returns> public IEnumerable <TestMethod> BuildFrom(IMethodInfo method, Test suite) { List <TestMethod> tests = new List <TestMethod>(); #if NETCF if (method.ContainsGenericParameters) { var genericParams = method.GetGenericArguments(); var numGenericParams = genericParams.Length; var o = new object(); var tryArgs = Enumerable.Repeat(o, numGenericParams).ToArray(); IMethodInfo mi; try { // This fails if the generic method has constraints // that are not met by object. mi = method.MakeGenericMethodEx(tryArgs); if (mi == null) { return(tests); } } catch { return(tests); } var par = mi.GetParameters(); if (par.Length == 0) { return(tests); } var sourceData = par.Select(p => _dataProvider.GetDataFor(p, suite)).ToArray(); foreach (var parms in _strategy.GetTestCases(sourceData)) { mi = method.MakeGenericMethodEx(parms.Arguments); if (mi == null) { var tm = new TestMethod(method, suite); tm.RunState = RunState.NotRunnable; tm.Properties.Set(PropertyNames.SkipReason, "Incompatible arguments"); tests.Add(tm); } else { tests.Add(_builder.BuildTestMethod(mi, suite, (TestCaseParameters)parms)); } } return(tests); } #endif IParameterInfo[] parameters = method.GetParameters(); if (parameters.Length > 0) { IEnumerable[] sources = new IEnumerable[parameters.Length]; try { for (int i = 0; i < parameters.Length; i++) { sources[i] = _dataProvider.GetDataFor(parameters[i], suite); } } catch (InvalidDataSourceException ex) { var parms = new TestCaseParameters(); parms.RunState = RunState.NotRunnable; parms.Properties.Set(PropertyNames.SkipReason, ex.Message); tests.Add(_builder.BuildTestMethod(method, suite, parms)); return(tests); } foreach (var parms in _strategy.GetTestCases(sources)) { tests.Add(_builder.BuildTestMethod(method, suite, (TestCaseParameters)parms)); } } return(tests); }
/// <summary> /// Helper method that checks the signature of a TestMethod and /// any supplied parameters to determine if the test is valid. /// /// Currently, NUnitTestMethods are required to be public, /// non-abstract methods, either static or instance, /// returning void. They may take arguments but the values must /// be provided or the TestMethod is not considered runnable. /// /// Methods not meeting these criteria will be marked as /// non-runnable and the method will return false in that case. /// </summary> /// <param name="testMethod">The TestMethod to be checked. If it /// is found to be non-runnable, it will be modified.</param> /// <param name="parms">Parameters to be used for this test, or null</param> /// <returns>True if the method signature is valid, false if not</returns> private static bool CheckTestMethodSignature(TestMethod testMethod, ParameterSet parms) { if (testMethod.Method.IsAbstract) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Method is abstract"; return(false); } if (!testMethod.Method.IsPublic) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Method is not public"; return(false); } ParameterInfo[] parameters = testMethod.Method.GetParameters(); int argsNeeded = parameters.Length; object[] arglist = null; int argsProvided = 0; if (parms != null) { testMethod.arguments = parms.Arguments; testMethod.hasExpectedResult = parms.HasExpectedResult; if (testMethod.hasExpectedResult) { testMethod.expectedResult = parms.Result; } testMethod.RunState = parms.RunState; testMethod.IgnoreReason = parms.IgnoreReason; testMethod.BuilderException = parms.ProviderException; arglist = parms.Arguments; if (arglist != null) { argsProvided = arglist.Length; } if (testMethod.RunState != RunState.Runnable) { return(false); } } #if CLR_2_0 || CLR_4_0 bool isAsyncMethod = Reflect.IsAsyncMethod(testMethod.Method); bool hasMeaningfulReturnType = isAsyncMethod ? testMethod.Method.ReturnType.IsGenericType : testMethod.Method.ReturnType != typeof(void); #else bool hasMeaningfulReturnType = testMethod.Method.ReturnType != typeof(void); #endif if (hasMeaningfulReturnType && (parms == null || !parms.HasExpectedResult && parms.ExpectedExceptionName == null)) { return(MarkAsNotRunnable(testMethod, "Test method has non-void return type, but no result is expected")); } if (!hasMeaningfulReturnType && parms != null && parms.HasExpectedResult) { return(MarkAsNotRunnable(testMethod, "Test method has void return type, but a result is expected")); } if (argsProvided > 0 && argsNeeded == 0) { return(MarkAsNotRunnable(testMethod, "Arguments provided for method not taking any")); } if (argsProvided == 0 && argsNeeded > 0) { return(MarkAsNotRunnable(testMethod, "No arguments were provided")); } //if (argsProvided > argsNeeded) //{ // ParameterInfo lastParameter = parameters[argsNeeded - 1]; // Type lastParameterType = lastParameter.ParameterType; // if (lastParameterType.IsArray && lastParameter.IsDefined(typeof(ParamArrayAttribute), false)) // { // object[] newArglist = new object[argsNeeded]; // for (int i = 0; i < argsNeeded; i++) // newArglist[i] = arglist[i]; // int length = argsProvided - argsNeeded + 1; // Array array = Array.CreateInstance(lastParameterType.GetElementType(), length); // for (int i = 0; i < length; i++) // array.SetValue(arglist[argsNeeded + i - 1], i); // newArglist[argsNeeded - 1] = array; // testMethod.arguments = arglist = newArglist; // argsProvided = argsNeeded; // } //} if (argsProvided != argsNeeded) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Wrong number of arguments provided"; return(false); } #if CLR_2_0 || CLR_4_0 if (testMethod.Method.IsGenericMethodDefinition) { Type[] typeArguments = GetTypeArgumentsForMethod(testMethod.Method, arglist); foreach (object o in typeArguments) { if (o == null) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = "Unable to determine type arguments for fixture"; return(false); } } testMethod.method = testMethod.Method.MakeGenericMethod(typeArguments); parameters = testMethod.Method.GetParameters(); for (int i = 0; i < parameters.Length; i++) { if (arglist[i].GetType() != parameters[i].ParameterType && arglist[i] is IConvertible) { try { arglist[i] = Convert.ChangeType(arglist[i], parameters[i].ParameterType); } catch (Exception) { // Do nothing - the incompatible argument will be reported below } } } } #endif return(true); }
/// <summary> /// utility function for ReplaceSameGuidToSameGuid /// </summary> /// <param name="count">loop count</param> /// <param name="repeat">repeat count</param> /// <param name="testMethod">test method</param> /// <param name="newSrcGuid">delegate for creating new source GUIDs</param> public void TestGuidByRandomValidFormat(int count, int repeat, TestMethod testMethod, ReplaceWithNewGuid.NewGuid newSrcGuid) { var builderInput = new StringBuilder(); var builderResult = new StringBuilder(); Array values = Enum.GetValues(typeof(ValidFormat)); var random = new Random(); // GUID generator for dest data var dstGuidGenerator = new GuidGenerater(); // create source data for (int i = 0; i < count; i++) { // choose enum 'Format' randomly var format = (ValidFormat)values.GetValue(random.Next(values.Length)); var separator = string.Format("------ {0} ------", i); #if PRINTF_DEBUG Console.WriteLine(i.ToString() + ": " + format.ToString()); #endif // PRINTF_DEBUG var srcGuid = newSrcGuid(); var dstGuid = Guid.Empty; if (testMethod == TestMethod.ReplaceSameGuidToSameGuid) { dstGuid = dstGuidGenerator.NewGuid(); } ValidFormatGuid formatGuid = delegate(StringBuilder builder, Guid guid, ValidFormat destFormat) { builder.Append(separator); builder.Append(Environment.NewLine); builder.Append(FormatGuidString(guid, destFormat)); builder.Append(Environment.NewLine); builder.Append(Environment.NewLine); }; for (int j = 0; j < repeat; j++) { // create input data formatGuid(builderInput, srcGuid, format); if (testMethod == TestMethod.ReplaceNewGuid) { dstGuid = dstGuidGenerator.NewGuid(); } // create expected data formatGuid(builderResult, dstGuid, format); } } var input = builderInput.ToString(); var expected = builderResult.ToString(); var replaceWithGuid = new ReplaceWithNewGuid(this.guidGenerator.NewGuid); var output = CallMethodOfReplaceWithNewGuid(replaceWithGuid, testMethod, input); #if PRINTF_DEBUG Console.WriteLine("input: "); Console.WriteLine(input); Console.WriteLine("output: "); Console.WriteLine(output); Console.WriteLine("expected: "); Console.WriteLine(expected); #endif // PRINTF_DEBUG Assert.That(output, Is.EqualTo(expected)); }
private void OnTestStarted(object sender, EventArgs <TestMethod> e) { _testExecuting = e.Value; UpdateTestExecutionState(); }
protected override void UpdateTestMethod(TestMethod testMethod) { testMethod.TestedMethodType = TestedMethodType.ExtensionMethod; testMethod.TestedMethod = "ToRoman"; }
private static TestItem AddTestItem(Dictionary <string, TestItem> items, CodeItemKind itemKind, string itemPath, TestCase testCase = null, string displayName = null, string testAdapterName = null) { var nameParts = itemPath.SplitPath(); var parentName = string.Join(string.Empty, nameParts.Take(nameParts.Length - 1)); var itemName = nameParts[nameParts.Length - 1]; TestItem parent; if (!items.TryGetValue(parentName, out parent)) { switch (itemKind) { case CodeItemKind.Data: parent = AddTestItem(items, CodeItemKind.Method, parentName); break; case CodeItemKind.Class: if (itemName.StartsWith("+")) { parent = AddTestItem(items, CodeItemKind.Class, parentName); } else { parent = AddTestItem(items, CodeItemKind.Namespace, parentName); } break; case CodeItemKind.Method: parent = AddTestItem(items, CodeItemKind.Class, parentName); break; default: parent = AddTestItem(items, CodeItemKind.Namespace, parentName); break; } } var name = itemName.TrimStart('.', '+'); TestItem item = null; switch (itemKind) { case CodeItemKind.Namespace: item = new TestNamespace(parent as TestNamespace, name); break; case CodeItemKind.Class: if (parent is TestClass) { item = new TestClass(parent as TestClass, name); } else { item = new TestClass(parent as TestNamespace, name); } break; case CodeItemKind.Method: item = new TestMethod(parent as TestClass, name, testCase, testAdapterName); break; case CodeItemKind.Data: item = new TestMethod(parent as TestMethod, name, displayName, testCase, testAdapterName); break; default: throw new NotImplementedException(); } items.Add(itemPath, item); return(item); }
public AsyncTestMethod(int iterations, TestMethod callback, params object[] args) : base(iterations) { Assert.IsNotNull(callback); this.args = args; this.callback = callback; }
protected override void UpdateTestMethod(TestMethod testMethod) { testMethod.Assert = RenderAssert(testMethod); }
public IEnumerable <TestMethod> BuildFrom(IMethodInfo method, Test suite) { var explic = method.GetCustomAttributes <ExplicitAttribute>(true) .Cast <IApplyToTest>() .Union(method.GetCustomAttributes <IgnoreAttribute>(true)) .ToList(); var builder = new NUnitTestCaseBuilder(); TestMethod test = null; var hasTest = false; foreach (var provider in _providerNames) { var isIgnore = !UserProviders.ContainsKey(provider); foreach (var parameters in GetParameters(provider)) { var data = new TestCaseParameters(parameters); test = builder.BuildTestMethod(method, suite, data); foreach (var attr in explic) { attr.ApplyToTest(test); } test.Properties.Set(PropertyNames.Order, GetOrder(method)); //test.Properties.Set(PropertyNames.ParallelScope, ParallelScope); test.Properties.Set(PropertyNames.Category, provider); SetName(test, method, provider, false); if (isIgnore) { if (test.RunState != RunState.NotRunnable && test.RunState != RunState.Explicit) { test.RunState = RunState.Ignored; } test.Properties.Set(PropertyNames.SkipReason, "Provider is disabled. See UserDataProviders.txt"); continue; } hasTest = true; yield return(test); } #if !NETSTANDARD && !MONO if (!isIgnore && _includeLinqService) { foreach (var paremeters in GetParameters(provider + ".LinqService")) { var data = new TestCaseParameters(paremeters); test = builder.BuildTestMethod(method, suite, data); foreach (var attr in explic) { attr.ApplyToTest(test); } test.Properties.Set(PropertyNames.Order, GetOrder(method)); //test.Properties.Set(PropertyNames.ParallelScope, ParallelScope); test.Properties.Set(PropertyNames.Category, provider); SetName(test, method, provider, true); yield return(test); } } #endif } if (!hasTest) { yield return(test); } }
/// <summary> /// Initializes a new instance of the <see cref="TestMethodCommand"/> class. /// </summary> /// <param name="testMethod">The test.</param> public TestMethodCommand(TestMethod testMethod) : base(testMethod) { this.testMethod = testMethod; this.arguments = testMethod.Arguments; }
/// <summary> /// The to unit test element. /// </summary> /// <param name="testCase"> The test case. </param> /// <param name="source"> The source. If deployed this is the full path of the source in the deployment directory. </param> /// <returns> The converted <see cref="UnitTestElement"/>. </returns> internal static UnitTestElement ToUnitTestElement(this TestCase testCase, string source) { var isAsync = (testCase.GetPropertyValue(Constants.AsyncTestProperty) as bool?) ?? false; var testClassName = testCase.GetPropertyValue(Constants.TestClassNameProperty) as string; var declaringClassName = testCase.GetPropertyValue(Constants.DeclaringClassNameProperty) as string; var fullyQualifiedName = testCase.FullyQualifiedName; // Not using Replace because there can be multiple instances of that string. var name = fullyQualifiedName.StartsWith($"{testClassName}.") ? fullyQualifiedName.Remove(0, $"{testClassName}.".Length) : fullyQualifiedName; TestMethod testMethod; if (testCase.ContainsManagedMethodAndType()) { testMethod = new TestMethod(testCase.GetManagedType(), testCase.GetManagedMethod(), testCase.GetHierarchy(), name, testClassName, source, isAsync); } else { testMethod = new TestMethod(name, testClassName, source, isAsync); } var dataType = (DynamicDataType)testCase.GetPropertyValue(Constants.TestDynamicDataTypeProperty, (int)DynamicDataType.None); if (dataType != DynamicDataType.None) { var data = testCase.GetPropertyValue <string[]>(Constants.TestDynamicDataProperty, null); testMethod.DataType = dataType; testMethod.SerializedData = data; } testMethod.DisplayName = testCase.DisplayName; if (declaringClassName != null && declaringClassName != testClassName) { testMethod.DeclaringClassFullName = declaringClassName; } UnitTestElement testElement = new UnitTestElement(testMethod) { IsAsync = isAsync, TestCategory = testCase.GetPropertyValue(Constants.TestCategoryProperty) as string[], Priority = testCase.GetPropertyValue(Constants.PriorityProperty) as int?, DisplayName = testCase.DisplayName }; if (testCase.Traits.Any()) { testElement.Traits = testCase.Traits.ToArray(); } var cssIteration = testCase.GetPropertyValue <string>(Constants.CssIterationProperty, null); if (!string.IsNullOrWhiteSpace(cssIteration)) { testElement.CssIteration = cssIteration; } var cssProjectStructure = testCase.GetPropertyValue <string>(Constants.CssProjectStructureProperty, null); if (!string.IsNullOrWhiteSpace(cssIteration)) { testElement.CssProjectStructure = cssProjectStructure; } var description = testCase.GetPropertyValue <string>(Constants.DescriptionProperty, null); if (!string.IsNullOrWhiteSpace(description)) { testElement.Description = description; } var workItemIds = testCase.GetPropertyValue <string[]>(Constants.WorkItemIdsProperty, null); if (workItemIds != null && workItemIds.Length > 0) { testElement.WorkItemIds = workItemIds; } var deploymentItems = testCase.GetPropertyValue <KeyValuePair <string, string>[]>(Constants.DeploymentItemsProperty, null); if (deploymentItems != null && deploymentItems.Length > 0) { testElement.DeploymentItems = deploymentItems; } testElement.DoNotParallelize = testCase.GetPropertyValue(Constants.DoNotParallelizeProperty, false); return(testElement); }
public static Testing.Data.TestResult ToTestResult(this Common.Models.TestResult testResult, TestMethod testMethod, int sessionId) { return(new Testing.Data.TestResult() { Method = testMethod, Duration = testResult.Duration, Outcome = testResult.Outcome.ToTestState(), ErrorMessage = GetShortErrorMessage(testResult.ErrorMessage), StackTrace = StackItem.FromStackTrace(testResult.ErrorStackTrace), Output = testResult.Messages?.Length > 0 ? string.Join(Environment.NewLine, testResult.Messages.Select(p => p.Text)).Trim() : null, SessionId = sessionId }); }
protected override void UpdateTestMethod(TestMethod testMethod) { testMethod.UseVariablesForInput = true; testMethod.Input["dominoes"] = ConvertDominoes(testMethod.Input["dominoes"]); }
private static bool MarkAsNotRunnable(TestMethod testMethod, string reason) { testMethod.RunState = RunState.NotRunnable; testMethod.IgnoreReason = reason; return(false); }