Skip to content

Commit

Permalink
Add LocalTest attribute and discoverer
Browse files Browse the repository at this point in the history
  • Loading branch information
stoberman37 committed Jan 14, 2022
1 parent fbaa2ea commit 54825ed
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Xunit.Categories/LocalTestAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using Xunit.Sdk;

namespace Xunit.Categories
{
/// <summary>
/// For tests that should only be executed locally and excluded from automated pipeline runs
/// </summary>
/// <example>trying out LINQ for the first time, writing a piece of code to understand IEnumerable.Take and Skip.</example>
[TraitDiscoverer(LocalTestDiscoverer.DiscovererTypeName, DiscovererUtil.AssemblyName)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public class LocalTestAttribute:Attribute,ITraitAttribute
{
public LocalTestAttribute(string id)
{
this.Id = id;
}

public LocalTestAttribute(long id)
{
this.Id = id.ToString();
}

public LocalTestAttribute()
{

}

public string Id { get; }
}
}
23 changes: 23 additions & 0 deletions src/Xunit.Categories/LocalTestDiscoverer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace Xunit.Categories
{
public class LocalTestDiscoverer : ITraitDiscoverer
{
internal const string DiscovererTypeName = DiscovererUtil.AssemblyName + "." + nameof(LocalTestDiscoverer);

public IEnumerable<KeyValuePair<string, string>> GetTraits(IAttributeInfo traitAttribute)
{
var id = traitAttribute.GetNamedArgument<string>("Id");


yield return new KeyValuePair<string, string>("Category", "LocalTest");

if (!string.IsNullOrWhiteSpace(id))
yield return new KeyValuePair<string, string>("LocalTest", id);

}
}
}
9 changes: 9 additions & 0 deletions test/Xunit.Categories.Test/Scratch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,14 @@ public void TestTestCase()
{
throw new NotImplementedException("I'm not that important, all the world knows about me, I'll be fixed in 2030");
}

[Fact]
[LocalTest]
[LocalTest(666)]
[LocalTest("666 a")]
public void TestLocalTest()
{
throw new NotImplementedException("I'm not that important, all the world knows about me, I'll be fixed in 2030");
}
}
}

0 comments on commit 54825ed

Please sign in to comment.