Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions MyNUnit/AttributesMyNunit/After.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace AttributesMyNUnit;

using System;

/// <summary>
/// Атрибут, которым обозначают методы, которые вызываются после каждого теста
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class After : Attribute
{
}
11 changes: 11 additions & 0 deletions MyNUnit/AttributesMyNunit/AfterClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace AttributesMyNUnit;

using System;

/// <summary>
/// Атрибут, которым обозначают методы, которые вызываются после тестов
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class AfterClass : Attribute
{
}
10 changes: 10 additions & 0 deletions MyNUnit/AttributesMyNunit/AttributesMyNunit.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Attributes</RootNamespace>
</PropertyGroup>

</Project>
11 changes: 11 additions & 0 deletions MyNUnit/AttributesMyNunit/Before.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace AttributesMyNUnit;

using System;

/// <summary>
/// Атрибут, которым обозначают методы, которые вызываются перед каждым тестом
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class Before : Attribute
{
}
11 changes: 11 additions & 0 deletions MyNUnit/AttributesMyNunit/BeforeClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace AttributesMyNUnit;

using System;

/// <summary>
/// Атрибут, которым обозначают методы, которые вызываются перед тестами
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class BeforeClass : Attribute
{
}
20 changes: 20 additions & 0 deletions MyNUnit/AttributesMyNunit/Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace AttributesMyNUnit;

using System;

/// <summary>
/// Атрибут, которым обозначают методы, которые являются тестами
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class Test : Attribute
{
public Type Expected { get; set; }

public string? Ignore { get; set; }

public Test(Type expected, string? ignore = null)
{
Expected = expected;
Ignore = ignore;
}
}
15 changes: 15 additions & 0 deletions MyNUnit/ForTests/ForTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>10</LangVersion>
<RootNamespace>ClassLibrary1</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\MyNUnit\MyNUnit.csproj" />
</ItemGroup>

</Project>
77 changes: 77 additions & 0 deletions MyNUnit/ForTests/Test1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
namespace ForTests;

using System;
using AttributesMyNUnit;

public class Test1
{
private int nonStaticCounter = 0;

[Before]
public void NonStaticIncrement() => nonStaticCounter++;

[Test(null)]
public void TestWithoutExpected()
{

}

[Test(null, "yes")]
public void TestShouldBeIgnored()
{

}

[Test(typeof(ArgumentException))]
public void TestWithExpectedException()
{
throw new ArgumentException();
}


[Test(null)]
public void TestBefore()
{
if (nonStaticCounter != 1)
{
throw new ArgumentException();
}
}

[Test(null)]
public void NullExpectedButThrowException()
{
throw new ArgumentException();
}

[Test(typeof(ArgumentException))]
public void ExceptionExpectedButWasNull()
{

}

[Test(typeof(ArgumentException))]
public void OneExceptionExpectedButWasAnother()
{
throw new AggregateException();
}

[BeforeClass]
public void NonStaticBeforeClass()
{

}

[AfterClass]
public static void ExceptionInAfterClass()
{
throw new AggregateException();
}

[After]
[Test(null)]
public void TestWithIncompatibleAttributes()
{

}
}
33 changes: 33 additions & 0 deletions MyNUnit/MyNUnit.Test/MyNUnit.Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace MyNUnit.Test;

using System.Linq;
using NUnit.Framework;

public class Tests
{
private readonly MyNUnit myNUnit = new ();
private readonly string[] answer = new string[]
{
"Проверка тестов из Test1",
"Метод NonStaticBeforeClass содержит атрибут BeforeClass, но не является статическим",
"Метод TestWithIncompatibleAttributes имеет два несовместимых атрибута",
"В методе ExceptionInAfterClass возникло исключение: System.AggregateException",
"Тест TestWithExpectedException пройден успешно",
"Тест TestShouldBeIgnored был игнорирован",
"Тест TestWithoutExpected пройден успешно",
"Тест TestBefore пройден успешно",
"Тест ExceptionExpectedButWasNull не пройден: ожидалось исключение типа System.ArgumentException",
"Тест OneExceptionExpectedButWasAnother не пройден: ожидалось исключение типа System.ArgumentException, возникло System.AggregateException",
"Тест NullExpectedButThrowException не пройден: возникло исключение System.ArgumentException"
};

[NUnit.Framework.Test]
public void TestForMessages()
{
var result = myNUnit.RunTests("../../../../ForTests/bin/Debug/net6.0/");
foreach (var m in result)
{
Assert.IsTrue(answer.Contains(m.Item1));
}
}
}
24 changes: 24 additions & 0 deletions MyNUnit/MyNUnit.Test/MyNUnit.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>

<LangVersion>10</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ForTests\ForTests.csproj" />
<ProjectReference Include="..\MyNUnit\MyNUnit.csproj" />
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions MyNUnit/MyNUnit.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyNUnit", "MyNUnit\MyNUnit.csproj", "{1DB5464C-6E66-40C9-87A3-6511DD33C324}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyNUnit.Test", "MyNUnit.Test\MyNUnit.Test.csproj", "{E9C716B6-D491-4B60-BA36-063C4902B355}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ForTests", "ForTests\ForTests.csproj", "{05442C5E-59D8-4BC3-805B-9FF732926D2B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AttributesMyNunit", "AttributesMyNunit\AttributesMyNunit.csproj", "{9EE0AFA0-1759-431B-9810-E49A29F36499}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1DB5464C-6E66-40C9-87A3-6511DD33C324}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1DB5464C-6E66-40C9-87A3-6511DD33C324}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1DB5464C-6E66-40C9-87A3-6511DD33C324}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1DB5464C-6E66-40C9-87A3-6511DD33C324}.Release|Any CPU.Build.0 = Release|Any CPU
{E9C716B6-D491-4B60-BA36-063C4902B355}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E9C716B6-D491-4B60-BA36-063C4902B355}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E9C716B6-D491-4B60-BA36-063C4902B355}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E9C716B6-D491-4B60-BA36-063C4902B355}.Release|Any CPU.Build.0 = Release|Any CPU
{05442C5E-59D8-4BC3-805B-9FF732926D2B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{05442C5E-59D8-4BC3-805B-9FF732926D2B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{05442C5E-59D8-4BC3-805B-9FF732926D2B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{05442C5E-59D8-4BC3-805B-9FF732926D2B}.Release|Any CPU.Build.0 = Release|Any CPU
{9EE0AFA0-1759-431B-9810-E49A29F36499}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9EE0AFA0-1759-431B-9810-E49A29F36499}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9EE0AFA0-1759-431B-9810-E49A29F36499}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9EE0AFA0-1759-431B-9810-E49A29F36499}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Loading