Execution Time

Edit this page

Members and Actions

Fluent Assertions also provides a method to assert that the execution time of particular method or action does not exceed a predefined value. To verify the execution time of a method, use the following syntax:

public class SomePotentiallyVerySlowClass
    public void ExpensiveMethod()
        for (short i = 0; i < short.MaxValue; i++)
            string tmp = " ";
            if (!string.IsNullOrEmpty(tmp))
                tmp += " ";
var subject = new SomePotentiallyVerySlowClass();
subject.ExecutionTimeOf(s => s.ExpensiveMethod()).Should().BeLessOrEqualTo(500.Milliseconds());

Alternatively, to verify the execution time of an arbitrary action, use this syntax:

Action someAction = () => Thread.Sleep(100);

The supported assertions on ExecutionTime() are a subset of those found for TimeSpans, namely:

someAction.ExecutionTime().Should().BeCloseTo(150.Milliseconds(), 50.Milliseconds());


If you’re dealing with a Task, you can also assert that it completed within a specified period of time:

Func<Task> someAsyncWork = () => SomethingReturningATask();

This will result in a blocking call, but going fully async is supported too:

await someAsyncWork.Should().CompleteWithinAsync(100.Milliseconds());

If the Task is generic and returns a value, you can use that to write a continuing assertion:

Func<Task<int>> someAsyncFunc;


A fully async version is available as well.