当前位置: 首页>>代码示例>>C#>>正文


C# ITestFrameworkDiscoveryOptions.PreEnumerateTheoriesOrDefault方法代码示例

本文整理汇总了C#中ITestFrameworkDiscoveryOptions.PreEnumerateTheoriesOrDefault方法的典型用法代码示例。如果您正苦于以下问题:C# ITestFrameworkDiscoveryOptions.PreEnumerateTheoriesOrDefault方法的具体用法?C# ITestFrameworkDiscoveryOptions.PreEnumerateTheoriesOrDefault怎么用?C# ITestFrameworkDiscoveryOptions.PreEnumerateTheoriesOrDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ITestFrameworkDiscoveryOptions的用法示例。


在下文中一共展示了ITestFrameworkDiscoveryOptions.PreEnumerateTheoriesOrDefault方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Discover

        /// <summary>
        /// Discover test cases from a test method.
        /// </summary>
        /// <remarks>
        /// This method performs the following steps:
        /// - If the theory attribute is marked with Skip, returns the single test case from <see cref="CreateTestCaseForSkip"/>;
        /// - If pre-enumeration is off, or any of the test data is non serializable, returns the single test case from <see cref="CreateTestCaseForTheory"/>;
        /// - If there is no theory data, returns a single test case of <see cref="ExecutionErrorTestCase"/> with the error in it;
        /// - Otherwise, it returns one test case per data row, created by calling <see cref="CreateTestCaseForDataRow"/>.
        /// </remarks>
        /// <param name="discoveryOptions">The discovery options to be used.</param>
        /// <param name="testMethod">The test method the test cases belong to.</param>
        /// <param name="theoryAttribute">The theory attribute attached to the test method.</param>
        /// <returns>Returns zero or more test cases represented by the test method.</returns>
        public virtual IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo theoryAttribute)
        {
            // Special case Skip, because we want a single Skip (not one per data item); plus, a skipped test may
            // not actually have any data (which is quasi-legal, since it's skipped).
            var skipReason = theoryAttribute.GetNamedArgument<string>("Skip");
            if (skipReason != null)
                return new[] { CreateTestCaseForSkip(discoveryOptions, testMethod, theoryAttribute, skipReason) };

            if (discoveryOptions.PreEnumerateTheoriesOrDefault())
            {
                try
                {
                    var dataAttributes = testMethod.Method.GetCustomAttributes(typeof(DataAttribute));
                    var results = new List<IXunitTestCase>();

                    foreach (var dataAttribute in dataAttributes)
                    {
                        var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();
                        var discoverer = ExtensibilityPointFactory.GetDataDiscoverer(diagnosticMessageSink, discovererAttribute);
                        if (!discoverer.SupportsDiscoveryEnumeration(dataAttribute, testMethod.Method))
                            return new[] { CreateTestCaseForTheory(discoveryOptions, testMethod, theoryAttribute) };

                        // GetData may return null, but that's okay; we'll let the NullRef happen and then catch it
                        // down below so that we get the composite test case.
                        foreach (var dataRow in discoverer.GetData(dataAttribute, testMethod.Method))
                        {
                            // Determine whether we can serialize the test case, since we need a way to uniquely
                            // identify a test and serialization is the best way to do that. If it's not serializable,
                            // this will throw and we will fall back to a single theory test case that gets its data at runtime.
                            if (!SerializationHelper.IsSerializable(dataRow))
                                return new[] { CreateTestCaseForTheory(discoveryOptions, testMethod, theoryAttribute) };

                            var testCase = CreateTestCaseForDataRow(discoveryOptions, testMethod, theoryAttribute, dataRow);
                            results.Add(testCase);
                        }
                    }

                    if (results.Count == 0)
                        results.Add(new ExecutionErrorTestCase(diagnosticMessageSink,
                                                               discoveryOptions.MethodDisplayOrDefault(),
                                                               testMethod,
                                                               $"No data found for {testMethod.TestClass.Class.Name}.{testMethod.Method.Name}"));

                    return results;
                }
                catch { }  // If something goes wrong, fall through to return just the XunitTestCase
            }

            return new[] { CreateTestCaseForTheory(discoveryOptions, testMethod, theoryAttribute) };
        }
