Skip to content

Commit

Permalink
Adding 'TestCase' trait attribute (#13)
Browse files Browse the repository at this point in the history
thanks for contributing!
  • Loading branch information
alexandrevribeiro authored and brendanconnolly committed Jun 23, 2019
1 parent b667319 commit 8d8177d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Xunit.Categories/TestCaseAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using Xunit.Sdk;

namespace Xunit.Categories
{
/// <summary>
/// For annotating tests that relate to a specific Test Case
/// </summary>
[TraitDiscoverer(TestCaseDiscoverer.DiscovererTypeName, DiscovererUtil.AssemblyName)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
public class TestCaseAttribute : Attribute, ITraitAttribute
{
public string TestCaseId { get; private set; }

public TestCaseAttribute(string testCaseId)
{
this.TestCaseId = testCaseId;
}

public TestCaseAttribute(long testCaseId)
{
this.TestCaseId = testCaseId.ToString();
}

public TestCaseAttribute()
{
}
}
}
28 changes: 28 additions & 0 deletions src/Xunit.Categories/TestCaseDiscoverer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Generic;
using Xunit.Abstractions;
using Xunit.Sdk;

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

/// <summary>
/// Gets the trait values from the trait attribute.
/// </summary>
/// <param name="traitAttribute"></param>
/// <returns></returns>
public IEnumerable<KeyValuePair<string, string>> GetTraits(IAttributeInfo traitAttribute)
{
var testCaseId = traitAttribute.GetNamedArgument<string>("TestCaseId");


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

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

}
}
}
9 changes: 9 additions & 0 deletions test/Xunit.Categories.Test/Scratch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,14 @@ public void TestSystemTest()
{
throw new NotImplementedException("request #8 - This is more black box, high level, and stress testing of a complete running application different to an IntegrationTest");
}

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

0 comments on commit 8d8177d

Please sign in to comment.