Skip to content

olegsych/fuzzy

Repository files navigation

Build Tests Nuget

Fuzzy is a simple .NET API for fuzz testing, or more specifically, for generating fuzzy inputs in unit tests.

Why use fuzz testing? To prevent system under test from making incorrect assumptions about its inputs and make the unit tests more robust. Using Fuzzy for built-in and custom types in your unit tests also helps to make them smaller and cleaner.

install

Add the fuzzy package to your .NET project.

dotnet add package fuzzy

import

Import the Fuzzy namespace in your .NET source file.

using Fuzzy;

use

Create an IFuzz instance. Fuzzy is a fluent API starting with this interface.

IFuzz fuzzy = new RandomFuzz();

To get a fuzzy value of a type you need, look for an IFuzz extension method with the name of the type.

int foo = fuzzy.Int32();
double bar = fuzzy.Double();
string baz = fuzzy.String();
Uri qux = fuzzy.Uri();
TypeCode quux = fuzzy.Enum<TypeCode>();
TimeSpan quuz = fuzzy.TimeSpan();
DateTime corge = fuzzy.DateTime();

For supported types, you can also specify constraints, such as the minimum or the maximum value.

int foo = fuzzy.Int32().Minimum(41);
int bar = fuzzy.Int32().Maximum(42);
int baz = fuzzy.Int32().Between(41, 43);

Fuzzy can also create collections (Array or List) with fuzzy size and fuzzy elements.

int[] foo = fuzzy.Array(fuzzy.Int32);

If needed, you can constrain collection size

int[] foo = fuzzy.Array(fuzzy.Int32, Length.Between(41, 43));

and collection elements.

int[] foo = fuzzy.Array(() => Environment.TickCount);
int[] bar = fuzzy.Array(new[] { 41, 42, 43 });

With collections, you can also get a fuzzy element or a fuzzy index.

IEnumerable<string> elements = new[] { "foo", "bar", "baz" };
string element = fuzzy.Element(elements);
int index = fuzzy.Index(elements);

extend

Suppose you have the following type in your application.

class CustomType
{
    public int Foo;
    public string Bar;
}

If you create an extension method like this.

static class IFuzzExtensions
{
    public static CustomType CustomType(this IFuzz fuzzy) =>
        new CustomType {
            Foo = fuzzy.Int32(),
            Bar = fuzzy.String(),
        };
}

Then, you can create fuzzy values of this custom types, just like the of types already supported by Fuzzy.

CustomType value = fuzzy.CustomType();

And, if you need fuzzy values of one of the .NET framework types, consider contributing it to Fuzzy.

debug

Sometimes fuzzy test inputs can produce test results that are hard to understand or repeat. This is a sign that either your tests or your system aren't handling a particular input correctly. You will need to correct this by making your tests handle this input specifically and possibly adjust your system code. In the meantime, you can replace the RandomFuzz with the SequentialFuzz to make fuzzy values more predictable.

IFuzz fuzzy = new SequentialFuzz();