Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to verify that #17 is fixed; cateorize tests by expected result #25

Merged
merged 2 commits into from
May 16, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions demo/NUnitTestDemo/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="test.setting" value="54321"/>
</appSettings>
</configuration>
18 changes: 9 additions & 9 deletions demo/NUnitTestDemo/AsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,67 @@ namespace NUnitTestDemo
{
public class AsyncTests
{
[Test]
[Test, Should("Pass")]
public async void AsyncVoidTestSucceeds()
{
var result = await ReturnOne();

Assert.AreEqual(1, result);
}

[Test]
[Test, Should("Fail")]
public async void AsyncVoidTestFails()
{
var result = await ReturnOne();

Assert.AreEqual(2, result);
}

[Test]
[Test, Should("Error")]
public async void AsyncVoidTestThrowsException()
{
await ThrowException();

Assert.Fail("Should never get here");
}

[Test]
[Test, Should("Pass")]
public async Task AsyncTaskTestSucceeds()
{
var result = await ReturnOne();

Assert.AreEqual(1, result);
}

[Test]
[Test, Should("Fail")]
public async Task AsyncTaskTestFails()
{
var result = await ReturnOne();

Assert.AreEqual(2, result);
}

[Test]
[Test, Should("Error")]
public async Task AsyncTaskTestThrowsException()
{
await ThrowException();

Assert.Fail("Should never get here");
}

[TestCase(ExpectedResult = 1)]
[TestCase(ExpectedResult = 1), Should("Pass")]
public async Task<int> AsyncTaskWithResultSucceeds()
{
return await ReturnOne();
}

[TestCase(ExpectedResult = 2)]
[TestCase(ExpectedResult = 2), Should("Fail")]
public async Task<int> AsyncTaskWithResultFails()
{
return await ReturnOne();
}

[TestCase(ExpectedResult = 0)]
[TestCase(ExpectedResult = 0), Should("Error")]
public async Task<int> AsyncTaskWithResultThrowsException()
{
return await ThrowException();
Expand Down
25 changes: 25 additions & 0 deletions demo/NUnitTestDemo/ConfigFileTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Configuration;
using System.IO;
using NUnit.Framework;

namespace NUnitTestDemo
{
[Should("Pass")]
public class ConfigFileTests
{
[Test]
public static void ProperConfigFileIsUsed()
{
var expectedPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "NUnit3TestDemo.dll.config");
Assert.That(expectedPath, Is.EqualTo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile));
}

[Test]
public static void CanReadConfigFile()
{
Assert.That(ConfigurationManager.AppSettings.Get("test.setting"), Is.EqualTo("54321"));
}

}
}
3 changes: 2 additions & 1 deletion demo/NUnitTestDemo/GenericTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ namespace NUnitTestDemo
[TestFixture(typeof(int))]
public class GenericTests<T>
{
[Test]
[Test, Should("Pass")]
public void TestIt() { }
}

[Should("Pass")]
[TestFixture(typeof(ArrayList))]
[TestFixture(typeof(List<int>))]
public class GenericTests_IList<TList> where TList : IList, new()
Expand Down
6 changes: 5 additions & 1 deletion demo/NUnitTestDemo/NUnit3TestDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,28 @@
<HintPath>..\packages\NUnit.3.0.0-beta-2\lib\net45\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AsyncTests.cs" />
<Compile Include="ConfigFileTests.cs" />
<Compile Include="ShouldAttribute.cs" />
<Compile Include="GenericTests.cs" />
<Compile Include="TextOutputTests.cs" />
<Compile Include="ParameterizedTests.cs" />
<Compile Include="SetUpFixtureTests.cs" />
<Compile Include="SimpleTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestFixtureSetUpTests.cs" />
<Compile Include="OneTimeSetUpTests.cs" />
<Compile Include="Theories.cs" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
17 changes: 10 additions & 7 deletions demo/NUnitTestDemo/ParameterizedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace NUnitTestDemo
{
public class ParameterizedTests
{
[Should("Pass")]
[TestCase(2, 2, 4)]
[TestCase(0, 5, 5)]
[TestCase(31, 11, 42)]
Expand All @@ -15,6 +16,7 @@ public void TestCaseSucceeds(int a, int b, int sum)
Assert.That(a + b, Is.EqualTo(sum));
}

[Should("Pass")]
[TestCase(2, 2, ExpectedResult = 4)]
[TestCase(0, 5, ExpectedResult = 5)]
[TestCase(31, 11, ExpectedResult = 42)]
Expand All @@ -23,48 +25,49 @@ public int TestCaseSucceeds_Result(int a, int b)
return a + b;
}

[Should("Fail")]
[TestCase(31, 11, 99)]
public void TestCaseFails(int a, int b, int sum)
{
Assert.That(a + b, Is.EqualTo(sum));
}

[TestCase(31, 11, ExpectedResult = 99)]
[TestCase(31, 11, ExpectedResult = 99), Should("Fail")]
public int TestCaseFails_Result(int a, int b)
{
return a + b;
}

[TestCase(31, 11)]
[TestCase(31, 11), Should("Inconclusive")]
public void TestCaseIsInconclusive(int a, int b)
{
Assert.Inconclusive("Inconclusive test case");
}

[Ignore("Ignored test")]
[Ignore("Ignored test"), Should("Ignore")]
[TestCase(31, 11)]
public void TestCaseIsIgnored_Attribute(int a, int b)
{
}

[TestCase(31, 11, Ignore = "Ignoring this")]
[TestCase(31, 11, Ignore = "Ignoring this"), Should("Ignore")]
public void TestCaseIsIgnored_Property(int a, int b)
{
}

[TestCase(31, 11)]
[TestCase(31, 11), Should("Ignore")]
public void TestCaseIsIgnored_Assert(int a, int b)
{
Assert.Ignore("Ignoring this test case");
}

[TestCase(31, 11)]
[TestCase(31, 11), Should("Error")]
public void TestCaseThrowsException(int a, int b)
{
throw new Exception("Exception from test case");
}

[TestCase(42, TestName="AlternateTestName")]
[TestCase(42, TestName="AlternateTestName"), Should("Pass")]
public void TestCaseWithAlternateName(int x)
{
}
Expand Down
4 changes: 2 additions & 2 deletions demo/NUnitTestDemo/SetUpFixtureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void AfterTests()
}
}

