0% found this document useful (0 votes)
1K views1 page

NUnit Cheat Sheet

This document provides an example of using NUnit to test a .NET class. It shows how to set up test fixtures and individual tests, perform different types of assertions to validate results, and attribute tests to indicate expected exceptions or that a test is not yet implemented. The test class initializes an invoice object before each test method and cleans it up after. A sample test validates that an invoice ID property was set correctly.

Uploaded by

nomaddarcy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views1 page

NUnit Cheat Sheet

This document provides an example of using NUnit to test a .NET class. It shows how to set up test fixtures and individual tests, perform different types of assertions to validate results, and attribute tests to indicate expected exceptions or that a test is not yet implemented. The test class initializes an invoice object before each test method and cleans it up after. A sample test validates that an invoice ID property was set correctly.

Uploaded by

nomaddarcy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

NUNIT Quick Reference www.kellermansoftware.com Free Quick References and .net components.

using System;
using Kellerman.Business; //Reference to class to test
using NUnit.Framework; //This must be included to do the asserts

namespace Kellerman.BusinessTest
{
//Tests for the Invoice Business Object
[TestFixture]
[Category(“Optional Category Attribute”), Description(“Optional Description”)]
public class InvoiceTest
{
private Invoice _invoice = null;

//Code that is run before each test


[SetUp]
public void Initialize()
{
_invoice = new Invoice ();
}

//Code that is run after each test


[TearDown]
public void Cleanup()
{
}

//Example test and asserts


[Test, Description(“Property Tests”)]
public void InvoiceIdTest()
{
int expected = 7;
_invoice.InvoiceId = expected;
Assert.AreEqual(expected,
_invoice.InvoiceId,
"Kellerman.Business.Invoice.InvoiceId not set correctly");

//Other example Asserts


Assert.IsTrue(true);
Assert.IsFalse(false);
Assert.IsNull(null);
Assert.IsNotNull(_invoice);
Assert.IsEmpty(string.Empty);
Assert.IsNotEmpty(“This string is not empty”);
Assert.Fail(“This is a failure message”);
}

//Expected Exception Test


[Test]
[ExpectedException(typeof(System.Security.SecurityException))]
public void DeleteTestNoRights()
{
_invoice.Delete();
}

//Not Implemented Test


[Test]
[Ignore(“Please implement”)]
public void UpdateTestNoRights()
{
}

}
}

You might also like