private static IEnumerable <MethodInfoWithLevel> GetMethodsInfos(TestStringCalculator testFixture) { var methodsInfos = testFixture.GetType() .GetMethods(BindingFlags.Public | BindingFlags.Instance) .Select(mi => { var testStepAttribute = mi.GetCustomAttributes().OfType <TestStepAttribute>().ToArray(); if (!testStepAttribute.Any()) { return(null); } var level = testStepAttribute.First().StepNumber; return(new MethodInfoWithLevel(mi, level)); }); return(methodsInfos.Where(mi => mi != null)); }
public void ExistingDataImplementations_ShouldAllPassCorrespondingTestLevelInDataTests() { //---------------Set up test pack------------------- var testFixture = new TestStringCalculator(); var methodsInfos = GetMethodsInfos(testFixture); var implementations = GetDataImplementations(); Func <int, IStringCalculator> GetInstanceOfImplementationAtLevel = (level) => { var matchingType = implementations.FirstOrDefault(i => i.Level == level); if (matchingType == null) { return(null); } return((IStringCalculator)Activator.CreateInstance(matchingType.Type)); }; //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var failed = methodsInfos.Aggregate(new List <int>(), (f, method) => { var instanceOfImplementationAtLevel = GetInstanceOfImplementationAtLevel(method.Level); if (instanceOfImplementationAtLevel == null) { return(f); } try { testFixture.CreateSUT = () => instanceOfImplementationAtLevel; method.MethodInfo.Invoke(testFixture, null); } catch (Exception) { f.Add(method.Level); } return(f); }); //---------------Test Result ----------------------- var distinct = failed.Distinct().ToArray(); if (distinct.Any()) { Assert.Fail("The following levels have crap implementations: " + string.Join(",", distinct)); } }
public void DataTestFixture_ShouldHaveCorrespondingDataImplementationsForAllTestLevels() { //---------------Set up test pack------------------- var testFixture = new TestStringCalculator(); var methodsInfos = GetMethodsInfos(testFixture); var implementations = GetDataImplementations(); Func <int, IStringCalculator> GetInstanceOfImplementationAtLevel = (level) => { var matchingType = implementations.FirstOrDefault(i => i.Level == level); if (matchingType == null) { return(null); } return((IStringCalculator)Activator.CreateInstance(matchingType.Type)); }; //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var dontHaveImplementationsFor = methodsInfos.Aggregate(new List <int>(), (implementationLevelsNotFoundFor, method) => { var instanceOfImplementationAtLevel = GetInstanceOfImplementationAtLevel(method.Level); if (instanceOfImplementationAtLevel == null) { implementationLevelsNotFoundFor.Add(method.Level); } return(implementationLevelsNotFoundFor); }); //---------------Test Result ----------------------- var distinct = dontHaveImplementationsFor.Distinct().ToArray(); if (distinct.Any()) { Assert.Fail("The following levels have no implementations: " + string.Join(",", distinct)); } }