Skip to content

Instantly share code, notes, and snippets.

@RhysC
Last active December 15, 2015 00:59
Show Gist options
  • Save RhysC/5176526 to your computer and use it in GitHub Desktop.
Save RhysC/5176526 to your computer and use it in GitHub Desktop.
Simple attribute and reporting for what tests are related to which work items (such as PBI's, QA Testcases, Tasks etc.... whatever you want really). Nothing magic, you just mark your test class or method with the attribute and put in the dataz.
void Main()
{
WorkItemReporter.GetTestCoverageReport(typeof(MyTest).Assembly).Dump();
}
// Define other methods and classes here
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class WorkItemAttribute : Attribute
{
public WorkItemAttribute(WorkItemType workItemType, string id)
{
WorkItem = workItemType;
Id = id;
}
public WorkItemType WorkItem { get; set; }
public string Id { get; set; }
}
public enum WorkItemType
{
PBI,
TestCase,
Task,
Bug
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class ProductBacklogItemAttribute : WorkItemAttribute
{
public ProductBacklogItemAttribute(string id)
: base(WorkItemType.PBI, id)
{ }
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class TestCaseAttribute : WorkItemAttribute
{
public TestCaseAttribute(string id)
: base(WorkItemType.TestCase, id)
{ }
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class TaskAttribute : WorkItemAttribute
{
public TaskAttribute(string id)
: base(WorkItemType.Task, id)
{ }
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class BugAttribute : WorkItemAttribute
{
public BugAttribute(string id)
: base(WorkItemType.Bug, id)
{ }
}
public class WorkItemReporter
{
public static IEnumerable<TestCoverageReportItem> GetTestCoverageReport(Assembly assembly)
{
var allTypes = assembly.GetTypes();
var validTypes = allTypes
.Where(t => t.GetCustomAttributes(typeof(WorkItemAttribute), true).Any())
.Select(TestCoverageReportItem);
var validMeths = allTypes
.SelectMany(t => t.GetMethods())
.Where(m => m.GetCustomAttributes(typeof(WorkItemAttribute), true).Any())
.Select(TestCoverageReportItem);
return validTypes.Concat(validMeths);
}
private static TestCoverageReportItem TestCoverageReportItem(Type validType)
{
var att = validType.GetCustomAttribute<WorkItemAttribute>();
return new TestCoverageReportItem
{
Id = att.Id,
TestType = TestType.Fixture,
Type = att.WorkItem,
Name = validType.FullName
};
}
private static TestCoverageReportItem TestCoverageReportItem(MethodInfo validMethod)
{
var att = validMethod.GetCustomAttribute<WorkItemAttribute>();
return new TestCoverageReportItem
{
Id = att.Id,
TestType = TestType.TestMethod,
Type = att.WorkItem,
Name = string.Format("{0} : {1}", validMethod.DeclaringType.FullName, validMethod.Name)
};
}
}
public class TestCoverageReportItem
{
public string Id { get; set; }
public WorkItemType Type { get; set; }
public TestType TestType { get; set; }
public string Name { get; set; }
}
public enum TestType
{
Fixture,
TestMethod
}
[ProductBacklogItem("456")]
public class MyTestFixture
{
[Task("123")]
public void MyTestMethodForTask()
{
}
[ProductBacklogItem("ABC")]
public void MyTestMethodForPbi()
{
}
[Bug("ABC")]
public void MyTestMethodForBug()
{
}
[TestCase("ABC")]
public void MyTestMethodForTestCase()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment