From 852448dfb38fe2439a9a7791e77dc54ffd4b53a7 Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 29 Nov 2023 22:38:26 +0300 Subject: [PATCH 1/6] initial commit --- MyNUnit/MyNUnit.sln | 31 ++++ MyNUnit/MyNUnit/ApplicationForTests.cs | 184 +++++++++++++++++++ MyNUnit/MyNUnit/Attributes.cs | 47 +++++ MyNUnit/MyNUnit/MyNUnit.csproj | 10 + MyNUnit/MyNUnit/Program.cs | 1 + MyNUnit/ProjectForTest/ClassForTests.cs | 62 +++++++ MyNUnit/ProjectForTest/ProjectForTest.csproj | 10 + MyNUnit/TestMyNUnit/TestMyNUnit.csproj | 19 ++ MyNUnit/TestMyNUnit/Usings.cs | 1 + 9 files changed, 365 insertions(+) create mode 100644 MyNUnit/MyNUnit.sln create mode 100644 MyNUnit/MyNUnit/ApplicationForTests.cs create mode 100644 MyNUnit/MyNUnit/Attributes.cs create mode 100644 MyNUnit/MyNUnit/MyNUnit.csproj create mode 100644 MyNUnit/MyNUnit/Program.cs create mode 100644 MyNUnit/ProjectForTest/ClassForTests.cs create mode 100644 MyNUnit/ProjectForTest/ProjectForTest.csproj create mode 100644 MyNUnit/TestMyNUnit/TestMyNUnit.csproj create mode 100644 MyNUnit/TestMyNUnit/Usings.cs diff --git a/MyNUnit/MyNUnit.sln b/MyNUnit/MyNUnit.sln new file mode 100644 index 0000000..47502ef --- /dev/null +++ b/MyNUnit/MyNUnit.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33403.182 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MyNUnit", "MyNUnit\MyNUnit.csproj", "{8A4A83AC-5456-4F54-9183-B053A0C05926}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestMyNUnit", "TestMyNUnit\TestMyNUnit.csproj", "{9508BABD-7DF4-4B36-B442-020EB706C229}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8A4A83AC-5456-4F54-9183-B053A0C05926}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8A4A83AC-5456-4F54-9183-B053A0C05926}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8A4A83AC-5456-4F54-9183-B053A0C05926}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8A4A83AC-5456-4F54-9183-B053A0C05926}.Release|Any CPU.Build.0 = Release|Any CPU + {9508BABD-7DF4-4B36-B442-020EB706C229}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9508BABD-7DF4-4B36-B442-020EB706C229}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9508BABD-7DF4-4B36-B442-020EB706C229}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9508BABD-7DF4-4B36-B442-020EB706C229}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {97E09C33-070C-4D39-BDE1-D7C045292CC6} + EndGlobalSection +EndGlobal diff --git a/MyNUnit/MyNUnit/ApplicationForTests.cs b/MyNUnit/MyNUnit/ApplicationForTests.cs new file mode 100644 index 0000000..2501164 --- /dev/null +++ b/MyNUnit/MyNUnit/ApplicationForTests.cs @@ -0,0 +1,184 @@ +using System.Diagnostics; +using System.Reflection; +using Attributes; + +namespace MyNUnit; + +public class ApplicationForTests +{ + public enum Status + { + Passed, + Failed, + Ignored + } + + public class ResultsTests + { + public string name { get; } + + public long workTime { get; } + + public Exception? reasonFail { get; } + + public Status statusTest { get; } + + public ResultsTests(string name, long workTime, Exception reasonFail, Status statusTest) + { + this.name = name; + this.workTime = workTime; + this.reasonFail = reasonFail; + this.statusTest = statusTest; + } + } + + public readonly List? listOfResults; + private object? locker; + + public ApplicationForTests(string path) + { + var listOfResults = new List(); + var files = Directory.EnumerateFiles(path, "*.dll", SearchOption.AllDirectories); + var classes = files.Select(Assembly.Load) + .SelectMany(a => a.ExportedTypes) + .Where(t => t.IsClass) + .Where(t => t.GetMethods() + .Where(t => t.GetCustomAttributes(true) + .Any(a => a is Test)) + .Any()); + + object? locker = new object(); + + Parallel.ForEach(classes, StartTests); + } + + private MethodInfo[]? GetMethodsByAtributeAndClass(Type _class, Type atribute) + { + return _class.GetMethods() + .Where(m => m.GetCustomAttributes(atribute, false).Length > 0) + .ToArray(); + } + + private void WorkWithClassMethods(Type _class, Type atribute) + { + var methods = GetMethodsByAtributeAndClass(_class, atribute); + LaunchMethods(_class, methods); + } + + private void WorkWithTestMethods(Type _class) + { + var instance = Activator.CreateInstance(_class); + var methodsBefore = GetMethodsByAtributeAndClass(_class, typeof(Before)); + var methodsAfter = GetMethodsByAtributeAndClass(_class, typeof(After)); + var methods = _class.GetMethods(); + foreach(var method in methods) + { + if (method.IsDefined(typeof(Test), true)) + { + RunMethodsBeforeAndAfter(methodsBefore, instance); + RunMethod(_class, method); + RunMethodsBeforeAndAfter(methodsAfter, instance); + } + } + } + private void StartTests(Type _class) + { + WorkWithClassMethods(_class, typeof(BeforeClass)); + WorkWithTestMethods(_class); + WorkWithClassMethods(_class, typeof(AfterClass)); + } + + private void LaunchMethods(Type _class, MethodInfo[]? methods) + { + if (_class == null || methods == null) + { + throw new ArgumentNullException(); + } + + foreach (var method in methods) + { + if (!method.IsStatic) + { + throw new InvalidOperationException(); + } + + method.Invoke(null, null); + } + } + + private void RunMethodsBeforeAndAfter(MethodInfo[]? methods, object? instance) + { + if (instance == null || methods == null) + { + throw new ArgumentNullException(); + } + foreach(var method in methods) + { + method.Invoke(instance, null); + } + } + + private void RunMethod(object? instance, MethodInfo? method) + { + if (instance == null || method == null || locker == null || listOfResults == null) + { + throw new InvalidOperationException(); + } + + var atribute = Attribute.GetCustomAttribute(method, typeof(Test)); + + if (atribute == null) + { + throw new InvalidOperationException(); + } + + if (((Test?)atribute).Ignored != null) + { + lock (locker) + { + listOfResults.Add(new ResultsTests(method.Name, 0, null, Status.Ignored)); + } + return; + } + + var stopWatch = new Stopwatch(); + + try + { + stopWatch.Start(); + method.Invoke(instance, null); + } + catch(Exception ex) + { + stopWatch.Stop(); + if (ex.InnerException != null && ((Test?)atribute) != null && ex.InnerException.GetType() == ((Test?)atribute).Expected) + { + lock (locker) + { + listOfResults.Add(new ResultsTests(method.Name, stopWatch.ElapsedMilliseconds, ex, Status.Passed)); + } + } + else + { + lock (locker) + { + listOfResults.Add(new ResultsTests(method.Name, stopWatch.ElapsedMilliseconds, ex, Status.Failed)); + } + } + return; + } + stopWatch.Stop(); + lock (locker) + { + if (((Test?)atribute) != null && ((Test?)atribute).Expected != null) + { + listOfResults.Add(new ResultsTests(method.Name, stopWatch.ElapsedMilliseconds, null, Status.Failed)); + } + else + { + listOfResults.Add(new ResultsTests(method.Name, stopWatch.ElapsedMilliseconds, null, Status.Passed)); + } + } + } + +} \ No newline at end of file diff --git a/MyNUnit/MyNUnit/Attributes.cs b/MyNUnit/MyNUnit/Attributes.cs new file mode 100644 index 0000000..7bbd9bb --- /dev/null +++ b/MyNUnit/MyNUnit/Attributes.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Attributes; + +[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] +public class Test : Attribute +{ + public Type? Expected { get; set; } + + public string? Ignored { get; set; } +} + +[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] +public class BeforeClass : Attribute +{ + +} + +[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] +public class AfterClass : Attribute +{ + +} + +[AttributeUsage(AttributeTargets.All)] +public class Before : Attribute +{ + [Before] + public void Method() + { + + } +} + +[AttributeUsage(AttributeTargets.All)] +public class After : Attribute +{ + [After] + public void Method() + { + + } +} \ No newline at end of file diff --git a/MyNUnit/MyNUnit/MyNUnit.csproj b/MyNUnit/MyNUnit/MyNUnit.csproj new file mode 100644 index 0000000..dc2bb05 --- /dev/null +++ b/MyNUnit/MyNUnit/MyNUnit.csproj @@ -0,0 +1,10 @@ + + + + Library + net7.0 + enable + enable + + + diff --git a/MyNUnit/MyNUnit/Program.cs b/MyNUnit/MyNUnit/Program.cs new file mode 100644 index 0000000..5f28270 --- /dev/null +++ b/MyNUnit/MyNUnit/Program.cs @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/MyNUnit/ProjectForTest/ClassForTests.cs b/MyNUnit/ProjectForTest/ClassForTests.cs new file mode 100644 index 0000000..246e0f5 --- /dev/null +++ b/MyNUnit/ProjectForTest/ClassForTests.cs @@ -0,0 +1,62 @@ +namespace ProjectForTest; + +using Attributes; + +public class ClassForTests +{ + public int counter = 0; + + [Test] + public void InvalidMethod() + { + throw new NotImplementedException(); + } + + [Test] + public int CorrectMethod() + { + return 1; + } + + [BeforeClass] + public void BeforeClass() + { + counter += 1; + } + + [AfterClass] + public void AfterClass() + { + counter += 1; + } + + [After] + public void BeforeMethod() + { + counter += 1; + } + + [Before] + public void AfterMethod() + { + counter += 1; + } + + [Test(Ignored = "Ignore")] + public void IgnoreTest() + { + ; + } + + [Test(Expected = typeof(InvalidCastException))] + public void InvalidCastException() + { + throw new InvalidCastException(); + } + + [Test(Expected = typeof(InvalidOperationException))] + public void InvalidException() + { + throw new InvalidProgramException(); + } +} \ No newline at end of file diff --git a/MyNUnit/ProjectForTest/ProjectForTest.csproj b/MyNUnit/ProjectForTest/ProjectForTest.csproj new file mode 100644 index 0000000..dc2bb05 --- /dev/null +++ b/MyNUnit/ProjectForTest/ProjectForTest.csproj @@ -0,0 +1,10 @@ + + + + Library + net7.0 + enable + enable + + + diff --git a/MyNUnit/TestMyNUnit/TestMyNUnit.csproj b/MyNUnit/TestMyNUnit/TestMyNUnit.csproj new file mode 100644 index 0000000..cbb7690 --- /dev/null +++ b/MyNUnit/TestMyNUnit/TestMyNUnit.csproj @@ -0,0 +1,19 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + diff --git a/MyNUnit/TestMyNUnit/Usings.cs b/MyNUnit/TestMyNUnit/Usings.cs new file mode 100644 index 0000000..cefced4 --- /dev/null +++ b/MyNUnit/TestMyNUnit/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file From 8f9930c94721e577289761c7be7dbfde6b423580 Mon Sep 17 00:00:00 2001 From: Artem Date: Wed, 29 Nov 2023 23:01:19 +0300 Subject: [PATCH 2/6] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MyNUnit/MyNUnit.sln | 8 +++++++- MyNUnit/MyNUnit/Attributes.cs | 4 ++-- MyNUnit/MyNUnit/Program.cs | 1 - MyNUnit/ProjectForTest/ProjectForTest.csproj | 4 ++++ MyNUnit/TestMyNUnit/TestMyNUnit.csproj | 5 +++++ 5 files changed, 18 insertions(+), 4 deletions(-) delete mode 100644 MyNUnit/MyNUnit/Program.cs diff --git a/MyNUnit/MyNUnit.sln b/MyNUnit/MyNUnit.sln index 47502ef..ae4a8b0 100644 --- a/MyNUnit/MyNUnit.sln +++ b/MyNUnit/MyNUnit.sln @@ -5,7 +5,9 @@ VisualStudioVersion = 17.4.33403.182 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MyNUnit", "MyNUnit\MyNUnit.csproj", "{8A4A83AC-5456-4F54-9183-B053A0C05926}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestMyNUnit", "TestMyNUnit\TestMyNUnit.csproj", "{9508BABD-7DF4-4B36-B442-020EB706C229}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestMyNUnit", "TestMyNUnit\TestMyNUnit.csproj", "{9508BABD-7DF4-4B36-B442-020EB706C229}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectForTest", "ProjectForTest\ProjectForTest.csproj", "{F9103AED-6C96-471A-9431-4F2E37A33EC9}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,6 +23,10 @@ Global {9508BABD-7DF4-4B36-B442-020EB706C229}.Debug|Any CPU.Build.0 = Debug|Any CPU {9508BABD-7DF4-4B36-B442-020EB706C229}.Release|Any CPU.ActiveCfg = Release|Any CPU {9508BABD-7DF4-4B36-B442-020EB706C229}.Release|Any CPU.Build.0 = Release|Any CPU + {F9103AED-6C96-471A-9431-4F2E37A33EC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F9103AED-6C96-471A-9431-4F2E37A33EC9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F9103AED-6C96-471A-9431-4F2E37A33EC9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F9103AED-6C96-471A-9431-4F2E37A33EC9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/MyNUnit/MyNUnit/Attributes.cs b/MyNUnit/MyNUnit/Attributes.cs index 7bbd9bb..2e4ba7d 100644 --- a/MyNUnit/MyNUnit/Attributes.cs +++ b/MyNUnit/MyNUnit/Attributes.cs @@ -14,13 +14,13 @@ public class Test : Attribute public string? Ignored { get; set; } } -[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] +[AttributeUsage(AttributeTargets.All)] public class BeforeClass : Attribute { } -[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] +[AttributeUsage(AttributeTargets.All)] public class AfterClass : Attribute { diff --git a/MyNUnit/MyNUnit/Program.cs b/MyNUnit/MyNUnit/Program.cs deleted file mode 100644 index 5f28270..0000000 --- a/MyNUnit/MyNUnit/Program.cs +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/MyNUnit/ProjectForTest/ProjectForTest.csproj b/MyNUnit/ProjectForTest/ProjectForTest.csproj index dc2bb05..776293d 100644 --- a/MyNUnit/ProjectForTest/ProjectForTest.csproj +++ b/MyNUnit/ProjectForTest/ProjectForTest.csproj @@ -7,4 +7,8 @@ enable + + + + diff --git a/MyNUnit/TestMyNUnit/TestMyNUnit.csproj b/MyNUnit/TestMyNUnit/TestMyNUnit.csproj index cbb7690..1859717 100644 --- a/MyNUnit/TestMyNUnit/TestMyNUnit.csproj +++ b/MyNUnit/TestMyNUnit/TestMyNUnit.csproj @@ -16,4 +16,9 @@ + + + + + From 8a9a03249537d63ebb21deb3783ac072a98e8d87 Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 28 Dec 2023 17:50:33 +0300 Subject: [PATCH 3/6] =?UTF-8?q?=D0=A0=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MyNUnit/MyNUnit/ApplicationForTests.cs | 77 ++++++------------ MyNUnit/MyNUnit/Atributes/AfterAtribute.cs | 14 ++++ .../MyNUnit/Atributes/AfterClassAtribute.cs | 7 ++ MyNUnit/MyNUnit/Atributes/BeforeAtribute.cs | 14 ++++ .../MyNUnit/Atributes/BeforeClassAtribute.cs | 7 ++ MyNUnit/MyNUnit/Atributes/TestAtribute.cs | 17 ++++ MyNUnit/MyNUnit/Attributes.cs | 47 ----------- MyNUnit/MyNUnit/ResultsTests.cs | 36 ++++++++ MyNUnit/ProjectForTest/ClassForTests.cs | 20 ++--- MyNUnit/TestMyNUnit/dll/ProjectForTest.dll | Bin 0 -> 5120 bytes 10 files changed, 131 insertions(+), 108 deletions(-) create mode 100644 MyNUnit/MyNUnit/Atributes/AfterAtribute.cs create mode 100644 MyNUnit/MyNUnit/Atributes/AfterClassAtribute.cs create mode 100644 MyNUnit/MyNUnit/Atributes/BeforeAtribute.cs create mode 100644 MyNUnit/MyNUnit/Atributes/BeforeClassAtribute.cs create mode 100644 MyNUnit/MyNUnit/Atributes/TestAtribute.cs delete mode 100644 MyNUnit/MyNUnit/Attributes.cs create mode 100644 MyNUnit/MyNUnit/ResultsTests.cs create mode 100644 MyNUnit/TestMyNUnit/dll/ProjectForTest.dll diff --git a/MyNUnit/MyNUnit/ApplicationForTests.cs b/MyNUnit/MyNUnit/ApplicationForTests.cs index 2501164..43d96f5 100644 --- a/MyNUnit/MyNUnit/ApplicationForTests.cs +++ b/MyNUnit/MyNUnit/ApplicationForTests.cs @@ -1,39 +1,14 @@ using System.Diagnostics; using System.Reflection; -using Attributes; + +using MyNUnit.Atributes; namespace MyNUnit; public class ApplicationForTests { - public enum Status - { - Passed, - Failed, - Ignored - } - - public class ResultsTests - { - public string name { get; } - - public long workTime { get; } - - public Exception? reasonFail { get; } - - public Status statusTest { get; } - - public ResultsTests(string name, long workTime, Exception reasonFail, Status statusTest) - { - this.name = name; - this.workTime = workTime; - this.reasonFail = reasonFail; - this.statusTest = statusTest; - } - } - public readonly List? listOfResults; - private object? locker; + private readonly object locker = new(); public ApplicationForTests(string path) { @@ -44,22 +19,20 @@ public ApplicationForTests(string path) .Where(t => t.IsClass) .Where(t => t.GetMethods() .Where(t => t.GetCustomAttributes(true) - .Any(a => a is Test)) + .Any(a => a is TestAtribute)) .Any()); - object? locker = new object(); - Parallel.ForEach(classes, StartTests); } - private MethodInfo[]? GetMethodsByAtributeAndClass(Type _class, Type atribute) + private static MethodInfo[]? GetMethodsByAtributeAndClass(Type _class, Type atribute) { return _class.GetMethods() .Where(m => m.GetCustomAttributes(atribute, false).Length > 0) .ToArray(); } - private void WorkWithClassMethods(Type _class, Type atribute) + private static void WorkWithClassMethods(Type _class, Type atribute) { var methods = GetMethodsByAtributeAndClass(_class, atribute); LaunchMethods(_class, methods); @@ -68,12 +41,12 @@ private void WorkWithClassMethods(Type _class, Type atribute) private void WorkWithTestMethods(Type _class) { var instance = Activator.CreateInstance(_class); - var methodsBefore = GetMethodsByAtributeAndClass(_class, typeof(Before)); - var methodsAfter = GetMethodsByAtributeAndClass(_class, typeof(After)); + var methodsBefore = GetMethodsByAtributeAndClass(_class, typeof(BeforeAtribute)); + var methodsAfter = GetMethodsByAtributeAndClass(_class, typeof(AfterAtribute)); var methods = _class.GetMethods(); foreach(var method in methods) { - if (method.IsDefined(typeof(Test), true)) + if (method.IsDefined(typeof(TestAtribute), true)) { RunMethodsBeforeAndAfter(methodsBefore, instance); RunMethod(_class, method); @@ -83,16 +56,16 @@ private void WorkWithTestMethods(Type _class) } private void StartTests(Type _class) { - WorkWithClassMethods(_class, typeof(BeforeClass)); + WorkWithClassMethods(_class, typeof(BeforeClassAtribute)); WorkWithTestMethods(_class); - WorkWithClassMethods(_class, typeof(AfterClass)); + WorkWithClassMethods(_class, typeof(AfterClassAtribute)); } - private void LaunchMethods(Type _class, MethodInfo[]? methods) + private static void LaunchMethods(Type _class, MethodInfo[]? methods) { if (_class == null || methods == null) { - throw new ArgumentNullException(); + throw new NullReferenceException(); } foreach (var method in methods) @@ -106,11 +79,11 @@ private void LaunchMethods(Type _class, MethodInfo[]? methods) } } - private void RunMethodsBeforeAndAfter(MethodInfo[]? methods, object? instance) + private static void RunMethodsBeforeAndAfter(MethodInfo[]? methods, object? instance) { if (instance == null || methods == null) { - throw new ArgumentNullException(); + throw new NullReferenceException(); } foreach(var method in methods) { @@ -125,18 +98,20 @@ private void RunMethod(object? instance, MethodInfo? method) throw new InvalidOperationException(); } - var atribute = Attribute.GetCustomAttribute(method, typeof(Test)); + var atribute = Attribute.GetCustomAttribute(method, typeof(TestAtribute)); if (atribute == null) { throw new InvalidOperationException(); } - if (((Test?)atribute).Ignored != null) + var testAtribute = (TestAtribute)atribute; + + if (testAtribute.Ignored != null) { lock (locker) { - listOfResults.Add(new ResultsTests(method.Name, 0, null, Status.Ignored)); + listOfResults.Add(new ResultsTests(method.Name, 0, ResultsTests.Status.Ignored)); } return; } @@ -151,18 +126,18 @@ private void RunMethod(object? instance, MethodInfo? method) catch(Exception ex) { stopWatch.Stop(); - if (ex.InnerException != null && ((Test?)atribute) != null && ex.InnerException.GetType() == ((Test?)atribute).Expected) + if (ex.InnerException != null && testAtribute != null && ex.InnerException.GetType() == testAtribute.Expected) { lock (locker) { - listOfResults.Add(new ResultsTests(method.Name, stopWatch.ElapsedMilliseconds, ex, Status.Passed)); + listOfResults.Add(new ResultsTests(method.Name, stopWatch.ElapsedMilliseconds, ex, ResultsTests.Status.Passed)); } } else { lock (locker) { - listOfResults.Add(new ResultsTests(method.Name, stopWatch.ElapsedMilliseconds, ex, Status.Failed)); + listOfResults.Add(new ResultsTests(method.Name, stopWatch.ElapsedMilliseconds, ex, ResultsTests.Status.Failed)); } } return; @@ -170,13 +145,13 @@ private void RunMethod(object? instance, MethodInfo? method) stopWatch.Stop(); lock (locker) { - if (((Test?)atribute) != null && ((Test?)atribute).Expected != null) + if (testAtribute != null && testAtribute.Expected != null) { - listOfResults.Add(new ResultsTests(method.Name, stopWatch.ElapsedMilliseconds, null, Status.Failed)); + listOfResults.Add(new ResultsTests(method.Name, stopWatch.ElapsedMilliseconds, ResultsTests.Status.Failed)); } else { - listOfResults.Add(new ResultsTests(method.Name, stopWatch.ElapsedMilliseconds, null, Status.Passed)); + listOfResults.Add(new ResultsTests(method.Name, stopWatch.ElapsedMilliseconds, ResultsTests.Status.Passed)); } } } diff --git a/MyNUnit/MyNUnit/Atributes/AfterAtribute.cs b/MyNUnit/MyNUnit/Atributes/AfterAtribute.cs new file mode 100644 index 0000000..4cbaf49 --- /dev/null +++ b/MyNUnit/MyNUnit/Atributes/AfterAtribute.cs @@ -0,0 +1,14 @@ +namespace MyNUnit.Atributes; + +[AttributeUsage(AttributeTargets.Method)] +public class AfterAtribute : Attribute +{ + public AfterAtribute() { } + + + [AfterAtribute] + public void Method() + { + ; + } +} \ No newline at end of file diff --git a/MyNUnit/MyNUnit/Atributes/AfterClassAtribute.cs b/MyNUnit/MyNUnit/Atributes/AfterClassAtribute.cs new file mode 100644 index 0000000..d143b87 --- /dev/null +++ b/MyNUnit/MyNUnit/Atributes/AfterClassAtribute.cs @@ -0,0 +1,7 @@ +namespace MyNUnit.Atributes; + +[AttributeUsage(AttributeTargets.Method)] +public class AfterClassAtribute : Attribute +{ + public AfterClassAtribute() { } +} \ No newline at end of file diff --git a/MyNUnit/MyNUnit/Atributes/BeforeAtribute.cs b/MyNUnit/MyNUnit/Atributes/BeforeAtribute.cs new file mode 100644 index 0000000..454a15e --- /dev/null +++ b/MyNUnit/MyNUnit/Atributes/BeforeAtribute.cs @@ -0,0 +1,14 @@ +namespace MyNUnit.Atributes; + +[AttributeUsage(AttributeTargets.Method)] +public class BeforeAtribute : Attribute +{ + public BeforeAtribute() { } + + + [BeforeAtribute] + public void Method() + { + ; + } +} \ No newline at end of file diff --git a/MyNUnit/MyNUnit/Atributes/BeforeClassAtribute.cs b/MyNUnit/MyNUnit/Atributes/BeforeClassAtribute.cs new file mode 100644 index 0000000..59de6ff --- /dev/null +++ b/MyNUnit/MyNUnit/Atributes/BeforeClassAtribute.cs @@ -0,0 +1,7 @@ +namespace MyNUnit.Atributes; + +[AttributeUsage(AttributeTargets.Method)] +public class BeforeClassAtribute : Attribute +{ + public BeforeClassAtribute() { } +} \ No newline at end of file diff --git a/MyNUnit/MyNUnit/Atributes/TestAtribute.cs b/MyNUnit/MyNUnit/Atributes/TestAtribute.cs new file mode 100644 index 0000000..91dbbf7 --- /dev/null +++ b/MyNUnit/MyNUnit/Atributes/TestAtribute.cs @@ -0,0 +1,17 @@ +namespace MyNUnit.Atributes; + +[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] +public class TestAtribute : Attribute +{ + public TestAtribute(Type? expected, string? ignored) + { + Expected = expected; + Ignored = ignored; + } + + public TestAtribute() { } + + public Type? Expected { get; set; } + + public string? Ignored { get; set; } +} \ No newline at end of file diff --git a/MyNUnit/MyNUnit/Attributes.cs b/MyNUnit/MyNUnit/Attributes.cs deleted file mode 100644 index 2e4ba7d..0000000 --- a/MyNUnit/MyNUnit/Attributes.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Attributes; - -[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] -public class Test : Attribute -{ - public Type? Expected { get; set; } - - public string? Ignored { get; set; } -} - -[AttributeUsage(AttributeTargets.All)] -public class BeforeClass : Attribute -{ - -} - -[AttributeUsage(AttributeTargets.All)] -public class AfterClass : Attribute -{ - -} - -[AttributeUsage(AttributeTargets.All)] -public class Before : Attribute -{ - [Before] - public void Method() - { - - } -} - -[AttributeUsage(AttributeTargets.All)] -public class After : Attribute -{ - [After] - public void Method() - { - - } -} \ No newline at end of file diff --git a/MyNUnit/MyNUnit/ResultsTests.cs b/MyNUnit/MyNUnit/ResultsTests.cs new file mode 100644 index 0000000..0d979b8 --- /dev/null +++ b/MyNUnit/MyNUnit/ResultsTests.cs @@ -0,0 +1,36 @@ +using static MyNUnit.ApplicationForTests; + +namespace MyNUnit; + +public class ResultsTests +{ + public enum Status + { + Passed, + Failed, + Ignored + } + + public string Name { get; } + + public long WorkTime { get; } + + public Exception? ReasonFail { get; } + + public Status StatusTest { get; } + + public ResultsTests(string name, long workTime, Exception reasonFail, Status statusTest) + { + Name = name; + WorkTime = workTime; + ReasonFail = reasonFail; + StatusTest = statusTest; + } + + public ResultsTests(string name, long workTime, Status statusTest) + { + Name = name; + WorkTime = workTime; + StatusTest = statusTest; + } +} diff --git a/MyNUnit/ProjectForTest/ClassForTests.cs b/MyNUnit/ProjectForTest/ClassForTests.cs index 246e0f5..8116e73 100644 --- a/MyNUnit/ProjectForTest/ClassForTests.cs +++ b/MyNUnit/ProjectForTest/ClassForTests.cs @@ -1,60 +1,60 @@ namespace ProjectForTest; -using Attributes; +using MyNUnit.Atributes; public class ClassForTests { public int counter = 0; - [Test] + [TestAtribute] public void InvalidMethod() { throw new NotImplementedException(); } - [Test] + [TestAtribute] public int CorrectMethod() { return 1; } - [BeforeClass] + [BeforeClassAtribute] public void BeforeClass() { counter += 1; } - [AfterClass] + [AfterClassAtribute] public void AfterClass() { counter += 1; } - [After] + [AfterAtribute] public void BeforeMethod() { counter += 1; } - [Before] + [BeforeAtribute] public void AfterMethod() { counter += 1; } - [Test(Ignored = "Ignore")] + [TestAtribute(Ignored = "Ignore")] public void IgnoreTest() { ; } - [Test(Expected = typeof(InvalidCastException))] + [TestAtribute(Expected = typeof(InvalidCastException))] public void InvalidCastException() { throw new InvalidCastException(); } - [Test(Expected = typeof(InvalidOperationException))] + [TestAtribute(Expected = typeof(InvalidOperationException))] public void InvalidException() { throw new InvalidProgramException(); diff --git a/MyNUnit/TestMyNUnit/dll/ProjectForTest.dll b/MyNUnit/TestMyNUnit/dll/ProjectForTest.dll new file mode 100644 index 0000000000000000000000000000000000000000..842c6542d31b2022eb6511e8af8ad37eb9ed22ee GIT binary patch literal 5120 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 b@-s5t@3DqN$yfBn2j63U|KHtrB?A8de@%%1 literal 0 HcmV?d00001 From 90d307746ec48723023d6beb76579282e549c2f3 Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 28 Dec 2023 22:54:01 +0300 Subject: [PATCH 4/6] =?UTF-8?q?=D0=94=D0=BE=D0=B4=D0=B5=D0=BB=D1=8B=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MyNUnit/MyNUnit/ApplicationForTests.cs | 78 +++++++++--------- MyNUnit/MyNUnit/Atributes/AfterAtribute.cs | 14 ---- .../MyNUnit/Atributes/AfterClassAtribute.cs | 7 -- MyNUnit/MyNUnit/Atributes/BeforeAtribute.cs | 14 ---- .../MyNUnit/Atributes/BeforeClassAtribute.cs | 7 -- MyNUnit/MyNUnit/Atributes/TestAtribute.cs | 17 ---- MyNUnit/ProjectForTest/ClassForTests.cs | 30 +++---- MyNUnit/TestMyNUnit/dll/ProjectForTest.dll | Bin 5120 -> 5632 bytes 8 files changed, 56 insertions(+), 111 deletions(-) delete mode 100644 MyNUnit/MyNUnit/Atributes/AfterAtribute.cs delete mode 100644 MyNUnit/MyNUnit/Atributes/AfterClassAtribute.cs delete mode 100644 MyNUnit/MyNUnit/Atributes/BeforeAtribute.cs delete mode 100644 MyNUnit/MyNUnit/Atributes/BeforeClassAtribute.cs delete mode 100644 MyNUnit/MyNUnit/Atributes/TestAtribute.cs diff --git a/MyNUnit/MyNUnit/ApplicationForTests.cs b/MyNUnit/MyNUnit/ApplicationForTests.cs index 43d96f5..5e7d203 100644 --- a/MyNUnit/MyNUnit/ApplicationForTests.cs +++ b/MyNUnit/MyNUnit/ApplicationForTests.cs @@ -7,22 +7,23 @@ namespace MyNUnit; public class ApplicationForTests { - public readonly List? listOfResults; + public readonly List? listOfResults = new(); private readonly object locker = new(); - public ApplicationForTests(string path) + public ApplicationForTests(Assembly assembly) { - var listOfResults = new List(); - var files = Directory.EnumerateFiles(path, "*.dll", SearchOption.AllDirectories); - var classes = files.Select(Assembly.Load) - .SelectMany(a => a.ExportedTypes) - .Where(t => t.IsClass) - .Where(t => t.GetMethods() - .Where(t => t.GetCustomAttributes(true) - .Any(a => a is TestAtribute)) - .Any()); - - Parallel.ForEach(classes, StartTests); + var classes = assembly.GetExportedTypes() + .Where(t => t.IsClass) + .Where(t => t.GetMethods() + .Where(m => m.GetCustomAttributes(true) + .Any(a => a is TestAttribute)) + .Any()); + + //Parallel.ForEach(classes, StartTests); + foreach (var _class in classes) + { + StartTests(_class); + } } private static MethodInfo[]? GetMethodsByAtributeAndClass(Type _class, Type atribute) @@ -41,12 +42,12 @@ private static void WorkWithClassMethods(Type _class, Type atribute) private void WorkWithTestMethods(Type _class) { var instance = Activator.CreateInstance(_class); - var methodsBefore = GetMethodsByAtributeAndClass(_class, typeof(BeforeAtribute)); - var methodsAfter = GetMethodsByAtributeAndClass(_class, typeof(AfterAtribute)); + var methodsBefore = GetMethodsByAtributeAndClass(_class, typeof(BeforeAttribute)); + var methodsAfter = GetMethodsByAtributeAndClass(_class, typeof(AfterAttribute)); var methods = _class.GetMethods(); foreach(var method in methods) { - if (method.IsDefined(typeof(TestAtribute), true)) + if (method.IsDefined(typeof(TestAttribute), true)) { RunMethodsBeforeAndAfter(methodsBefore, instance); RunMethod(_class, method); @@ -56,9 +57,9 @@ private void WorkWithTestMethods(Type _class) } private void StartTests(Type _class) { - WorkWithClassMethods(_class, typeof(BeforeClassAtribute)); + WorkWithClassMethods(_class, typeof(BeforeClassAttribute)); WorkWithTestMethods(_class); - WorkWithClassMethods(_class, typeof(AfterClassAtribute)); + WorkWithClassMethods(_class, typeof(AfterClassAttribute)); } private static void LaunchMethods(Type _class, MethodInfo[]? methods) @@ -93,21 +94,21 @@ private static void RunMethodsBeforeAndAfter(MethodInfo[]? methods, object? inst private void RunMethod(object? instance, MethodInfo? method) { - if (instance == null || method == null || locker == null || listOfResults == null) + if (instance == null || method == null || locker == null) { throw new InvalidOperationException(); } - var atribute = Attribute.GetCustomAttribute(method, typeof(TestAtribute)); + var atribute = Attribute.GetCustomAttribute(method, typeof(TestAttribute)); if (atribute == null) { throw new InvalidOperationException(); } - var testAtribute = (TestAtribute)atribute; + var testAttribute = (TestAttribute)atribute; - if (testAtribute.Ignored != null) + if (testAttribute.Ignored != null) { lock (locker) { @@ -120,13 +121,28 @@ private void RunMethod(object? instance, MethodInfo? method) try { - stopWatch.Start(); - method.Invoke(instance, null); + stopWatch.Start(); + method.Invoke(instance, null); + stopWatch.Stop(); + lock (locker) + { + if (testAttribute != null && testAttribute.Expected != null) + { + listOfResults.Add(new ResultsTests(method.Name, stopWatch.ElapsedMilliseconds, ResultsTests.Status.Failed)); + } + else + { + listOfResults.Add(new ResultsTests(method.Name, stopWatch.ElapsedMilliseconds, ResultsTests.Status.Passed)); + } + } } catch(Exception ex) { stopWatch.Stop(); - if (ex.InnerException != null && testAtribute != null && ex.InnerException.GetType() == testAtribute.Expected) + var exceptionType = ex.GetType(); + + if (testAttribute != null && testAttribute.Expected.IsAssignableFrom(exceptionType) || + (ex.InnerException != null && testAttribute != null && testAttribute.Expected.IsAssignableFrom(ex.InnerException.GetType()))) { lock (locker) { @@ -142,18 +158,6 @@ private void RunMethod(object? instance, MethodInfo? method) } return; } - stopWatch.Stop(); - lock (locker) - { - if (testAtribute != null && testAtribute.Expected != null) - { - listOfResults.Add(new ResultsTests(method.Name, stopWatch.ElapsedMilliseconds, ResultsTests.Status.Failed)); - } - else - { - listOfResults.Add(new ResultsTests(method.Name, stopWatch.ElapsedMilliseconds, ResultsTests.Status.Passed)); - } - } } } \ No newline at end of file diff --git a/MyNUnit/MyNUnit/Atributes/AfterAtribute.cs b/MyNUnit/MyNUnit/Atributes/AfterAtribute.cs deleted file mode 100644 index 4cbaf49..0000000 --- a/MyNUnit/MyNUnit/Atributes/AfterAtribute.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace MyNUnit.Atributes; - -[AttributeUsage(AttributeTargets.Method)] -public class AfterAtribute : Attribute -{ - public AfterAtribute() { } - - - [AfterAtribute] - public void Method() - { - ; - } -} \ No newline at end of file diff --git a/MyNUnit/MyNUnit/Atributes/AfterClassAtribute.cs b/MyNUnit/MyNUnit/Atributes/AfterClassAtribute.cs deleted file mode 100644 index d143b87..0000000 --- a/MyNUnit/MyNUnit/Atributes/AfterClassAtribute.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace MyNUnit.Atributes; - -[AttributeUsage(AttributeTargets.Method)] -public class AfterClassAtribute : Attribute -{ - public AfterClassAtribute() { } -} \ No newline at end of file diff --git a/MyNUnit/MyNUnit/Atributes/BeforeAtribute.cs b/MyNUnit/MyNUnit/Atributes/BeforeAtribute.cs deleted file mode 100644 index 454a15e..0000000 --- a/MyNUnit/MyNUnit/Atributes/BeforeAtribute.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace MyNUnit.Atributes; - -[AttributeUsage(AttributeTargets.Method)] -public class BeforeAtribute : Attribute -{ - public BeforeAtribute() { } - - - [BeforeAtribute] - public void Method() - { - ; - } -} \ No newline at end of file diff --git a/MyNUnit/MyNUnit/Atributes/BeforeClassAtribute.cs b/MyNUnit/MyNUnit/Atributes/BeforeClassAtribute.cs deleted file mode 100644 index 59de6ff..0000000 --- a/MyNUnit/MyNUnit/Atributes/BeforeClassAtribute.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace MyNUnit.Atributes; - -[AttributeUsage(AttributeTargets.Method)] -public class BeforeClassAtribute : Attribute -{ - public BeforeClassAtribute() { } -} \ No newline at end of file diff --git a/MyNUnit/MyNUnit/Atributes/TestAtribute.cs b/MyNUnit/MyNUnit/Atributes/TestAtribute.cs deleted file mode 100644 index 91dbbf7..0000000 --- a/MyNUnit/MyNUnit/Atributes/TestAtribute.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace MyNUnit.Atributes; - -[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] -public class TestAtribute : Attribute -{ - public TestAtribute(Type? expected, string? ignored) - { - Expected = expected; - Ignored = ignored; - } - - public TestAtribute() { } - - public Type? Expected { get; set; } - - public string? Ignored { get; set; } -} \ No newline at end of file diff --git a/MyNUnit/ProjectForTest/ClassForTests.cs b/MyNUnit/ProjectForTest/ClassForTests.cs index 8116e73..ccd98a3 100644 --- a/MyNUnit/ProjectForTest/ClassForTests.cs +++ b/MyNUnit/ProjectForTest/ClassForTests.cs @@ -4,58 +4,58 @@ public class ClassForTests { - public int counter = 0; + public static int counter = 0; - [TestAtribute] + [TestAttribute(Expected = typeof(IndexOutOfRangeException))] public void InvalidMethod() { throw new NotImplementedException(); } - [TestAtribute] + [TestAttribute(Expected = typeof(FileNotFoundException))] public int CorrectMethod() { - return 1; + throw new FileNotFoundException(); } - [BeforeClassAtribute] - public void BeforeClass() + [BeforeClassAttribute] + public static void BeforeClass() { counter += 1; } - [AfterClassAtribute] - public void AfterClass() + [AfterClassAttribute] + public static void AfterClass() { counter += 1; } - [AfterAtribute] + [AfterAttribute] public void BeforeMethod() { counter += 1; } - [BeforeAtribute] + [BeforeAttribute] public void AfterMethod() { counter += 1; } - [TestAtribute(Ignored = "Ignore")] + [TestAttribute(Ignored = "Ignore")] public void IgnoreTest() { ; } - [TestAtribute(Expected = typeof(InvalidCastException))] - public void InvalidCastException() + [TestAttribute(Expected = typeof(InvalidCastException))] + public void OneMoreCorrectMethod() { throw new InvalidCastException(); } - [TestAtribute(Expected = typeof(InvalidOperationException))] - public void InvalidException() + [TestAttribute(Expected = typeof(InvalidOperationException))] + public void OneMoreIncorrectMethod() { throw new InvalidProgramException(); } diff --git a/MyNUnit/TestMyNUnit/dll/ProjectForTest.dll b/MyNUnit/TestMyNUnit/dll/ProjectForTest.dll index 842c6542d31b2022eb6511e8af8ad37eb9ed22ee..205d8ff4b03db15b7e729b747b4319992ed01388 100644 GIT binary patch delta 1581 zcma)+e`uRU7{|Z&zHL&|CQH|BwOuJ`+O=_su~Vm;E2V9=x+pc>lDd|Oq$Y1`Xq(jJ z&1@_*al_&d#_WzM;;&UI#`#1?9Nx1lBA7AnIMo*PJ4Kq=+ugJb!{9$kBpEF!IOyyQ2}AXu zjZ{XeSMZwZA$8PH?@BdiM7rd>w}r}K7I#T`))-uo3w~gz-E!=_q57q|R8q#)Zi8E8 zs3w}|#kuJ@-9fZtyW}p(Zpnb;e#s%pVdTWLEQw8cfoQ_}k{?QbBDpLXm%J)DFZre9 z50cMHwqYZ-syL^x!SxJ@|;Y8=n#VIebOU!*U=*^y3g#nV;VI zDvMwY-ys#@$Rfn7QkK7YA7GSq zy0-KQP}x;-wqR6tO4p|30pC%WQ9AmPvCO#EiBLAJYZJaekjG8#&R|08OQtk`GL_Pz zdY`60nT)|O1BvJ@wlVWSCZWeCG=#I0TrsE0d$f^cipR~f<=rE?mf}B}dm5#u`NxOT z>294q=_fL}201J|-cZq^hP5=`QBm018z0vmN$S1HOd_^-DymKD@niylL`<6sX7u35 zf#JlcwzyVtC$r!9=0xaTYqa;G|Li*tHB2w_7K^8hj>ve>QgG*wh^rm$uh=^^NoUbw zshdZ=H5b27`PhF12YvtEkqBP=%`(95{`~2S$HDsg#n+h^4^{j$#n0F%;>p)gZKIl} zvAm7XRJrUY9VeHX9LM%8T*#hzb?nnFey%F*Gt5HFFe&$j!1R3a^76vYaG*GJ#rPqM k)azcp-&(I+^YRJnJdaerv1qduRprl)#p;c}u(@{g-;umK!TxSa-PYVc+rNt#CT~=gYgBmO7y%#FN!g87meBviSd+>G-@s? zHl}w$TiV*UV@ew(iow!1K`d7+ZJ{Jkh?RZ`ZE3%xgcbt&Awp}53Z;GK+4Z0wJ21a_ z{{MM)_SxOp$t=&*z5Pn{AG0S$;ItW3`-{X%pqLEPHs1|pKiTV!gn-@J*aEa0*+5xw zhn*Fx2NEhEm#h6EXpI(8il2J1dQF6cTip^tQL26s)xOiTf^ISD5>MefKXA4X5K7En zr&?vzKC7a>NyEVBCv%WIr1*{h;Qw#M_CF{!7gbX#+89>d8z&kYF)Ny>E zzq{{fN>>zh(XD8#X^SI9u}8<-+nS>^wv%2eRlTt+T}e`>QDOXk%4zgb7mf!sN*(Jr z_o4Vpns~&#i{IpYl+#U3ctr*IGa^nr&RoIV!JK6N4Ly4cdW8}hB_<8xHcUZ_^CT?1 zhg!qJaprmEA!eLtqJ=ZZ`Qi`kC75r@s0tL6rQgRoV=Wa<#dtT`^j@k+qb@p~2&TTGSA*{krN}>FaMiy Date: Fri, 29 Dec 2023 13:13:40 +0300 Subject: [PATCH 5/6] =?UTF-8?q?=D1=80=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MyNUnit/MyNUnit/ApplicationForTests.cs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/MyNUnit/MyNUnit/ApplicationForTests.cs b/MyNUnit/MyNUnit/ApplicationForTests.cs index 5e7d203..3aa3483 100644 --- a/MyNUnit/MyNUnit/ApplicationForTests.cs +++ b/MyNUnit/MyNUnit/ApplicationForTests.cs @@ -7,7 +7,7 @@ namespace MyNUnit; public class ApplicationForTests { - public readonly List? listOfResults = new(); + public readonly List listOfResults = new(); private readonly object locker = new(); public ApplicationForTests(Assembly assembly) @@ -19,11 +19,7 @@ public ApplicationForTests(Assembly assembly) .Any(a => a is TestAttribute)) .Any()); - //Parallel.ForEach(classes, StartTests); - foreach (var _class in classes) - { - StartTests(_class); - } + Parallel.ForEach(classes, StartTests); } private static MethodInfo[]? GetMethodsByAtributeAndClass(Type _class, Type atribute) @@ -141,8 +137,8 @@ private void RunMethod(object? instance, MethodInfo? method) stopWatch.Stop(); var exceptionType = ex.GetType(); - if (testAttribute != null && testAttribute.Expected.IsAssignableFrom(exceptionType) || - (ex.InnerException != null && testAttribute != null && testAttribute.Expected.IsAssignableFrom(ex.InnerException.GetType()))) + if (testAttribute != null && testAttribute.Expected != null && testAttribute.Expected.IsAssignableFrom(exceptionType) || + (ex.InnerException != null && testAttribute != null && testAttribute.Expected != null && testAttribute.Expected.IsAssignableFrom(ex.InnerException.GetType()))) { lock (locker) { @@ -156,7 +152,6 @@ private void RunMethod(object? instance, MethodInfo? method) listOfResults.Add(new ResultsTests(method.Name, stopWatch.ElapsedMilliseconds, ex, ResultsTests.Status.Failed)); } } - return; } } From 1845fc7f7a4742dc579fad7e0f1863a0fd7231b3 Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 29 Dec 2023 14:42:16 +0300 Subject: [PATCH 6/6] =?UTF-8?q?=D0=AF=20=D0=BD=D0=B0=D0=B7=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=83=20=D0=BF=D0=B0=D0=BF=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BC=D0=B5=D0=BD=D1=8F=D0=BB=20=D0=B8=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=B1=D1=8B=D0=BB=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D1=82=D1=8C.=20=D0=9C=D0=BE=D0=B6=D0=B5=D1=88=D1=8C=20=D0=BD?= =?UTF-8?q?=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D1=8F=D1=82=D1=8C?= =?UTF-8?q?,=20=D1=8F=20=D0=B7=D0=B0=D1=87=D1=91=D1=82=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D1=83=D1=87=D0=B8=D0=BB.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MyNUnit/MyNUnit/Attributes/AfterAttribute.cs | 14 ++++++++++ .../MyNUnit/Attributes/AfterClassAttribute.cs | 7 +++++ MyNUnit/MyNUnit/Attributes/BeforeAttribute.cs | 14 ++++++++++ .../Attributes/BeforeClassAttribute.cs | 7 +++++ MyNUnit/MyNUnit/Attributes/TestAttribute.cs | 27 +++++++++++++++++++ 5 files changed, 69 insertions(+) create mode 100644 MyNUnit/MyNUnit/Attributes/AfterAttribute.cs create mode 100644 MyNUnit/MyNUnit/Attributes/AfterClassAttribute.cs create mode 100644 MyNUnit/MyNUnit/Attributes/BeforeAttribute.cs create mode 100644 MyNUnit/MyNUnit/Attributes/BeforeClassAttribute.cs create mode 100644 MyNUnit/MyNUnit/Attributes/TestAttribute.cs diff --git a/MyNUnit/MyNUnit/Attributes/AfterAttribute.cs b/MyNUnit/MyNUnit/Attributes/AfterAttribute.cs new file mode 100644 index 0000000..a8b4ebb --- /dev/null +++ b/MyNUnit/MyNUnit/Attributes/AfterAttribute.cs @@ -0,0 +1,14 @@ +namespace MyNUnit.Atributes; + +[AttributeUsage(AttributeTargets.Method)] +public class AfterAttribute : Attribute +{ + public AfterAttribute() { } + + + [AfterAttribute] + public void Method() + { + ; + } +} \ No newline at end of file diff --git a/MyNUnit/MyNUnit/Attributes/AfterClassAttribute.cs b/MyNUnit/MyNUnit/Attributes/AfterClassAttribute.cs new file mode 100644 index 0000000..3ff1eac --- /dev/null +++ b/MyNUnit/MyNUnit/Attributes/AfterClassAttribute.cs @@ -0,0 +1,7 @@ +namespace MyNUnit.Atributes; + +[AttributeUsage(AttributeTargets.Method)] +public class AfterClassAttribute : Attribute +{ + public AfterClassAttribute() { } +} \ No newline at end of file diff --git a/MyNUnit/MyNUnit/Attributes/BeforeAttribute.cs b/MyNUnit/MyNUnit/Attributes/BeforeAttribute.cs new file mode 100644 index 0000000..fe780ea --- /dev/null +++ b/MyNUnit/MyNUnit/Attributes/BeforeAttribute.cs @@ -0,0 +1,14 @@ +namespace MyNUnit.Atributes; + +[AttributeUsage(AttributeTargets.Method)] +public class BeforeAttribute : Attribute +{ + public BeforeAttribute() { } + + + [BeforeAttribute] + public void Method() + { + ; + } +} \ No newline at end of file diff --git a/MyNUnit/MyNUnit/Attributes/BeforeClassAttribute.cs b/MyNUnit/MyNUnit/Attributes/BeforeClassAttribute.cs new file mode 100644 index 0000000..993eb25 --- /dev/null +++ b/MyNUnit/MyNUnit/Attributes/BeforeClassAttribute.cs @@ -0,0 +1,7 @@ +namespace MyNUnit.Atributes; + +[AttributeUsage(AttributeTargets.Method)] +public class BeforeClassAttribute : Attribute +{ + public BeforeClassAttribute() { } +} \ No newline at end of file diff --git a/MyNUnit/MyNUnit/Attributes/TestAttribute.cs b/MyNUnit/MyNUnit/Attributes/TestAttribute.cs new file mode 100644 index 0000000..6e86626 --- /dev/null +++ b/MyNUnit/MyNUnit/Attributes/TestAttribute.cs @@ -0,0 +1,27 @@ +namespace MyNUnit.Atributes; + +[AttributeUsage(AttributeTargets.Method, Inherited = false)] +public class TestAttribute : Attribute +{ + public TestAttribute() { } + + public TestAttribute(Type? expected) + { + Expected = expected; + } + + public TestAttribute(string ignored) + { + Ignored = ignored; + } + + public TestAttribute(Type? expected, string? ignored) + { + Expected = expected; + Ignored = ignored; + } + + public Type? Expected { get; set; } + + public string? Ignored { get; set; } +} \ No newline at end of file