Example #1
0
        public void Execute(Type testClass, Convention convention, Case[] cases)
        {
            foreach (var @case in cases)
            {
                var exceptions = @case.Exceptions;

                try
                {
                    var instance = construct(testClass);

                    var fixture = new Fixture(testClass, instance, convention.CaseExecution.Behavior, new[] { @case });
                    convention.InstanceExecution.Behavior.Execute(fixture);

                    Lifecycle.Dispose(instance);
                }
                catch (PreservedException preservedException)
                {
                    var constructionException = preservedException.OriginalException;
                    exceptions.Add(constructionException);
                }
                catch (Exception constructionException)
                {
                    exceptions.Add(constructionException);
                }
            }
        }
Example #2
0
        protected LifecycleTests()
        {
            FailingMembers = null;

            Convention = new Convention();
            Convention.Classes.Where(testClass => testClass == typeof(SampleTestClass));
            Convention.Methods.Where(method => method.Name == "Pass" || method.Name == "Fail");
        }
Example #3
0
        public void EmptyConventionShouldDiscoverConcreteClassesAsTestClasses()
        {
            var emptyConvention = new Convention();

            emptyConvention.Classes
                           .Filter(CandidateTypes)
                           .Select(x => x.Name)
                           .ShouldEqual("PublicTests", "OtherPublicTests", "PublicMissingNamingConvention", "PublicWithNoDefaultConstructorTests",
                                        "PrivateTests", "OtherPrivateTests", "PrivateMissingNamingConvention", "PrivateWithNoDefaultConstructorTests");
        }
Example #4
0
        public void CanDiscoverMethodsByNonInheritedAttributes()
        {
            var customConvention = new Convention();

            customConvention
                .Classes
                .Has<NonInheritedAttribute>();

            DiscoveredTestClasses(customConvention)
                .ShouldEqual(typeof(AttributeSample));
        }
Example #5
0
        public void EmptyConventionShouldDiscoverPublicInstanceMethodsForTestCases()
        {
            var emptyConvention = new Convention();
            var testClass = typeof(DiscoveryTestClass);

            emptyConvention.Methods
                           .Filter(testClass)
                           .OrderBy(x => x.Name, StringComparer.Ordinal)
                           .Select(x => x.Name)
                           .ShouldEqual("PublicInstanceNoArgsVoid", "PublicInstanceNoArgsWithReturn",
                                        "PublicInstanceWithArgsVoid", "PublicInstanceWithArgsWithReturn");
        }
Example #6
0
        public ConventionResult Run(Convention convention, Listener listener, params Type[] candidateTypes)
        {
            var executionPlan = new ExecutionPlan(convention);
            var conventionResult = new ConventionResult(convention.GetType().FullName);

            foreach (var testClass in convention.Classes.Filter(candidateTypes))
            {
                var classResult = new ClassResult(testClass.FullName);

                var methods = convention.Methods.Filter(testClass);

                var cases = methods.SelectMany(method => CasesForMethod(convention, method)).ToArray();
                var casesBySkipState = cases.ToLookup(convention.CaseExecution.SkipPredicate);
                var casesToSkip = casesBySkipState[true];
                var casesToExecute = casesBySkipState[false];
                foreach (var @case in casesToSkip)
                {
                    var skipResult = new SkipResult(@case, convention.CaseExecution.SkipReasonProvider(@case));
                    listener.CaseSkipped(skipResult);
                    classResult.Add(CaseResult.Skipped(skipResult.Case.Name, skipResult.Reason));
                }

                var caseExecutions = casesToExecute.Select(@case => new CaseExecution(@case)).ToArray();
                if (caseExecutions.Any())
                {
                    convention.ClassExecution.OrderCases(caseExecutions);

                    var classExecution = new ClassExecution(executionPlan, testClass, caseExecutions);
                    executionPlan.Execute(classExecution);

                    foreach (var caseExecution in caseExecutions)
                    {
                        if (caseExecution.Exceptions.Any())
                        {
                            var failResult = new FailResult(caseExecution, convention.HideExceptionDetails);
                            listener.CaseFailed(failResult);
                            classResult.Add(CaseResult.Failed(failResult.Case.Name, failResult.Duration, failResult.ExceptionSummary));
                        }
                        else
                        {
                            var passResult = new PassResult(caseExecution);
                            listener.CasePassed(passResult);
                            classResult.Add(CaseResult.Passed(passResult.Case.Name, passResult.Duration));
                        }
                    }

                }

                conventionResult.Add(classResult);
            }

            return conventionResult;
        }
