Search⌘ K
AI Features

Parametrized vs. Nonparametrized Tests

Explore the distinctions between parametrized and nonparametrized tests in xUnit to understand how to write effective automated tests in .NET. Learn how to use the Fact and Theory attributes and apply the DRY principle to reduce repetitive code. Discover best practices to organize tests for clear identification of failing scenarios, enabling you to create maintainable and comprehensive test suites.

In this lesson, we’ll learn the difference between parametrized and nonparametrized tests. We’ll also learn the best practices for using either of these. We will do so with the help of the following playground:

namespace MainApp;

public class Calculator
{
    public int MultiplyByTwo(int value)
    {
        return value * 2;
    }
}
Parametrized test demonstration

In this playground, we use the MainApp project that represents our assembly under test. The project has the Calculator class with the MultiplyByTwo() method. The method multiplies the input parameter by 2 and returns the result. All our tests cover this method.

Nonparametrized tests

Nonparametrized tests in xUnit are represented by methods marked with the Fact attribute. These methods don’t accept any parameters, and each of such methods represents a single test. We can find an example of this in the CalculatorTests.cs file inside the MainApp.Tests project of the above playground. The Fact attribute can be found on line 5.

Everything is hard-coded because we don’t have any parameters in this ...