Test events with nunit

Checking if events were fired can be done by subscribing to that event and setting a boolean value: var wasCalled = false; foo.NyEvent += (o,e) => wasCalled = true; … Assert.IsTrue(wasCalled); Due to request – without lambdas: var wasCalled = false; foo.NyEvent += delegate(o,e){ wasCalled = true;} … Assert.IsTrue(wasCalled);

What is the difference if TestFixture attribute is used or not

Is purely a convenience, beginning with NUnit 2.5, if a class satisfies the conditions to be a test fixture and specifies at least a method marked with Test, TestCase or TestCaseSource than that class is treated as a test fixture. The TestFixture attribute is required however for parameterized or generic test fixture because in that … Read more

ExpectedException in nUnit gave me an error

If you’re using NUnit 3.0, then your error is because the ExpectedExceptionAttribute has been removed. You should instead use a construct like the Throws Constraint. For example, the tutorial you linked has this test: [Test] [ExpectedException(typeof(InsufficientFundsException))] public void TransferWithInsufficientFunds() { Account source = new Account(); source.Deposit(200m); Account destination = new Account(); destination.Deposit(150m); source.TransferFunds(destination, 300m); } … Read more

FluentAssertions: equivalence of sorted lists

By default, ShouldBeEquivalentTo() will ignore the order in the collections because in most cases two collections are equivalent if they contain the same items in any order. If you do care about the order, just use one of the overloads of WithStrictOrdering() on the options => parameter. Example: var myList = Enumerable.Range(1, 5); var expected … Read more

How do I install NUnit 3 console on Windows and run tests?

It is hard to find, because there is a lot of outdated documentation, either for NUnit2 or NUnit3. Steps: Official NUnit3 console installers are here: https://github.com/nunit/nunit-console/releases (path is different than in docs) Download NUnit.Console-*.msi package and install Add to system PATH variable this: C:\Program Files (x86)\NUnit.org\nunit-console Open command line Type: $ nunit3-console test.dll // For … Read more

.NET NUnit test – Assembly.GetEntryAssembly() is null

Implement the SetEntryAssembly(Assembly assembly) method given in http://frostwave.googlecode.com/svn-history/r75/trunk/F2DUnitTests/Code/AssemblyUtilities.cs to your unit test project. /// <summary> /// Use as first line in ad hoc tests (needed by XNA specifically) /// </summary> public static void SetEntryAssembly() { SetEntryAssembly(Assembly.GetCallingAssembly()); } /// <summary> /// Allows setting the Entry Assembly when needed. /// Use AssemblyUtilities.SetEntryAssembly() as first line in XNA … Read more

NUnit – cleanup after test failure

Since version 2.5.7, NUnit allows Teardown to detect if last test failed. A new TestContext class allows tests to access information about themselves including the TestStauts. For more details, please refer to http://nunit.org/?p=releaseNotes&r=2.5.7 [TearDown] public void TearDown() { if (TestContext.CurrentContext.Result.Status == TestStatus.Failed) { PerformCleanUpFromTest(); } }

NUnit global initialization – bad idea?

[SetUpFixture] This is the attribute that marks a class that contains the one-time setup or teardown methods for all the test fixtures under a given namespace. The SetUp method in a SetUpFixture is executed once before any of the fixtures contained in its namespace. The TearDown method is executed once after all the fixtures have … Read more