Example #7
0
        public ExecuteCasesTests()
        {
            log = new List<string>();
            testClass = typeof(SampleTestClass);

            convention = new SelfTestConvention();
            convention.CaseExecution.Wrap((@case, instance, innerBehavior) =>
            {
                log.Add(@case.Method.Name);
                innerBehavior();
            });
        }
Example #8
0
        public void EmptyConventionShouldTreatPublicInstanceMethodsAsCases()
        {
            var emptyConvention = new Convention();
            var testClass = typeof(DiscoveryTestClass);

            emptyConvention.Cases
                           .Filter(testClass)
                           .OrderBy(x => x.Name)
                           .Select(x => x.Name)
                           .ShouldEqual("PublicInstanceNoArgsVoid", "PublicInstanceNoArgsWithReturn",
                                        "PublicInstanceWithArgsVoid", "PublicInstanceWithArgsWithReturn");
        }
Example #9
0
        public void ShouldDiscoverClassesSatisfyingAllSpecifiedConditions()
        {
            var customConvention = new Convention();

            customConvention
                .Classes
                .Where(type => type.IsInNamespace("Fixie.Tests"))
                .Where(type => type.Name.StartsWith("No"));

            DiscoveredTestClasses(customConvention)
                .ShouldEqual(typeof(NoDefaultConstructor));
        }
Example #10
0
        public void CanDiscoverClassesByInheritedAttributes()
        {
            var customConvention = new Convention();

            customConvention
                .Classes
                .HasOrInherits<InheritedAttribute>();

            DiscoveredTestClasses(customConvention)
                .ShouldEqual(
                    typeof(AttributeSampleBase),
                    typeof(AttributeSample));
        }
Example #11
0
        public void ShouldConsiderOnlyConcreteClasses()
        {
            var customConvention = new Convention();

            DiscoveredTestClasses(customConvention)
                .ShouldEqual(
                    typeof(DefaultConstructor),
                    typeof(NoDefaultConstructor),
                    typeof(NameEndsWithTests),
                    typeof(String),
                    typeof(AttributeSampleBase),
                    typeof(AttributeSample));
        }
Example #12
0
        public void CanDiscoverClassesByTypeNameSuffixes()
        {
            var convention = new Convention();

            convention
                .Classes
                .NameEndsWith("Constructor", "Sample");

            DiscoveredTestClasses(convention)
                .ShouldEqual(
                    typeof(DefaultConstructor),
                    typeof(NoDefaultConstructor),
                    typeof(AttributeSample));
        }
Example #13
0
 public void Execute(Type testClass, Convention convention, CaseExecution[] caseExecutions)
 {
     try
     {
         outer(testClass, convention, caseExecutions, () => inner.Execute(testClass, convention, caseExecutions));
     }
     catch (Exception exception)
     {
         foreach (var caseExecution in caseExecutions)
         {
             caseExecution.Fail(exception);
         }
     }
 }
Example #14
0
 public void Execute(Type testClass, Convention convention, Case[] cases)
 {
     try
     {
         outer(testClass, convention, cases, () => inner.Execute(testClass, convention, cases));
     }
     catch (Exception exception)
     {
         foreach (var @case in cases)
         {
             @case.Exceptions.Add(exception);
         }
     }
 }
Example #15
0
        public void Execute(Type testClass, Convention convention, CaseExecution[] caseExecutions)
        {
            try
            {
                var instance = construct(testClass);

                var fixture = new Fixture(testClass, instance, convention.CaseExecution.Behavior, caseExecutions);
                convention.InstanceExecution.Behavior.Execute(fixture);

                Dispose(instance);
            }
            catch (Exception exception)
            {
                foreach (var caseExecution in caseExecutions)
                    caseExecution.Fail(exception);
            }
        }
Example #16
0
        public void Execute(Type testClass, Convention convention, Case[] cases)
        {
            foreach (var @case in cases)
            {
                try
                {
                    var instance = construct(testClass);

                    var fixture = new Fixture(testClass, instance, convention.CaseExecution.Behavior, new InvokeMethod(), new[] { @case });
                    convention.InstanceExecution.Behavior.Execute(fixture);

                    Dispose(instance);
                }
                catch (Exception exception)
                {
                    @case.Fail(exception);
                }
            }
        }
