79 6

Create your own test suite to run

This set of methods allow to run a test suite without unit test frameworks like XUnit etc.
It can be used across all the C# applications, e.g. UWP
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
public static async Task<List<TestResult>> RunTests() { List<TestResult> res = new List<TestResult>(); // Consider the namespace of the tests: IEnumerable<Type> typelist = GetTypesInNamespace(Assembly.GetExecutingAssembly(), "LiquidTextUWP.Tests"); foreach (Type testClass in typelist) { // Get all the test methods in the class: foreach (MethodInfo method in testClass.GetMethods()) { LTTestAttribute testAttr = method.GetCustomAttributes<LTTestAttribute>().FirstOrDefault(); if (testAttr == null) continue; object instance = Activator.CreateInstance(testClass); try { if (testAttr.IsAsync) await (Task) method.Invoke(instance, null); else method.Invoke(instance, null); res.Add(new TestResult(true)); } catch (Exception e) { res.Add(new TestResult(false, string.Format("{0} - {1}", e.Message, e.InnerException?.Message))); } } } return res; } private static IEnumerable<Type> GetTypesInNamespace(Assembly assembly, string nameSpace) { return assembly.GetTypes().Where(t => (t.Namespace != null) && t.Namespace.StartsWith(nameSpace)); } public struct TestResult { public bool Success { get; set; } public string ErrorMsg { get; set; } public TestResult(bool success, string msg = "") { Success = success; ErrorMsg = msg; } } [AttributeUsage(AttributeTargets.Method)] public class LTTestAttribute : Attribute { public bool IsAsync { get; set; } = false; }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus