fluent-asserts 1.0.0-beta.1

Fluent assertions done right

To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

Line Coverage DUB Version DUB Installs

Writing unit tests is easy with Dlang. The unittest block allows you to start writing tests and to be productive with no special setup.

Unfortunately the assert expression does not help you to write expressive asserts, and in case of a failure it's hard to find why an assert failed. The fluent-asserts library allows you to more naturally specify the expected outcome of a TDD or BDD-style test.

To begin

  1. Add the DUB dependency: https://code.dlang.org/packages/fluent-asserts
$ dub add fluent-asserts
  1. Use it:
    unittest {
        true.should.equal(false).because("this is a failing assert");

    unittest {
        Assert.equal(true, false, "this is a failing assert");
  1. Run the tests:
➜  dub test --compiler=ldc2


API Docs

The library provides the expect, should templates and the Assert struct.


expect is the main assert function exposed by this library. It takes a parameter which is the value that is tested. You can use any assert operation provided by the base library or any other operations that was registered by a third party library.

Expect expect(T)(lazy T testedValue, ...);
Expect expect(void delegate() callable, ...);


In addition, the library provides the not and because modifiers that allow to improve your asserts.

not negates the assert condition:


because allows you to add a custom message:

    expect(true).to.equal(false).because("of test reasons");
    /// will output this message: Because of test reasons, true should equal `false`.


should is designed to be used in combination with Uniform Function Call Syntax (UFCS), and is an alias for expect.

    auto should(T)(lazy T testData, ...);

So the following statements are equivalent


In addition, you can use not and because modifiers with should.

not negates the assert condition:

    true.should.equal(false).because("of test reasons");


Assert is a wrapper for the expect function, that allows you to use the asserts with a different syntax.

For example, the following lines are equivalent:

    Assert.equal(testedValue, 42);

All the asserts that are available using the expect syntax are available with Assert. If you want to negate the check, just add not before the assert name:

    Assert.notEqual(testedValue, 42);

Built in operations

Extend the library

Registering new operations

Even though this library has an extensive set of operations, sometimes a new operation might be needed to test your code. Operations are functions that recieve an Evaluation and returns an IResult list in case there was a failure. You can check any of the built in operations for a refference implementation.

IResult[] customOperation(ref Evaluation evaluation) @safe nothrow {

Once the operation is ready to use, it has to be registered with the global registry:

static this() {
    /// bind the type to different matchers
    Registry.instance.register!(SysTime, SysTime)("between", &customOperation);
    Registry.instance.register!(SysTime, SysTime)("within", &customOperation);

    /// or use * to match any type
    Registry.instance.register("*", "*", "customOperation", &customOperation);

Registering new serializers

In order to setup an Evaluation, the actual and expected values need to be converted to a string. Most of the time, the default serializer will do a great job, but sometimes you might want to add a custom serializer for your types.

static this() {

string jsonToString(Json value) {
    /// you can add here your custom serializer for Jsons


MIT. See LICENSE for details.

  • Szabo Bogdan
ddmp, unit-threaded, libdparse
1.0.1 2024-Jun-19
1.0.0 2022-Aug-24
1.0.0-beta.2 2022-Aug-24
1.0.0-beta.1 2022-Aug-12
0.14.0-alpha.13 2022-May-02
Show all 64 versions
Download Stats:
  • 48 downloads today

  • 190 downloads this week

  • 405 downloads this month

  • 49680 downloads total

Short URL: