diff --git a/sdk/core/Azure.Core.TestFramework/src/PlaybackOnlyAttribute.cs b/sdk/core/Azure.Core.TestFramework/src/PlaybackOnlyAttribute.cs new file mode 100644 index 000000000000..706390608816 --- /dev/null +++ b/sdk/core/Azure.Core.TestFramework/src/PlaybackOnlyAttribute.cs @@ -0,0 +1,46 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using NUnit.Framework; +using NUnit.Framework.Interfaces; +using NUnit.Framework.Internal; + +namespace Azure.Core.TestFramework +{ + /// + /// Attribute on test assemblies, classes, or methods that run only against playback resources. + /// + [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true, Inherited = true)] + public class PlaybackOnlyAttribute : NUnitAttribute, IApplyToTest + { + private readonly string _reason; + + /// + /// Constructs the attribute giving a reason the associated tests is playback only. + /// + public PlaybackOnlyAttribute(string reason) + { + _reason = reason; + } + + /// + /// Modifies the by adding categories to it and changing the run state as needed. + /// + /// The to modify. + public void ApplyToTest(Test test) + { + test.Properties.Add("Category", "Playback"); + + if (test.RunState != RunState.NotRunnable) + { + RecordedTestMode mode = RecordedTestUtilities.GetModeFromEnvironment(); + if (mode != RecordedTestMode.Playback) + { + test.RunState = RunState.Ignored; + test.Properties.Set("_SKIPREASON", $"Playback tests will not run when AZURE_TEST_MODE is {mode}. This test was skipped for the following reason: {_reason}"); + } + } + } + } +}