Hitta testmetoder via reflexion
This commit is contained in:
@ -30,10 +30,7 @@ namespace TestRunner
|
|||||||
|
|
||||||
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
|
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
var testClass = new TestingLib.TestThePaymentDate();
|
TestingLib.Assert.RunTests();
|
||||||
testClass.DateIs30DaysInFuture();
|
|
||||||
testClass.ReturnsMondayIfProposedIsSaturday();
|
|
||||||
testClass.ReturnsMondayIfProposedIsSunday();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,10 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Reflection;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace TestingLib
|
namespace TestingLib
|
||||||
@ -32,5 +35,24 @@ namespace TestingLib
|
|||||||
testInfo.MethodName = methodName;
|
testInfo.MethodName = methodName;
|
||||||
TestResults.Add(testInfo);
|
TestResults.Add(testInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void RunTests()
|
||||||
|
{
|
||||||
|
foreach (MethodInfo method in GetTestMethods())
|
||||||
|
{
|
||||||
|
var instanceOfTestClass = Activator.CreateInstance(method.DeclaringType);
|
||||||
|
method.Invoke(instanceOfTestClass, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private static List<MethodInfo> GetTestMethods()
|
||||||
|
{
|
||||||
|
var q = from t in Assembly.GetExecutingAssembly().GetTypes()
|
||||||
|
from m in t.GetMethods()
|
||||||
|
from att in m.GetCustomAttributes(true)
|
||||||
|
where att.GetType() == typeof(CheckThisMethod)
|
||||||
|
select m;
|
||||||
|
|
||||||
|
return q.ToList();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
11
TestingLib/CheckThisMethod.cs
Normal file
11
TestingLib/CheckThisMethod.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace TestingLib
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Method)]
|
||||||
|
public class CheckThisMethod : Attribute
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -7,6 +7,7 @@ namespace TestingLib
|
|||||||
{
|
{
|
||||||
public class TestThePaymentDate
|
public class TestThePaymentDate
|
||||||
{
|
{
|
||||||
|
[CheckThisMethod]
|
||||||
public void DateIs30DaysInFuture()
|
public void DateIs30DaysInFuture()
|
||||||
{
|
{
|
||||||
var pd = new SUT.PaymentSystem.PaymentDate();
|
var pd = new SUT.PaymentSystem.PaymentDate();
|
||||||
@ -17,6 +18,7 @@ namespace TestingLib
|
|||||||
Assert.AreEqual(futureDate, sampleDate.AddDays(30), $"Expected date is not 30 days in the future.");
|
Assert.AreEqual(futureDate, sampleDate.AddDays(30), $"Expected date is not 30 days in the future.");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
[CheckThisMethod]
|
||||||
public void ReturnsMondayIfProposedIsSunday()
|
public void ReturnsMondayIfProposedIsSunday()
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -26,6 +28,7 @@ namespace TestingLib
|
|||||||
var futureDate = pd.CalculateFuturePaymentDate(sampleDate);
|
var futureDate = pd.CalculateFuturePaymentDate(sampleDate);
|
||||||
Assert.AreEqual(futureDate.DayOfWeek, DayOfWeek.Monday, $"Expected date is not Monday.");
|
Assert.AreEqual(futureDate.DayOfWeek, DayOfWeek.Monday, $"Expected date is not Monday.");
|
||||||
}
|
}
|
||||||
|
[CheckThisMethod]
|
||||||
public void ReturnsMondayIfProposedIsSaturday()
|
public void ReturnsMondayIfProposedIsSaturday()
|
||||||
{
|
{
|
||||||
var pd = new SUT.PaymentSystem.PaymentDate();
|
var pd = new SUT.PaymentSystem.PaymentDate();
|
||||||
|
|||||||
Reference in New Issue
Block a user