Skip to content

Instantly share code, notes, and snippets.

@Scooletz
Created July 28, 2013 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Scooletz/6099052 to your computer and use it in GitHub Desktop.
Save Scooletz/6099052 to your computer and use it in GitHub Desktop.
A simple way of wrapping a test in a time measuring scope.
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class MeasureTimeAttribute : Attribute, ITestAction
{
private const string Key = "stopWatch";
public void BeforeTest(TestDetails testDetails)
{
TestContext.CurrentContext.Test.Properties[Key] = Stopwatch.StartNew();
}
public void AfterTest(TestDetails testDetails)
{
var sw = (Stopwatch) TestContext.CurrentContext.Test.Properties[Key];
sw.Stop();
Console.WriteLine("Test took: " + sw.Elapsed.ToString("g"));
}
public ActionTargets Targets { get{return ActionTargets.Test;}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment