private void ExploreTestMethod(IProject project, XunitTestClassElement classUnitTestElement, UnitTestElementConsumer consumer, IMethodInfo methodInfo) { var methodUnitTestElement = unitTestElementFactory.GetOrCreateTestMethod(project, classUnitTestElement, new ClrTypeName(methodInfo.TypeName), methodInfo.Name, MethodUtility.GetSkipReason(methodInfo), methodInfo.SafelyGetTraits(), false); consumer(methodUnitTestElement); }
public void TestMethodHasSkipReasonParameter() { Type testClassType = typeof(SkipTestClass); MethodInfo methodInfo = testClassType.GetMethod("TestAttributeWithSkipReason"); Assert.True(MethodUtility.IsSkip(Reflector.Wrap(methodInfo))); }
public void TestMethodsCanHaveNonVoidReturnType() { Type testClassType = typeof(NonVoidReturnClass); MethodInfo methodInfo = testClassType.GetMethod("NonVoidTest"); Assert.True(MethodUtility.IsTest(Reflector.Wrap(methodInfo))); }
public void TestMethodIsStatic() { Type testClassType = typeof(StaticTestMethodClass); MethodInfo methodInfo = testClassType.GetMethod("StaticTestMethod"); Assert.True(MethodUtility.IsTest(Reflector.Wrap(methodInfo))); }
private void ExploreTestMethod(IProject project, XunitTestClassElement classUnitTestElement, IUnitTestElementsObserver observer, IMethodInfo methodInfo) { var methodUnitTestElement = unitTestElementFactory.GetOrCreateTestMethod(project, classUnitTestElement, new ClrTypeName(methodInfo.TypeName), methodInfo.Name, MethodUtility.GetSkipReason(methodInfo), methodInfo.GetTraits(), false); observer.OnUnitTestElement(methodUnitTestElement); }
private IUnitTestElement ProcessTestMethod(IMethod method, IList<IUnitTestElement> subElements) { var type = method.GetContainingType(); var @class = type as IClass; if (type == null || @class == null) return null; var typeInfo = @class.AsTypeInfo(); if (@class.IsAbstract && TypeUtility.ContainsTestMethods(typeInfo)) return ProcessTestMethodInAbstractClass(method, subElements); if (!IsValidTestClass(@class)) return null; var command = TestClassCommandFactory.Make(typeInfo); if (command == null) return null; var testClassElement = classes[type]; if (testClassElement == null) return null; var methodInfo = method.AsMethodInfo(typeInfo); if (command.IsTestMethod(methodInfo)) { var clrTypeName = type.GetClrName(); return unitTestElementFactory.GetOrCreateTestMethod(project, testClassElement, clrTypeName, method.ShortName, MethodUtility.GetSkipReason(methodInfo), methodInfo.GetTraits(), false); } return null; }
public IEnumerable <ITestCommand> AssertCommands(string name, IMethodInfo method) { foreach (var assertion in _asserts) { // do not capture the iteration variable because // all tests would point to the same assertion var capturableAssertion = assertion; Action test = () => { using (SpecificationPrimitiveExecutor.Execute(_context)) { if (_do != null) { SpecificationPrimitiveExecutor.Execute(_do); } SpecificationPrimitiveExecutor.Execute(capturableAssertion); } }; string testDescription = String.Format("{0}, {1}", name, assertion.Message); yield return(new ActionTestCommand(method, testDescription, MethodUtility.GetTimeoutParameter(method), test)); } }
public void TestMethodIsAbstract() { Type testClassType = typeof(AbstractMethodClass); MethodInfo methodInfo = testClassType.GetMethod("AbstractMethod"); Assert.False(MethodUtility.IsTest(Reflector.Wrap(methodInfo))); }
public TheoryCommand(IMethodInfo testMethod, object[] parameters, Type[] genericTypes) : base(testMethod, MethodUtility.GetDisplayName(testMethod), MethodUtility.GetTimeoutParameter(testMethod)) { int idx; Parameters = parameters ?? new object[0]; if (genericTypes != null && genericTypes.Length > 0) { string[] typeNames = new string[genericTypes.Length]; for (idx = 0; idx < genericTypes.Length; idx++) { typeNames[idx] = ConvertToSimpleTypeName(genericTypes[idx]); } DisplayName = String.Format("{0}<{1}>", DisplayName, string.Join(", ", typeNames)); } ParameterInfo[] parameterInfos = testMethod.MethodInfo.GetParameters(); string[] displayValues = new string[Math.Max(Parameters.Length, parameterInfos.Length)]; for (idx = 0; idx < Parameters.Length; idx++) { displayValues[idx] = ParameterToDisplayValue(GetParameterName(parameterInfos, idx), Parameters[idx]); } for (; idx < parameterInfos.Length; idx++) // Fill-in any missing parameters with "???" { displayValues[idx] = parameterInfos[idx].Name + ": ???"; } DisplayName = String.Format("{0}({1})", DisplayName, string.Join(", ", displayValues)); }
private void AppendTests(XunitTestClassElement classElement, ITypeInfo typeInfo, IEnumerable <IDeclaredType> types) { foreach (var declaredType in types) { if (declaredType.GetClrName().Equals(PredefinedType.OBJECT_FQN)) { continue; } var typeElement = declaredType.GetTypeElement(); if (typeElement != null) { var methodInfo = new PsiMethodInfoAdapter(); foreach (var method in typeElement.GetMembers().OfType <IMethod>()) { methodInfo.Reset(method, typeInfo); if (MethodUtility.IsTest(methodInfo)) { var element = unitTestElementFactory.GetOrCreateTestMethod(project, classElement, typeElement.GetClrName(), method.ShortName, MethodUtility.GetSkipReason(methodInfo), methodInfo.GetTraits(), false); observer.OnUnitTestElementDisposition(UnitTestElementDisposition.NotYetClear(element)); } } } } }
public void MethodDoesNotHaveTestAttribute() { Type testClassType = typeof(NoTestAttributeClass); MethodInfo methodInfo = testClassType.GetMethod("NoTestAttribute"); Assert.False(MethodUtility.IsTest(Reflector.Wrap(methodInfo))); }
public void TestMethodWithTimeoutParameter() { Type testClassType = typeof(TimeoutTestClass); MethodInfo methodInfo = testClassType.GetMethod("TestAttributeWithTimeoutParameter"); Assert.True(MethodUtility.HasTimeout(Reflector.Wrap(methodInfo))); }
public void ClassHasTrait() { Type testClassType = typeof(ClassWithClassTraits); MethodInfo methodInfo = testClassType.GetMethod("TestMethod"); Assert.True(MethodUtility.HasTraits(Reflector.Wrap(methodInfo))); }
public void MethodHasATrait() { Type testClassType = typeof(ClassWithTraits); MethodInfo methodInfo = testClassType.GetMethod("SingleTraitOnAMethod"); Assert.True(MethodUtility.HasTraits(Reflector.Wrap(methodInfo))); }
public void MethodDoesNotHaveTraits() { Type testClassType = typeof(ClassWithTraits); MethodInfo methodInfo = testClassType.GetMethod("MethodDoesNotHaveTestTraits"); Assert.False(MethodUtility.HasTraits(Reflector.Wrap(methodInfo))); }
public void CanDecorateTestMethodWithMultipleFactDerivedAttributes() { MethodInfo method = typeof(Spy).GetMethod("TestMethod"); List <ITestCommand> commands = new List <ITestCommand>(MethodUtility.GetTestCommands(Reflector.Wrap(method))); Assert.Equal(2, commands.Count); }
public void SkipReasonParameter() { Type testClassType = typeof(SkipTestClass); MethodInfo methodInfo = testClassType.GetMethod("TestAttributeWithSkipReason"); string skipReason = MethodUtility.GetSkipReason(Reflector.Wrap(methodInfo)); Assert.Equal("reason", skipReason); }
public void TimeoutParameter() { Type testClassType = typeof(TimeoutTestClass); MethodInfo methodInfo = testClassType.GetMethod("TestAttributeWithTimeoutParameter"); long timeout = MethodUtility.GetTimeoutParameter(Reflector.Wrap(methodInfo)); Assert.Equal <long>(1000, timeout); }
public void NoTraits() { Type testClassType = typeof(ClassWithTraits); MethodInfo methodInfo = testClassType.GetMethod("MethodDoesNotHaveTestTraits"); var traits = MethodUtility.GetTraits(Reflector.Wrap(methodInfo)); Assert.Equal(0, traits.Count); }
private void HandleTraits() { var traits = MethodUtility.GetTraits(methodInfo); categories = ExtractTraitValues(traits, "category"); owners = ExtractTraitValues(traits, "owner"); descriptions = ExtractTraitValues(traits, "description"); testProperties = ConvertToTestPropeties(traits); }
public void ReturnsNullWithNoDisplayName() { string methodName = "WithoutDisplayName"; Type testClassType = typeof(ClassWithDisplayName); MethodInfo methodInfo = testClassType.GetMethod(methodName); var result = MethodUtility.GetDisplayName(Reflector.Wrap(methodInfo)); Assert.Null(result); }
public void ReturnsDisplayNameFromFact() { string methodName = "WithDisplayName"; Type testClassType = typeof(ClassWithDisplayName); MethodInfo methodInfo = testClassType.GetMethod(methodName); var result = MethodUtility.GetDisplayName(Reflector.Wrap(methodInfo)); Assert.Equal("My Display Name", result); }
protected override IEnumerable <ITestCommand> EnumerateTestCommands(IMethodInfo testMethod) { if (Helper.Credentials == null) { yield return(new SkipCommand(testMethod, MethodUtility.GetDisplayName(testMethod), "Automation settings not configured. Please set the OCTOKIT_GITHUBUSERNAME and OCTOKIT_GITHUBPASSWORD environment variables to a GitHub test account (i.e, DO NOT USE A \"REAL\" ACCOUNT).")); } else { yield return(new FactCommand(testMethod)); } }
public void SingleTraitOnATestMethod() { Type testClassType = typeof(ClassWithTraits); MethodInfo methodInfo = testClassType.GetMethod("SingleTraitOnAMethod"); var traits = MethodUtility.GetTraits(Reflector.Wrap(methodInfo)); Assert.Single(traits); string description = Assert.Single(traits["Description"]); Assert.Equal("more than just the test method name", description); }
private IUnitTestElement ProcessTestMethodInAbstractClass(ITypeInfo typeInfo, IMethod method, IList <IUnitTestElement> subElements) { var containingType = method.GetContainingType(); if (containingType == null) { return(null); } IList <XunitTestClassElement> derivedElements; if (!derivedTestClassElements.TryGetValue(containingType, out derivedElements)) { return(null); } var methodInfo = method.AsMethodInfo(typeInfo); if (!MethodUtility.IsTest(methodInfo)) { return(null); } var inheritedTestMethodContainerElement = unitTestElementFactory.GetOrCreateInheritedTestMethodContainer(project, containingType.GetClrName(), method.ShortName); foreach (var derivedClassElement in derivedElements) { XunitTestMethodElement methodInDerivedClass = null; foreach (var testMethodElement in derivedClassElement.Children.OfType <XunitTestMethodElement>()) { if (testMethodElement.Id.Equals(inheritedTestMethodContainerElement.Id)) { testMethodElement.State = UnitTestElementState.Valid; methodInDerivedClass = testMethodElement; break; } } if (methodInDerivedClass == null) { // TODO: Add traits methodInDerivedClass = unitTestElementFactory.GetOrCreateTestMethod(project, derivedClassElement, containingType.GetClrName().GetPersistent(), method.ShortName, string.Empty, new OneToSetMap <string, string>(), false); } subElements.Add(methodInDerivedClass); } return(inheritedTestMethodContainerElement); }
public IEnumerable <ITestCommand> EnumerateTestCommands(IMethodInfo testMethod) { string skipReason = MethodUtility.GetSkipReason(testMethod); if (skipReason != null) { yield return(new SkipCommand(testMethod, MethodUtility.GetDisplayName(testMethod), skipReason)); } else { yield return(new FixtureCommand(new FactCommand(testMethod), _fixtures)); } }
private static bool IsUnitTestMethod(IMethod testMethod) { if (testMethod == null) { return(false); } var containingType = testMethod.GetContainingType() as IClass; if (containingType == null) { return(false); } return(MethodUtility.IsTest(testMethod.AsMethodInfo(containingType.AsTypeInfo()))); }
protected override IEnumerable <ITestCommand> EnumerateTestCommands(IMethodInfo testMethod) { if (Helper.Organization == null) { return new[] { new SkipCommand(testMethod, MethodUtility.GetDisplayName(testMethod), "Automation settings not configured. Please set the OCTOKIT_GITHUBORGANIZATION environment variable to a GitHub organization owned by the test account specified in OCTOKIT_GITHUBUSERNAME.") } } ; else { return(base.EnumerateTestCommands(testMethod)); } }
public void TraitsOnClass() { Type testClassType = typeof(ClassWithClassTraits); MethodInfo methodInfo = testClassType.GetMethod("TestMethod"); var traits = MethodUtility.GetTraits(Reflector.Wrap(methodInfo)); Assert.Equal(2, traits.Count); string description = Assert.Single(traits["Description"]); Assert.Equal("more than just the test method name", description); string author = Assert.Single(traits["Author"]); Assert.Equal("James Newkirk", author); }
public static MultiValueDictionary <string, string> SafelyGetTraits(this IMethodInfo methodInfo) { try { // GetTraits relies on our attribute parsing to get at a property // value. Unless that value is set in the attribute constructor // via a named parameter (property) or a parameter with a name // similar to the required property, we'll return null, and that // can cause exceptions. If we get an exception, fail as nicely // as we can, with an empty collection of traits return(MethodUtility.GetTraits(methodInfo)); } catch (Exception) { return(new MultiValueDictionary <string, string>()); } }