Click here to Skip to main content
15,884,237 members
Articles / Programming Languages / C#

Fluent-Endurance: Fluent Endurance Automation Framework to Fluently Create Endurance Tests

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
2 Mar 2021MIT1 min read 4.7K   2   2   2
This project is about a fluent endurance automation framework whose API helps you to fluently create your endurance tests defining repetitions and timeouts.
In this post, we share samples (the solution contains set of tests which simulate certain car features), walk through setting up your tests, and look at the output.

.NET Core Publish NuGet

FluentEndurance

A fluent endurance automation framework whose API helps you to fluently create your endurance tests defining repetitions and timeouts.

Samples

For the sake of providing some examples, the solution contains set of tests which simulate certain car features.

In the following snippet, the engine must start within 1200 ms and stop within 800 ms. If any of these two operations reach the timing, the execution fails. This set of operations will repeat for 50 times.

C#
[Fact]
public Task EngineShouldStartAndStop()
    => UseFeatureSetGroup().For(Times.As(50))
        .WithSet(group => group.Create()
            .WithStep(_engineFeature, (engine, ct) => engine.Start(ct), Milliseconds.As(1200))
            .WithStep(_engineFeature, (engine, ct) => engine.Stop(ct), Milliseconds.As(800)))
        .Run();

Which outputs:

Executed engine.Start(ct) taking 1007.4665 ms
Executed engine.Stop(ct) taking 60.9244 ms
Executed engine.Start(ct) taking 1002.506 ms
Executed engine.Stop(ct) taking 58.2176 ms
...

Timespans can be used in order to define how long the operations must take instead of repeating them certain times.

C#
[Fact]
public Task EngineShouldStartRevAndStopDuringTime()
    => UseFeatureSetGroup().During(Minutes.As(1))
        .WithSet(group => group.Create().During(Seconds.As(20))
            .WithStep(_engineFeature, (engine, ct) => engine.Start(ct))
            .WithStep(_engineFeature, (engine, ct) => engine.Rev3000(ct))
            .WithStep(_engineFeature, (engine, ct) => engine.Stop(ct)))
        .Run();

Another more complete sample.

C#
[Fact]
public Task CarShouldMakeItsRoutineTwice()
    => UseFeatureSetGroup().For(Times.As(2))
        .WithSet(group => group.Create().As(<span class="pl-pds">"Warm up"</span>)
            .WithStep(_engineFeature, (engine, ct) => engine.Start(ct))
            .WithStep(_engineFeature, (engine, ct) => engine.Rev3000(ct)))
        .WithSet(group => group.Create().As("Routine")
            .WithStep(_autopilotFeature, (autopilot, ct) => autopilot.UnPark(ct))
            .WithStep(_gearsFeature, (gears, ct) => gears.ChangeToNeutral(ct))
            .WithStep(_gearsFeature, (gears, ct) => gears.ChangeToDrive(ct)))
        .WithSet(group => group.Create().As("Maneuvers").For(Times.Twice)
            .WithStep(_engineFeature, (engine, ct) => engine.Accelerate100(ct))
            .WithStep(_autopilotFeature, (autopilot, ct) => autopilot.Drive(ct))
            .WithStep(_steeringFeature, (steering, ct) => steering.Left(ct))
            .WithStep(_steeringFeature, (steering, ct) => steering.Forward(ct))
            .WithStep(_steeringFeature, (steering, ct) => steering.Right(ct))
            .WithStep(_steeringFeature, (steering, ct) => steering.Forward(ct))
            .WithStep(_engineFeature, (engine, ct) => engine.Accelerate150(ct))
            .WithStep(_brakesFeature, (brakes, ct) => brakes.BrakeTo50(ct))
            .WithStep(_autopilotFeature, (autopilot, ct) => autopilot.Drive(ct))
            .WithStep(_brakesFeature, (brakes, ct) => brakes.BrakeTo0(ct)))
        .WithSet(group => group.Create().As("Park")
            .WithStep(_engineFeature, (engine, ct) => engine.Stop(ct))
            .WithStep(_autopilotFeature, (autopilot, ct) => autopilot.Park(ct)))
        .Run();

Setup Your Tests

This library depends on IMediator for triggering events in order to provide certain status about performance during the execution.

The library provides a base class BaseTest which registers the IMediator default implementation and also provides a helper method to easily start defining the tests. Nonetheless, is up to you whether to use this class or implement yours depending on your needs.

C#
protected BaseTest(Action<IConfigurationBuilder> configureApp,  
                   Action<IServiceCollection> configureServices)
{
    var hostBuilder = new HostBuilder();
    hostBuilder.ConfigureAppConfiguration(builder =>
    {
        configureApp(builder);
        _configuration = builder.Build();
    });

    hostBuilder.ConfigureServices(collection =>
    {
        collection.AddScoped<FeatureSetGroup>();
        collection.AddSingleton<IMediator>(sp => new Mediator(sp.GetService));
        configureServices(collection);
    });

    _host = hostBuilder.Build();
}

Output Status

There are two ways of collecting notifications:

  • Live notifications by subscribing to these events.
  • Reading the events each feature has triggered at the end of the test execution.

Live Notifications Using Notification Handlers

C#
public MotionTests(ITestOutputHelper output)
    : base(
        _ => { }, 
        services =>
        {
            var notificationHandler = new StatusNotificationHandler();
            notificationHandler.Write += output.WriteLine;
            services.AddSingleton<INotificationHandler
                                 <StatusNotification>>(notificationHandler);
            services.AddSingleton<INotificationHandler
                                 <PerformanceStatusNotification>>(notificationHandler);
        })
{
    // ...
}

Reading Feature Events

C#
public Task DisposeAsync()
{
    foreach (var notification in _engineFeature.Notifications)
    {
        _output.WriteLine(notification.Content);
    }

    return Task.CompletedTask;
}
This article was originally posted at https://github.com/gcastellov/fluent-endurance

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer (Senior)
Spain Spain
http://www.linkedin.com/in/gerard-castello-viader
https://github.com/gcastellov

Comments and Discussions

 
QuestionMore examples? Pin
Bohdan Stupak7-Apr-21 22:17
professionalBohdan Stupak7-Apr-21 22:17 
QuestionMessage Closed Pin
26-Dec-20 6:06
Member 1503088826-Dec-20 6:06 
GeneralMy vote of 5 Pin
LightTempler22-Dec-20 22:04
LightTempler22-Dec-20 22:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.