0% found this document useful (0 votes)
463 views1 page

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)
463 views1 page

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

NUnit Unit Testing Framework Cheat Sheet

Installation Attributes

Install-Package NUnit NUnit 3.x MSTest v2.x. [Link] 2.x Comments


Install-Package [Link]
Install-Package [Link]
[Test] [TestMethod] [Fact] Marks a test method.

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

using [Link];
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] [Link]
{ 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

[Link](28, _actualFuel); // Tests whether the specified values are equal.


[Link](28, _actualFuel); // Tests whether the specified values are unequal. Same as AreEqual for numeric values.
[Link](_expectedRocket, _actualRocket); // Tests whether the specified objects both refer to the same object
[Link](_expectedRocket, _actualRocket); // Tests whether the specified objects refer to different objects
[Link](_isThereEnoughFuel); // Tests whether the specified condition is true
[Link](_isThereEnoughFuel); // Tests whether the specified condition is false
[Link](_actualRocket); // Tests whether the specified object is null
[Link](_actualRocket); // Tests whether the specified object is non-null
[Link](_actualRocket, typeof(Falcon9Rocket)); // Tests whether the specified object is an instance of the expected type
[Link](_actualRocket, typeof(Falcon9Rocket)); // Tests whether the specified object is not an instance of type
[Link](_expectedBellatrixTitle, "Bellatrix"); // Tests whether the specified strings are equal ignoring their casing
[Link](_expectedBellatrixTitle, "Bellatrix"); // Tests whether the specified string contains the specified substring
[Link](_expectedBellatrixTitle, "Bellatrix"); // Tests whether the specified string doesn't contain the specified substring
[Link](_expectedBellatrixTitle, "Bellatrix"); // Tests whether the specified string begins with the specified substring
[Link](_expectedBellatrixTitle, "Bellatrix"); // Tests whether the specified string begins with the specified substring
[Link]("(281)388-0388", @"(?d{3})?-? *d{3}-? *-?d{4}"); // Tests whether the specified string matches a regular expression
[Link]("281)388-0388", @"(?d{3})?-? *d{3}-? *-?d{4}"); // Tests whether the specified string does not match a regular expression
[Link](_expectedRockets, _actualRockets); // Tests whether the specified collections have the same elements in the same order and quantity.
[Link](_expectedRockets, _actualRockets); // Tests whether the specified collections does not have the same elements or the elements are in a different order and quantity.
[Link](_expectedRockets, _actualRockets); // Tests whether two collections contain the same elements.
[Link](_expectedRockets, _actualRockets); // Tests whether two collections contain different elements.
[Link](_expectedRockets, _actualRockets); // Tests whether all elements in the specified collection are instances of the expected type
[Link](_expectedRockets); // Tests whether all items in the specified collection are non-null
[Link](_expectedRockets); // Tests whether all items in the specified collection are unique
[Link](_actualRockets, falcon9); // Tests whether the specified collection contains the specified element
[Link](_actualRockets, falcon9); // Tests whether the specified collection does not contain the specified element
[Link](_expectedRockets, _actualRockets); // Tests whether one collection is a subset of another collection
[Link](_expectedRockets, _actualRockets); // Tests whether one collection is not a subset of another collection
[Link]<ArgumentNullException>(() => new Regex(null)); // Tests whether the code specified by delegate throws exact given exception of type T

Assertions- Constraint Model

[Link](28, [Link](_actualFuel)); // Tests whether the specified values are equal.


[Link](28, [Link](_actualFuel)); // Tests whether the specified values are unequal. Same as AreEqual for numeric values.
[Link](_expectedRocket, [Link](_actualRocket)); // Tests whether the specified objects both refer to the same object
[Link](_expectedRocket, [Link](_actualRocket)); // Tests whether the specified objects refer to different objects
[Link](_isThereEnoughFuel, [Link]); // Tests whether the specified condition is true
[Link](_isThereEnoughFuel, [Link]); // Tests whether the specified condition is false
[Link](_actualRocket, [Link]); // Tests whether the specified object is null
[Link](_actualRocket, [Link]); // Tests whether the specified object is non-null
[Link](_actualRocket, [Link]<Falcon9Rocket>()); // Tests whether the specified object is an instance of the expected type
[Link](_actualRocket, [Link]<Falcon9Rocket>()); // Tests whether the specified object is not an instance of type
[Link](_actualFuel, [Link](20)); // Tests whether the specified object greater than the specified value

Author Attribute Repeat Attribute

[TestFixture] [Test]
[Author("Joro Doev", "[Link]@[Link]")] [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
{
[Link]("{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([Link])] [TestFixture]


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

You might also like