Example #17
0
        static IEnumerable<Case> CasesForMethod(Convention convention, MethodInfo method)
        {
            var casesForKnownInputParameters = convention.MethodCallParameterBuilder(method)
                .Select(parameters => new Case(method, parameters));

            bool any = false;

            foreach (var actualCase in casesForKnownInputParameters)
            {
                any = true;
                yield return actualCase;
            }

            if (!any)
            {
                if (method.GetParameters().Any())
                    yield return new UncallableParameterizedCase(method);
                else
                    yield return new Case(method);
            }
        }
Example #18
0
        public void ShouldNotConsiderCompilerGeneratedClosureClasses()
        {
            var nested = typeof(ClassThatCausesCompilerGeneratedNestedClosureClass)
                .GetNestedTypes(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)
                .Single();

            //Confirm that a nested closure class has actually been generated.
            nested.Has<CompilerGeneratedAttribute>().ShouldBeTrue();

            //Confirm that the nested closure class is omitted from test class discovery.
            var customConvention = new Convention();

            DiscoveredTestClasses(customConvention, nested)
                .ShouldEqual(
                    typeof(DefaultConstructor),
                    typeof(NoDefaultConstructor),
                    typeof(NameEndsWithTests),
                    typeof(String),
                    typeof(AttributeSampleBase),
                    typeof(AttributeSample));
        }
Example #19
0
 public void Execute(Type testClass, Convention convention, Case[] cases)
 {
     try
     {
         outer(testClass, convention, cases, () => inner.Execute(testClass, convention, cases));
     }
     catch (Exception exception)
     {
         foreach (var @case in cases)
             @case.Fail(exception);
     }
 }
Example #20
0
        public void DoesNotMindIfMultipleTypesPointToSameNamespace()
        {
            var convention = new Convention();

            convention
                .Classes
                .InTheSameNamespaceAs(typeof(DefaultConstructor), typeof(NestedNamespace.InNestedNamespace), typeof(NoDefaultConstructor));

            DiscoveredTestClasses(convention, typeof(NestedNamespace.InNestedNamespace))
                .ShouldEqual(
                    typeof(DefaultConstructor),
                    typeof(NoDefaultConstructor),
                    typeof(NameEndsWithTests),
                    typeof(AttributeSampleBase),
                    typeof(AttributeSample),
                    typeof(NestedNamespace.InNestedNamespace));
        }
Example #21
0
 static IEnumerable<Type> DiscoveredTestClasses(Convention convention, params Type[] additionalCandidates)
 {
     return new ClassDiscoverer(convention.Config)
         .TestClasses(CandidateTypes.Concat(additionalCandidates));
 }
Example #22
0
        public void CanDiscoverClassesInAnyOfTheSpecifiedNamespaces()
        {
            var convention = new Convention();

            convention
                .Classes
                .InTheSameNamespaceAs(typeof(DefaultConstructor), typeof(DateTime));

            DiscoveredTestClasses(convention, typeof(NestedNamespace.InNestedNamespace))
                .ShouldEqual(
                    typeof(DefaultConstructor),
                    typeof(NoDefaultConstructor),
                    typeof(NameEndsWithTests),
                    typeof(String),
                    typeof(AttributeSampleBase),
                    typeof(AttributeSample),
                    typeof(NestedNamespace.InNestedNamespace));
        }
Example #23
0
 static IEnumerable<Type> DiscoveredTestClasses(Convention convention)
 {
     return new ClassDiscoverer(convention.Config)
         .TestClasses(CandidateTypes);
 }
Example #24
0
        public void ShouldFailWithClearExplanationWhenAnyGivenConditionThrows()
        {
            var customConvention = new Convention();

            customConvention
                .Classes
                .Where(type => { throw new Exception("Unsafe class-discovery predicate threw!"); });

            Action attemptFaultyDiscovery = () => DiscoveredTestClasses(customConvention);

            var exception = attemptFaultyDiscovery.ShouldThrow<Exception>(
                "Exception thrown while attempting to run a custom class-discovery predicate. " +
                "Check the inner exception for more details.");

            exception.InnerException.Message.ShouldEqual("Unsafe class-discovery predicate threw!");
        }
Example #25
0
 public static void Run(this Type sampleTestClass, Listener listener, Convention convention)
 {
     new Runner(listener).RunType(sampleTestClass.Assembly, convention, sampleTestClass);
 }