[TestFixture]
[TestFixture, Should("Pass")]
public class TestFixture1
{
[Test]
Expand All @@ -37,7 +37,7 @@ public void Test1()
}
}

[TestFixture]
[TestFixture, Should("Pass")]
public class TestFixture2
{
[Test]
Expand Down
14 changes: 14 additions & 0 deletions demo/NUnitTestDemo/ShouldAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;

namespace NUnitTestDemo
{
public class ShouldAttribute : PropertyAttribute
{
public ShouldAttribute(string result) : base(result) { }
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only demo-code but it may still be used as guidelines. I don't think use of strings as parameters should be encouraged, when it is sprinkled all over the code. Could this be solved just as well by using distinct properties, one for each case: ShouldFail, ShouldIgnore, ShouldPass and so on, and just pass the string inside them to the base in the ctor ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had it that way originally, as custom category, and then switched to a property, primarily because of how VS displays it. For categories, you get Category [ShouldPass], etc.

I could certainly make separate property attributes for each outcome.

BTW, I never thought of this as a model for other people, but I suppose it should be taken that way. I use it as my primary acceptance test for the adapter.

24 changes: 12 additions & 12 deletions demo/NUnitTestDemo/SimpleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,75 +7,75 @@ namespace NUnitTestDemo
{
public class SimpleTests
{
[Test]
[Test, Should("Pass")]
public void TestSucceeds()
{
Console.WriteLine("Simple test running");
Assert.That(2 + 2, Is.EqualTo(4));
}

[Test]
[Test, Should("Pass")]
public void TestSucceeds_Message()
{
Assert.That(2 + 2, Is.EqualTo(4));
Assert.Pass("Simple arithmetic!");
}

[Test]
[Test, Should("Fail")]
public void TestFails()
{
Assert.That(2 + 2, Is.EqualTo(5));
}

[Test]
[Test, Should("Fail")]
public void TestFails_StringEquality()
{
Assert.That("Hello" + "World" + "!", Is.EqualTo("Hello World!"));
}

[Test]
[Test, Should("Inconclusive")]
public void TestIsInconclusive()
{
Assert.Inconclusive("Testing");
}

[Test, Ignore("Ignoring this test deliberately")]
[Test, Ignore("Ignoring this test deliberately"), Should("Ignore")]
public void TestIsIgnored_Attribute()
{
}

[Test]
[Test, Should("Ignore")]
public void TestIsIgnored_Assert()
{
Assert.Ignore("Ignoring this test deliberately");
}

[Test]
[Test, Should("Error")]
public void TestThrowsException()
{
throw new Exception("Deliberate exception thrown");
}

[Test]
[Test, Should("Pass")]
[Property("Priority", "High")]
public void TestWithProperty()
{
}

[Test]
[Test, Should("Pass")]
[Property("Priority", "Low")]
[Property("Action", "Ignore")]
public void TestWithTwoProperties()
{
}

[Test]
[Test, Should("Pass")]
[Category("Slow")]
public void TestWithCategory()
{
}

[Test]
[Test, Should("Pass")]
[Category("Slow")]
[Category("Data")]
public void TestWithTwoCategories()
Expand Down
1 change: 1 addition & 0 deletions demo/NUnitTestDemo/TextOutputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace NUnitTestDemo
{
[Should("Pass")]
public class TextOutputTests
{
[Test]
Expand Down
6 changes: 3 additions & 3 deletions demo/NUnitTestDemo/Theories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ public class Theories
[Datapoints]
int[] data = new int[] { 0, 1, 42 };

[Theory]
[Theory, Should("Pass")]
public void Theory_AllCasesSucceed(int a, int b)
{
Assert.That(a + b, Is.EqualTo(b + a));
}

[Theory]
[Theory, Should("Mixed")]
public void Theory_SomeCasesAreInconclusive(int a, int b)
{
Assume.That(b != 0);
}

[Theory]
[Theory, Should("Mixed")]
public void Theory_SomeCasesFail(int a, int b)
{
Assert.That(b != 0);
Expand Down