0% found this document useful (0 votes)
351 views

NUnit Unit Testing Framework Cheat Sheet

The document provides information about the NUnit testing framework, including how to install and configure it, the various attributes used to mark test methods and classes, and the different assertions that can be used to validate test results. It explains the typical test execution workflow using NUnit including setting up test fixtures and individual test methods. Finally, it outlines the classic model for writing assertions to validate conditions, object equality, null checks, type checks, string matching, and collection contents.

Uploaded by

Daniel Fedeles
Copyright
© © All Rights Reserved
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)
351 views

NUnit Unit Testing Framework Cheat Sheet

The document provides information about the NUnit testing framework, including how to install and configure it, the various attributes used to mark test methods and classes, and the different assertions that can be used to validate test results. It explains the typical test execution workflow using NUnit including setting up test fixtures and individual test methods. Finally, it outlines the classic model for writing assertions to validate conditions, object equality, null checks, type checks, string matching, and collection contents.

Uploaded by

Daniel Fedeles
Copyright
© © All Rights Reserved
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 Unit Testing Framework Cheat Sheet

Installation Attributes

Install-Package NUnit NUnit 3.x MSTest v2.x. xUnit.net 2.x Comments


Install-Package NUnit.TestAdapter
Install-Package Microsoft.NET.Test.Sdk
[Test] [TestMethod] [Fact] Marks a test method.

Test Execution Workflow [TestFixture] [TestClass] n/a Marks a test class.

