Unit Testing in C#
  • Unit testing in C#
  • Unit testing
    • What to test
    • When to test
    • Qualities of a good unit test suite
    • Qualities of a good unit test
    • Dealing with dependencies
    • Running the tests
  • NUnit
    • Quick glance at NUnit
    • Creating a NUnit test project
    • Anatomy of a test fixture
    • Lifecycle of a test fixture
    • Assertions
    • Asynchronous executions
    • Parameterized tests
    • Assumptions
    • Describing your tests
  • Moq
    • Quick glance at Moq
    • Method arguments
    • Method calls
    • Properties
    • Results
    • Callbacks
    • Exceptions
    • Events
    • Verifications
    • Base class
    • Mock customization
    • Implicit mocks
    • Mock repository
    • Custom matchers
    • Multiple interfaces
    • Protected members
    • Generic methods
    • Delegates
  • AutoFixture
    • Quick glance at AutoFixture
    • Fixture
    • Create and Build
    • Type customization
    • Data annotations
    • Default configurations
    • Building custom types
    • Relays
    • Tricks
    • Idioms
    • Integration with NUnit
    • Integration with Moq
    • Combining AutoFixture with NUnit and Moq
    • Extending AutoFixture
  • Advanced topics
    • Testing HttpClient
Powered by GitBook
On this page
  • Category
  • Property
  • Description
  • TestOf
  1. NUnit

Describing your tests

PreviousAssumptionsNextQuick glance at Moq

Last updated 4 years ago

To facilitate working with test suites composed by several tests, NUnit offers different attributes to further describe your fixtures and their tests.

.

Category

The Category attribute provides an additional dimension to group tests. The attribute can be applied to both a fixture or individual tests. The same test can belong to more than one category.

Most runners offer the possibility to specify which categories to include or exclude in a test run. Tests belonging to excluded categories will not be reported at all.

[Test]
[Category("FirstCategory")]
[Category("SecondCategory")]
public void This_is_a_test() { }

Optionally, you can create your own category attributes by inheriting from the built-in one. This is useful to avoid the risk of mispell of the category names.

To facilitate this scenario, the Category attribute exposes a parameterless protected constructor that will use the attribute name as name of the category.

public class ThirdCategoryAttribute : CategoryAttribute { }

public class FourthCategoryAttribute : CategoryAttribute 
{
    public FourthCategoryAttribute() : base("CustomNameForMyFourthCategory") { }    
}

You can then proceed using the new attribute as usual

[Test]
[ThirdCategory]
[FourthCategory]
public void This_is_another_test() { }

Property

Similarly to the Category attribute, the Property one gives the developer to specify additional properties of the unit test. The attribute can be applied to both a fixture or individual tests.

[Test]
[Property("APIVersion", "1.0")]
public void This_is_a_test() { }

Developers can also create their own attributes by inheriting the Property attribute and providing the value via the protected constructor while the property name will be inferred from the attribute class name.

public class APIVersionAttribute : PropertyAttribute
{
    public APIVersionAttribute(string value) : base(value) { }
}

You can then proceed using the new attribute as usual

[Test]
[APIVersion("1.0")]
public void This_is_another_test() { }

Description

The Description attribute lets the developer apply a descriptive test to the test or the whole fixture.

[Test]
[Description("This is a human readable description about this test")]
public void This_is_a_test_with_a_description() { }

TestOf

The TestOf attribute lets the developer specify the class that is being tested. The attribute can be applied to both a fixture or individual tests.

[TestFixture]
[TestOf(typeof(PersonStore))]
public class PersonStoreTests { }

This attribute can be used by certain IDE to bind tests to tested components.

.

.

.

.

Here is a list of all the attributes supported in NUnit
Here is the official documentation for the Category attribute
Here is the official documentation for the Property attribute
Here is the official documentation for the Description attribute
Here is the official documentation for the TestOf attribute