开发者ID:antonfirsov,项目名称:xunit,代码行数:64,代码来源:TheoryDiscoverer.cs

示例2: Discover

        /// <inheritdoc/>
        public IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
        {
            var defaultMethodDisplay = discoveryOptions.MethodDisplayOrDefault();

            // Special case Skip, because we want a single Skip (not one per data item), and a skipped test may
            // not actually have any data (which is quasi-legal, since it's skipped).
            if (factAttribute.GetNamedArgument<string>("Skip") != null)
                return new[] { new XunitTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod) };

            var dataAttributes = testMethod.Method.GetCustomAttributes(typeof(DataAttribute));

            if (discoveryOptions.PreEnumerateTheoriesOrDefault())
            {
                try
                {
                    var results = new List<XunitTestCase>();

                    foreach (var dataAttribute in dataAttributes)
                    {
                        var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();
                        var discoverer = ExtensibilityPointFactory.GetDataDiscoverer(diagnosticMessageSink, discovererAttribute);
                        if (!discoverer.SupportsDiscoveryEnumeration(dataAttribute, testMethod.Method))
                            return new XunitTestCase[] { new XunitTheoryTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod) };

                        // GetData may return null, but that's okay; we'll let the NullRef happen and then catch it
                        // down below so that we get the composite test case.
                        foreach (var dataRow in discoverer.GetData(dataAttribute, testMethod.Method))
                        {
                            // Attempt to serialize the test case, since we need a way to uniquely identify a test
                            // and serialization is the best way to do that. If it's not serializable, this will
                            // throw and we will fall back to a single theory test case that gets its data
                            // at runtime.
                            var testCase = new XunitTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod, dataRow);
                            SerializationHelper.Serialize(testCase);
                            results.Add(testCase);
                        }
                    }

                    if (results.Count == 0)
                        results.Add(new ExecutionErrorTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod,
                                                               String.Format("No data found for {0}.{1}", testMethod.TestClass.Class.Name, testMethod.Method.Name)));

                    return results;
                }
                catch { }  // If there are serialization issues with the theory data, fall through to return just the XunitTestCase
            }

            return new XunitTestCase[] { new XunitTheoryTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod) };
        }
开发者ID:remcomulder,项目名称:xunit,代码行数:50,代码来源:TheoryDiscoverer.cs

示例3: discoverTestCases

        private IEnumerable<IXunitTestCase> discoverTestCases(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod)
        {
            if (!discoveryOptions.PreEnumerateTheoriesOrDefault())
                return new XunitTestCase[] { new XunitTheoryTestCase(_messageSink, TestMethodDisplay.Method, testMethod) };

            var withValuesAttributes = testMethod.Method.GetCustomAttributes(typeof(WithValuesAttribute).AssemblyQualifiedName);
            var testCase = new List<XunitTestCase>();

            foreach (var withValuesAttribute in withValuesAttributes)
            {
                var discovererAttribute = withValuesAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute).AssemblyQualifiedName).Single();
                var discoverer = ExtensibilityPointFactory.GetDataDiscoverer(_messageSink, discovererAttribute);
                if (!discoverer.SupportsDiscoveryEnumeration(withValuesAttribute, testMethod.Method))
                    return new XunitTestCase[] { new XunitTheoryTestCase(_messageSink, TestMethodDisplay.Method, testMethod) };

                foreach (var data in discoverer.GetData(withValuesAttribute, testMethod.Method))
                {
                    var test = new XunitTestCase(_messageSink, TestMethodDisplay.Method, testMethod, data);
                    testCase.Add(test);
                }
            }

            return testCase;
        }
开发者ID:danielcirket,项目名称:TestAttributes,代码行数:24,代码来源:TestAttributesDiscoverer.cs