using NUnit.Framework;
namespace NUnitUnitTests
{ Triggered before
// A class that contains NUnit unit tests. (Required)
[SetUp] [TestInitialize] Constructor
every test case.
[TestFixture]
public class NonBellatrixTests
{
[OneTimeSetUp] Triggered after
public void ClassInit() [TearDown] [TestCleanup] IDisposable.Dispose
{ every test case.
// Executes once for the test class. (Optional)
}
[SetUp]
public void TestInit() One-time triggered
{
// Runs before each test. (Optional) [OneTimeSetUp] [ClassInitialize] IClassFixture<T> method before test
}
cases start.
[Test]
public void TestMethod()
{
}
[TearDown]
public void TestCleanup()
One-time triggered
{ [OneTimeTearDown] [ClassCleanup] IClassFixture<T> method after test
// Runs after each test. (Optional)
}
cases end.
[OneTimeTearDown]
public void ClassCleanup()
{
// Runs once after all tests in this class are [Fact(Skip="reason"
executed. (Optional) [Ignore("reason")] [Ignore] Ignores a test case.
// Not guaranteed that it executes instantly after all )]
tests from the class.
}
}
} Sets arbitrary
// A SetUpFixture outside of any namespace provides [Property] [TestProperty] [Trait]
SetUp and TearDown for the entire assembly. metadata on a test.
[SetUpFixture]
public class MySetUpClass
{
[OneTimeSetUp] Configures a
public void RunBeforeAnyTests() [Theory] [DataRow] [Theory]
{ data-driven test.
// Executes once before the test run. (Optional)
}
[OneTimeTearDown]
Categorizes the
public void RunAfterAnyTests()
[Trait("Category",
{ [Category("")] [TestCategory("") test cases or
// Executes once after the test run. (Optional) ] "")]
}
classes.
}

Assertions- Classic Model

Assert.AreEqual(28, _actualFuel); // Tests whether the specified values are equal.


Assert.AreNotEqual(28, _actualFuel); // Tests whether the specified values are unequal. Same as AreEqual for numeric values.
Assert.AreSame(_expectedRocket, _actualRocket); // Tests whether the specified objects both refer to the same object
Assert.AreNotSame(_expectedRocket, _actualRocket); // Tests whether the specified objects refer to different objects
Assert.IsTrue(_isThereEnoughFuel); // Tests whether the specified condition is true
Assert.IsFalse(_isThereEnoughFuel); // Tests whether the specified condition is false
Assert.IsNull(_actualRocket); // Tests whether the specified object is null
Assert.IsNotNull(_actualRocket); // Tests whether the specified object is non-null
Assert.IsInstanceOf(_actualRocket, typeof(Falcon9Rocket)); // Tests whether the specified object is an instance of the expected type
Assert.IsNotInstanceOf(_actualRocket, typeof(Falcon9Rocket)); // Tests whether the specified object is not an instance of type
StringAssert.AreEqualIgnoringCase(_expectedBellatrixTitle, "Bellatrix"); // Tests whether the specified strings are equal ignoring their casing
StringAssert.Contains(_expectedBellatrixTitle, "Bellatrix"); // Tests whether the specified string contains the specified substring
StringAssert.DoesNotContain(_expectedBellatrixTitle, "Bellatrix"); // Tests whether the specified string doesn't contain the specified substring
StringAssert.StartsWith(_expectedBellatrixTitle, "Bellatrix"); // Tests whether the specified string begins with the specified substring
StringAssert.StartsWith(_expectedBellatrixTitle, "Bellatrix"); // Tests whether the specified string begins with the specified substring
StringAssert.IsMatch("(281)388-0388", @"(?d{3})?-? *d{3}-? *-?d{4}"); // Tests whether the specified string matches a regular expression
StringAssert.DoesNotMatch("281)388-0388", @"(?d{3})?-? *d{3}-? *-?d{4}"); // Tests whether the specified string does not match a regular expression
CollectionAssert.AreEqual(_expectedRockets, _actualRockets); // Tests whether the specified collections have the same elements in the same order and quantity.
CollectionAssert.AreNotEqual(_expectedRockets, _actualRockets); // Tests whether the specified collections does not have the same elements or the elements are in a different order and quantity.
CollectionAssert.AreEquivalent(_expectedRockets, _actualRockets); // Tests whether two collections contain the same elements.
CollectionAssert.AreNotEquivalent(_expectedRockets, _actualRockets); // Tests whether two collections contain different elements.
CollectionAssert.AllItemsAreInstancesOfType(_expectedRockets, _actualRockets); // Tests whether all elements in the specified collection are instances of the expected type
CollectionAssert.AllItemsAreNotNull(_expectedRockets); // Tests whether all items in the specified collection are non-null
CollectionAssert.AllItemsAreUnique(_expectedRockets); // Tests whether all items in the specified collection are unique
CollectionAssert.Contains(_actualRockets, falcon9); // Tests whether the specified collection contains the specified element
CollectionAssert.DoesNotContain(_actualRockets, falcon9); // Tests whether the specified collection does not contain the specified element
CollectionAssert.IsSubsetOf(_expectedRockets, _actualRockets); // Tests whether one collection is a subset of another collection
CollectionAssert.IsNotSubsetOf(_expectedRockets, _actualRockets); // Tests whether one collection is not a subset of another collection
Assert.Throws<ArgumentNullException>(() => new Regex(null)); // Tests whether the code specified by delegate throws exact given exception of type T

Assertions- Constraint Model

Assert.That(28, Is.EqualTo(_actualFuel)); // Tests whether the specified values are equal.


Assert.That(28, Is.Not.EqualTo(_actualFuel)); // Tests whether the specified values are unequal. Same as AreEqual for numeric values.
Assert.That(_expectedRocket, Is.SameAs(_actualRocket)); // Tests whether the specified objects both refer to the same object
Assert.That(_expectedRocket, Is.Not.SameAs(_actualRocket)); // Tests whether the specified objects refer to different objects
Assert.That(_isThereEnoughFuel, Is.True); // Tests whether the specified condition is true
Assert.That(_isThereEnoughFuel, Is.False); // Tests whether the specified condition is false
Assert.That(_actualRocket, Is.Null); // Tests whether the specified object is null
Assert.That(_actualRocket, Is.Not.Null); // Tests whether the specified object is non-null
Assert.That(_actualRocket, Is.InstanceOf<Falcon9Rocket>()); // Tests whether the specified object is an instance of the expected type
Assert.That(_actualRocket, Is.Not.InstanceOf<Falcon9Rocket>()); // Tests whether the specified object is not an instance of type
Assert.That(_actualFuel, Is.GreaterThan(20)); // Tests whether the specified object greater than the specified value

Author Attribute Repeat Attribute

[TestFixture] [Test]
[Author("Joro Doev", "[email protected]")] [Repeat(10)]
public class RocketFuelTests public void RocketFuelMeassuredCorrectly_When_Flying() { /* ... */ }
{
[Test]

Combinatorial Attribute
public void RocketFuelMeassuredCorrectly_When_Landing() { /* ... */ }
[Test]
[Author("Ivan Penchev")]
public void RocketFuelMeassuredCorrectly_When_Flying() { /* ... */ }
[Test, Combinatorial]
}
public void CorrectFuelMeassured_When_X_Site([Values(1,2,3)] int x, [Values("A","B")] string s)
{
...

Pairwise Attribute
}

[Test, Pairwise]
public void ValidateLandingSiteOfRover_When_GoingToMars
([Values("a", "b", "c")] string a, [Values("+", "-")] string b, [Values("x", "y")] string c) Random Attribute
{
Debug.WriteLine("{0} {1} {2}", a, b, c);
[Test]
}
public void GenerateRandomLandingSiteOnMoon([Values(1,2,3)] int x, [Random(-1.0,
1.0, 5)] double d)
{
Range Attribute ...
}

[Test]
public void CalculateJupiterBaseLandingPoint([Values(1,2,3)] int x, [Range(0.2,0.6)] double y)
{ Retry Attribute
//...
}
[Test]
[Retry(3)]
public void CalculateJupiterBaseLandingPoint([Values(1,2,3)] int x, [Range(0.2,0.6)] double y)
{

Timeout Attribute
//...
}

[Test, Timeout(2000)]
public void FireRocketToProximaCentauri()
{
...
}

Execute Tests in Parallel

[assembly: Parallelizable(ParallelScope.Fixtures)] [TestFixture]


[assembly:LevelOfParallelism(3)] [Parallelizable(ParallelScope.Fixtures)]
public class TestFalcon9EngineLevels
{
// ...
}

You might also like