예제 #1
0
        /// <summary>
        /// Builds a single test from the specified method and context,
        /// possibly containing child test cases.
        /// </summary>
        /// <param name="method">The method to be used as a test.</param>
        /// <param name="suite">The parent to which the test will be added.</param>
        public Test BuildFrom(FixtureMethod method, Test suite)
        {
            var tests = new List<TestMethod>();

            List<ITestBuilder> builders = new List<ITestBuilder>(
                method.Method.GetAttributes<ITestBuilder>(false));

            // See if we need to add a CombinatorialAttribute for parameterized data
            if (method.Method.GetParameters().Any(param => param.HasAttribute<IParameterDataSource>(false))
                && !builders.Any(builder => builder is CombiningStrategyAttribute))
                builders.Add(new CombinatorialAttribute());

            foreach (var attr in builders)
            {
                foreach (var test in attr.BuildFrom(method, suite))
                    tests.Add(test); 
            }

            return builders.Count > 0 && method.Method.GetParameters().Length > 0 || tests.Count > 0
                ? BuildParameterizedMethodSuite(method, tests)
                : BuildSingleTestMethod(method, suite);
        }
예제 #2
0
        public override IFixtureMethods Build()
        {
            var scenarioMethods = new ScenarioMethods(this.scenario);
            var method          = new FixtureMethod(this.background, scenarioMethods, this.scenario, this.options, this.fixtureInvariants);

            method.AddComments(CodeGeneration.ToXmlSummary(new[]
            {
                method.Identity.TestCase, this.scenario.Name, this.scenario.Description
            }));

            method.AddComments(CodeGeneration.ToXmlRemarks(this.scenario.Gherkin, "example", $"code language=\"none\" title=\"Gherkin\""));

            method.Add(Steps(GherkinStep.Given, this.scenario));
            method.Add(Steps(GherkinStep.When, this.scenario));
            method.Add(Steps(GherkinStep.Then, this.scenario));

            var methods = new List <IFixtureMethod> {
                method
            };

            return(new FixtureMethods(methods));
        }
예제 #3
0
파일: Fixture.cs 프로젝트: averrunci/Carna
    private void RunCore(object fixtureInstance)
    {
        void PerformFixtureMethod() => (FixtureMethod.Invoke(fixtureInstance, SampleData) as Task)?.GetAwaiter().GetResult();

        if (fixtureInstance is IDisposable disposable)
        {
            using (disposable) PerformFixtureMethod();
        }
        else if (fixtureInstance is IAsyncDisposable asyncDisposable)
        {
            try
            {
                PerformFixtureMethod();
            }
            finally
            {
                asyncDisposable.DisposeAsync().GetAwaiter().GetResult();
            }
        }
        else
        {
            PerformFixtureMethod();
        }
    }
예제 #4
0
 /// <summary>
 /// Determines if the method can be used to build an NUnit test
 /// test method of some kind. The method must normally be marked
 /// with an identifying attribute for this to be true.
 ///
 /// Note that this method does not check that the signature
 /// of the method for validity. If we did that here, any
 /// test methods with invalid signatures would be passed
 /// over in silence in the test run. Since we want such
 /// methods to be reported, the check for validity is made
 /// in BuildFrom rather than here.
 /// </summary>
 /// <param name="method">The method to be used as a test.</param>
 /// <param name="suite">The parent to which the test will be added.</param>
 public bool CanBuildFrom(FixtureMethod method, Test suite)
 {
     return(CanBuildFrom(method));
 }
예제 #5
0
 /// <summary>
 /// Builds a single test from the specified method and context,
 /// possibly containing child test cases.
 /// </summary>
 /// <param name="method">The method to be used as a test.</param>
 public Test BuildFrom(FixtureMethod method)
 {
     return(BuildFrom(method, null));
 }
