< Summary

Class:Azure.Core.TestFramework.ClientTestFixtureAttribute
Assembly:Azure.Core.TestFramework
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\ClientTestFixtureAttribute.cs
Covered lines:64
Uncovered lines:3
Coverable lines:67
Total lines:182
Line coverage:95.5% (64 of 67)
Covered branches:52
Total branches:52
Branch coverage:100% (52 of 52)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-100%100%
.ctor(...)-100%100%
.ctor(...)-100%100%
BuildFrom(...)-0%100%
BuildFrom()-100%100%
GeneratePermutations()-100%100%
Process(...)-100%100%
ProcessTest(...)-100%100%
NUnit.Framework.Interfaces.IPreFilter.IsMatch(...)-0%100%
NUnit.Framework.Interfaces.IPreFilter.IsMatch(...)-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\ClientTestFixtureAttribute.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Linq;
 7using System.Reflection;
 8using NUnit.Framework;
 9using NUnit.Framework.Interfaces;
 10using NUnit.Framework.Internal;
 11
 12namespace Azure.Core.TestFramework
 13{
 14    public class ClientTestFixtureAttribute : NUnitAttribute, IFixtureBuilder2, IPreFilter
 15    {
 6616        public static readonly string SyncOnlyKey = "SyncOnly";
 6617        public static readonly string RecordingDirectorySuffixKey = "RecordingDirectory";
 18
 19        private readonly object[] _additionalParameters;
 20        private readonly object[] _serviceVersions;
 21        private readonly int? _maxServiceVersion;
 22
 23        /// <summary>
 24        /// Initializes an instance of the <see cref="ClientTestFixtureAttribute"/> accepting additional fixture paramet
 25        /// </summary>
 26        /// <param name="serviceVersions">The set of service versions that will be passed to the test suite.</param>
 47227        public ClientTestFixtureAttribute(params object[] serviceVersions) : this(serviceVersions: serviceVersions, defa
 47228        { }
 29
 30        /// <summary>
 31        /// Initializes an instance of the <see cref="ClientTestFixtureAttribute"/> accepting additional fixture paramet
 32        /// </summary>
 33        /// <param name="serviceVersions">The set of service versions that will be passed to the test suite.</param>
 34        /// <param name="additionalParameters">An array of additional parameters that will be passed to the test suite.<
 48435        public ClientTestFixtureAttribute(object[] serviceVersions, object[] additionalParameters)
 36        {
 48437            _additionalParameters = additionalParameters ?? new object[] { };
 48438            _serviceVersions = serviceVersions ?? new object[] { };
 39
 28240            _maxServiceVersion = _serviceVersions.Any() ? _serviceVersions.Max(s => Convert.ToInt32(s)) : (int?)null;
 48441        }
 42
 43        public IEnumerable<TestSuite> BuildFrom(ITypeInfo typeInfo)
 44        {
 045            return BuildFrom(typeInfo, this);
 46        }
 47
 48        public IEnumerable<TestSuite> BuildFrom(ITypeInfo typeInfo, IPreFilter filter)
 49        {
 50
 48451            var suitePermutations = GeneratePermutations();
 52
 342453            foreach (var (fixture, isAsync, serviceVersion, parameter) in suitePermutations)
 54            {
 491255                foreach (TestSuite testSuite in fixture.BuildFrom(typeInfo, filter))
 56                {
 122857                    Process(testSuite, serviceVersion, isAsync, parameter);
 122858                    yield return testSuite;
 59                }
 122860            }
 48461        }
 62
 63        private List<(TestFixtureAttribute suite, bool isAsync, object serviceVersion, object parameter)> GeneratePermut
 64        {
 48465            var result = new List<(TestFixtureAttribute suite, bool isAsync, object serviceVersion, object parameter)>()
 66
 48467            if (_serviceVersions.Any())
 68            {
 52469                foreach (object serviceVersion in _serviceVersions)
 70                {
 18871                    if (_additionalParameters.Any())
 72                    {
 3673                        foreach (var parameter in _additionalParameters)
 74                        {
 1275                            result.Add((new TestFixtureAttribute(false, serviceVersion, parameter), false, serviceVersio
 1276                            result.Add((new TestFixtureAttribute(true, serviceVersion, parameter), true, serviceVersion,
 77                        }
 78                    }
 79                    else
 80                    {
 81                        // No additional parameters defined
 18282                        result.Add((new TestFixtureAttribute(false, serviceVersion), false, serviceVersion, null));
 18283                        result.Add((new TestFixtureAttribute(true, serviceVersion), true, serviceVersion, null));
 84                    }
 85                }
 86            }
 87            else
 88            {
 41089                if (_additionalParameters.Any())
 90                {
 6091                    foreach (var parameter in _additionalParameters)
 92                    {
 2093                        result.Add((new TestFixtureAttribute(false, parameter), false, null, parameter));
 2094                        result.Add((new TestFixtureAttribute(true, parameter), true, null, parameter));
 95                    }
 96                }
 97                else
 98                {
 99                    // No additional parameters defined
 400100                    result.Add((new TestFixtureAttribute(false), false, null, null));
 400101                    result.Add((new TestFixtureAttribute(true), true, null, null));
 102                }
 103            }
 104
 484105            return result;
 106        }
 107
 108        private void Process(TestSuite testSuite, object serviceVersion, bool isAsync, object parameter)
 109        {
 1228110            var serviceVersionNumber = Convert.ToInt32(serviceVersion);
 43216111            foreach (Test test in testSuite.Tests)
 112            {
 20380113                if (test is ParameterizedMethodSuite parameterizedMethodSuite)
 114                {
 10420115                    foreach (Test parameterizedTest in parameterizedMethodSuite.Tests)
 116                    {
 4452117                        ProcessTest(serviceVersion, isAsync, serviceVersionNumber, parameter, parameterizedTest);
 118                    }
 119                }
 120                else
 121                {
 19622122                    ProcessTest(serviceVersion, isAsync, serviceVersionNumber, parameter, test);
 123                }
 124            }
 1228125        }
 126
 127        private void ProcessTest(object serviceVersion, bool isAsync, int serviceVersionNumber, object parameter, Test t
 128        {
 24074129            if (parameter != null)
 130            {
 544131                test.Properties.Set(RecordingDirectorySuffixKey, parameter.ToString());
 132            }
 24074133            if (test.GetCustomAttributes<SyncOnlyAttribute>(true).Any())
 134            {
 144135                test.Properties.Set(SyncOnlyKey, true);
 144136                if (isAsync)
 137                {
 72138                    test.RunState = RunState.Ignored;
 72139                    test.Properties.Set("_SKIPREASON", $"Test ignored in async run because it's marked with {nameof(Sync
 140                }
 141            }
 142
 24074143            if (!isAsync && test.GetCustomAttributes<AsyncOnlyAttribute>(true).Any())
 144            {
 79145                test.RunState = RunState.Ignored;
 79146                test.Properties.Set("_SKIPREASON", $"Test ignored in sync run because it's marked with {nameof(AsyncOnly
 147            }
 148
 24074149            if (serviceVersion == null)
 150            {
 6044151                return;
 152            }
 153
 18030154            if (serviceVersionNumber != _maxServiceVersion)
 155            {
 11628156                test.Properties.Add("SkipRecordings", $"Test is ignored when not running live because the service versio
 157            }
 158
 18030159            var minServiceVersion = test.GetCustomAttributes<ServiceVersionAttribute>(true);
 38784160            foreach (ServiceVersionAttribute serviceVersionAttribute in minServiceVersion)
 161            {
 2820162                if (serviceVersionAttribute.Min != null &&
 2820163                    Convert.ToInt32(serviceVersionAttribute.Min) > serviceVersionNumber)
 164                {
 1662165                    test.RunState = RunState.Ignored;
 1662166                    test.Properties.Set("_SKIPREASON", $"Test ignored because it's minimum service version is set to {se
 167                }
 168
 2820169                if (serviceVersionAttribute.Max != null &&
 2820170                    Convert.ToInt32(serviceVersionAttribute.Max) < serviceVersionNumber)
 171                {
 12172                    test.RunState = RunState.Ignored;
 12173                    test.Properties.Set("_SKIPREASON", $"Test ignored because it's maximum service version is set to {se
 174                }
 175            }
 18030176        }
 177
 0178        bool IPreFilter.IsMatch(Type type) => true;
 179
 0180        bool IPreFilter.IsMatch(Type type, MethodInfo method) => true;
 181    }
 182}