using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnitTestSharp;
namespace TestsForUnitTestSharp
{
///
/// Tests the test runner functions
///
public class TestRunnerTests : TestFixture
{
public void Hidden()
{
var output = TestRunner.NotifyUserOfHiddenTests(new[] { GetType().GetMethod("Hidden") });
CheckEqualRegex("Hidden", output);
}
public void NotifyUserOfFinalStatus_Success()
{
var actual = TestRunner.NotifyUserOfFinalStatus(0, 0, 1);
CheckEqualRegex("Success: 1 tests pass", actual);
}
public void NotifyUserOfFinalStatus_Failure()
{
var actual = TestRunner.NotifyUserOfFinalStatus(1, 1, 1);
CheckEqualRegex("FAILURE", actual);
}
public void NotifyUserOfFinalStatus_FailedChecks()
{
var actual = TestRunner.NotifyUserOfFinalStatus(0, 1, 1);
CheckEqualRegex("FAILURE", actual);
}
public void NotifyUserOfFinalStatus_FailedTests()
{
var actual = TestRunner.NotifyUserOfFinalStatus(1, 0, 1);
CheckEqualRegex("FAILURE", actual);
}
public void NotifyUserOfFinalStatus_MoreFailedThanTests()
{
CheckThrow();
var actual = TestRunner.NotifyUserOfFinalStatus(2, 1, 1);
}
public class BasicTests : TestFixture
{
[IgnoreFixture]
class FauxFixture : TestFixture
{
public void Test()
{
}
void HiddenTest()
{
}
[IgnoreTest]
void IgnoredTest()
{
}
}
public void ExtractMethodsToRun_Test()
{
int ignored, hidden;
IList testsToRun;
string outputExpected = BuildMessages.MSBuildMessage(
"Method is non public. Did you forget " +
"to mark it public? If you don't want the method tested, " +
"mark it with the IgnoreMethod attribute.",
"TestsForUnitTestSharp::TestRunnerTests.BasicTests.FauxFixture::HiddenTest",
false,
null,
null
) + "\n";
string outputActual = "";
FauxFixture fixture = new FauxFixture();
TestRunner.ExtractMethodsToRun(
fixture, (outputString) => outputActual += outputString,
out testsToRun, out ignored, out hidden);
CheckEqual(1, ignored);
CheckEqual(1, hidden);
CheckEqual(1, testsToRun.Count);
CheckEqual(outputExpected, outputActual);
}
[IgnoreFixture]
class OverriddenFindTestMethods : TestFixture
{
public override IList FindTestableMethods(ref int ignored)
{
return new List() { typeof(OverriddenFindTestMethods).GetMethod("Test") };
}
[IgnoreTest]
private void Test() { }
}
public void ExtractMethodsToRun_OverriddenFindTestableMethods()
{
int ignored, hidden;
IList testsToRun;
string outputActual = "";
TestRunner.ExtractMethodsToRun(new OverriddenFindTestMethods(),
(outputString) => outputActual += outputString,
out testsToRun, out ignored, out hidden);
CheckEqual(1, ignored);
CheckEqual(0, hidden);
CheckEqual(new List {typeof(OverriddenFindTestMethods).GetMethod("Test")}, testsToRun);
}
[IgnoreFixture]
class OverriddenCheckPrivateMethods : TestFixture
{
public override void CheckPrivateMethods(ref List missingIgnoreAttr, ref int ignored) { }
[IgnoreTest]
private void Test() { }
}
public void ExtractMethodsToRun_OverriddenCheckPrivateMethods()
{
int ignored, hidden;
IList testsToRun;
string outputActual = "";
TestRunner.ExtractMethodsToRun(new OverriddenCheckPrivateMethods(),
(outputString) => outputActual += outputString,
out testsToRun, out ignored, out hidden);
CheckEqual(0, ignored);
CheckEqual(0, hidden);
CheckEqual(new List {}, testsToRun);
}
}
public class CanCount : TestFixture
{
[IgnoreFixture]
class FauxFixture : TestFixture
{
public void Test1() { }
public void Test2()
{
Check(false);
Check(false);
}
public void Test3()
{
Check(false);
}
}
public void CanCountTest()
{
string output = "";
int totalTests = 0;
int failedTests = 0;
int failedChecks = 0;
TestRunner.RunTestFixture(typeof(FauxFixture), (outputString) => output += outputString,
ref totalTests, ref failedTests, ref failedChecks);
CheckEqual(3, totalTests);
CheckEqual(2, failedTests);
CheckEqual(3, failedChecks);
}
}
public class RunTestFixtureTests : UnitTestSharp.TestFixture
{
private static int ThrowException()
{
throw new System.Exception("");
}
[IgnoreFixture]
public class BadFixture : UnitTestSharp.TestFixture
{
int x = ThrowException();
}
public void ThrowsAnException()
{
string output = "";
int totalTests = 0, failedTests = 0, failedChecks = 0;
TestRunner.RunTestFixture(typeof(BadFixture), (outputString) => output += outputString,
ref totalTests, ref failedTests, ref failedChecks);
CheckEqualRegex("Failure creating test fixture", output);
}
}
public class ParameterizedFromDerived : UnitTestSharp.TestFixture
{
[IgnoreFixture]
public class InternalTestBase : UnitTestSharp.TestFixture
{
[Parameterized("TestData")]
public void Test(int value)
{
CheckEqual(2, value);
}
}
[IgnoreFixture]
public class InternalTestDerivedStatic : InternalTestBase
{
public static dynamic[] TestData = new dynamic[] { 1, 2, 3};
}
[IgnoreFixture]
public class InternalTestDerivedInstance : InternalTestBase
{
public dynamic[] TestData = new dynamic[] { 1, 2, 3 };
}
public void SourceIsFromDerivedStatic()
{
string output = "";
int totalTests = 0, failedTests = 0, failedChecks = 0;
TestRunner.RunTestFixture(typeof(InternalTestDerivedStatic), (outputString) => output += outputString,
ref totalTests, ref failedTests, ref failedChecks);
CheckEqual(3, totalTests);
CheckEqual(2, failedTests);
CheckEqual(2, failedChecks);
CheckEqualRegex(
"TestsForUnitTestSharp::TestRunnerTests.ParameterizedFromDerived.InternalTestDerivedStatic",
output);
}
public void SourceIsFromDerivedInstance()
{
string output = "";
int totalTests = 0, failedTests = 0, failedChecks = 0;
TestRunner.RunTestFixture(typeof(InternalTestDerivedInstance), (outputString) => output += outputString,
ref totalTests, ref failedTests, ref failedChecks);
CheckEqual(3, totalTests);
CheckEqual(2, failedTests);
CheckEqual(2, failedChecks);
CheckEqualRegex(
"TestsForUnitTestSharp::TestRunnerTests.ParameterizedFromDerived.InternalTestDerivedInstance",
output);
}
}
public class ExceptionInsideFixtureSetup : TestFixture
{
[IgnoreFixture]
public class Testee : TestFixture
{
public override void FixtureSetup()
{
throw new System.Exception();
}
}
public void DoesNotIncludeTestRunnerInStackTrace()
{
string output = "";
int totalTests = 0, failedTests = 0, failedChecks = 0;
TestRunner.RunTestFixture(typeof(Testee), (outputString) => output += outputString,
ref totalTests, ref failedTests, ref failedChecks);
CheckEqual(0, output.Count(c => c.Equals('\n')));
}
}
}
}