示例4: Discover

        /// <summary>
        /// Discover test cases from a test method.
        /// </summary>
        /// <remarks>
        /// This method performs the following steps:
        /// - If the theory attribute is marked with Skip, returns the single test case from <see cref="CreateTestCaseForSkip"/>;
        /// - If pre-enumeration is off, or any of the test data is non serializable, returns the single test case from <see cref="CreateTestCaseForTheory"/>;
        /// - If there is no theory data, returns a single test case of <see cref="ExecutionErrorTestCase"/> with the error in it;
        /// - Otherwise, it returns one test case per data row, created by calling <see cref="CreateTestCaseForDataRow"/> or <see cref="CreateTestCaseForSkippedDataRow"/> if the data attribute has a skip reason.
        /// </remarks>
        /// <param name="discoveryOptions">The discovery options to be used.</param>
        /// <param name="testMethod">The test method the test cases belong to.</param>
        /// <param name="theoryAttribute">The theory attribute attached to the test method.</param>
        /// <returns>Returns zero or more test cases represented by the test method.</returns>
        public virtual IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo theoryAttribute)
        {
            // Special case Skip, because we want a single Skip (not one per data item); plus, a skipped test may
            // not actually have any data (which is quasi-legal, since it's skipped).
            var skipReason = theoryAttribute.GetNamedArgument<string>("Skip");
            if (skipReason != null)
                return new[] { CreateTestCaseForSkip(discoveryOptions, testMethod, theoryAttribute, skipReason) };

            if (discoveryOptions.PreEnumerateTheoriesOrDefault())
            {
                try
                {
                    var dataAttributes = testMethod.Method.GetCustomAttributes(typeof(DataAttribute));
                    var results = new List<IXunitTestCase>();

                    foreach (var dataAttribute in dataAttributes)
                    {
                        var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();
                        var discoverer = ExtensibilityPointFactory.GetDataDiscoverer(diagnosticMessageSink, discovererAttribute);
                        skipReason = dataAttribute.GetNamedArgument<string>("Skip");

                        if (!discoverer.SupportsDiscoveryEnumeration(dataAttribute, testMethod.Method))
                            return new[] { CreateTestCaseForTheory(discoveryOptions, testMethod, theoryAttribute) };

                        IEnumerable<object[]> data = discoverer.GetData(dataAttribute, testMethod.Method);
                        if (data == null)
                        {
                            results.Add(new ExecutionErrorTestCase(diagnosticMessageSink,
                                          discoveryOptions.MethodDisplayOrDefault(),
                                          testMethod,
                                          $"Test data returned null for {testMethod.TestClass.Class.Name}.{testMethod.Method.Name}. Make sure it is statically initialized before this test method is called."));
                            continue;
                        }
                        foreach (var dataRow in data)
                        {
                            // Determine whether we can serialize the test case, since we need a way to uniquely
                            // identify a test and serialization is the best way to do that. If it's not serializable,
                            // this will throw and we will fall back to a single theory test case that gets its data at runtime.
                            if (!SerializationHelper.IsSerializable(dataRow))
                            {
                                diagnosticMessageSink.OnMessage(new DiagnosticMessage($"Non-serializable data ('{dataRow.GetType().FullName}') found for '{testMethod.TestClass.Class.Name}.{testMethod.Method.Name}'; falling back to single test case."));
                                return new[] { CreateTestCaseForTheory(discoveryOptions, testMethod, theoryAttribute) };
                            }

                            var testCase =
                                skipReason != null
                                    ? CreateTestCaseForSkippedDataRow(discoveryOptions, testMethod, theoryAttribute, dataRow, skipReason)
                                    : CreateTestCaseForDataRow(discoveryOptions, testMethod, theoryAttribute, dataRow);

                            results.Add(testCase);
                        }
                    }

                    if (results.Count == 0)
                        results.Add(new ExecutionErrorTestCase(diagnosticMessageSink,
                                                               discoveryOptions.MethodDisplayOrDefault(),
                                                               testMethod,
                                                               $"No data found for {testMethod.TestClass.Class.Name}.{testMethod.Method.Name}"));

                    return results;
                }
                catch (Exception ex)    // If something goes wrong, fall through to return just the XunitTestCase
                {
                    diagnosticMessageSink.OnMessage(new DiagnosticMessage($"Exception thrown during theory discovery on '{testMethod.TestClass.Class.Name}.{testMethod.Method.Name}'; falling back to single test case.{Environment.NewLine}{ex}"));
                }
            }

            return new[] { CreateTestCaseForTheory(discoveryOptions, testMethod, theoryAttribute) };
        }
开发者ID:JeremyCade,项目名称:xunit,代码行数:83,代码来源:TheoryDiscoverer.cs


注:本文中的ITestFrameworkDiscoveryOptions.PreEnumerateTheoriesOrDefault方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。