MultiAssemblyTestEnvironment.cs :  » Testing » xUnit.net » Xunit » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Testing » xUnit.net 
xUnit.net » Xunit » MultiAssemblyTestEnvironment.cs
using System;
using System.Collections.Generic;
using Xunit.Sdk;

namespace Xunit{
    /// <summary>
    /// Represents the ability to load and unload test assemblies, as well as enumerate
    /// the test assemblies, the test methods, and run tests.
    /// </summary>
    public class MultiAssemblyTestEnvironment : ITestMethodEnumerator, ITestMethodRunner
    {
        /// <summary>
        /// The test assemblies loaded into the environment.
        /// </summary>
        protected List<TestAssembly> testAssemblies = new List<TestAssembly>();

        /// <summary>
        /// Enumerates the test assemblies in the environment.
        /// </summary>
        public IEnumerable<TestAssembly> EnumerateTestAssemblies()
        {
            return testAssemblies;
        }

        /// <inheritdoc/>
        public IEnumerable<TestMethod> EnumerateTestMethods()
        {
            return EnumerateTestMethods(testMethod => true);
        }

        /// <inheritdoc/>
        public IEnumerable<TestMethod> EnumerateTestMethods(Predicate<TestMethod> filter)
        {
            Guard.ArgumentNotNull("filter", filter);

            foreach (TestAssembly testAssembly in testAssemblies)
                foreach (TestMethod method in testAssembly.EnumerateTestMethods(filter))
                    yield return method;
        }

        /// <summary>
        /// Enumerates the traits across all the loaded assemblies.
        /// </summary>
        public MultiValueDictionary<string, string> EnumerateTraits()
        {
            var results = new MultiValueDictionary<string, string>();

            foreach (TestAssembly testAssembly in testAssemblies)
                foreach (TestClass testClass in testAssembly.EnumerateClasses())
                    foreach (TestMethod testMethod in testClass.EnumerateTestMethods())
                        testMethod.Traits.ForEach((name, value) => results.AddValue(name, value));

            return results;
        }

        /// <summary>
        /// Loads the specified assembly, using the default configuration file.
        /// </summary>
        /// <param name="assemblyFilename">The assembly filename.</param>
        /// <returns>The <see cref="TestAssembly"/> which represents the newly
        /// loaded test assembly.</returns>
        public TestAssembly Load(string assemblyFilename)
        {
            return Load(assemblyFilename, null, true);
        }

        /// <summary>
        /// Loads the specified assembly using the specified configuration file.
        /// </summary>
        /// <param name="assemblyFilename">The assembly filename.</param>
        /// <param name="configFilename">The config filename.</param>
        /// <returns>The <see cref="TestAssembly"/> which represents the newly
        /// loaded test assembly.</returns>
        public TestAssembly Load(string assemblyFilename, string configFilename)
        {
            return Load(assemblyFilename, configFilename, true);
        }

        /// <summary>
        /// Loads the specified assembly using the specified configuration file.
        /// </summary>
        /// <param name="assemblyFilename">The assembly filename.</param>
        /// <param name="configFilename">The config filename.</param>
        /// <param name="shadowCopy">Whether the DLLs should be shadow copied.</param>
        /// <returns>The <see cref="TestAssembly"/> which represents the newly
        /// loaded test assembly.</returns>
        public TestAssembly Load(string assemblyFilename, string configFilename, bool shadowCopy)
        {
            Guard.ArgumentNotNull("assemblyFilename", assemblyFilename);

            return Load(new ExecutorWrapper(assemblyFilename, configFilename, shadowCopy));
        }

        /// <summary>
        /// Adds the assembly loaded into the given <see cref="IExecutorWrapper"/>
        /// into the environment.
        /// </summary>
        /// <param name="executorWrapper">The executor wrapper.</param>
        /// <returns>The <see cref="TestAssembly"/> which represents the newly
        /// loaded test assembly.</returns>
        protected TestAssembly Load(IExecutorWrapper executorWrapper)
        {
            Guard.ArgumentNotNull("executorWrapper", executorWrapper);

            TestAssembly testAssembly = TestAssemblyBuilder.Build(executorWrapper);

            testAssemblies.Add(testAssembly);

            return testAssembly;
        }

        /// <inheritdoc/>
        public void Run(IEnumerable<TestMethod> testMethods, ITestMethodRunnerCallback callback)
        {
            Guard.ArgumentNotNullOrEmpty("testMethods", testMethods);
            Guard.ArgumentNotNull("callback", callback);

            var sortedMethods = new Dictionary<TestAssembly, List<TestMethod>>();

            foreach (TestAssembly testAssembly in testAssemblies)
                sortedMethods[testAssembly] = new List<TestMethod>();

            foreach (TestMethod testMethod in testMethods)
            {
                List<TestMethod> methodList = null;

                if (!sortedMethods.TryGetValue(testMethod.TestClass.TestAssembly, out methodList))
                    throw new ArgumentException("Test method " + testMethod.MethodName +
                                                " on test class " + testMethod.TestClass.TypeName +
                                                " in test assembly " + testMethod.TestClass.TestAssembly.AssemblyFilename +
                                                " is not in this test environment", "testMethods");

                methodList.Add(testMethod);
            }

            foreach (var kvp in sortedMethods)
                if (kvp.Value.Count > 0)
                    kvp.Key.Run(kvp.Value, callback);
        }

        /// <summary>
        /// Unloads the specified assembly.
        /// </summary>
        /// <param name="assembly">The assembly to unload.</param>
        public void Unload(TestAssembly assembly)
        {
            Guard.ArgumentNotNull("assembly", assembly);
            Guard.ArgumentValid("assembly", "Assembly not loaded in this environment", testAssemblies.Contains(assembly));

            testAssemblies.Remove(assembly);
            assembly.Dispose();
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.