예제 #6
0
 /// <summary>
 /// Determines if the method can be used to build an NUnit test
 /// test method of some kind. The method must normally be marked
 /// with an identifying attribute for this to be true.
 ///
 /// Note that this method does not check that the signature
 /// of the method for validity. If we did that here, any
 /// test methods with invalid signatures would be passed
 /// over in silence in the test run. Since we want such
 /// methods to be reported, the check for validity is made
 /// in BuildFrom rather than here.
 /// </summary>
 /// <param name="method">The method to be used as a test.</param>
 public bool CanBuildFrom(FixtureMethod method)
 {
     return(method.Method.HasAttribute <ITestBuilder>(false) ||
            method.Method.HasAttribute <ISimpleTestBuilder>(false));
 }
예제 #7
0
 /// <summary>
 /// Method to create a test case from a MethodInfo and add
 /// it to the fixture being built. It first checks to see if
 /// any global TestCaseBuilder addin wants to build the
 /// test case. If not, it uses the internal builder
 /// collection maintained by this fixture builder.
 ///
 /// The default implementation has no test case builders.
 /// Derived classes should add builders to the collection
 /// in their constructor.
 /// </summary>
 /// <param name="method">The method for which a test is to be created</param>
 /// <param name="suite">The test suite being built.</param>
 /// <returns>A newly constructed Test</returns>
 private Test BuildTestCase(FixtureMethod method, TestSuite suite)
 {
     return(_testBuilder.CanBuildFrom(method, suite)
         ? _testBuilder.BuildFrom(method, suite)
         : null);
 }
예제 #8
0
        private IEnumerable <ITestCaseData> GetTestCasesFor(FixtureMethod method)
        {
            List <ITestCaseData> data = new List <ITestCaseData>();

            try
            {
                IEnumerable source = GetTestCaseSource(method.FixtureType);

                if (source != null)
                {
                    foreach (object item in source)
                    {
                        // First handle two easy cases:
                        // 1. Source is null. This is really an error but if we
                        //    throw an exception we simply get an invalid fixture
                        //    without good info as to what caused it. Passing a
                        //    single null argument will cause an error to be
                        //    reported at the test level, in most cases.
                        // 2. User provided an ITestCaseData and we just use it.
                        ITestCaseData parms = item == null
                            ? new TestCaseParameters(new object[] { null })
                            : item as ITestCaseData;

                        if (parms == null)
                        {
                            object[] args = null;

                            // 3. An array was passed, it may be an object[]
                            //    or possibly some other kind of array, which
                            //    TestCaseSource can accept.
                            var array = item as Array;
                            if (array != null)
                            {
                                // If array has the same number of elements as parameters
                                // and it does not fit exactly into single existing parameter
                                // we believe that this array contains arguments, not is a bare
                                // argument itself.
                                var parameters = method.Method.GetParameters();
                                var argsNeeded = parameters.Length;
                                if (argsNeeded > 0 && argsNeeded == array.Length && parameters[0].ParameterType != array.GetType())
                                {
                                    args = new object[array.Length];
                                    for (var i = 0; i < array.Length; i++)
                                    {
                                        args[i] = array.GetValue(i);
                                    }
                                }
                            }

                            if (args == null)
                            {
                                args = new object[] { item };
                            }

                            parms = new TestCaseParameters(args);
                        }

                        if (this.Category != null)
                        {
                            foreach (string cat in this.Category.Split(new char[] { ',' }))
                            {
                                parms.Properties.Add(PropertyNames.Category, cat);
                            }
                        }

                        data.Add(parms);
                    }
                }
                else
                {
                    data.Clear();
                    data.Add(new TestCaseParameters(new Exception("The test case source could not be found.")));
                }
            }
            catch (Exception ex)
            {
                data.Clear();
                data.Add(new TestCaseParameters(ex));
            }

            return(data);
        }
예제 #9
0
파일: Fixture.cs 프로젝트: averrunci/Carna
 /// <summary>
 /// Retrieves around fixture attributes that specify a fixture.
 /// </summary>
 /// <returns>The around fixture attributes that specify a fixture.</returns>
 protected override IEnumerable <AroundFixtureAttribute> RetrieveAroundFixtureAttributes()
 => FixtureMethod.GetCustomAttributes <AroundFixtureAttribute>();
예제 #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TestMethod"/> class.
 /// </summary>
 /// <param name="method">The method to be used as a test.</param>
 public TestMethod(FixtureMethod method) : base(method)
 {
 }