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
  1. NUnit

Assumptions

PreviousParameterized testsNextDescribing your tests

Last updated 4 years ago

Sometimes, unit tests can be relevant only when certain conditions are met. This is often the case with .

In these cases, mapping these conditions as assertions would mark as failed tests that simply received invalid data. To obviate this issue, NUnit offers the possibility to make assumptions on the incoming data. Unlike assertions, unmet assumptions make the runner mark the test as Invalid instead of Failed.

[Test]
public void A_test_with_assumptions (
    [Values(2020, 2025, 2030)] int year,
    [Range(1, 12)] int month,
    [Range(1, 31)] int day)
{
    Assume.That(day, Is.LessOrEqualThan(DateTime.DaysInMonth(year, month)));

    ...
}

In the example above, test runs with invalid day/month/year combinations (like June 31st) are ignored.

Please note that assumptions make use of the Assume static class. Assume.That has the same set of overloads as Assert.That.

parameterized tests