Skip to content

Automated tests

With Pororoca, you can make automated tests with .NET testing tools, such as xUnit. These tests can be executed in a pipeline or via command line, sending requests to a server.

To create and run these tests, you need to have .NET SDK in your computer.

Creating the test project

Create a test project through Visual Studio or via command line. For this latter option, type the following command on your console:

sh
mkdir MyPororocaTest
cd ./MyPororocaTest/
dotnet new xunit
# other testing libraries can be used

After that, in the created test project, the .csproj file must be edited to include the Pororoca.Test NuGet package.

Be aware that if you will run your unit tests on a Linux machine and those tests include HTTP/3, msquic needs to be installed. Check the Download page for the appropriate version.

xml
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <!-- Pororoca.Test v3 uses .net8.0 -->
    <TargetFramework>net8.0</TargetFramework>
    <!-- line below is required -->
    <EnablePreviewFeatures>True</EnablePreviewFeatures>

  </PropertyGroup>
  <ItemGroup>
    <!-- The line below adds Pororoca.Test package to the project -->
    <PackageReference Include="Pororoca.Test" Version="3.3.0" />

  </ItemGroup>

</Project>

Making your first test

The code below shows how to use the Pororoca.Test in a xUnit test. First, it loads a Pororoca collection from a file. Then, defines the environment that will be used.

cs
using Xunit;
using System.Net;
using Pororoca.Test;

namespace Pororoca.Test.Tests;

public class MyPororocaTest
{
    private readonly PororocaTest pororocaTest;

    public MyPororocaTest()
    {
        string filePath = @"C:\Tests\MyCollection.pororoca_collection.json";
        pororocaTest = PororocaTest.LoadCollectionFromFile(filePath)
                                   .AndUseTheEnvironment("Local");
    }

    [Fact]
    public async Task Should_get_JSON_successfully()
    {
        var res = await pororocaTest.SendRequestAsync("Get JSON");

        Assert.NotNull(res);
        Assert.Equal(HttpStatusCode.OK, res.StatusCode);
        Assert.Equal("application/json; charset=utf-8", res.ContentType);
        Assert.Contains("\"id\": 1", res.GetBodyAsText());
    }
}

There are methods in the PororocaTest class to set values of variables during the tests executions. They can be used to set an authentication token, for example:

cs
pororocaTest.SetCollectionVariable("MyAuthenticationToken", "token_auth");

The test project Pororoca.Test.Tests can guide you - it shows how to use the Pororoca.Test package, how to load the collection file and how to set variables.

WebSocket tests

You can also make Pororoca tests for WebSockets. The code below shows an example:

cs
[Fact]
public async Task Should_connect_and_disconnect_with_client_closing_message_successfully()
{
    // GIVEN AND WHEN
    var ws = await this.pororocaTest.ConnectWebSocketAsync("WebSocket HTTP1");
    // THEN
    Assert.Equal(PororocaWebSocketConnectorState.Connected, ws.State);

    // WHEN
    await ws.SendMessageAsync("Bye");
    // THEN
    Assert.Null(ws.ConnectionException);
    Assert.Equal(PororocaWebSocketConnectorState.Disconnected, ws.State);

    var msg = Assert.IsType<PororocaWebSocketClientMessageToSend>(ws.ExchangedMessages[0]);
    Assert.Equal(PororocaWebSocketMessageType.Close, msg.MessageType);
    Assert.Equal("Adiós", msg.Text);
}

Repeaters tests

Below is an example of a repeater test. The channelReader is an object for receiving the results of the repetitions, that run in background threads. The await foreach line is required to wait until all requests are finished.

cs
[Fact]
public async Task Should_run_repetition_successfully()
{
    var channelReader = await this.pororocaTest.StartHttpRepetitionAsync("My repetition");

    Stopwatch sw = new();
    sw.Start();
    await foreach (var result in channelReader.ReadAllAsync())
    {
        // assertions here  
    }
    sw.Stop();
    // sw.Elapsed --> total execution time
}

It is your decision how to inspect the results.

If you want to run and generate a report at the end:

cs
using Pororoca.Domain.Feature.Entities.Pororoca.Repetition;
using static Pororoca.Domain.Features.RequestRepeater.HttpRepetitionReporter;

...

[Fact]
public async Task Should_run_repetition_successfully_and_generate_report()
{
    var channelReader = await this.pororocaTest.StartHttpRepetitionAsync("My repetition");

    List<PororocaHttpRepetitionResult> results = new();

    await foreach (var result in channelReader.ReadAllAsync())
    {
        results.Add(result);
    }
    
    string reportFilePath = "C:\\Tests\\report.csv";
    await WriteReportAsync(results, reportFilePath);
}

Running the tests

You can run the tests on Visual Studio or executing dotnet test through the command line.

Why automated tests outside the GUI?

Other HTTP inspection tools, like Postman and SoapUI, have their testing areas inside the GUI. In Pororoca, the choice was to have tests done outside the GUI, for the following reasons:

  • Automated tests often require a setup, like running scripts in a database to pre-add or pre-remove data, or pulling information from an external data source, to compare with responses from the APIs. Testing in a GUI does not allow to easily do that, whereas testing in an isolated program does.

  • You can freely choose your assertion and testing frameworks, such as xUnit and FluentAssertions.

  • By having the tests in a dotnet project, they are already prepared to be run in a CI, only requiring a dotnet test step to execute them.