Skip to content

Instantly share code, notes, and snippets.

@omerfarukz
Created December 16, 2013 20:51
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 omerfarukz/7994182 to your computer and use it in GitHub Desktop.
Save omerfarukz/7994182 to your computer and use it in GitHub Desktop.
simple console test executer
using System;
using System.Linq;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var tests = typeof(Program)
.GetMethods()
.Where(f => f.GetCustomAttribute(typeof(TestMethodAttribute)) != null)
.OrderBy(f => f.Name)
.ToArray();
//TODO: group by category name
ListTests(tests);
int order = -1;
while (order <= tests.Length)
{
int.TryParse(Console.ReadLine(), out order);
if (order > 0 && order <= tests.Length)
{
Console.WriteLine("Calling method [{0}]\n\n", order);
tests[order - 1].Invoke(null, null);
}
else
{
Console.WriteLine("Test is out of index");
}
ListTests(tests);
}
Console.WriteLine("!!! Program terminated");
Console.ReadKey();
}
private static void ListTests(MethodInfo[] tests)
{
Console.WriteLine("\n\nListing all test methods");
for (int i = 0; i < tests.Length; i++)
{
Console.WriteLine("{0}. {1}", i + 1, tests[i].Name);
}
Console.WriteLine("Please select test method...\n\n");
}
[TestMethod("Test 1", "Test 1 description")]
public static void Test1()
{
System.Console.WriteLine("test 1");
}
[TestMethod("Cat1", "test two description", Order = 2)]
public static void Test2()
{
System.Console.WriteLine("test 22");
}
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
class TestMethodAttribute : Attribute
{
public string Category { get; set; }
public string Name { get; set; }
public int Order { get; set; }
public TestMethodAttribute(string category, string name)
{
Order = Int32.MaxValue;
Category = category;
Name = name;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment