Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions sdk/core/Azure.Core.TestFramework/src/PlaybackOnlyAttribute.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Attribute on test assemblies, classes, or methods that run only against playback resources.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple = true, Inherited = true)]
public class PlaybackOnlyAttribute : NUnitAttribute, IApplyToTest
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this prevent someone from being able to re-record the test just by changing the ctor parameter?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which constructor parameter are you referring to?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

: base(async, serviceVersion, null /* RecordedTestMode.Record /* to re-record */)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume so, yes, but this is by design. The workflow to re-record would be to comment out the attribute.

{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a "reason" required ctor parameter.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

private readonly string _reason;

/// <summary>
/// Constructs the attribute giving a reason the associated tests is playback only.
/// </summary>
public PlaybackOnlyAttribute(string reason)
{
_reason = reason;
}

/// <summary>
/// Modifies the <paramref name="test"/> by adding categories to it and changing the run state as needed.
/// </summary>
/// <param name="test">The <see cref="Test"/> to modify.</param>
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}");
}
}
}
}
}